修改 CentOS 7 SSH 端口

修改 sshd_config 端口

$ vi /etc/ssh/sshd_config

取消 #Port 22 的注释,在下一行添加你需要修改的新端口 Port 2048。(这里不删除 22 端口是为了防止修改后新端口无法访问,造成无法用 ssh 连接服务器。)

Port 22  
Port 2048  

修改保存 sshd_config 文件后重启 sshd 服务:

$ systemctl restart sshd

为 ssh 打开 2048 端口

# 为 ssh 添加新的允许端口
$ semanage port -a -t ssh_port_t -p tcp 2048
# 查看当前 SELinux 允许的端口
$ semanage port -l | grep ssh
ssh_port_t                     tcp      2048, 22  

配置防火墙 firewalld, 为 public zone 永久开放 2048/TCP 端口:

# 以防新端口不生效,先把 22 端口暴露
$ firewall-cmd --permanent --zone=public --add-port=22/tcp
$ firewall-cmd --permanent --zone=public --add-port=2048/tcp
success  
# 重载防火墙
$ firewall-cmd --reload
# 查看暴露端口规则
$ firewall-cmd --permanent --list-port
443/tcp 80/tcp 22/tcp 2048/tcp  
$ firewall-cmd --zone=public --list-all
public (default, active)  
  interfaces: eth0 eth1
  sources:
  services: dhcpv6-client ssh
  ports: 443/tcp 80/tcp 22/tcp 2048/tcp
  masquerade: no
  forward-ports:
  icmp-blocks:
  rich rules:

退出 ssh 后,尝试连接新端口

禁用 22 端口

删除 ssh 允许端口

$ vi /etc/ssh/sshd_config
#Port 22
Port 2048  
$ systemctl restart sshd
# 用 ss 命令检查 ssh 监听的端口,没有 22 证明修改成功
$ ss -tnlp | grep ssh
LISTEN     0      128                       *:2048                    *:* 

防火墙移除 22 端口

$ firewall-cmd --permanent --zone=public --remove-port=22/tcp
success  
$ firewall-cmd --reload
$ firewall-cmd --permanent --list-port
443/tcp 80/tcp 2048/tcp  
原文链接:,转发请注明来源!

发表回复