title: CentOS修改ssh端口date: 2021-01-10 12:38:56
tags: Linux
categories: Linux

备份SSH配置文件

  1. [root@wing-centos ~]# cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak

修改配置文件

  1. [root@wing-centos ~]# vim /etc/ssh/sshd_config

将端口22改为新端口

  1. # If you want to change the port on a SELinux system, you have to tell
  2. # SELinux about this change.
  3. # semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
  4. #
  5. Port 28818
  6. #AddressFamily any
  7. #ListenAddress 0.0.0.0
  8. #ListenAddress ::

允许新端口通过防火墙

  1. [root@wing-centos ~]# firewall-cmd --permanent --zone=public --add-port=28818/tcp
  1. [root@wing-centos ~]# firewall-cmd --reload

重启sshd服务并验证

重启sshd服务

  1. [root@wing-centos ~]# systemctl restart sshd.service

验证SSH现在是否在新端口上运行

  1. [root@wing-centos ~]# ss -tnlp | grep ssh

连接时通过-p参数指定端口

  1. [root@wing-centos ~]# ssh root@192.168.10.11 -p 28818