SSH/zh-cn

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

SSH已经发展成为一组软件系列，除了提供用于远程终端访问的ssh这个主要的程序，还包括其他的工具如 scp (secure copy) 和 sftp (secure file transfer protocol).

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

安装
多数的部署已经安装了OpenSSH(通过软件包). 这个软件包使用的USE标记如下：

修改USE标记之后，不要忘记重新编译OpenSSH:

服务器端
OpenSSH 服务器可以通过 /etc/init.d/ssh start，/etc/init.d/ssh stop 和 /etc/init.d/ssh restart 来控制，就像其它 OpenRC 管理的服务一样.

将openssh添加到默认的运行级别，如果之前没有做的话.

用下面的命令启动sshd守护进程：

OpenSSH服务器可以像其他的由OpenRC管理的服务那样控制：

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

在第一次启动SSH服务的时候，会生成系统密钥. 密钥可以使用 （重新）生成.

生成用于SSH协议版本1的密钥（通常并不启用，因为已经被废弃，取而代之的是协议版本2）：

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

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

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

客户端配置
ssh 客户端与相关的程序（scp，sftp 等等）可以通过下面的文件进行配置：

更多的信息请阅读 手册：

无密码验证
对于管理 git 服务器非常便利.

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

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

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

故障排除
There are 3 different levels of debug modes that can help troubleshooting issues. With -v ssh prints debugging messages about its progress. This is helpful in debugging connection, authentication, and configuration problems. Multiple -v options increase the verbosity. The maximum is 3.

Death of Long-lived Connections
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, though the ssh 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 ClientAliveInterval 30 (or some other value, in seconds) within the file.
 * To enable keep alive for all servers connected to by your local client, set ServerAliveInterval 30 (or some other value, in seconds) within the file.

X11 Forwarding Not Forwarding or Tunneling!
Problem: After you have made the necessary changes to the configuration files for permitting X11 Forwarding, you find X applications are executing on the server and are not being forwarded to the client.

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

Test for this scenario as follows after logging in remotely:

You should get something similar to "localhost:10.0" or "localhost2.local:10.0" using server side  setting. If you're getting the usual ":0.0", check to make sure you are not unsetting or initializing the  variable within. If so, remove or comment your custom initialization of  or prevent  from executing during SSH login:

A trick would be to alias this within.

参考

 * Securing OpenSSH - Gentoo开发者文档
 * OpenSSH Key Management Part1 - Gentoo文档, 请将全部的3部分都看一下
 * Gentoo Linux Keychain Guide
 * autossh - 侦测何时SSH连接被断开并自动重新连接