Bird
Raised Fist0
Linux CLIscripting~10 mins

SSH tunneling (port forwarding) in Linux CLI - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - SSH tunneling (port forwarding)
Start SSH command
↓
Specify local port
↓
Specify remote host and port
↓
Establish SSH connection
↓
Create encrypted tunnel
↓
Forward local port traffic through tunnel
↓
Access remote service via local port
↓
End
The flow shows how SSH sets up a secure tunnel by forwarding a local port to a remote service through an encrypted connection.
Execution Sample
Linux CLI
ssh -L 8080:remotehost:80 user@sshserver
This command forwards local port 8080 to port 80 on remotehost via sshserver.
Execution Table
StepActionDetailsResult
1Start SSH commandssh -L 8080:remotehost:80 user@sshserverSSH client prepares connection
2Specify local portLocal port 8080 chosenLocal port 8080 reserved for forwarding
3Specify remote host and portremotehost:80Target remote service identified
4Establish SSH connectionConnect to sshserver as userEncrypted SSH session established
5Create encrypted tunnelTunnel from local 8080 to remotehost:80Tunnel ready to forward traffic
6Forward local port trafficTraffic to localhost:8080Forwarded through tunnel to remotehost:80
7Access remote serviceUser accesses http://localhost:8080Remote web service accessed securely
8ExitUser closes SSH sessionTunnel closed, forwarding stops
💡 SSH session ends, tunnel closes, local port forwarding stops
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6Final
local_portnone8080 reserved8080 reserved8080 forwarding activenone (after exit)
remote_hostnoneremotehost:80 setremotehost:80 setremotehost:80 setnone (after exit)
ssh_connectionnonenoneestablishedestablishedclosed
Key Moments - 3 Insights
Why do we specify a local port in SSH tunneling?
The local port is where your computer listens for traffic to forward through the tunnel. See execution_table step 2 where local port 8080 is reserved.
What happens if the SSH connection drops?
The tunnel closes and port forwarding stops, as shown in execution_table step 8 where the SSH session ends.
Can you access the remote service directly without SSH tunneling?
Only if the remote service is publicly accessible. SSH tunneling allows secure access even if the service is behind a firewall, shown in step 6 forwarding traffic.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step is the encrypted SSH session established?
AStep 2
BStep 4
CStep 6
DStep 8
💡 Hint
Check the 'Result' column for when the SSH session is established.
According to variable_tracker, what is the state of local_port after step 6?
Anone
B8080 reserved
C8080 forwarding active
Dclosed
💡 Hint
Look at the 'local_port' row under 'After Step 6' column.
If the SSH session closes, what happens to the port forwarding?
AIt stops
BIt switches to another port
CIt continues running
DIt becomes unencrypted
💡 Hint
Refer to execution_table step 8 and exit_note about tunnel closing.
Concept Snapshot
SSH tunneling forwards a local port to a remote service securely.
Use: ssh -L local_port:remote_host:remote_port user@ssh_server
Local port listens on your machine.
Traffic is encrypted and sent through SSH.
Tunnel closes when SSH session ends.
Full Transcript
SSH tunneling, also called port forwarding, lets you securely access a remote service by forwarding a local port through an encrypted SSH connection. You start by running an SSH command with the -L option, specifying a local port, the remote host and port, and the SSH server to connect to. The SSH client reserves the local port and establishes a secure connection to the SSH server. Then it creates a tunnel that forwards any traffic sent to the local port through the SSH connection to the remote service. When you access the local port, you are actually reaching the remote service securely. When the SSH session ends, the tunnel closes and forwarding stops.

Practice

(1/5)
1. What does the SSH command ssh -L 8080:localhost:80 user@remote do?
easy
A. Creates a VPN connection between your machine and the remote server
B. Forwards remote port 8080 to your local port 80
C. Forwards your local port 8080 to port 80 on the remote server's localhost
D. Copies files from port 8080 on remote to port 80 locally

Solution

  1. Step 1: Understand the -L option in SSH

    The -L option sets up local port forwarding, mapping a local port to a remote address and port.
  2. Step 2: Analyze the command ports and hosts

    Local port 8080 is forwarded to port 80 on the remote server's localhost, allowing local access to remote web service.
  3. Final Answer:

    Forwards your local port 8080 to port 80 on the remote server's localhost -> Option C
  4. Quick Check:

    ssh -L local_port:remote_host:remote_port = local to remote [OK]
Hint: Remember -L means local port to remote host:port [OK]
Common Mistakes:
  • Confusing -L with -R (remote forwarding)
  • Mixing up local and remote ports
  • Thinking it copies files instead of forwarding ports
2. Which of the following is the correct syntax to forward local port 3306 to remote port 3306 on host db.example.com using SSH?
easy
A. ssh -L 3306:db.example.com:3306 user@remote
B. ssh -R 3306:db.example.com:3306 user@remote
C. ssh -F 3306:db.example.com:3306 user@remote
D. ssh -P 3306:db.example.com:3306 user@remote

Solution

  1. Step 1: Identify the correct option for local port forwarding

    The -L flag is used for local port forwarding, which forwards a local port to a remote host and port.
  2. Step 2: Check the syntax correctness

    ssh -L 3306:db.example.com:3306 user@remote correctly forwards local port 3306 to remote db.example.com port 3306.
  3. Final Answer:

    ssh -L 3306:db.example.com:3306 user@remote -> Option A
  4. Quick Check:

    -L for local forwarding syntax [OK]
Hint: Use -L for local forwarding, not -R or others [OK]
Common Mistakes:
  • Using -R instead of -L for local forwarding
  • Using invalid flags like -F or -P
  • Swapping local and remote ports
3. What will be the output of the following command if you run ssh -L 9000:localhost:5432 user@remote and then connect your local machine to port 9000?

Assuming PostgreSQL runs on port 5432 on the remote server, what happens?
medium
A. You connect to the remote PostgreSQL database through local port 9000
B. You connect to a local PostgreSQL database on your machine on port 5432
C. The SSH connection fails because port 9000 is blocked
D. You connect to port 9000 on the remote server directly

Solution

  1. Step 1: Understand the port forwarding setup

    The command forwards local port 9000 to remote localhost port 5432, where PostgreSQL runs.
  2. Step 2: Analyze the connection effect

    Connecting locally to port 9000 actually connects through SSH tunnel to remote PostgreSQL on port 5432.
  3. Final Answer:

    You connect to the remote PostgreSQL database through local port 9000 -> Option A
  4. Quick Check:

    Local port 9000 tunnels to remote 5432 [OK]
Hint: Local port connects to remote service via tunnel [OK]
Common Mistakes:
  • Thinking it connects to local PostgreSQL instead
  • Assuming SSH connection fails without checking firewall
  • Confusing local and remote ports
4. You tried to run ssh -L 8080:localhost:80 user@remote but get an error: "bind: Address already in use". What is the most likely cause and fix?
medium
A. Remote port 80 is blocked; use a different remote port
B. Local port 8080 is already used; choose a different local port
C. User authentication failed; check username and password
D. SSH service is not running on remote server

Solution

  1. Step 1: Understand the error message

    "bind: Address already in use" means the local port 8080 is occupied by another process.
  2. Step 2: Fix by changing local port

    Choose a different unused local port (e.g., 8081) to avoid conflict.
  3. Final Answer:

    Local port 8080 is already used; choose a different local port -> Option B
  4. Quick Check:

    Port bind error means local port busy [OK]
Hint: Check if local port is free before forwarding [OK]
Common Mistakes:
  • Assuming remote port or authentication causes bind error
  • Ignoring local port conflicts
  • Not checking running processes using the port
5. You want to securely access a web service running on port 5000 of a remote server at 192.168.1.10, but the server only allows SSH connections. Which command correctly sets up a tunnel so you can open http://localhost:7000 in your browser to access the remote service?
hard
A. ssh -L 5000:localhost:7000 user@192.168.1.10
B. ssh -R 7000:localhost:5000 user@192.168.1.10
C. ssh -L 7000:192.168.1.10:5000 user@localhost
D. ssh -L 7000:localhost:5000 user@192.168.1.10

Solution

  1. Step 1: Identify the correct port forwarding direction

    You want to forward your local port 7000 to remote port 5000 on the server 192.168.1.10.
  2. Step 2: Verify the command syntax

    ssh -L 7000:localhost:5000 user@192.168.1.10 forwards local port 7000 to remote localhost port 5000 correctly.
  3. Final Answer:

    ssh -L 7000:localhost:5000 user@192.168.1.10 -> Option D
  4. Quick Check:

    Local port 7000 tunnels to remote 5000 [OK]
Hint: Local port first, then remote host:port in -L [OK]
Common Mistakes:
  • Using -R instead of -L for local forwarding
  • Swapping local and remote ports
  • Incorrect remote host or user syntax
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard