我已禁用SSH,并使用公钥-私钥进行登录。但是,我看到的PasswordAuthentication许多指南(特别是本指南)建议仅允许从我的特定 IP 地址进行 SSH 访问。
/etc/ssh/sshd_config:
AllowUsers deploy@(your-ip) deploy@(another-ip-if-any)
ufw allow from {your-ip} to any port 22
Run Code Online (Sandbox Code Playgroud)
sshd_config在添加防火墙规则中通过 IP 地址进行限制有什么意义吗?
假设我有一个动态 IP 地址,那么这样的规则将显得毫无用处,因为我必须使用托管提供商控制台终端,登录到 root 以允许从我的家庭 IP 进行访问。在这种情况下:建议的路线是什么?
1:禁止所有通过 SSH 登录,除非我首先使用托管提供商终端,登录 root 以允许从我的家庭 IP 访问 SSH?
2:保持SSH端口打开并信任用于登录的公私钥。
3:还有其他建议吗?
好吧,我做了很多挖掘,但一无所获。因此,我使用动态 dns 服务设置了我的家用计算机,并创建了以下 bash 脚本,希望它对其他人有用。
该脚本并非特定于 SSH,而是一种允许动态主机使用 ufw 永久访问目标端口的方法 - 与在该端口上侦听的服务无关。因此,使用端口 22(默认情况下)将允许从动态主机永久访问 SSH。同样,您可以使用此脚本来确保从动态主机永久访问 FTP(端口 21)等内容。
所需要做的就是修改文件顶部的RemoteHostName和变量。PortNumber显然,这RemoteHostName是动态 DNS 提供商提供的主机名,也是PortNumber我们允许访问的端口。
如果从 ufw 中解析出的 IPRemoteHostName已存在且可以访问目标端口,则脚本将退出,因为没有任何可更改的内容。
如果不存在,则删除 ufw 中具有目标端口号的所有条目,然后使用更新的 IP 地址和目标端口添加新条目。简而言之,不要尝试将 ufw 用于脚本中涉及端口的其他规则,因为脚本将遍历它们。
如果这个脚本被 cron 作业或其他东西调用,那么理论上它应该始终在 ufw 中保持 IP 地址更新。
该脚本输出以下内容:
1) Standard output: Could not resolve the IP address of the RemoveHostName XYZ
2) Standard output: No change necessary as the IP address XYZ already has access to port XYZ.
3) Standard output: Deleting old ufw rule: XYZ
4) Standard output: Adding new ufw rule: XYZ
5) Error output: Could not resolve the IP address of the RemoveHostName XYZ
Run Code Online (Sandbox Code Playgroud)但请注意,我不太熟悉 bash 脚本,也不熟悉正则表达式。因此,如果您使用此脚本,请帮自己(和我!)一个忙并查看一下它。如果您发现任何错误,请留下评论和/或新答案。
1) Standard output: Could not resolve the IP address of the RemoveHostName XYZ
2) Standard output: No change necessary as the IP address XYZ already has access to port XYZ.
3) Standard output: Deleting old ufw rule: XYZ
4) Standard output: Adding new ufw rule: XYZ
5) Error output: Could not resolve the IP address of the RemoveHostName XYZ
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3061 次 |
| 最近记录: |