SSH keys
More detailed guide by Digital Ocean https://www.digitalocean.com/community/tutorials/how-to-configure-ssh-key-based-authentication-on-a-linux-server
You will have two keys (in a pair)
- Public
This one will be on the server you are logging to. If this one gets stolen it isn't that big of a deal. - Private
This is the one that will live on your machine, be careful with it. This will allow you to log into any server that has public key from its pair.
Keep in mind you don't need a new pair for every server, you can use one pair for everything if you want (but if one gets compromised all of them do).
For SSH
Generate
You can do this on a Windows machine if you have open-ssh installed.
- Start generating key
ssh-keygen
- Pick name for your private ssh key (default is id_rsa without any file extension)
- You can give it a password if you want, adds another layer of security.
Move private key to proper location
Linux: ~/.ssh
mv example ~/.ssh
Windows: C:\Users\username\.ssh
Copy public key to target server
Using scp
scp .\example.pub user@serverip:/home/user/.ssh
Add public key to authorized_keys
cat /home/user/.ssh/example.pub > /home/user/.ssh/authorized_keys
Connect using your private key
By default open-ssh is looking for id_rsa file. If you haven't changed your key's name, you can use
ssh user@serverip
If you changed your key's name, you need to specify what key you want to use using the -i parameter
ssh user@serverip -i /path/to/example
This will work on Linux and Windows, but if you need to use Powershell (which translates some unix commands to Windows APIs, so ~ is your home directory, even on Windows)
ssh [email protected] -i ~\.ssh\example
Disable password login
Edit /etc/ssh/sshd_config
sudo nano /etc/ssh/sshd_config
- On line 57 find “#PasswordAuthentication Yes”
- Change Yes to No
- Remove # from the line
- Restart sshd service
sudo systemctl restart sshd
Optional
- Change ChallengeResponseAuthentication to No
- Change UsePAM to No
- Restart sshd service
sudo systemctl restart sshd
Now you must log in using your ssh key. Do not lose it.
For Filezilla (SFTP)
Filezilla can't to sftp connect using the open-ssh key, so we need to convert it to a PuTTY compatible format.
- Install PuTTY
winget install PuTTY.PuTTY
Convert key
- Run PuTTYgen
- In the Conversion tab use “Import Key”
- (Optional) You can remove the commend and add a password for the key
- In the top bar, open Key and Parameters for saving key files
- Select PPK Version 2 and Ok
- Click “Save Private Key”
- I recommend you name the file the same as the openssh one with .ppk, for example “example.ppk”
Load key
- Run Pageant (installed alongside PuTTY)
- Open it by double clicking it in the system tray
- Add key
- Sidenote: this isn't persistent. Your key will only be loaded until you quit Pageant.
You can get around this by creating a full PuTTY profile, but that is for another guide
Log in using key
- Run FileZilla
- host
sftp://serverip
- username
username
- port
your SSH port (default is 22)
- password
keep empty even if Filezilla asks you again
- You should be logged in