SSH

SSH (Secure SHell) is Article description::the ubiquitous tool for loging into and working on remote machines securely. All sensitive information is strongly encrypted, and in addition to the remote shell, SSH supports file transfer, and port forwarding for arbitrary protocols, allowing secure access to remote services. It replaces the classic telnet, rlogin, and similar non-secure tools - but SSH is not just a remote shell, it is a complete environment for working with remote systems.

In addition to the main command, the SSH suite of programs includes tools such as scp (Secure Copy Program), sftp (Secure File Transfer Protocol), or Keychain to help with key management. The standard SSH port is port 22.

Several versions of SSH have existed. Today the most popular implementation of SSH, and de-facto standard, is OpenBSD's OpenSSH. This comes pre-installed on Gentoo, and is published under a BSD ("and freer") license.

SSH is multi-platform, and is very widely used: OpenSSH is installed by default on most Unix-like OSs, on Windows10, on MacOS, and can be installed on Android or "jailbroken" iOS (SSH clients are available). This makes SSH a great tool for working with heterogeneous systems.

Check install
Deployments of Gentoo Linux should already have OpenSSH installed, as the package is part of the system set. The presence and proper functioning of OpenSSH can be checked by running the command, which should output a usage statement:

If no usage statement is printed, OpenSSH may be corrupt, or not installed. Try re-installation by following the emerge section, just as if rebuilding after a USE flag change. If OpenSSH were uninstalled, this should reinstall it. It should then remain installed, as part of the system set.

If this does not try to install OpenSSH, the package may have been masked, or even listed in package.provided, though this would be unusual.

Emerge
After changing USE flags just for the OpenSSH package, rebuild OpenSSH for the new flags to be applied. As OpenSSH is in the system set,  should be used to avoid adding it to the world file:

After changing any global USE flags in make.conf that affect the OpenSSH package, emerge world to update to the new USE flags:

Create keys
In order to provide a secure shell, cryptographic keys are used to manage the encryption, decryption, and hashing functionalities offered by SSH.

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

To generate the keys for SSH protocol version 2 (DSA and RSA algorithms):

The article Secure Secure Shell suggests using Ed25519 and RSA public key algorithms with:

Server configuration
The SSH server is usually configured in the file, though it is also possible to perform further configuration in OpenRC's, including changing the location of the configuration file. For detailed information on how to configure the server see the sshd_config man page.

The server provides means to validate its configuration using test mode:

Client configuration
The client and related programs (,, etc.) can be configured using the following files:



For more information read the manual:

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.

Services
Commands to run the SSH server will depend on active init 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:

Commands
OpenSSH provides several commands, see each command's man page for usage information:


 * - secure file copy
 * - secure file transfer
 * - add private key identities to the authentication agent
 * - authentication agent
 * - use locally available keys to authorize logins on a remote machine
 * - authentication key utility
 * - gather SSH public keys from servers
 * - OpenSSH daemon

Escape sequences
During an active SSH session, pressing the tilde key starts an escape sequence. Enter the following for a list of options:

Note that escapes are only recognized immediately after a newline. They may not always work with some shells, such as fish.

Passwordless authentication
Handy for git server management. See also the Security Handbook.

Client
On the client, if not already done, create a key pair. This can be done by running the following command (of course, not entering a passphrase):

Server
Make sure an account for the user exists on the server, and then place the clients' file into the server's  file in the user's home directory. This can be done by running the following command on the client computer (here, the user's passphrase on the server needs to be entered):

Afterwards a passwordless login should be possible doing

Then on the server, the file should be set to.

Single machine testing
The above procedure can be tested out locally:

Terminal multiplexers to preserve sessions
It is possible to use a terminal multiplexer to resume a session after a dropped connection. Tmux and screen are two popular multiplexers that can be used to be able to reconnect to a session, even if a command was running when the connection dropped out.

Mosh may be an alternative for some of SSH's functionality, for spotty connections.

Remote services over ssh
SSH may be used to access remote services through an encrypted "tunnel". Remote service access is detailed in the SSH tunneling and SSH jump host articles.

Troubleshooting
There are 3 different levels of debug modes that can help troubleshooting issues. With the  option SSH prints debugging messages about its progress. This is helpful in debugging connection, authentication, and configuration problems. Multiple  options increase the verbosity. Maximum verbosity is three levels deep.

Permissions are too open
An ssh connection will only work if the file permissions of the directory and contents are correct.
 * The directory permissions should be 700 (drwx--), i.e. the owner has full access and no one else has any access.
 * Under :
 * public key files' permissions should be 644 (-rw-r--r--), i.e. anyone may read the file, only the owner can write.
 * all other files' permissions should be 600 (-rw---), i.e. only the owner may read or write the file.

These permissions need to be correct on the client and server.

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, 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 the local server, set  (or some other value, in seconds) within the  file.
 * To enable keep alive for all servers connected to by the local client, set  (or some other value, in seconds) within the  or  file.
 * Set  to help eliminate disconnections.

For example, to modify the server's configuration:

To modify the client's configuration:

X11 forwarding, not forwarding, or tunneling
Problem: After having made the necessary changes to the configuration files for permitting X11 forwarding, it is discovered 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 DISPLAY variable is either being unset or is being set after the SSH session sets it.

Test for this scenario perform the following after logging in remotely:

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.

A trick that works to complete this task would be to define an alias within the users' file.

ssh-agent
OpenSSH comes with, a daemon to cache and prevent from frequent ssh password entries. When run, the environment variable SSH_AUTH_SOCK is used to point to ssh-agent's communication socket. The normal way to setup is to run it as the top most process of the user's session. Otherwise the environment variables will not be visible inside the session.

Depending on the way the graphical user session is configured to launch, it can be tricky to find a suitable way to launch. As an example for the display manager, you may edit and change  from

into

To tell the password once per session, either run   manually or make use of the   option.

Recent Xfce will start (and ) automatically. If both are installed both will be started which makes identity management especially with SmartCards more complicated. Either stop XFCE from autostarting at least SSH's agent or disable both and use your shell, X-session or similar.

Copying files to a remote host
The SFTP command, a part of SSH, uses the SSH File Transfer Protocol to copy files to a remote host. rsync is also an alternative for this.



External resources

 * — SSH Proxy Command -- connect.c
 * https://lonesysadmin.net/2011/11/08/ssh-escape-sequences-aka-kill-dead-ssh-sessions/amp/ - A blog entry on escape sequences.
 * https://hackaday.com/2017/10/18/practical-public-key-cryptography/ - Practical public key cryptography (Hackaday).