• Linux修改ssh端口


    记录: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

    解析:查看原始文件,可以了解更多信息。

    1. # $OpenBSD: sshd_config,v 1.100 2016/08/15 12:32:04 naddy Exp $
    2. # This is the sshd server system-wide configuration file. See
    3. # sshd_config(5) for more information.
    4. # This sshd was compiled with PATH=/usr/local/bin:/usr/bin
    5. # The strategy used for options in the default sshd_config shipped with
    6. # OpenSSH is to specify options with their default value where
    7. # possible, but leave them commented. Uncommented options override the
    8. # default value.
    9. # If you want to change the port on a SELinux system, you have to tell
    10. # SELinux about this change.
    11. # semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
    12. #
    13. #Port 22
    14. #AddressFamily any
    15. #ListenAddress 0.0.0.0
    16. #ListenAddress ::
    17. HostKey /etc/ssh/ssh_host_rsa_key
    18. #HostKey /etc/ssh/ssh_host_dsa_key
    19. HostKey /etc/ssh/ssh_host_ecdsa_key
    20. HostKey /etc/ssh/ssh_host_ed25519_key
    21. # Ciphers and keying
    22. #RekeyLimit default none
    23. # Logging
    24. #SyslogFacility AUTH
    25. SyslogFacility AUTHPRIV
    26. #LogLevel INFO
    27. # Authentication:
    28. #LoginGraceTime 2m
    29. #PermitRootLogin yes
    30. #StrictModes yes
    31. #MaxAuthTries 6
    32. #MaxSessions 10
    33. #PubkeyAuthentication yes
    34. # The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
    35. # but this is overridden so installations will only check .ssh/authorized_keys
    36. AuthorizedKeysFile .ssh/authorized_keys
    37. #AuthorizedPrincipalsFile none
    38. #AuthorizedKeysCommand none
    39. #AuthorizedKeysCommandUser nobody
    40. # For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
    41. #HostbasedAuthentication no
    42. # Change to yes if you don't trust ~/.ssh/known_hosts for
    43. # HostbasedAuthentication
    44. #IgnoreUserKnownHosts no
    45. # Don't read the user's ~/.rhosts and ~/.shosts files
    46. #IgnoreRhosts yes
    47. # To disable tunneled clear text passwords, change to no here!
    48. #PasswordAuthentication yes
    49. #PermitEmptyPasswords no
    50. PasswordAuthentication yes
    51. # Change to no to disable s/key passwords
    52. #ChallengeResponseAuthentication yes
    53. ChallengeResponseAuthentication no
    54. # Kerberos options
    55. #KerberosAuthentication no
    56. #KerberosOrLocalPasswd yes
    57. #KerberosTicketCleanup yes
    58. #KerberosGetAFSToken no
    59. #KerberosUseKuserok yes
    60. # GSSAPI options
    61. GSSAPIAuthentication yes
    62. GSSAPICleanupCredentials no
    63. #GSSAPIStrictAcceptorCheck yes
    64. #GSSAPIKeyExchange no
    65. #GSSAPIEnablek5users no
    66. # Set this to 'yes' to enable PAM authentication, account processing,
    67. # and session processing. If this is enabled, PAM authentication will
    68. # be allowed through the ChallengeResponseAuthentication and
    69. # PasswordAuthentication. Depending on your PAM configuration,
    70. # PAM authentication via ChallengeResponseAuthentication may bypass
    71. # the setting of "PermitRootLogin without-password".
    72. # If you just want the PAM account and session checks to run without
    73. # PAM authentication, then enable this but set PasswordAuthentication
    74. # and ChallengeResponseAuthentication to 'no'.
    75. # WARNING: 'UsePAM no' is not supported in Red Hat Enterprise Linux and may cause several
    76. # problems.
    77. UsePAM yes
    78. #AllowAgentForwarding yes
    79. #AllowTcpForwarding yes
    80. #GatewayPorts no
    81. X11Forwarding yes
    82. #X11DisplayOffset 10
    83. #X11UseLocalhost yes
    84. #PermitTTY yes
    85. #PrintMotd yes
    86. #PrintLastLog yes
    87. #TCPKeepAlive yes
    88. #UseLogin no
    89. #UsePrivilegeSeparation sandbox
    90. #PermitUserEnvironment no
    91. #Compression delayed
    92. #ClientAliveInterval 0
    93. #ClientAliveCountMax 3
    94. #ShowPatchLevel no
    95. #UseDNS yes
    96. #PidFile /var/run/sshd.pid
    97. #MaxStartups 10:30:100
    98. #PermitTunnel no
    99. #ChrootDirectory none
    100. #VersionAddendum none
    101. # no default banner path
    102. #Banner none
    103. # Accept locale-related environment variables
    104. AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
    105. AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
    106. AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
    107. AcceptEnv XMODIFIERS
    108. # override default of no subsystems
    109. Subsystem sftp /usr/libexec/openssh/sftp-server
    110. # Example of overriding settings on a per-user basis
    111. #Match User anoncvs
    112. # X11Forwarding no
    113. # AllowTcpForwarding no
    114. # PermitTTY no
    115. # ForceCommand cvs server

    以上,感谢。

    2022年11月27日

  • 相关阅读:
    【计算机视觉 | 目标检测】arxiv 计算机视觉关于目标检测的学术速递(5月26日论文合集)
    【leetcode】【2022/9/6】828. 统计子串中的唯一字符
    基于java《组成原理》课程智能组卷计算机毕业设计源码+系统+lw文档+mysql数据库+调试部署
    【图像去噪】基于非线性扩散PM算法实现图像去噪附matlab代码
    RDD编程进阶自定义累加器_大数据培训
    波奇学Linux:日志
    MR756-ASEMI汽车用整流二极管MR756
    stm32----用状态机判断单双击
    Linux测试常用命令
    activiti-bpmn-converter
  • 原文地址:https://blog.csdn.net/zhangbeizhen18/article/details/128061979