SSH

From Gentoo Wiki
Jump to: navigation, search
This page contains changes which are not marked for translation.

Other languages:
Deutsch • ‎English • ‎español • ‎français • ‎italiano • ‎日本語 • ‎한국어 • ‎русский • ‎中文(中国大陆)‎


External resources

SSH (Secure SHell) is an encrypted terminal program that replaces the classic telnet tool on Unix-like operating systems.

In addition to remote terminal access provided by the main ssh binary, the SSH suite of programs has grown to include other tools such as scp (Secure Copy Program) and sftp (Secure File Transfer Protocol).

Originally, SSH was not free. However, today the most popular and de-facto standard implementation of SSH is OpenBSD's OpenSSH, which comes pre-installed on Gentoo.

Installation

Check install

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

user $ssh
usage: ssh [-1246AaCfgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]
           [-D [bind_address:]port] [-E log_file] [-e escape_char]
           [-F configfile] [-I pkcs11] [-i identity_file]
           [-L [bind_address:]port:host:hostport] [-l login_name] [-m mac_spec]
           [-O ctl_cmd] [-o option] [-p port]
           [-Q cipher | cipher-auth | mac | kex | key]
           [-R [bind_address:]port:host:hostport] [-S ctl_path] [-W host:port]
           [-w local_tun[:remote_tun]] [user@]hostname [command]

If no usage statement is printed ssh 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.

USE flags

The net-misc/openssh package is capable of the following USE flags:
USE flag (what is that?) Default Recommended Description
bindist No Disable EC/RC5 algorithms in OpenSSL for patent reasons.
hpn No Enable high performance ssh
pam No Add support for PAM (Pluggable Authentication Modules) - DANGEROUS to arbitrarily flip
tcpd No Add support for TCP wrappers
kerberos No Add kerberos support
ldap No Add support for storing SSH public keys in LDAP
libedit No Use the libedit library (replacement for readline)
selinux No  !!internal use only!! Security Enhanced Linux support, this must be set by the selinux profile or breakage will occur
skey No Enable S/Key (Single use password) authentication support
static No  !!do not set this during bootstrap!! Causes binaries to be statically linked instead of dynamically
X No Add support for X11
X509 No Adds support for X.509 certificate authentication

Emerge OpenSSH

After changing the necessary USE flags, do not forget to install (or rebuild) OpenSSH:

root #emerge --ask --changed-use net-misc/openssh

Running OpenSSH

Server

Add openssh to the default runlevel if this has not been done already:

root #rc-update add sshd default

Start the sshd daemon with:

root #/etc/init.d/sshd start

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

root #/etc/init.d/sshd start
root #/etc/init.d/sshd stop
root #/etc/init.d/sshd restart
Note
Active SSH connections to the server remain unaffected when issuing /etc/init.d/sshd restart.

Configuration

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 ssh-keygen command.

Important
Be sure to substitute <SecretPassphrase> in the following examples with a personal passphrase.

To generate the key used for SSH protocol version 1 (which usually is not enabled anymore; it has been deprecated in favor of protocol version 2) use:

root #/usr/bin/ssh-keygen -t rsa1 -b 1024 -f /etc/ssh/ssh_host_key -N "<SecretPassphrase>"

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

root #/usr/bin/ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key -N "<SecretPassphrase>"
root #/usr/bin/ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N "<SecretPassphrase>"

Server configuration

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

Users should study Sven's OpenSSH guide for a security focused configuration.

Client configuration

The ssh client and related programs (scp, sftp, etc.) can be configured using the following files:

  • ~/.ssh/config
  • /etc/ssh/ssh_config

For more information read the ssh_config manual:

user $man ssh_config

Passwordless Authentication

Handy for git server management.

Client

On the client run the following command:

user $ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/larry/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/larry/.ssh/id_rsa.
Your public key has been saved in /home/larry/.ssh/id_rsa.pub.
The key fingerprint is:
de:ad:be:ef:15:g0:0d:13:37:15:ad:cc:dd:ee:ff:61 larry@client
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|     .           |
| . .. n   .      |
|   . (: . .      |
|  o   . . : .    |
| . ..: >.> .     |
|  * ?. .         |
| o.. .. ..       |
| :. .  ! .       |
+-----------------+

Server

Make sure an account for the user exists on the server, and then place the clients' id_rsa.pub file into the ~/.ssh/authorized_keys file.

Single machine testing

The above procedure can be tested out locally:

user $ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/larry/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
...
user $mv ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys
user $ssh localhost

Troubleshooting

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

user $ssh example.org -v
user $ssh example.org -vv
user $ssh example.org -vvv

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 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 /etc/ssh/sshd_config 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 /etc/ssh/ssh_config file.

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:

user $echo $DISPLAY
localhost:10.0

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

user $ssh -t larry@localhost2 bash --noprofile
Note
Be sure to substitute "larry" 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' ~/.bashrc file.

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. Utilized them as needed to secure a frequently attacked system.

See also

  • 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.

External resources