Chapter 2 · 2.4
An SSH config: log in with a short name
Typing ssh anna@203.0.113.10 every time is a hassle. In an SSH config, you give your server a short name.
Creating the file
On your own computer, the file config lives in your .ssh folder: ~/.ssh/config on Mac and Linux, C:\Users\jouwnaam\.ssh\config on Windows (without an extension). Put this in it:
Host web1
HostName 203.0.113.10
User anna
IdentityFile ~/.ssh/id_ed25519
ServerAliveInterval 60
Now you log in with:
ssh web1
ServerAliveInterval 60 sends a signal every minute, so your connection doesn't drop when you're idle for a while.
Copying files
The short name also works for copying:
scp rapport.pdf web1:/home/anna/
scp web1:/var/log/nginx/error.log .
rsync -av ./site/ web1:/srv/site/
rsync only sends what has changed and needs to be installed on both computers. Prefer dragging with the mouse? An SFTP program such as FileZilla or WinSCP works with the same key.
A tunnel instead of an open port
Want to reach an admin panel or database on your server that only listens on the server itself (127.0.0.1)? Don't open a port. Create a tunnel instead:
ssh -L 8080:127.0.0.1:3001 web1
As long as this connection is open, http://localhost:8080 in your browser opens the service on port 3001 of your server. From the outside, that port stays closed.
