linux centos修改ssh端口脚本
2022年9月1日
没有评论
linux修改ssh端口脚本
方法一:
复制以下内容到脚本ssh.sh然后使用命令sh ssh.sh就可以了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | # Use default SSH port 22. If you use another SSH port on your server if [ -e "/etc/ssh/sshd_config" ];then [ -z "`grep ^Port /etc/ssh/sshd_config`" ] && ssh_port=22 || ssh_port=`grep ^Port /etc/ssh/sshd_config | awk '{print $2}'` while :; do echo read -p "Please input SSH port(Default: $ssh_port): " SSH_PORT [ -z "$SSH_PORT" ] && SSH_PORT=$ssh_port if [ $SSH_PORT -eq 22 >/dev/null 2>&1 -o $SSH_PORT -gt 1024 >/dev/null 2>&1 -a $SSH_PORT -lt 65535 >/dev/null 2>&1 ];then break else echo "${CWARNING}input error! Input range: 22,1025~65534${CEND}" fi done if [ -z "`grep ^Port /etc/ssh/sshd_config`" -a "$SSH_PORT" != '22' ];then sed -i "s@^#Port.*@&\nPort $SSH_PORT@" /etc/ssh/sshd_config elif [ -n "`grep ^Port /etc/ssh/sshd_config`" ];then sed -i "s@^Port.*@Port $SSH_PORT@" /etc/ssh/sshd_config fi cp /etc/selinux/config /etc/selinux/config_$baktime sed -i '/SELINUX/s/\(enforcing\|permissive\)/disabled/' /etc/selinux/config if [ -e /etc/sysconfig/selinux ] then cp /etc/sysconfig/selinux /etc/sysconfig/selinux_$baktime sed -i '/SELINUX/s/\(enforcing\|permissive\)/disabled/' /etc/sysconfig/selinux fi setenforce 0 systemctl enable firewalld systemctl start firewalld firewall-cmd --zone=public --add-port=$SSH_PORT/tcp --permanent firewall-cmd --reload service sshd restart fi |
方法二:(推荐)
以下这个直接运行就更快捷。
1 2 3 4 5 6 | firewall-cmd --zone=public --add-port=8899/tcp --permanent firewall-cmd --reload sed -i '/#Port 22/s/#Port 22/Port 8899/' /etc/ssh/sshd_config sed -i '/#UseDNS yes/s/#UseDNS yes/UseDNS no/' /etc/ssh/sshd_config service sshd restart /bin/systemctl restart sshd.service |
8899就是更改后的端口。自己改变一下。