Sagar.BlogArticle
All posts
All posts
Linux

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 command

Common SSH Flags

OptionDescription
-p PORTCustom port
-i KEYSpecify identity (key) file
-vVerbose/debug output
-XEnable X11 forwarding
-LLocal port forwarding
-RRemote port forwarding
-NNo 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@server

SSH 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 production

Set 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.