{{ define "styles" }} {{ end }} {{ define "content" }}
The information provided on this page assumes you are using sshd
as the SSH server on a Linux machine.
sshd
is configured using the sshd_config
file, typically located in /etc/ssh/
.
Generally, using private key authentication for SSH is preferred instead of password authentication. To disable password auth, add/uncomment the following line in sshd_config
, save, and restart sshd
:
PasswordAuthentication no
Even with password auth disabled, it is still a good idea to disable root login over SSH: you should set up a non-root user and use that user to log in. To disable root login over SSH, add/uncomment the following line in sshd_config
, save, and restart sshd
:
PermitRootLogin no
First, generate a key on the local machine:
ssh-keygen
You can provide a path for the file containing the new key. It can be useful to give the file a distinct name so you are not using the same key for multiple systems. That said, it is advisable to keep the path the same (typically ~/.ssh/
), since most tools that use SSH look in that directory for keys by default.
You can optionally provide a passphrase. This passphrase (if provided) will need to be entered at every login.
Now that the key has been generated, it needs to be copied to the server. Many online guides will cover using ssh-copy-id
to to just this. However, ssh-copy-id
only works if password authentication is enabled on the remote machine, and password authentication should be disabled to harden SSH access against attacks.
Instead, we will need to copy the new key to the remote machine manually. This requires access to the remote machine, likely via another local machine already cofigured for SSH access.
Copy the newly-generated public key (contained in the .pub
file generated when you ran ssh-keygen
) from the local machine. Then log into the remote machine and paste the public key into the ~/.ssh/authorized_keys
file and save.
Now the new key can be used to SSH into the remote machine. If your key was generated using the default name of id_rsa
, the following should work:
ssh <USER>@<REMOTE_HOST>
If a different name was chosen for the key file (e.g. my-server
), use the -i
("identity") flag to indicate which key file to use:
ssh -i ~/.ssh/my-server <USER>@<REMOTE_HOST>{{ end }} {{ define "scripts" }} {{ end }}