Skip to content
Go back

SSH keys and secure file copy

Updated:
By SumGuy 4 min read
SSH keys and secure file copy

SSH is the backbone of working with remote Linux servers. You’re not using it right if you’re still typing passwords. Here’s the complete flow: generate a key, get it on the server, configure your client, and then move files securely.

Generating SSH Keys

You want ed25519 keys. They’re smaller, faster, and more secure than RSA. If your target server is ancient (pre-2014), fall back to RSA.

Ed25519 (modern servers — do this):

Generate ed25519 key
ssh-keygen -t ed25519 -C "you@laptop" -f ~/.ssh/id_ed25519

The -C flag is just a comment. The -f flag specifies where to save it. When prompted for a passphrase, use one — your key file itself is encrypted on disk.

RSA (legacy servers):

Generate RSA key
ssh-keygen -t rsa -b 4096 -C "you@laptop" -f ~/.ssh/id_rsa

The -b 4096 sets the key size to 4096 bits (stronger than the 2048 default, and still fast). Anything under 2048 is obsolete — don’t bother.

You now have two files: ~/.ssh/id_ed25519 (private key — never share) and ~/.ssh/id_ed25519.pub (public key — safe to distribute).

Getting Your Public Key to the Server

The easy way:

Copy public key to server
ssh-copy-id -i ~/.ssh/id_ed25519.pub username@example.com

ssh-copy-id handles all the fiddly details: it logs in (you’ll type your password one last time), appends your public key to ~/.ssh/authorized_keys on the server, and sets permissions correctly so SSH won’t complain.

If ssh-copy-id isn’t available (some Windows setups), do it manually:

Manual public key installation
cat ~/.ssh/id_ed25519.pub | ssh username@example.com "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"

Now test passwordless login:

Test SSH access
ssh username@example.com

No password prompt? You’re done with auth. Close the connection and move on.

Configuring ~/.ssh/config

Stop typing ssh -p 2222 username@192.168.1.100. Create ~/.ssh/config:

~/.ssh/config
Host homelab
HostName 192.168.1.100
User username
Port 2222
IdentityFile ~/.ssh/id_ed25519
Host production
HostName prod.example.com
User deploy
IdentityFile ~/.ssh/id_rsa
StrictHostKeyChecking accept-new

Now you just type ssh homelab. SSH uses the right user, port, and key automatically. You can add multiple hosts — each gets its own section.

Common options:

Copying Files with SCP

SCP (secure copy) works like cp, except it can reach across the network over SSH.

Remote to local:

Download a file
scp username@example.com:/var/log/app.log ~/Downloads/

Local to remote:

Upload a file
scp ~/myapp.jar username@example.com:/opt/apps/

Recursive (directories):

Copy a directory tree
scp -r ~/myconfig/ username@example.com:/etc/myapp/

The -r flag means “recursively copy everything inside.” No -r? SCP treats it as a file and fails.

Using a non-standard SSH port:

SCP with custom port
scp -P 2222 ~/file.txt username@example.com:/tmp/

Note: it’s -P (capital), not -p. (Confusingly, ssh uses lowercase -p for ports. Different tools, different conventions.)

Host-to-host (remote to remote):

Copy between two remote servers
scp username@server1.com:/data/backup.tar.gz \
username@server2.com:/backups/

Copying multiple files:

Multiple files to remote
scp file1.txt file2.txt username@example.com:~/

Or use glob patterns:

Files matching a pattern
scp username@example.com:~/logs/*.gz ~/backups/

Quick Cheatsheet

TaskCommand
Generate ed25519 keyssh-keygen -t ed25519 -C "comment" -f ~/.ssh/id_ed25519
Copy public key to serverssh-copy-id -i ~/.ssh/id_ed25519.pub user@host
SSH to a hostssh user@host (or ssh hostname if in ~/.ssh/config)
Download a filescp user@host:/path/file ~/local/
Upload a filescp ~/file user@host:/path/
Copy a directoryscp -r ~/dir/ user@host:/path/
Download on custom portscp -P 2222 user@host:/file ~/
Copy between remotesscp user1@host1:/file user2@host2:/path/

That’s it. Keys, config, and file transfers — the foundation of working with remote machines. Your 2 AM self will appreciate never having to type a password again.


Share this post on:

Send a Webmention

Written about this post on your own site? Send a webmention and it'll show up above once verified.


Previous Post
Sed 101
Next Post
Why You Should Switch to ZShell (zsh)

Discussion

Powered by Garrul . Sign in with GitHub or Google, or post anonymously.

Related Posts