SSH Basics

SSH (Secure Shell) is how you connect to remote servers. If you work with servers, you'll use SSH daily.

Basic Connection

Terminal
$ssh user@hostname
user@hostname's password: (enter password) user@hostname:~$

You're now on the remote server. Run commands there. Type exit to disconnect.

SSH with IP Address

Terminal
$ssh user@192.168.1.100
(connects to that IP)

Specify Port

Terminal
$ssh -p 2222 user@hostname
(connects on port 2222 instead of 22)

SSH Keys (The Better Way)

Password authentication works, but keys are:

  • More secure
  • More convenient (no typing passwords)
  • Required by many services (GitHub, AWS)

Generate a Key Pair

Terminal
$ssh-keygen -t ed25519 -C 'your@email.com'
Generating public/private ed25519 key pair. Enter file in which to save the key (/home/user/.ssh/id_ed25519): Enter passphrase (empty for no passphrase): Your identification has been saved in /home/user/.ssh/id_ed25519 Your public key has been saved in /home/user/.ssh/id_ed25519.pub

Key Types

ed25519 is modern and secure. rsa (4096 bit) is the older alternative. Avoid dsa.

Copy Key to Server

Terminal
$ssh-copy-id user@hostname
user@hostname's password: Number of key(s) added: 1

Now you can connect without a password.

Manual Key Copy

If ssh-copy-id isn't available:

Terminal
$cat ~/.ssh/id_ed25519.pub
ssh-ed25519 AAAAC3... your@email.com
$# Copy that output and add it to:
$# ~/.ssh/authorized_keys on the server

SSH Config File

Create ~/.ssh/config for shortcuts:

Host prod
    HostName 192.168.1.100
    User deploy
    Port 22

Host staging
    HostName 192.168.1.101
    User deploy
    IdentityFile ~/.ssh/staging_key

Now connect with just:

Terminal
$ssh prod
(connects to 192.168.1.100 as deploy)

SSH Config is Powerful

No more remembering IPs, usernames, and ports. Define once in config, use shortcuts forever.

Running Remote Commands

Terminal
$ssh user@host 'ls -la /var/log'
(runs command, shows output, disconnects)
$ssh user@host 'df -h && free -h'
(run multiple commands)

Copying Files: scp

Terminal
$# Local to remote
$scp file.txt user@host:/path/to/destination/
$
$# Remote to local
$scp user@host:/path/to/file.txt ./local-copy.txt
$
$# Recursive (directories)
$scp -r folder/ user@host:/path/

SSH Tunneling (Port Forwarding)

Access a remote service through SSH:

Terminal
$ssh -L 3306:localhost:3306 user@dbserver
(now localhost:3306 connects to remote MySQL)
Knowledge Check

What's the advantage of SSH keys over passwords?

Quick Reference

CommandPurpose
ssh user@hostConnect to server
ssh -p port user@hostConnect on specific port
ssh-keygen -t ed25519Generate SSH key
ssh-copy-id user@hostCopy key to server
scp file user@host:pathCopy file to server
scp user@host:path fileCopy file from server

Key Takeaways

  • ssh user@host connects to servers
  • Keys are more secure than passwords
  • Use ssh-keygen to create keys
  • ~/.ssh/config saves time with shortcuts
  • scp copies files over SSH
  • SSH is the foundation of remote server work

Congratulations! You've completed Chapter 9: Networking Basics.

Next chapter: Package Management - installing and managing software.