如何设置 SSH 以使其仅限于我的本地网络?

kle*_*ell 46 networking 11.10 lan

我正在尝试使用 SSH 通过路由器将运行 11.10 的新笔记本电脑链接到运行 8.04 的旧笔记本电脑。

这个问题是在 ubuntuforums 上提出和回答的:

http://ubuntuforums.org/showthread.php?t=1648965

我认为在这里有一个更明确的答案会有所帮助。

注意:我需要首先在我尝试连接的笔记本电脑上安装 openssh-server,并使用 firestarter 在我的防火墙中打开 SSH 端口。

Pan*_*her 56

您可以通过多种方式限制对 ssh 服务器的访问。

IMO 最重要的是使用 ssh 密钥并禁用密码身份验证。

有关详细信息,请参阅以下维基页面

您可以通过多种方式限制对特定子网的访问。我假设您的 ssh 服务器位于子网 192.168.0.0/16 上,IP 地址为 192.168.0.10,请相应调整;)

路由器

一道防线是使用路由器。请务必禁用 UPnP 并且不允许端口转发。

SSH 配置

您可以在/etc/ssh/sshd_config. 一是监听地址。如果您在子网上设置了侦听地址。私有IP 地址无法通过 Internet 路由。

ListenAddress 192.168.0.10
Run Code Online (Sandbox Code Playgroud)

您还可以使用 AllowUsers

AllowUsers you@192.168.0.0/16
Run Code Online (Sandbox Code Playgroud)

有点相关,你也可以改变端口

Port 1234
Run Code Online (Sandbox Code Playgroud)

请参阅:http : //manpages.ubuntu.com/manpages/precise/man5/sshd_config.5.html

TCP包装器

正如论坛帖子中所述,您可以使用 TCP Wrapper 。TCP 包装器使用 2 个文件,/etc/hosts.allow并且/etc/hosts.deny

编辑/etc/hosts.allow并添加您的子网

sshd : 192.168.0.
Run Code Online (Sandbox Code Playgroud)

编辑/etc/hosts.deny,并拒绝所有

ALL : ALL
Run Code Online (Sandbox Code Playgroud)

另见:http : //ubuntu-tutorials.com/2007/09/02/network-security-with-tcpwrappers-hostsallow-and-hostsdeny/

防火墙

最后你可以防火墙你的服务器。您可以使用 iptables、ufw 或 gufw。

iptables

sudo iptables -I INPUT -p tcp --dport 22 -s 192.168.0.0/16 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 22 -j REJECT
Run Code Online (Sandbox Code Playgroud)

请不要DROPiptables.

ufw

sudo ufw allow from 192.168.0.0/16 to any port 22
Run Code Online (Sandbox Code Playgroud)

ufw 有图形界面:gufw

广州市妇联


bru*_*ton 22

2020 更新

由于这个问题,现在可以使用OpenSSH 6.5/6.5p1 (2014) 中Match引入的关键字来实现一个简单的方法:

# Disable all auth by default
PasswordAuthentication no
PubkeyAuthentication no

[.. then, at the end of the file ..]

# Allow auth from local network
Match Address  192.168.1.*
    PubkeyAuthentication yes
    # if you want, you can even restrict to a specified user
    AllowUsers stephan
Run Code Online (Sandbox Code Playgroud)

man sshd_config 更多细节

  • 对于像我这样的菜鸟来说,这个文件位于 /etc/ssh/sshd_config (3认同)