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):
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).
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.
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
- Man pages for OpenSSH: https://www.openssh.com/manual.html
« Previous 1 2
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
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.
News
-
The Gnome Foundation Struggling to Stay Afloat
The foundation behind the Gnome desktop environment is having to go through some serious belt-tightening due to continued financial problems.
-
Thousands of Linux Servers Infected with Stealth Malware Since 2021
Perfctl is capable of remaining undetected, which makes it dangerous and hard to mitigate.
-
Halcyon Creates Anti-Ransomware Protection for Linux
As more Linux systems are targeted by ransomware, Halcyon is stepping up its protection.
-
Valve and Arch Linux Announce Collaboration
Valve and Arch have come together for two projects that will have a serious impact on the Linux distribution.
-
Hacker Successfully Runs Linux on a CPU from the Early ‘70s
From the office of "Look what I can do," Dmitry Grinberg was able to get Linux running on a processor that was created in 1971.
-
OSI and LPI Form Strategic Alliance
With a goal of strengthening Linux and open source communities, this new alliance aims to nurture the growth of more highly skilled professionals.
-
Fedora 41 Beta Available with Some Interesting Additions
If you're a Fedora fan, you'll be excited to hear the beta version of the latest release is now available for testing and includes plenty of updates.
-
AlmaLinux Unveils New Hardware Certification Process
The AlmaLinux Hardware Certification Program run by the Certification Special Interest Group (SIG) aims to ensure seamless compatibility between AlmaLinux and a wide range of hardware configurations.
-
Wind River Introduces eLxr Pro Linux Solution
eLxr Pro offers an end-to-end Linux solution backed by expert commercial support.
-
Juno Tab 3 Launches with Ubuntu 24.04
Anyone looking for a full-blown Linux tablet need look no further. Juno has released the Tab 3.