SSH — Securely Connect to Remote Machines
Learn to use SSH for remote logins, run remote commands, set up SSH config shortcuts, and configure port forwarding tunnels.
March 12, 20255 min read
linuxnetworkingsshremote
Basic SSH Connection
ssh user@hostname # Connect to server
ssh user@192.168.1.100 # By IP address
ssh -p 2222 user@hostname # Custom port
ssh user@hostname ls -la # Run single remote commandCommon SSH Flags
| Option | Description |
|---|---|
-p PORT | Custom port |
-i KEY | Specify identity (key) file |
-v | Verbose/debug output |
-X | Enable X11 forwarding |
-L | Local port forwarding |
-R | Remote port forwarding |
-N | No command (tunnel only) |
Port Forwarding (Tunnels)
# Local: access remote service via local port
ssh -L 8080:localhost:80 user@server
# Now your localhost:8080 → server's port 80
# Remote: expose local service on remote server
ssh -R 9090:localhost:3000 user@server
# server:9090 → your localhost:3000
# Dynamic SOCKS proxy
ssh -D 1080 user@serverSSH Config — Shortcuts
Create ~/.ssh/config to define named shortcuts so you don't have to type long SSH commands.
# ~/.ssh/config
Host myserver
HostName 192.168.1.100
User sagar
Port 22
IdentityFile ~/.ssh/id_rsa
Host production
HostName prod.example.com
User deploy
Port 2222
# Keep-alive settings
Host *
ServerAliveInterval 60
ServerAliveCountMax 3# After config — connect with just:
ssh myserver
ssh productionSet ServerAliveInterval 60 in your SSH config to prevent idle connections from being dropped by firewalls.
Quick Check
Which SSH flag sets up local port forwarding?
Exercise
Create a ~/.ssh/config entry called local that connects to 127.0.0.1 as your current user on port 22.