Improve the way you work with Secure Shell

X, Please!

SSH can transfer graphical applications that are running on a remote computer to your own desktop. Assuming the SSH server on the other side allows X-forwarding (X11Forwarding yes in /etc/ssh/sshd_config) and the xauth package is installed, users can launch X11 programs on the remote machine and control them locally as a one off using ssh -X or permanently with an entry in the configuration file (ForwardX11 yes).

The whole thing becomes interesting when several computers are involved. Let's assume that host1 is allowed to access host2, and host2 can access host3, but a direct connection between host1 and host3 is not possible. Instead of multiple commands, users can type a single command (Figure 2):

Figure 2: The xeyes program runs on the third host named Sion; however, the display appears in the graphical interface of the first computer (jessie).
ssh -t -X host2 ssh -t -X host3 xeyes &

This is where you use the -t option (see the "On My Mark!" section). If an alias is defined in the configuration file, you can define the option ForwardX11 yes for host2 and host3. The third computer is also given instructions for the ProxyCommand (Listing 4).

Listing 4

X11 forwarding for multiple hosts in ~/.ssh/config

 

Secure Shell routes other TCP connections through a secure tunnel, if desired. Much like a proxy, SSH accepts the connection on a port on one side and links it to a port on the remote side.

Into the Pipe

Port forwarding works in both directions. Local port forwarding is enabled with the -L option and forwards a connection that arrives on an arbitrary local port through the SSH tunnel to a port on a remote server:

ssh -L <Local_port>:localhost:<Remote_port> <User>@<Remote_host>

Users can use such a tunnel to lift restrictions at the opposite end and, for example, reach a remote MySQL server that only listens to local connections (bind-address = 127.0.0.1) from the local computer (Figure 3).

Figure 3: In the graphical MySQL client, users define 127.0.0.1 as the address of the remote MySQL server and the port number specified in the tunnel (3307 here).

Tunneling is also possible in the opposite direction: In remote port forwarding, the connection arrives at a port on the remote computer, and the data are routed through the SSH tunnel to any port on the client side. Users can take advantage of the feature to share a service offered by a computer on the local network with others outside the LAN.

Instead of -L, the option used is -R.

ssh -R <Remote_port>:localhost:<Local_port> <User>@<Remote_host>

To make this happen, the GatewayPorts yes option must be set on the SSH server at the other end so that the remote computers can connect to local, forwarded ports. Figure 4 shows an example in which the web server (port 80), a Raspberry Pi (which is available only on the local network) is connected via an SSH tunnel to port 9000 on a remote server. The remote server has a public IP address, so the web server using port 9000 is now also reachable from the outside.

Figure 4: A local web server is now accessible from the outside on port 9000 through an SSH tunnel.

If you want to define the settings for one or more SSH tunnels in the configuration file; Listing 5 shows an example.

Listing 5

Local/remote port forwarding in ~/.ssh/config

 

Infos

  1. Man pages for OpenSSH: https://www.openssh.com/manual.html

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News