多个端口上的 SSH 服务 运行,自定义规则在 Linux

SSH service running on multiple ports with custom rules in Linux

我正致力于在多个端口(例如端口 22 和 5522)上设置具有 SSH 服务 运行 的服务器,这些端口应该具有一组不同的规则,即:我们添加的规则端口 22 不应与端口 5522 的规则冲突。

最初,可以通过将以下行添加到 /etc/ssh/sshd_config.

来使 SSH 服务监听多个端口
Port 22
Port 5522

在这种情况下,您不能为不同的端口定义不同的规则。

我能找到的解决方案之一是在端口 5522 上为 运行 SSH 服务创建一个新服务,然后 运行将该服务作为守护进程。

为此,请按照以下步骤操作:-

  1. 创建SSH服务的副本并命名,这里我将副本命名为sshd_config_custom
cp /etc/ssh/sshd_config /etc/ssh/sshd_config_custom
  1. 同样,也创建服务的副本。
cp /lib/systemd/system/ssh.service /lib/systemd/system/sshd-custom.service
  1. 使用任何舒适的编辑器打开 /lib/systemd/system/sshd-custom.service 并更改
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

保存并退出文件。

  1. 现在您可以在 /etc/ssh/sshd_config_custom 中添加行 Port 5522 并且可以对该 conf 文件进行任何必要的更改。

  2. 启用并启动我们创建的自定义服务。

systemctl enable sshd-custom.service
systemctl start sshd-custom.service

如果有任何其他建议,请告诉我