Leo
修改SSH端口
SSH默认的端口是22,如果不改的话会一直被扫,不仅不安全,还会消耗系统资源,所以最好改一下。
过程基本分为四步,目标是把端口由22改为2222
修改SSH端口
配置SELinux端口
打开防火墙端口
禁用22端口
修改SSH端口
一般情况下,ssh的配置文件是 /etc/ssh/sshd_config或 /etc/sshd/sshd_config,不同系统可能不太一样。需要你找到这个配置文件,并使用vi或其它文本编辑软件打开它。
找到#Port 22的一行,这里的#是注释,我们把注释去掉,并在Port 22下面加一行Port 2222,这里保留22端口是防止有什么意外导致连不上主机。
修改好了以后就保存并重启sshd服务service sshd restart
配置SELINUX端口
如果你观察仔细的话,可以看到Port 22上面有几行英文,告诉你如果要更改SSH端口需要告诉SELinux。
`If you want to change the port on a SELinux system, you have to tell
SELinux about this change.
semanage port -a -t ssh_port_t -p tcp #PORTNUMBER`
我们要用2222作为SSH端口,就需要运行semanage port -a -t ssh_port_t -p tcp 2222
Centos7 系统可能没有内置semanage,需要手动安装yum install policycoreutils-python -y
出现以下错误说明你的系统没有启用SELinux,不用管直接下一步
[[email protected] ~]# semanage port -a -t ssh_port_t -p tcp 2222
SELinux: Could not downgrade policy file /etc/selinux/targeted/policy/policy.30, searching for an older version.
SELinux: Could not open policy file <= /etc/selinux/targeted/policy/policy.30: No such file or directory
/sbin/load_policy: Can't load policy: No such file or directory
libsemanage.semanage_reload_policy: load_policy returned error code 2. (No such file or directory).
SELinux: Could not downgrade policy file /etc/selinux/targeted/policy/policy.30, searching for an older version.
SELinux: Could not open policy file <= /etc/selinux/targeted/policy/policy.30: No such file or directory
/sbin/load_policy: Can't load policy: No such file or directory
libsemanage.semanage_reload_policy: load_policy returned error code 2. (No such file or directory).
OSError: No such file or directory
打开防火墙端口
如果使用的防火墙是firewalld,那么添加端口firewall-cmd --permanent --zone=public --add-port=2222/tcp,再firewall-cmd --reload一下,就可以开放2222端口了
iptables也差不多,iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 2222 -j ACCEPT添加端口,再重启服务service iptables restart,就可以了
禁用22端口
先去SSH配置文件那里把之前保留的Port 22删除,并重启sshd服务service sshd restart
然后在防火墙禁用22端口firewall-cmd --permanent --zone=public --remove-port=22/tcp或iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j DROP
就完成了