SSH & Remote Access

💤0
Lv 10 XP
← 🧱 Foundations · Command Line Skills

SSH & Remote Access

Beginner ⭐ 50 XP ⏱ 18 min #cli#ssh#security

Connect to remote machines securely with SSH keys, and copy files between them.

📖Theory

SSH (Secure Shell) gives you an encrypted terminal session on a remote machine over port 22. The secure, password-free way to authenticate is with a key pair: a private key stays on your machine; the matching public key goes into ~/.ssh/authorized_keys on the server.

When you connect, the server challenges you using your public key, and only your private key can answer — so no password crosses the wire. Use ~/.ssh/config to save host aliases, users, and keys so connecting is a short, memorable command.

🌍Real-World Example
ssh-keygen -t ed25519 -C "you@example.com"   # generate a key pair
ssh-copy-id alex@server                        # install your public key

ssh alex@server                                # connect
ssh -i ~/.ssh/id_ed25519 alex@10.0.0.5         # specify a key explicitly

scp file.txt alex@server:/tmp/                 # copy a file up
scp alex@server:/var/log/app.log .             # copy a file down

# ~/.ssh/config
# Host prod
#   HostName 10.0.0.5
#   User alex
#   IdentityFile ~/.ssh/id_ed25519
ssh prod                                        # now just this
✍️Hands-On Exercise
  1. Generate an ed25519 key pair and inspect the two files created.
  2. Add a Host alias to ~/.ssh/config and connect using only the alias.
  3. Copy a file to a remote server with scp, then copy one back.
  4. Explain why key-based auth is more secure than passwords.
🧾Cheat Sheet
TaskCommand
Generate keyssh-keygen -t ed25519
Install public keyssh-copy-id user@host
Connectssh user@host
Use specific keyssh -i key user@host
Copy up / downscp src user@host:dst
Sync directoriesrsync -av src/ user@host:dst/
Config aliases~/.ssh/config
💬Common Interview Questions
How does SSH key-based authentication work?

You hold a private key; its public key is on the server. The server issues a challenge only your private key can answer, proving identity without sending a password over the network.

Which key is safe to share — public or private?

The public key. It only verifies signatures. The private key must stay secret and ideally be protected with a passphrase.

How do you copy a file to a remote server from the CLI?

scp localfile user@host:/path/ (or rsync for larger/incremental transfers).

📚Official Documentation

📝 My notes on this topic

Auto-saves as you type