What if you could securely reach any private service with just one simple command?
Why SSH tunneling (port forwarding) in Linux CLI? - Purpose & Use Cases
Imagine you need to access a private database on your company network from home. Without special tools, you try to connect directly but the network blocks you. You ask IT to open ports or send files back and forth manually.
Manually requesting network changes is slow and risky. Opening ports can expose your system to hackers. Copying files repeatedly wastes time and can cause version mix-ups. You feel stuck and frustrated.
SSH tunneling creates a secure, invisible bridge from your computer to the private network. It forwards the needed ports safely through an encrypted connection, letting you work as if you were inside the company network without exposing anything.
telnet private-db.company.com 3306ssh -L 3306:private-db.company.com:3306 user@company-gateway
SSH tunneling lets you securely access remote services as if they were local, unlocking powerful remote work and automation possibilities.
A developer uses SSH tunneling to connect their local browser to a remote web server's admin panel that is only accessible inside the company network, without exposing the server to the internet.
Manual remote access is slow, risky, and frustrating.
SSH tunneling creates a secure, encrypted path for port forwarding.
This enables safe, seamless access to remote services as if local.
Practice
ssh -L 8080:localhost:80 user@remote do?Solution
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.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.Final Answer:
Forwards your local port 8080 to port 80 on the remote server's localhost -> Option CQuick Check:
ssh -L local_port:remote_host:remote_port = local to remote [OK]
- Confusing -L with -R (remote forwarding)
- Mixing up local and remote ports
- Thinking it copies files instead of forwarding ports
Solution
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.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.Final Answer:
ssh -L 3306:db.example.com:3306 user@remote -> Option AQuick Check:
-L for local forwarding syntax [OK]
- Using -R instead of -L for local forwarding
- Using invalid flags like -F or -P
- Swapping local and remote ports
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?
Solution
Step 1: Understand the port forwarding setup
The command forwards local port 9000 to remote localhost port 5432, where PostgreSQL runs.Step 2: Analyze the connection effect
Connecting locally to port 9000 actually connects through SSH tunnel to remote PostgreSQL on port 5432.Final Answer:
You connect to the remote PostgreSQL database through local port 9000 -> Option AQuick Check:
Local port 9000 tunnels to remote 5432 [OK]
- Thinking it connects to local PostgreSQL instead
- Assuming SSH connection fails without checking firewall
- Confusing local and remote ports
ssh -L 8080:localhost:80 user@remote but get an error: "bind: Address already in use". What is the most likely cause and fix?Solution
Step 1: Understand the error message
"bind: Address already in use" means the local port 8080 is occupied by another process.Step 2: Fix by changing local port
Choose a different unused local port (e.g., 8081) to avoid conflict.Final Answer:
Local port 8080 is already used; choose a different local port -> Option BQuick Check:
Port bind error means local port busy [OK]
- Assuming remote port or authentication causes bind error
- Ignoring local port conflicts
- Not checking running processes using the port
http://localhost:7000 in your browser to access the remote service?Solution
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.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.Final Answer:
ssh -L 7000:localhost:5000 user@192.168.1.10 -> Option DQuick Check:
Local port 7000 tunnels to remote 5000 [OK]
- Using -R instead of -L for local forwarding
- Swapping local and remote ports
- Incorrect remote host or user syntax
Ready to prove it?
Pass the test and this pattern moves unknown
