在Linux中更改SSH默认端口号22为自己指定的端口号

SSH或安全SSH守护程序是一种网络协议,它用于通过在不安全网络上使用强密码的安全通道执行对Linux系统的远程安全登录。

SSH协议的最基本功能之一是访问远程Linux机器上的Unix Shell并且执行命令的能力。但SSH协议能够提供其它实现,诸如在这个协议上创建安全的TCP通道,在机器之间远程和安全地传输文件或者作为像FTP服务的能力。

SSH服务使用的标准端口是22/TCP。但,为了通过不常用端口号(因为标准的22/TCP端口一直是作为网络中黑客和肉鸡攻击的目标)实现某种安全,你可能想要在你的Linux服务器中更改SSH默认端口。

要在Linux中更改SSH服务默认端口,首先你需要通过发出以下命令用你的文本编辑器打开主SSH守护程序配置文件用来编辑并且进行以下更改。

[root@localhost blctrl]# vim /etc/ssh/sshd_config

在ssh_config文件中,搜索以Port 22开头的行并且通过在这行之前添加#注释掉这一行。在这行之下,添加一新端口行并且指定所需端口绑定到SSH。

在本例中,我们将配置SSH服务绑定并且在端口6666/TCP上监听。确认你选择了一个随机端口,最好高于1024。可为为SSH设置的最大端口是65535/TCP。

[root@localhost blctrl]# vim /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
Port 6666
#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

在你完成以上更改后,重启SSH守护程序反映变化并且发出netstat或者ss命令来确认SSH服务监听在了新的TCP端口。在CentOS或基于RHEL的发行版中,为了SSH守护程序绑定在新端口,安装policycoreutils包并且添加以下规则到SELinux策略中。

[root@localhost blctrl]# yum install policycoreutils
[root@localhost blctrl]# semanage port -a -t ssh_port_t -p tcp 6666
[root@localhost blctrl]# semanage port -m -t ssh_port_t -p tcp 6666
[root@localhost blctrl]# systemctl restart sshd
[root@localhost blctrl]# ss -tnlp | grep ssh
LISTEN     0      128          *:6666                     *:*                   users:(("sshd",pid=7446,fd=3))
LISTEN     0      128    127.0.0.1:6010                     *:*                   users:(("sshd",pid=7500,fd=10))
LISTEN     0      128       [::]:6666                  [::]:*                   users:(("sshd",pid=7446,fd=4))
LISTEN     0      128      [::1]:6010                  [::]:*                   users:(("sshd",pid=7500,fd=9))

为了在这个新添加的SSH端口上允许建立到来的连接,也不要忘记专用于你自己安装的Linux发行版更新防火墙规则。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值