vi port_change.sh 将下面代码复制粘贴到脚本里,然后bash port_change.sh 后接要修改的端口号
#!/bin/bash
##Write By ZaLinux
if [ $# != 1 ];then
echo "please input new ssh port"
exit
else
cat /etc/ssh/sshd_config |grep ^Port
if [ $? == 0 ];then
port=`cat /etc/ssh/sshd_config |grep ^Port`
oldport=`echo $port|awk {print $2}`
sed -i -e "/^Port/s/$port/Port $1/" /etc/ssh/sshd_config
service sshd restart
iptables -D INPUT -p tcp --dport $oldport -j ACCEPT
iptables -I INPUT -p tcp --dport $1 -j ACCEPT
service iptables save
else
sed -i -e "/^#Port/a\$1" /etc/ssh/sshd_config
service sshd restart
iptables -I INPUT -p tcp --dport $1 -j ACCEPT
fi
fi
交流QQ:2881064157
时间: 2024-10-15 20:12:46