文章目录
- 修改Linux中SSH的端口
- Linux中默认的ssh端口
- 关闭SELinux
- 测试新端口
修改Linux中SSH的端口
Linux中默认的ssh端口
使用root用户操作
修改前先备份ssh_config
cp /etc/ssh/sshd_config /etc/ssh/sshd_config_`date "+%Y%m%d%H%M%S"`
修改配置文件,找到已经注释掉的22端口
vim /etc/ssh/sshd_config17 #Port 2218 #AddressFamily any19 #ListenAddress 0.0.0.020 #ListenAddress ::
将其修改为
17 #Port 22
18 Port 22
19 Port 18181
20 #AddressFamily any
21 #ListenAddress 0.0.0.0
22 #ListenAddress ::
小技巧,当进行却去掉注释修改的时候,可以新增一行去掉注释,尽可能的不修改原来的行
设置的时候一定要先将22端口保留,等到测试通过后将22端口注释掉,如果防火墙没有开好,那就是自己给自己留坑
重启sshd服务
systemctl restart sshd.service
查看是否新端口运行
[root@WDQCVM ~]# ss -tnlp | grep ssh
LISTEN 0 128 *:22 *:* users:(("sshd",pid=3668,fd=3))
LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=3668,fd=4))
哎呦不对呀,这没有刚才配置的端口
[root@WDQCVM ~]# systemctl status sshd
● sshd.service - OpenSSH server daemonLoaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)Active: active (running) since Mon 2023-08-14 14:37:05 CST; 5min agoDocs: man:sshd(8)man:sshd_config(5)Main PID: 11371 (sshd)CGroup: /system.slice/sshd.service└─11371 /usr/sbin/sshd -DAug 14 14:37:05 WDQCVM systemd[1]: Starting OpenSSH server daemon...
Aug 14 14:37:05 WDQCVM sshd[11371]: error: Bind to port 18181 on 0.0.0.0 failed: Permission denied.
Aug 14 14:37:05 WDQCVM sshd[11371]: error: Bind to port 18181 on :: failed: Permission denied.
Aug 14 14:37:05 WDQCVM sshd[11371]: Server listening on 0.0.0.0 port 22.
Aug 14 14:37:05 WDQCVM sshd[11371]: Server listening on :: port 22.
Aug 14 14:37:05 WDQCVM systemd[1]: Started OpenSSH server daemon.
看到是权限的问题,心里想这不可能有问题呀,我root启动的
- 端口问题
- 权限问题
貌似这两个都不行,我还更新了系统内核,都没有解决问题
最后在网上查资料才发现是SELinux的问题
关闭SELinux
查看状态
sestatus
[root@WDQCVM ~]# sestatus
SELinux status: enabled
SELinuxfs mount: /sys/fs/selinux
SELinux root directory: /etc/selinux
Loaded policy name: targeted
Current mode: enforcing
Mode from config file: enforcing
Policy MLS status: enabled
Policy deny_unknown status: allowed
Max kernel policy version: 31
[root@WDQCVM ~]#
修改为disabled,重启机器生效
sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
关闭SELinux后18181端口被放出来了
[root@WDQCVM ~]# ss -tnlp | grep ssh
LISTEN 0 128 *:18181 *:* users:(("sshd",pid=977,fd=3))
LISTEN 0 128 *:22 *:* users:(("sshd",pid=977,fd=5))
LISTEN 0 128 [::]:18181 [::]:* users:(("sshd",pid=977,fd=4))
LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=977,fd=6))
[root@WDQCVM ~]#
测试新端口
[root@WDQCVM ~]# ssh root@10.0.2.15 -p 18181
The authenticity of host '[10.0.2.15]:18181 ([10.0.2.15]:18181)' can't be established.
ECDSA key fingerprint is SHA256:yjNMl5BfVvsJ3HmpCPv3B3Y+5XbT4mxwMnDGUHSpnBs.
ECDSA key fingerprint is MD5:0d:08:47:ee:4f:32:a6:8b:8f:2d:dd:fa:f4:f3:e0:5a.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[10.0.2.15]:18181' (ECDSA) to the list of known hosts.
root@10.0.2.15's password:
Last login: Mon Aug 14 15:04:06 2023 from 192.168.31.63
[root@WDQCVM ~]#
然后禁用22端口
vim /etc/ssh/sshd_config
将Port 22添加注释
重启sshd服务
systemctl restart sshd.service
ss -tnlp | grep ssh
总结:ssh端口如果是暴露在公网,则建议改掉22端口,端口设置在1024~65366之间,建议关闭SELinux ,这个是Linux自带的安全机制,是防止网络攻击的,但也给使用带来了诸多不便。生产环境建议使用专用的硬件防火墙和iptables,一般情况下这种是可以实现的固定IP访问固定端口的。如果不小心禁用了22端口只能通过终端去设置,或者拿着屏幕去机房设置了。