为多个ssh端口分别设置密码连接,密匙连接,指定ip允许连接
一、仅配置多个SSH端口
1.修改sshd_config配置文件
打开配置文件
sudo vim /etc/ssh/sshd_config
添加多个端口:
添加
Port 22
Port 2222
Port 10022
允许密码认证:
PasswordAuthentication yes
允许密匙认证:
PubkeyAuthentication yes
不允许设置成 no
即可
限定特定ip可以连接:
例如,允许 192.168.1.100
连接
AllowUsers username@192.168.1.100
username
是允许连接的用户名。
root登陆设置:
不允许
PermitRootLogin no
仅密匙登陆
PermitRootLogin prohibit-password
2.重启SSH服务
修改配置文件后,需要重启SSH服务以使更改生效:
sudo systemctl restart ssh
3.配置防火墙
确保防火墙允许这些端口的流量:
sudo ufw allow 22/tcp
sudo ufw allow 2222/tcp
sudo ufw allow 10022/tcp
然后检查防火墙状态
sudo ufw status
二、为多个ssh端口分别配置
以2222端口为例
1.为该端口单独配置一个 sshd_config
文件
创建新的配置文件
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config_port_2222
打开新文件
sudo vim /etc/ssh/sshd_config_port_2222
修改或添加
Port 2222
#如上修改密码,密匙,ip等配置
启动额外的SSH服务
sudo /usr/sbin/sshd -f /etc/ssh/sshd_config_port_2222
停止服务
sudo pkill -f sshd_config_port_2222
3.配置防火墙
确保防火墙允许这两个端口的流量。
三、开机自启
1.创建新的systemd服务单元文件
创建 sshd_config_port_2222.service
:
sudo vim /etc/systemd/system/sshd_config_port_2222.service
添加
[Unit]
Description=OpenBSD Secure Shell server
After=sshd.service
[Service]
ExecStart=/usr/sbin/sshd -f /etc/ssh/sshd_config_port_2222
[Install]
WantedBy=multi-user.target
2.重新加载systemd配置
sudo systemctl daemon-reload
3.启用并启动新的SSH服务
sudo systemctl enable sshd_config_port_2222
sudo systemctl start sshd_config_port_2222
4.检查服务状态
检查两个SSH服务是否正常运行:
sudo systemctl status sshd
sudo systemctl status sshd_config_port_2222
5.验证端口是否生效
sudo netstat -tulnp | grep sshd
输出显示SSH服务正在监听两个端口:
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1234/sshd
tcp 0 0 0.0.0.0:2222 0.0.0.0:* LISTEN 5678/sshd
通过以上步骤,可以配置不同的ssh端口,增强系统的灵活性和安全性。