SSH, scp and Working with Remote Servers

advanced
15 min

SSH, scp and Working with Remote Servers

Almost every real Linux server you will touch in your career lives in a data center or the cloud, and you will manage it over the network. The tool for that is SSH, the Secure Shell. In this advanced lesson you learn how to connect to remote machines, authenticate with keys instead of passwords, copy files with scp, and make daily remote work fast and safe.

Connecting with SSH

SSH gives you an encrypted terminal session on another machine. The basic form is ssh user@host, where host is a hostname or IP address:

bash
ssh deploy@203.0.113.10 ssh -p 2222 deploy@example.com # non-default port ssh deploy@example.com 'uptime' # run one command and exit

The first time you connect, SSH shows the server's key fingerprint and asks you to confirm it. After you accept, the fingerprint is stored in ~/.ssh/known_hosts, and SSH will warn you loudly if it ever changes, which protects you against man-in-the-middle attacks.

Key-Based Authentication

Passwords over SSH work, but keys are more secure and more convenient. You generate a key pair locally: the private key stays on your laptop, and the public key is copied to the server.

bash
ssh-keygen -t ed25519 -C "you@example.com" ssh-copy-id deploy@example.com ssh deploy@example.com # now logs in without a password

Protect the private key with a passphrase when prompted. On servers you control, consider disabling password authentication entirely in /etc/ssh/sshd_config by setting PasswordAuthentication no, which shuts down brute-force attacks.

Copying Files with scp

scp uses the SSH connection to copy files between machines. The syntax mirrors cp, with remote paths written as user@host:path:

bash
scp report.pdf deploy@example.com:/var/www/files/ scp deploy@example.com:/var/log/nginx/access.log . scp -r ./site deploy@example.com:/var/www/ # copy a directory

For large or repeated transfers, rsync -avz over SSH is usually better because it only sends the differences, but scp remains the quickest tool for one-off copies.

The SSH Config File

Typing full connection strings gets old fast. The file ~/.ssh/config lets you define short, memorable aliases:

Host web HostName 203.0.113.10 User deploy Port 2222 IdentityFile ~/.ssh/id_ed25519

Now ssh web and scp file.txt web:/tmp/ just work. The config file supports dozens of options, including ForwardAgent for jumping between servers and LocalForward for tunneling ports to services like remote databases.

Staying Sane on Remote Servers

Two habits will save you repeatedly. First, run long tasks inside tmux or screen so that a dropped connection does not kill the job; reconnect and reattach with tmux attach. Second, before editing firewall rules or sshd_config remotely, keep a second SSH session open. If your change locks you out, the surviving session is your lifeline to undo it.

Key Takeaways

  • ssh user@host opens an encrypted shell on a remote machine; fingerprints guard against impostor servers.
  • Generate keys with ssh-keygen and install them with ssh-copy-id; prefer keys over passwords.
  • scp copies files over SSH; rsync is more efficient for big or repeated transfers.
  • Define aliases in ~/.ssh/config to shorten daily connections.
  • Use tmux for long-running jobs and keep a backup session open when changing SSH settings.

Congratulations, you have completed the Linux Command Line course. A great next step is our Next.js course, where you can put a Linux server to work hosting a modern web application.

SSH, scp and Working with Remote Servers - Linux & Command Line | CodeYourCraft | CodeYourCraft