SSH/zh-cn

SSH (Secure SHell) 是一个加密的终端程序，用于替代类Unix操作系统上传统的 telnet 工具.

SSH已经发展成为一组软件系列，除了提供用于远程终端访问的 这个主要的程序，还包括其他的工具如 (Secure Copy Program) 和  (Secure File Transfer Protocol).

最初，SSH并不是免费的. 然而，当今最流行并成为实际标准的SSH实现是 OpenBSD 的OpenSSH，它在Gentoo中已预安装.

检查安装
Most deployments of Gentoo Linux will already have OpenSSH installed on the system. This can be checked by running the command. If it is installed a usage statement should be printed:

If no usage statement is printed is either corrupted or not installed. It is also possible that a user is simply rebuilding OpenSSH to include a new USE configuration. Whatever the case, proceed on to view possible USE settings.

Emerge
在修改了必要的 USE 标志之后，不要忘记安装（或重装） OpenSSH：

创建密钥
为了提供一个安全的 shell，加密的密钥用于管理 SSH 提供的加密、解密和哈希功能.

On the first start of the SSH service, system keys will be generated. Keys can be (re)generated using the command.

生成用于 SSH 协议版本1的密钥（通常并不再使用，取而代之的是协议版本2）：

生成SSH协议2的密钥（DSA和RSA算法）：

服务器端配置
SSH服务器配置文件通常是 ，虽然也有可能通过OpenRC的文件进行进一步的配置，包括修改配置文件的位置. 关于如何配置服务器的详细信息请参考sshd_config man page.

您也应该学习这篇偏重于安全配置的Sven 的 OpenSSH 指南.

客户端配置
The client and related programs (,, etc.) can be configured using the following files:



更多的信息请阅读 手册：

Passwordless authentication
对于管理 git 服务器非常便利.

客户端
在客户端执行下面的命令：

服务器
确保服务器上存在用户的帐号，然后把客户端的 文件拷贝到服务器端用户 home 目录的  文件.

单机测试
上面的步骤可以在本地测试：

Intrusion prevention
SSH is a commonly attacked service. Tools such as sshguard and fail2ban monitor logs and black list remote users who have repeatedly attempted, yet failed to login. Utilize them as needed to secure a frequently attacked system.

OpenRC
Add the OpenSSH daemon to the default runlevel:

Start the sshd daemon with:

The OpenSSH server can be controlled like any other OpenRC-managed service:

Systemd
To have the OpenSSH daemon start when the system starts:

To start the OpenSSH daemon now:

To check if the service has started:

Troubleshooting
有三种不同级别的调试模式可以帮助排除故障. 配合  参数 SSH 会输出关于其进度的调试信息. 这有助于调试连接、验证和配置的问题. 多个  参数会增加调试信息的详细程度. 最大的详细程度是三级.

长连接被关闭
Many internet access devices perform Network Address Translation (NAT), a process that enables devices on a private network such as that typically found in a home or business place to access foreign networks, such as the internet, despite only having a single IP address on that network. Unfortunately, not all NAT devices are created equal, and some of them incorrectly close long-lived, occasional-use TCP connections such as those used by SSH. This is generally observable as a sudden inability to interact with the remote server, even though the client program has not exited.

In order to resolve the issue, OpenSSH clients and servers can be configured to send a 'keep alive', or invisible message aimed at maintaining and confirming the live status of the link:


 * To enable keep alive for all clients connecting to your local server, set  (or some other value, in seconds) within the  file.
 * To enable keep alive for all servers connected to by your local client, set  (or some other value, in seconds) within the  file.

X11转发/隧道不工作
问题: 在对配置文件做了必要的修改以允许 X11 转发, 却发现 X 应用程序在服务器端执行却没有转发到客户端.

Solution: What is likely occurring during SSH login into the remote server or host, the DISPLAY variable is either being unset or is being set after the SSH session sets it.

远程登录后按如下步骤测试这一现象：

The output should be something similar to  or   using server side   setting. If the usual  is not displayed, check to make sure the DISPLAY variable within  is not being unset or re-initializing. If it is, remove or comment out any custom initialization of the DISPLAY variable to prevent the code in from executing during a SSH login:

Be sure to substitute  in the command above with the proper username.

一个小技巧是在用户的 中将此命令定义为一个 alias.

参考

 * Securing the SSH service in the Gentoo Security Handbook
 * Gentoo Linux Keychain Guide
 * autossh - Detects when SSH connections drop and auto-reconnects.
 * SCP - A Secure Copy Program that comes with the SSH suite.
 * SFTP - Secure File Transfer Protocol client that comes with the SSH suite.
 * SSHFS - A FUSE based SSH powered mount client.

外部资源

 * Securing OpenSSH - Gentoo developer documentation.