记录:340
场景:在CentOS 7.9操作系统上,修改默认的ssh端口。ssh默认22端口,在实际开展业务中的生产环境中,通常会修改为指定端口号,以满足规范。
版本:
操作系统:CentOS 7.9
1.修改sshd_config配置文件
修改命令:vi /etc/ssh/sshd_config
修改内容:Port 10022
解析:在/etc/ssh/目录下,是ssh相关的配置信息。
2.重启sshd服务
重启命令:systemctl restart sshd
解析:修改端口后,需重启服务
3.查看端口信息
查看命令:netstat -atunlp | grep sshd
解析:使用netstat查看监听的端口和进程。

4.查看防火墙状态
查看状态:systemctl status firewalld
解析:查看防火墙状态,如果是Active: active (running),则已经开启防火墙。如果是Active: inactive (dead),则已关闭防火墙。
5.把端口10022添加到防火墙开放端口列表
添加端口:firewall-cmd --zone=public --add-port="10022"/tcp --permanent
解析:把10022端口持久化配置到开放端口列表中。
6.重新加载防火墙
命令:firewall-cmd --reload
解析:加载防火墙,使最新配置生效。
7.查看端口防火墙放行状态
命令:firewall-cmd --permanent --query-port="10022"/tcp
解析:查看端口防火墙已放行。
8.查看防火墙已放行的端口号列表
命令:firewall-cmd --zone=public --list-ports
解析:查看防火墙已放行的端口号列表。
9.使用Xshell客户端连接服务器
确认修改端口后,且开通防火墙后,可以使用Xshell等客户端工具连接服务器。
10.小结
(1)sshd服务支持配置多个端口号
ssh的配置文件sshd_config支持设置多个端口,比如22和10022可以都使用。

(2)确认新增端口可以使用后再关闭22端口
在实际工作中,确认新增端口可以使用后,再关闭22端口。因为一旦重启sshd服务后,22端口失效了。如果只修改了sshd端口,而没有开放防火墙,那么客户端是连接不上服务器。
这种情况,一旦发生,极有可能要到机房操作。一般而言,进机房审批流程等非常麻烦。因此,修改系统级别配置时,需多次自行确认无误,再下手操作。
11.附录sshd_config原始文件
命令:cat /etc/ssh/sshd_config
解析:查看原始文件,可以了解更多信息。
- # $OpenBSD: sshd_config,v 1.100 2016/08/15 12:32:04 naddy Exp $
-
- # This is the sshd server system-wide configuration file. See
- # sshd_config(5) for more information.
-
- # This sshd was compiled with PATH=/usr/local/bin:/usr/bin
-
- # The strategy used for options in the default sshd_config shipped with
- # OpenSSH is to specify options with their default value where
- # possible, but leave them commented. Uncommented options override the
- # default value.
-
- # 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
- #
- #Port 22
- #AddressFamily any
- #ListenAddress 0.0.0.0
- #ListenAddress ::
-
- HostKey /etc/ssh/ssh_host_rsa_key
- #HostKey /etc/ssh/ssh_host_dsa_key
- HostKey /etc/ssh/ssh_host_ecdsa_key
- HostKey /etc/ssh/ssh_host_ed25519_key
-
- # Ciphers and keying
- #RekeyLimit default none
-
- # Logging
- #SyslogFacility AUTH
- SyslogFacility AUTHPRIV
- #LogLevel INFO
-
- # Authentication:
-
- #LoginGraceTime 2m
- #PermitRootLogin yes
- #StrictModes yes
- #MaxAuthTries 6
- #MaxSessions 10
-
- #PubkeyAuthentication yes
-
- # The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
- # but this is overridden so installations will only check .ssh/authorized_keys
- AuthorizedKeysFile .ssh/authorized_keys
-
- #AuthorizedPrincipalsFile none
-
- #AuthorizedKeysCommand none
- #AuthorizedKeysCommandUser nobody
-
- # For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
- #HostbasedAuthentication no
- # Change to yes if you don't trust ~/.ssh/known_hosts for
- # HostbasedAuthentication
- #IgnoreUserKnownHosts no
- # Don't read the user's ~/.rhosts and ~/.shosts files
- #IgnoreRhosts yes
-
- # To disable tunneled clear text passwords, change to no here!
- #PasswordAuthentication yes
- #PermitEmptyPasswords no
- PasswordAuthentication yes
-
- # Change to no to disable s/key passwords
- #ChallengeResponseAuthentication yes
- ChallengeResponseAuthentication no
-
- # Kerberos options
- #KerberosAuthentication no
- #KerberosOrLocalPasswd yes
- #KerberosTicketCleanup yes
- #KerberosGetAFSToken no
- #KerberosUseKuserok yes
-
- # GSSAPI options
- GSSAPIAuthentication yes
- GSSAPICleanupCredentials no
- #GSSAPIStrictAcceptorCheck yes
- #GSSAPIKeyExchange no
- #GSSAPIEnablek5users no
-
- # Set this to 'yes' to enable PAM authentication, account processing,
- # and session processing. If this is enabled, PAM authentication will
- # be allowed through the ChallengeResponseAuthentication and
- # PasswordAuthentication. Depending on your PAM configuration,
- # PAM authentication via ChallengeResponseAuthentication may bypass
- # the setting of "PermitRootLogin without-password".
- # If you just want the PAM account and session checks to run without
- # PAM authentication, then enable this but set PasswordAuthentication
- # and ChallengeResponseAuthentication to 'no'.
- # WARNING: 'UsePAM no' is not supported in Red Hat Enterprise Linux and may cause several
- # problems.
- UsePAM yes
-
- #AllowAgentForwarding yes
- #AllowTcpForwarding yes
- #GatewayPorts no
- X11Forwarding yes
- #X11DisplayOffset 10
- #X11UseLocalhost yes
- #PermitTTY yes
- #PrintMotd yes
- #PrintLastLog yes
- #TCPKeepAlive yes
- #UseLogin no
- #UsePrivilegeSeparation sandbox
- #PermitUserEnvironment no
- #Compression delayed
- #ClientAliveInterval 0
- #ClientAliveCountMax 3
- #ShowPatchLevel no
- #UseDNS yes
- #PidFile /var/run/sshd.pid
- #MaxStartups 10:30:100
- #PermitTunnel no
- #ChrootDirectory none
- #VersionAddendum none
-
- # no default banner path
- #Banner none
-
- # Accept locale-related environment variables
- AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
- AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
- AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
- AcceptEnv XMODIFIERS
-
- # override default of no subsystems
- Subsystem sftp /usr/libexec/openssh/sftp-server
-
- # Example of overriding settings on a per-user basis
- #Match User anoncvs
- # X11Forwarding no
- # AllowTcpForwarding no
- # PermitTTY no
- # ForceCommand cvs server
以上,感谢。
2022年11月27日