问题描述
我正在设置一个运行在多个端口上的 SSH 服务的服务器,比如端口 22 和 5522,这些端口应该有一组不同的规则,即:我们为端口 22 添加的规则不应该冲突使用 5522 端口的规则.
I was working on setting up a server with SSH service running on multiple ports like say, port 22, and 5522 and these port should be having a different set of rules, ie: The rules we added for port 22 should not conflict with the rules for port 5522.
推荐答案
最初,可以通过在/etc/ssh/sshd_config
中添加以下行来使SSH服务监听多个端口.
Initially, the SSH service can be made to listen to multiple ports by adding the following line to /etc/ssh/sshd_config
.
Port 22
Port 5522
在这种情况下,您不能为不同的端口定义不同的规则.
In this scenario, you cannot define different rules for different ports.
我能找到的解决方案之一是创建一个新服务以在端口 5522 上运行 SSH 服务,然后将该服务作为守护进程运行.
One of the solutions I could find is to create a new service to run SSH service on port 5522 and then running the service as daemon.
为此,请按照以下步骤操作:-
To do so please follow steps below:-
- 创建SSH服务的副本并命名,这里我将副本命名为
sshd_config_custom
cp /etc/ssh/sshd_config /etc/ssh/sshd_config_custom
- 同样,也可以创建服务的副本.
cp /lib/systemd/system/ssh.service /lib/systemd/system/sshd-custom.service
- 使用任何舒适的编辑器打开
/lib/systemd/system/sshd-custom.service
并进行更改
- open
/lib/systemd/system/sshd-custom.service
using any comfortable editor and change
ExecStart=/usr/sbin/sshd -D $SSHD_OPTS
到
ExecStart=/usr/sbin/sshd -D $SSHD_OPTS -f /etc/ssh/sshd_config_custom
和
Alias=sshd.service
到
Alias=sshd-custom.service
保存并退出文件.
现在您可以在
/etc/ssh/sshd_config_custom
中添加Port 5522
行,并且可以对这个 conf 文件进行任何必要的更改.
Now you can add the line
Port 5522
in/etc/ssh/sshd_config_custom
and can make any required changes to this conf file.
启用并启动我们创建的自定义服务.
Enable and start the custom service that we have created.
systemctl enable sshd-custom.service
systemctl start sshd-custom.service
如果有其他建议请告诉我
Let me know if there is any other suggestions
这篇关于在 Linux 中使用自定义规则在多个端口上运行的 SSH 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!