centos修改ssh端口

时间:2021-03-04 05:15:27

1、编辑防火墙配置:
vi /etc/sysconfig/iptables
防火墙增加新端口2222
-A INPUT -m state --state NEW -m tcp -p tcp --dport 2222 -j ACCEPT
======================================================================
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state –state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state –state NEW -m tcp -p tcp –dport 22 -j ACCEPT
-A INPUT -m state –state NEW -m tcp -p tcp –dport 2222 -j ACCEPT
-A INPUT -j REJECT –reject-with icmp-host-prohibited
-A FORWARD -j REJECT –reject-with icmp-host-prohibited
COMMIT
重启防火墙,使配置生效:
/etc/init.d/iptables restart
service iptables restart

2、备份ssh端口配置文件
cp /etc/ssh/ssh_config /etc/ssh/ssh_configbak
cp /etc/ssh/sshd_config /etc/ssh/sshd_configbak
修改ssh端口为:2222
vi /etc/ssh/sshd_config
在端口#Port 22下面增加Port 2222
vi /etc/ssh/ssh_config
在端口#Port 22下面增加Port 2222
重启:
/etc/init.d/sshd restart 或
service sshd restart
用2222端口可以正常连接之后,再返回去重复上面的步骤。把22端口禁用了,以后ssh就只能用2222端口连接了!