Advanced SSH usage
This topic walks through common SSH use cases that combine server-side configuration with client-side commands: certificate-based authentication, X11 forwarding, and port forwarding. It focuses on example configuration and commands for typical scenarios — for complete directive and option references, see OpenSSH Server Configuration and OpenSSH Client Configuration. For background on the SSH protocol, see About SSH and OpenSSH.
|
Status: work in progress. Content in this section is in review and being updated. Feedback welcome! |
Use case: SSH certificate authentication
Introduction to SSH certificates
Using public key cryptography for authentication requires copying the public key from every client to every server that the client intends to log into. This system does not scale well and can be an administrative burden. Using a public key from a certificate authority (CA) to authenticate client certificates removes the need to copy keys between multiple systems. While the X.509 Public Key Infrastructure Certificate system provides a solution to this issue, there is a submission and validation process, with associated fees, to go through in order to get a certificate signed. As an alternative, OpenSSH supports the creation of simple certificates and associated CA infrastructure.
OpenSSH certificates contain a public key, identity information, and validity constraints. They are signed with a standard SSH public key using the ssh-keygen utility. The format of the certificate is described in /usr/share/doc/openssh-version/PROTOCOL.certkeys.
The ssh-keygen utility supports two types of certificates: user and host. User certificates authenticate users to servers, whereas host certificates authenticate server hosts to users. For certificates to be used for user or host authentication, sshd must be configured to trust the CA public key.
Creating SSH CA certificate signing keys
Two types of certificates are required, host certificates and user certificates. It is considered better to have two separate keys for signing the two certificates, for example ca_user_key and ca_host_key, however it is possible to use just one CA key to sign both certificates. It is also easier to follow the procedures if separate keys are used, so the examples that follow will use separate keys.
The basic format of the command to sign user’s public key to create a user certificate is as follows:
$ ssh-keygen -s ca_user_key -I certificate_ID id_rsa.pub
Where -s indicates the private key used to sign the certificate, -I indicates an identity string, the certificate_ID, which can be any alpha numeric value. It is stored as a zero terminated string in the certificate. The certificate_ID is logged whenever the certificate is used for identification and it is also used when revoking a certificate. Having a long value would make logs hard to read, therefore using the host name for host certificates and the user name for user certificates is a safe choice.
To sign a host’s public key to create a host certificate, add the -h option:
$ ssh-keygen -s ca_host_key -I certificate_ID -h ssh_host_rsa_key.pub
Host keys are generated on the system by default, to list the keys, enter a command as follows:
$ ls -l /etc/ssh/ssh_host*
-rw-------. 1 root root 480 May 13 16:11 /etc/ssh/ssh_host_ecdsa_key
-rw-r--r--. 1 root root 162 May 13 16:11 /etc/ssh/ssh_host_ecdsa_key.pub
-rw-------. 1 root root 387 May 13 16:11 /etc/ssh/ssh_host_ed25519_key
-rw-r--r--. 1 root root 82 May 13 16:11 /etc/ssh/ssh_host_ed25519_key.pub
-rw-------. 1 root root 2578 May 13 16:11 /etc/ssh/ssh_host_rsa_key
-rw-r--r--. 1 root root 554 May 13 16:11 /etc/ssh/ssh_host_rsa_key.pub
|
It is recommended to create and store CA keys in a safe place just as with any other private key. In these examples the |
-
On the server designated to be the CA, generate two keys for use in signing certificates. These are the keys that all other hosts need to trust. Choose suitable names, for example
ca_user_keyandca_host_key. To generate the user certificate signing key, enter the following command:sudo ssh-keygen -t rsa -f /root/.ssh/ca_user_key Generating public/private rsa key pair. Created directory '/root/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/ca_user_key Your public key has been saved in /root/.ssh/ca_user_key.pub The key fingerprint is: SHA256:y6f0DGlHe28YWotEypnhfk3WLYQ5TgaQwoSlOFwmmm0 root@host_name.example.com The key's randomart image is: ---[RSA 3072]---- | .. o| | . o +.| | o + . . o| | o + . . ..| | S . ... *| | . . . .*.| | = E .. | | . o . | | . | +----[SHA256]-----Generate a host certificate signing key,
ca_host_key, as follows:sudo ssh-keygen -t rsa -f /root/.ssh/ca_host_key Generating public/private rsa key pair. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/ca_host_key Your public key has been saved in /root/.ssh/ca_host_key.pub The key fingerprint is: SHA256:y6f0DGlHe28YWotEypnhfk3WLYQ5TgaQwoSlOFwmmm0 root@host_name.example.com The key's randomart image is: ---[RSA 3072]---- | .. | | . ....| | . . o oo| | o . o *o| | S = .| | o. .| | *.E. | | +o= | | .oo. | +----[SHA256]-----If required, confirm the permissions are correct:
sudo ls -la /root/.ssh total 40 drwxrwxrwx. 2 root root 4096 May 22 13:18 . dr-xr-x---. 3 root root 4096 May 8 08:34 .. -rw-------. 1 root root 1743 May 22 13:15 ca_host_key -rw-r--r--. 1 root root 420 May 22 13:15 ca_host_key.pub -rw-------. 1 root root 1743 May 22 13:14 ca_user_key -rw-r--r--. 1 root root 420 May 22 13:14 ca_user_key.pub -rw-r--r--. 1 root root 854 May 8 05:55 known_hosts -r--------. 1 root root 1671 May 6 17:13 ssh_host_rsa -rw-r--r--. 1 root root 1370 May 7 14:30 ssh_host_rsa-cert.pub -rw-------. 1 root root 420 May 6 17:13 ssh_host_rsa.pub -
Create the CA server’s own host certificate by signing the server’s host public key together with an identification string such as the host name, the CA server’s fully qualified domain name (FQDN) but without the trailing
., and a validity period. The command takes the following form:# ssh-keygen -s ~/.ssh/ca_host_key -I certificate_ID -h -n host_name.example.com -V -start:+end /etc/ssh/ssh_host_rsa.pubThe
-noption restricts this certificate to a specific host within the domain. The-Voption is for adding a validity period; this is highly recommend. Where the validity period is intended to be one year, fifty two weeks, consider the need for time to change the certificates and any holiday periods around the time of certificate expiry.For example:
$ sudo ssh-keygen -s /root/.ssh/ca_host_key -I host_name -h -n host_name.example.com -V -1w:+54w5d /etc/ssh/ssh_host_rsa.pub Enter passphrase: Signed host key /root/.ssh/ssh_host_rsa-cert.pub: id "host_name" serial 0 for host_name.example.com valid from 2020-05-15T13:52:29 to 2021-06-08T13:52:29
Distributing and trusting SSH CA public keys
Hosts that are to allow certificate authenticated log in from users must be configured to trust the CA’s public key that was used to sign the user certificates, in order to authenticate user’s certificates. In this example that is the ca_user_key.pub.
Publish the ca_user_key.pub key and download it to all hosts that are required to allow remote users to log in. Alternately, copy the CA user public key to all the hosts. In a production environment, consider copying the public key to an administrator account first. The secure copy command can be used to copy the public key to remote hosts. The command has the following format:
# scp ~/.ssh/ca_user_key.pub root@host_name.example.com:/etc/ssh/
Where host_name is the host name of a server the is required to authenticate user’s certificates presented during the login process. Ensure you copy the public key not the private key. For example:
$ sudo scp /root/.ssh/ca_user_key.pub root@host_name.example.com:/etc/ssh/
The authenticity of host 'host_name.example.com (192.0.2.1)' can't be established.
ED25519 key fingerprint is SHA256:ZYEUaevOAEASvYjm58PiPdMebxhhlaTZBjTMr/N2I3c.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'host_name.example.com' (ED25519) to the list of known hosts.
root@host_name.example.com's password:
ca_user_key.pub 100% 420 0.4KB/s 00:00
For remote user authentication, CA keys can be marked as trusted per-user in the ~/.ssh/authorized_keys file using the cert-authority directive or for global use by means of the TrustedUserCAKeys directive in the /etc/ssh/sshd_config file. For remote host authentication, CA keys can be marked as trusted globally in the /etc/ssh/known_hosts file or per-user in the ~/.ssh/ssh_known_hosts file.
-
For user certificates which have one or more principles listed, and where the setting is to have global effect, edit the
/etc/ssh/sshd_configfile as follows:TrustedUserCAKeys /etc/ssh/ca_user_key.pubRestart
sshdto make the changes take effect:$ sudo systemctl restart sshd.service
To avoid being presented with the warning about an unknown host, a user’s system must trust the CA’s public key that was used to sign the host certificates. In this example that is ca_host_key.pub.
-
Extract the contents of the public key used to sign the host certificate. For example, on the CA:
sudo cat /root/.ssh/ca_host_key.pub sudo ssh-rsa AAAAB5Wm.== root@ca-server.example.com -
To configure client systems to trust servers' signed host certificates, add the contents of the
ca_host_key.pubinto the globalknown_hostsfile. This will automatically check a server’s host advertised certificate against the CA public key for all users every time a new machine is connected to in the domain*.example.com. Configure the/etc/ssh/ssh_known_hostsfile, as follows:$ sudo vi /etc/ssh/ssh_known_hosts # A CA key, accepted for any host in *.example.com @cert-authority *.example.com ssh-rsa AAAAB5Wm.Where
ssh-rsa AAAAB5Wm.is the contents ofca_host_key.pub. The above configures the system to trust the CA servers host public key. This enables global authentication of the certificates presented by hosts to remote users.
Creating SSH certificates
A certificate is a signed public key. The user’s and host’s public keys must be copied to the CA server for signing by the CA server’s private key.
|
Copying many keys to the CA to be signed can create confusion if they are not uniquely named. If the default name is always used then the latest key to be copied will overwrite the previously copied key, which may be an acceptable method for one administrator. In the example below the default name is used. In a production environment, consider using easily recognizable names. It is recommend to have a designated directory on the CA server owned by an administrative user for the keys to be copied into. Copying these keys to the |
Create an administrator account, in this example admin, and a directory to receive the user’s keys. For example:
$ mkdir keys
Set the permissions to allow keys to be copied in:
$ chmod o+w keys
$ ls -la keys
total 8
drwxrwxrwx. 2 admin admin 4096 May 22 16:17 .
drwx------. 3 admin admin 4096 May 22 16:17 ..
Creating SSH certificates to authenticate hosts
The command to sign a host certificate has the following format:
$ ssh-keygen -s ca_host_key -I host_name -h ssh_host_rsa_key.pub
The host certificate will named ssh_host_rsa_key-cert.pub.
To authenticate a host to a user, a public key must be generated on the host, passed to the CA server, signed by the CA, and then passed back to be stored on the host to present to a user attempting to log into the host.
-
Host keys are generated automatically on the system. To list them enter the following command:
$ sudo ls -l /etc/ssh/ssh_host* -rw-------. 1 root root 480 May 13 16:11 /etc/ssh/ssh_host_ecdsa_key -rw-r--r--. 1 root root 162 May 13 16:11 /etc/ssh/ssh_host_ecdsa_key.pub -rw-------. 1 root root 387 May 13 16:11 /etc/ssh/ssh_host_ed25519_key -rw-r--r--. 1 root root 82 May 13 16:11 /etc/ssh/ssh_host_ed25519_key.pub -rw-------. 1 root root 2578 May 13 16:11 /etc/ssh/ssh_host_rsa_key -rw-r--r--. 1 root root 554 May 13 16:11 /etc/ssh/ssh_host_rsa_key.pub -
Copy the chosen public key to the server designated as the CA. For example, from the host:
$ sudo scp /etc/ssh/ssh_host_rsa_key.pub admin@ca-server.example.com:~/keys/ssh_host_rsa_key.pub The authenticity of host 'ca-server.example.com (192.0.2.2)' can't be established. ED25519 key fingerprint is SHA256:ZYEUaevOAEASvYjm58PiPdMebxhhlaTZBjTMr/N2I3c. This key is not known by any other names. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes Warning: Permanently added 'ca-server.example.com' (ED25519) to the list of known hosts. admin@ca-server.example.com's password: ssh_host_rsa_key.pub 100% 382 0.4KB/s 00:00Alternately, from the CA:
$ sudo scp root@host_name.example.com:/etc/ssh/ssh_host_rsa_key.pub ~/keys/ssh_host_rsa_key.pub -
On the CA server, sign the host’s public key. For example:
$ sudo ssh-keygen -s ~/.ssh/ca_host_key -I host_name -h -n host_name.example.com -V -1d:+54w /home/admin/keys/ssh_host_rsa_key.pub Enter passphrase: Signed host key /home/admin/keys/ssh_host_rsa_key-cert.pub: id "host_name" serial 0 for host_name.example.com valid from 2020-05-26T12:21:54 to 2021-06-08T12:21:54Where host_name is the host name of the system requiring the certificate.
-
Copy the certificate to the host. For example, from the CA:
$ sudo scp /home/admin/keys/ssh_host_rsa_key-cert.pub root@host_name.example.com:/etc/ssh/ root@host_name.example.com's password: ssh_host_rsa_key-cert.pub 100% 1384 1.5KB/s 00:00 -
Configure the host to present the certificate to a user’s system when a user initiates the login process. As
root, edit the/etc/ssh/sshd_configfile as follows:HostCertificate /etc/ssh/ssh_host_rsa_key-cert.pub
-
Restart
sshdto make the changes take effect:$ sudo systemctl restart sshd.service -
On user’s systems, remove keys belonging to hosts from the
~/.ssh/known_hostsfile if the user has previously logged into the host configured above. When a user logs into the host they should no longer be presented with the warning about the hosts authenticity.
To test the host certificate, on a client system, ensure the client has set up the global /etc/ssh/known_hosts file, as described in Trusting the Host Signing Key, and that the server’s public key is not in the ~/.ssh/known_hosts file. Then attempt to log into the server over SSH as a remote user. You should not see a warning about the authenticity of the host. If required, add the -v option to the SSH command to see logging information.
Creating SSH certificates for authenticating users
To sign a user’s certificate, use a command in the following format:
$ ssh-keygen -s ca_user_key -I user_name -n user_name -V -start:+end id_rsa.pub
The resulting certificate will be named id_rsa-cert.pub.
The default behavior of OpenSSH is that a user is allowed to log in as a remote user if one of the principals specified in the certificate matches the remote user’s name. This can be adjusted in the following ways:
-
Add more user’s names to the certificate during the signing process using the
-noption:-n "name1[,name2,...]"
-
On the user’s system, add the public key of the CA in the
~/.ssh/authorized_keysfile using the cert-authority directive and list the principals names as follows:$ vi ~/.ssh/authorized_keys # A CA key, accepted for any host in *.example.com @cert-authority principals="name1,name2" *.example.com ssh-rsa AAAAB5Wm. -
On the server, create an
AuthorizedPrincipalsFilefile, either per user or globally, and add the principles' names to the file for those users allowed to log in. Then in the/etc/ssh/sshd_configfile, specify the file using the AuthorizedPrincipalsFile directive.
To authenticate a user to a remote host, a public key must be generated by the user, passed to the CA server, signed by the CA, and then passed back to be stored by the user for use when logging in to a host.
-
On client systems, login as the user who requires the certificate. Check for available keys as follows:
$ ls -l ~/.ssh/If no suitable public key exists, generate one and set the directory permissions if the directory is not the default directory. For example, enter the following command:
$ ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/home/user1/.ssh/id_rsa): Created directory '/home/user1/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/user1/.ssh/id_rsa Your public key has been saved in /home/user1/.ssh/id_rsa.pub The key fingerprint is: SHA256:y6f0DGlHe28YWotEypnhfk3WLYQ5TgaQwoSlOFwmmm0 user1@host1.example.com The key's randomart image is: ---[RSA 3072]---- | oo+. | | o.o.o. | | .o o . | | oo . o | | . oo.S | | o=.. | | .Eo | | .= | | .. | ----[SHA256]-----By default the directory permissions for a user’s keys are
drwx------., or octal 0700. If required, confirm the permissions are correct:$ ls -la ~/.ssh total 16 drwx------. 2 user1 user1 4096 May 7 12:37 . drwx------. 3 user1 user1 4096 May 7 12:37 .. -rw-------. 1 user1 user1 1679 May 7 12:37 id_rsa -rw-r--r--. 1 user1 user1 421 May 7 12:37 id_rsa.pubSee Generating Key Pairs in OpenSSH Client Configuration for more examples of key generation and for instructions on setting the correct directory permissions.
-
The chosen public key must be copied to the server designated as the CA, in order to be signed. The secure copy command can be used to do this, the command has the following format:
$ scp ~/.ssh/id_protocol.pub admin@ca_server.example.com:~/keys/Where protocol is the part of the file name indicating the protocol used to generate the key, for example
rsa, admin is an account on the CA server, and /keys/ is a directory setup to receive the keys to be signed.Copy the chosen public key to the server designated as the CA. For example:
$ scp ~/.ssh/id_rsa.pub admin@ca-server.example.com:~/keys/ admin@ca-server.example.com's password: id_rsa.pub 100% 421 0.4KB/s 00:00If you have configured the client system to trust the host signing key as described in Trusting the Host Signing Key then you should not see a warning about the authenticity of the remote host.
-
On the CA server, sign the user’s public key. For example, as
root:$ sudo ssh-keygen -s /root/.ssh/ca_user_key -I user1 -n user1 -V -1d:+54w /home/admin/keys/id_rsa.pub Enter passphrase: Signed user key /home/admin/keys/id_rsa-cert.pub: id "user1" serial 0 for host_name.example.com valid from 2020-05-21T16:43:17 to 2021-06-03T16:43:17 -
Copy the resulting certificate to the user’s
~/.ssh/directory on their system. For example:sudo scp /home/admin/keys/id_rsa-cert.pub user1@host_name.example.com:~/.ssh/ user1@host_name.example.com's password: id_rsa-cert.pub 100% 1498 1.5KB/s 00:00 -
If using the standard file names and location then no further configuration is required as the SSH daemon will search for user certificates ending in
-cert.puband use them automatically if it finds them. Note that the default location and file names for SSH version 2 keys are:~/.ssh/id_ecdsa,~/.ssh/id_ed25519and~/.ssh/id_rsaas explained in thessh_config(5)manual page. If you use these locations and naming conventions then there is no need for editing the configuration files to enablesshdto present the certificate. They will be used automatically when logging in to a remote system. In this is the case then skip to step 6.If required to use a non-default directory or file naming convention, then as
root, add the following line to the/etc/ssh/ssh_configor~/.ssh/configfiles:IdentityFile ~/path/key_file
Note that this must be the private key name, do not had
.pubor-cert.pub. Ensure the file permission are correct. For example:$ ls -la ~/.ssh/config -rw-rw-r--. 1 user1 user1 36 May 27 21:49 /home/user1/.ssh/config $ chmod 700 ~/.ssh/config $ ls -la ~/.ssh/config -rwx------. 1 user1 user1 36 May 27 21:49 /home/user1/.ssh/configThis will enable the user of this system to be authenticated by a user certificate when logging into a remote system configured to trust the CA user certificate signing key.
-
To test the user certificate, attempt to log into a server over SSH from the user’s account. You should do this as the user listed as a principle in the certificate, if any are specified. You should not be prompted for a password. If required, add the
-voption to the SSH command to see logging information.
Signing an SSH certificate using a PKCS#11 token
It is possible to sign a host key using a CA key stored in a PKCS#11 token by providing the token library using the -D and identifying the CA key by providing its public half as an argument to the -s option:
$ ssh-keygen -s ca_host_key.pub -D libpkcs11.so -I certificate_ID host_key.pub
In all cases, certificate_ID is a “key identifier” that is logged by the server when the certificate is used for authentication.
Certificates may be configured to be valid only for a set of users or host names, the principals. By default, generated certificates are valid for all users or hosts. To generate a certificate for a specified set of principals, use a comma separated list with the -n option as follows:
$ ssh-keygen -s ca_user_key.pub -D libpkcs11.so -I certificate_ID -n user1,user2 id_rsa.pub
and for hosts:
$ ssh-keygen -s ca_host_key.pub -D libpkcs11.so -I certificate_ID -h -n host.domain ssh_host_rsa_key.pub
Additional limitations on the validity and use of user certificates may be specified through certificate options.
A certificate option may disable features of the SSH session, may be valid only when presented from particular
source addresses or may force the use of a specific command. For a list of valid certificate options, see the
ssh-keygen(1) manual page for the -O option.
Certificates may be defined to be valid for a specific lifetime. The -V option allows specifying a certificates
start and end times. For example:
$ ssh-keygen -s ca_user_key -I certificate_ID -V "-1w:+54w5d" id_rsa.pub
A certificate that is presented at a time outside this range will not be considered valid. By default, certificates are valid indefinitely starting from UNIX Epoch.
Viewing an SSH CA certificate
To view a certificate, use the -L to list the contents. For example, for a user’s certificate:
$ ssh-keygen -L -f ~/.ssh/id_rsa-cert.pub
/home/user1/.ssh/id_rsa-cert.pub:
Type: ssh-rsa-cert-v01@openssh.com user certificate
Public key: RSA-CERT SHA256:y6f0DGlHe28YWotEypnhfk3WLYQ5TgaQwoSlOFwmmm0
Signing CA: RSA SHA256:y6f0DGlHe28YWotEypnhfk3WLYQ5TgaQwoSlOFwmmm0
Key ID: "user1"
Serial: 0
Valid: from 2020-05-27T00:09:16 to 2021-06-09T00:09:16
Principals:
user1
Critical Options: (none)
Extensions:
permit-X11-forwarding
permit-agent-forwarding
permit-port-forwarding
permit-pty
permit-user-rc
To view a host certificate:
$ sudo ssh-keygen -L -f /etc/ssh/ssh_host_rsa_key-cert.pub
/etc/ssh/ssh_host_rsa_key-cert.pub:
Type: ssh-rsa-cert-v01@openssh.com host certificate
Public key: RSA-CERT SHA256:y6f0DGlHe28YWotEypnhfk3WLYQ5TgaQwoSlOFwmmm0
Signing CA: RSA SHA256:y6f0DGlHe28YWotEypnhfk3WLYQ5TgaQwoSlOFwmmm0
Key ID: "host_name"
Serial: 0
Valid: from 2020-05-26T17:19:01 to 2021-06-08T17:19:01
Principals:
host_name.example.com
Critical Options: (none)
Extensions: (none)
Revoking an SSH CA certificate
If a certificate is stolen, it should be revoked. Although OpenSSH does not provide a mechanism to distribute the revocation list it is still easier to create the revocation list and distribute it by other means then to change the CA keys and all host and user certificates previously created and distributed.
Keys can be revoked by adding them to the revoked_keys file and specifying the file name in the sshd_config file as follows:
RevokedKeys /etc/ssh/revoked_keys
Note that if this file is not readable, then public key authentication will be refused for all users.
A new key revocation list can be generated as follows:
$ ssh-keygen -kf /etc/ssh/revoked_keys -z 1 ~/.ssh/id_rsa.pub
To add lines to the list, use the -u option to update the list:
$ ssh-keygen -ukf /etc/ssh/revoked_keys -z integer ~/.ssh/id_rsa.pub
where integer is the line number.
To test if a key has been revoked, query the revocation list for the presence of the key. Use a command as follows:
$ ssh-keygen -Qf /etc/ssh/revoked_keys ~/.ssh/id_rsa.pub
A user can revoke a CA certificate by changing the cert-authority directive to revoke in the known_hosts file.
Use case: X11 and port forwarding
A secure command line interface is just the beginning of the many ways SSH can be used. Given the proper amount of bandwidth, X11 sessions can be directed over an SSH channel. Or, by using TCP/IP forwarding, previously insecure port connections between systems can be mapped to specific SSH channels.
X11 forwarding
To open an X11 session over an SSH connection, use a command in the following form:
$ ssh -Y username@hostname
For example, to log in to a remote machine named penguin.example.com with USER as a user name, type:
$ ssh -Y USER@penguin.example.com
USER@penguin.example.com's password:
When an X program is run from the secure shell prompt, the SSH client and server create a new secure channel, and the X program data is sent over that channel to the client machine transparently.
For X11 forwarding to work, the SSH server must allow it. Ensure X11Forwarding yes is set in /etc/ssh/sshd_config (see OpenSSH Server Configuration)
|
The remote system must also be able to run X11 applications and authenticate X11 sessions. The xorg-x11-xauth package is required for this purpose.
$ sudo dnf install xorg-x11-xauth
X11 forwarding can be useful for accessing graphical applications on a remote system without running a full remote desktop sharing environment. For example, to open and interact with the firefox browser on a remote system, you can run:
$ ssh -Y user@penguin.example.com firefox
The Firefox application opens, allowing you to browse using the firefox application as if you were on the remote system. When you close the browser, the SSH connection drops. You could equally SSH into the remote system and then run any graphical application from the command line and use the & option to background the process. If you do this, the SSH connection stays open after you close the graphical application, and you are able to run multiple graphical applications in the same session.
Note that although Wayland replaces the legacy X11 server as the display server on Fedora, X11 forwarding of applications over SSH is still functional.
Local port forwarding (-L)
SSH can secure otherwise insecure TCP/IP protocols via port forwarding. When using this technique, the SSH server becomes an encrypted conduit to the SSH client.
Port forwarding works by mapping a local port on the client to a remote port on the server. SSH can map any port from the server to any port on the client. Port numbers do not need to match for this technique to work.
|
Using reserved port numbers
Setting up port forwarding to listen on ports below 1024 requires |
To create a TCP/IP port forwarding channel which listens for connections on the localhost, use a command in the following form:
$ ssh -L local-port:remote-hostname:remote-port username@hostname
For example, to check email on a server called mail.example.com using POP3 through an encrypted connection, use the following command:
$ ssh -L 1100:mail.example.com:110 mail.example.com
Once the port forwarding channel is in place between the client machine and the mail server, direct a POP3 mail client to use port 1100 on the localhost to check for new email. Any requests sent to port 1100 on the client system will be directed securely to the mail.example.com server.
If mail.example.com is not running an SSH server, but another machine on the same network is, SSH can still be used to secure part of the connection. However, a slightly different command is necessary:
$ ssh -L 1100:mail.example.com:110 other.example.com
In this example, POP3 requests from port 1100 on the client machine are forwarded through the SSH connection on port 22 to the SSH server, other.example.com. Then, other.example.com connects to port 110 on mail.example.com to check for new email. Note that when using this technique, only the connection between the client system and other.example.com SSH server is secure.
Port forwarding can also be used to get information securely through network firewalls. If the firewall is configured to allow SSH traffic via its standard port (that is, port 22) but blocks access to other ports, a connection between two hosts using the blocked ports is still possible by redirecting their communication over an established SSH connection.
|
A connection is only as secure as a client system
Using port forwarding to forward connections in this manner allows any user on the client system to connect to that service. If the client system becomes compromised, the attacker also has access to forwarded services. System administrators concerned about port forwarding can disable this functionality on the server by specifying a |
See also
-
About SSH and OpenSSH — conceptual background on the SSH protocol.
-
OpenSSH Server Configuration — full reference for
sshdand server-side configuration. -
OpenSSH Client Configuration — full reference for
ssh,scp,sftp, and client-side configuration.
Want to help? Learn how to contribute to Fedora Docs ›