23.8 C

How to enable local port forwarding using Ubuntu 20.04 and ssh

SSH, a very popular tool found almost with every IT guy. Commonly, when we talk about the insecure network for data, ssh is the tool that is most trustworthy. To protect data against attacks local port forwarding can help, the ssh protocol uses encryption to secure connections. Almost all of the ssh tools, come as free. Putty can be an example.

What all we require to make ssh run.

  • Here, in our scenario, we gonna use Ubuntu 20.04 as OS.
  • Open TCP port 22 on firewall, if required.
  • Create local users.
  • Control user access with directory server-PAM, or manually in /etc/sshd/sshd_config

Install SSH Services

Lets install application first.

#apt-get update
- Advertisement -

Install required package.

#apt install openssh-server

Check what is the IP address of ssh server.

# ip a | grep inet 

SSH server IP is

Check service status.

# systemctl status ssh
Check ssh service status.
Check ssh service status.

Local port forwarding with ssh server.

A tunnel can be created with SSH to forward a port on another server. SSH can be used to provide the proxy, which can be used to send web traffic.

Let’s see how to use ssh to protect the network by using port forwarding.


Here we have,

  • A web server- installed with ssh service as well.
  • An SSH server-
  • Localhost –, where local port forwarding required to configure, installed with ssh service.

As described, in the illustrated diagram below, a local host machine wants to connect with the web server over the internet. A firewall is protecting that webserver. But for a web server, problem is that all communication taking place is in plain text. A secure tunnel protected with ssh can resolve the security issue here and we can establish secure communication with any remote user. We will establish a secure ssh tunnel between ssh server sitting next to the web server and all our communication from localhost will be via ssh.

Illustration to represent the local port forwarding
Illustration to represent the local port forwarding

To run our test lets install webservices on

# apt update 
# apt-install apache2

Check if service are running or not, on webserver.

#systemctl status apache2
check apache service status
check apache service status

Note: Refer previous steps, for how-to install ssh.

Enable local port forwarding

On server let’s have a look at the webserver status, working fine.

browse webserver and check status
browse webserver and check status

Let’s Login to our local host machine i.e. to establish a secure ssh tunnel.

# ssh -L 9090: [email protected]

Where -L is the localhost machine we are logged in. 9090 any free port we can use, is the address of HTTP server, is ssh server with which localhost will establish a tunnel.

ssh port forwarding explained
ssh port forwarding explained

Now, we will see that webserver is available on localhost magically. Have a look:

webserver status after port forwarding with ssh
webserver status after port forwarding with ssh

Even if we disconnect ssh session, port forwarding will be still effective.

Port forwarding working after exit from ssh remote server.
Port forwarding working after exit from ssh remote server.

Lets close that tunnel, get ssh process id.

ps -x | grep ssh
864 ? Ss 0:00 sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups
5131 pts/0 S+ 0:00 grep --color=auto ssh

Kill ssh process

pkill -9 5319   

Is service up now ? No, its not!

local port forwarding service disabled.
local port forwarding service disabled.


SSH enable a secure tunneling, which help to protect webservices and enable local host to access as if they are installed locally. SSH tunnel can be stopped or stopped as and when required.

- Advertisement -
Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
"The best Linux newsletter on the web"


Please enter your comment!
Please enter your name here

Latest article