3

#Ubuntu 更改ssh默认连接端口

 2 years ago
source link: https://xmanyou.com/ubuntu-change-ssh-port/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
8 March 2021 / Ubuntu

#Ubuntu 更改ssh默认连接端口

ssh默认的连接端口是22

但是,为了安全起见,一般会改成其他端口,具体步骤如下

  • 0). 开启云服务器的对应端口
    如果是阿里云之类的云服务器,需要先从云服务器后台打开对应的端口。

  • 1). 修改/etc/ssh/sshd_config
    查看当前ssh的端口

grep -i port /etc/ssh/sshd_config
#Port 22
#GatewayPorts no

表示当前端口是22

使用sudo修改#Port这行,将22改成想要的端口,比如6666

sudo vi /etc/ssh/sshd_config
...
Port 6666
...
  • 2). 重启sshd服务
sudo systemctl restart sshd

查看是否生效

sudo netstat -tulpn | grep ssh
tcp        0      0 0.0.0.0:6666           0.0.0.0:*               LISTEN      18138/sshd
  • 3). 重新连接
ssh <server-ip> -p <新端口>

其他副作用

客户端连接时,如果发现以下错误:

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.

ssh-known-host-key-changed

表示客户端之前已经连接过,需要修改 .ssh/known_hosts

删掉之前的host记录,然后重新连接即可。

阿斌

Read more posts by this author.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK