SSH and Port Mapping
This guide covers:
- Connecting one laptop’s terminal to another using SSH.
- Mapping ports from one laptop to another using SSH port forwarding.
- Using port forwarding to share Android Debug Bridge (ADB) between two laptops.
1. Connecting One Laptop’s Terminal to Another
To connect Laptop 2 to the terminal of Laptop 1 remotely, follow these steps:
Step 1: Enable SSH on Laptop 1
On Laptop 1 (the one you want to access), install and enable the SSH server:
sudo apt update
sudo apt install openssh-server
sudo systemctl enable - now sshThis ensures that Laptop 1 is accessible over SSH.To check the IP address of Laptop 1, run:
ip a | grep inetStep 2: Connect from Laptop 2
On Laptop 2, open a terminal and run:
ssh username@<Laptop_1_IP>Example:
ssh inxeoz@192.168.0.163- Replace
usernamewith the actual username of Laptop 1. - Replace
<Laptop_1_IP>with the actual IP of Laptop 1. - If prompted, type yes to accept the connection and enter the password of Laptop1 of user.
Once connected, you can execute commands on Laptop 1 directly from Laptop 2.
2. Mapping One Laptop’s Port to Another (SSH Port Forwarding)
SSH allows you to map a port from one machine to another. This is useful for forwarding services like ADB, web servers, databases, etc.
Step 1: Understand SSH Port Forwarding
The basic syntax for local port forwarding is:
ssh -L <local_port>:<target_host>:<remote_port> username@remote_host-L: Enables local port forwarding.<local_port>: The port on Laptop 2 (your current machine).<target_host>: The host where the service runs (usuallylocalhost).<remote_port>: The actual port on Laptop 1.username@remote_host: The SSH credentials of Laptop 1.
Step 2: Forwarding a Port (Example: ADB Debugging)
If Laptop 1 has a phone connected via ADB and you want to access it from Laptop 2, run:
ssh -L 5037:localhost:5037 inxeoz@192.168.0.163- This maps Laptop 2’s local port 5037 to Laptop 1’s ADB service (port 5037).
- Any ADB command on Laptop 2 will now be sent to Laptop 1.
On Laptop 2, restart ADB to recognize the forwarded connection:
adb kill-server
adb start-server
adb devicesNow, Laptop 2 will detect the phone connected to Laptop 1!
3. Using SSH Reverse Port Forwarding
Reverse forwarding allows Laptop 1 to access services running on Laptop2.
Example: If you want to expose Laptop 2’s port 8080 to Laptop 1, run on Laptop 2:
ssh -R 8080:localhost:8080 inxeoz@192.168.0.163Now, Laptop 1 can access localhost:8080 as if the service were running there.
4. Persistent Port Forwarding with SSH Tunnels
If you need persistent port forwarding, add this to ~/.ssh/config on Laptop 2:
Host laptop1
HostName 192.168.0.163
User inxeoz
LocalForward 5037 localhost:5037
Then simply connect using:
ssh laptop1This will automatically forward port 5037 each time you SSH into Laptop 1.