Tango-preferences-desktop-locale-modified.png这篇文章或章节的翻译不反映原文。Tango-preferences-desktop-locale-modified.png

原因:Partial translation, missing a huge chunk of the English page.(在 Talk:OpenSSH (简体中文)# 中讨论)

OpenSSH(OpenBSD Secure Shell)是使用Secure Shell(SSH)协议,在计算机网络上提供加密通信会话的程序包。它是 SSH Communications Security 提供的专有 Secure Shell 软件的开源替代方案。OpenSSH 是 OpenBSD 项目的一部分,该项目由 Theo de Raadt 领导。

OpenSSH 有时候会与名字相似的 OpenSSL 相混淆;然而,它们的目的不同,开发团队也不同,名字相似只因为两者目标相似。

安装

安装 openssh 包。

客户端用法

连接服务端:

$ ssh -p 端口 用户名@服务器地址

如果服务端只允许公钥认证,参考 SSH 密钥

配置

客户端可以配置一些通用的参数和主机信息。所有的参数都可以被声明为全局的或者限制为特定主机,例如:

~/.ssh/config
# global options
User 用户名

# host-specific options
Host myserver
    Hostname 服务器地址
    Port     端口

这样配置之后,下面的命令是等价的:

$ ssh -p 端口 用户名@服务器地址
$ ssh myserver

参考 ssh_config(5) 可以获得更多的信息。

一些参数没有对应的命令行选项,但你可以在命令行上使用 -o 指定配置选项。例如 -oKexAlgorithms=+diffie-hellman-group1-sha1

服务端用法

sshd 是 OpenSSH 服务器守护程序,由 /etc/ssh/sshd_config 配置并由 sshd.service 管理。每次更改配置后,请在重新启动服务前在测试模式下运行 sshd 以确保它能够干净地启动。有效配置将不会产生输出。

# sshd -t

配置

要仅允许某些用户访问,请添加以下一行:

AllowUsers    用户1 用户2

要仅允许某些用户组访问:

AllowGroups   用户组1 用户组2

要添加漂亮的欢迎消息(例如,输出 /etc/issue 文件),请配置 Banner 选项:

Banner /etc/issue

Public and private host keys are automatically generated in /etc/ssh by the sshdgenkeys service and regenerated if missing even if HostKeyAlgorithms option in sshd_config allows only some. Four key pairs are provided based on the algorithms dsa, rsa, ecdsa and ed25519. 要让 sshd 使用特定的密钥,请指定以下选项:

HostKey /etc/ssh/ssh_host_rsa_key

如果服务器要暴露在 WAN 中,建议将默认端口从 22 更改为更高的随机端口,例如:

Port 39901
提示:
  • 要选择尚未分配给常见服务的备用端口,请查看 TCP 和 UDP 端口号列表。还可以在本地的 /etc/services 中查找端口信息。A port change from default port 22 will reduce the number of log entries caused by automated authentication attempts but will not eliminate them. See Port knocking for related information.
  • 建议完全禁用密码登录。这将大大提高安全性,更多信息请参见#强制公钥认证。更多推荐的安全方法,请参阅#保护
  • OpenSSH 可以监听多个端口,只需在配置文件中添加多个 Port port_number
  • 可以通过从 /etc/ssh 中删除要替换的主机密钥对并以 root 身份运行 ssh-keygen -A 来生成新的(或缺失的)主机密钥对。

管理守护程序

启动/启用 sshd.service。它将保持 SSH 守护程序始终处于活动状态,并为每个传入连接 fork。[1]

注意: openssh 8.0p1-3 removed sshd.socket that used systemd's socket activation due to it being susceptible to denial of service. See FS#62248 for details. If sshd.socket is enabled when updating to openssh 8.0p1-3, the sshd.socket and sshd@.service units will be copied to /etc/systemd/system/ and reenabled. This is only done to not break existing setups, users are still advised to migrate to sshd.service.
警告: If you continue using sshd.socket, be aware of its issues:
  • sshd.socket unit may fail (e.g. due to out-of-memory situation) and Restart=always cannot be specified on socket units. See systemd issue 11553.
  • Using socket activation can result in denial of service, as too many connections can cause refusal to further activate the service. See FS#62248.
注意: Using sshd.socket negates the ListenAddress setting, so it will allow connections over any address. To achieve the effect of setting ListenAddress, you must specify the port and IP for ListenStream (e.g. ListenStream=192.168.1.100:22) by editing sshd.socket. You must also add FreeBind=true under [Socket] or else setting the IP address will have the same drawback as setting ListenAddress: the socket will fail to start if the network is not up in time.
提示: When using socket activation a transient instance of sshd@.service will be started for each connection (with different instance names). Therefore, neither sshd.socket nor the daemon's regular sshd.service allow to monitor connection attempts in the log. The logs of socket-activated instances of SSH can be seen by running journalctl -u "sshd@*" as root or by running journalctl /usr/bin/sshd as root.

保护

允许通过 SSH 远程登录有利于管理,但可能会威胁服务器的安全。由于 SSH 访问通常是暴力攻击的目标,因此需要适当限制 SSH 访问以防止第三方访问服务器。

ssh-audit 提供对服务端和客户端配置的自动分析。关于这个主题,还有其他几个很好的指南和工具,例如:

强制公钥认证

如果客户端无法通过公钥进行身份验证,SSH 服务端默认会退回到使用密码进行身份验证,从而允许恶意用户尝试通过暴力穷举密码来获取访问权限。防止这种攻击的最有效方法之一是完全禁用密码登录,并强制使用 SSH 密钥。这可以通过在守护程序配置文件中设置以下选项来完成:

/etc/ssh/sshd_config
PasswordAuthentication no
AuthenticationMethods publickey
警告: 在将此添加到配置之前,请确保所有需要 SSH 访问的帐户都在相应的 authorized_keys 文件中设置了公钥身份验证。详细信息请参阅 SSH 密钥#将公钥复制到远程服务器上

双重身份认证

身份验证提供程序
PAM 设置

防止暴力穷举攻击

限制root用户登陆

拒绝
限制

对 authorized_keys 文件加锁

参见