There are a few different ways of logging into an SSH server, SSH-key-based authentication and password-based authentication are the most basic methods . Here we'll focus on the SSH-key based authentication.
Before we go, make sure we have the SSH key on the local computer.
path: ~/.ssh
create a SSH-key if necessary:
ssh-keygen
(optional)copy the public key to the clipboard:
pbcopy < ~/.ssh/id_rsa.pub
Option1: Copy the public key to the remote server:
ssh-copy-id username@remote_host
Option2: Logging into the remote server and check if the authorized_keys file is exists.
path: ~/.ssh/authorized_keys
create the .ssh directory and the authorized_keys if necessary
mkdir .ssh
touch ~/.ssh/authorized_keys
copy the public key to authorized_keys
echo public_key_string >> ~/.ssh/authorized_keys
(optional) Disable Password Authentication on the Server
sudo vi /etc/ssh/sshd_config
Inside the file, search for a directive called PasswordAuthentication
. This may be commented out. Press i
to insert text, and then uncomment the line and set the value to "no". This will disable your ability to log in via SSH using account passwords. To actually implement these changes, we need to restart the sshd
service:
sudo systemctl restart sshd.service