export the public key from the client to the server using ssh-copy-id
log in with the password via ssh to the server and switch to the root account using the sudo - su or sudo -i command
enable key login and disable password login. Save the changes and restart the ssh service.
do not close the current session. Open a new ssh session and log in to the server with your private key.
if you have successfully logged in, secure the server using the information below and then restart the ssh service on the second session.
remember to keep the first ssh session open all the time so you can undo changes if necessary.
restart the ssh service and check if you can log in with the third session to the server. If so, you have successfully configured the ssh server correctly.
if you are willing, write a script using sed or awk to make server-side changes to the sshd_config file so that you don’t have to manually apply the changes.
OpenSSH : KeyBoard-Intereractive Auth
OpenSSH is already installed by default, so it’s not necessarry to install new packages. You can login with KeyBoard-Intereractive Authentication by default, but change some settings for security like follows.
If OpenSSH, however, is not yet installed you can install it with the following command:
# ssh [login_user@hostname_or_IP_address]adrian@client:~> ssh adrian@example.com
The authenticity of host 'example.com (10.0.0.50)' can't be established.
ECDSA key fingerprint is SHA256:h0QhlXgCZ860UjM8sAjY6Wmrr2EqSIY5UADBi0wAFV4.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'example.com,10.0.0.50' (ECDSA) to the list of known hosts.
Password: # login user's password
adrian@example.com:~> # just logined
SSH Key-Pair Authentication
Configure SSH server to login with Key-Pair Authentication. Create a private key for client and a public key for server to do it.
Create Key-Pair for each user, so login with a common user on SSH Server Host and work like follows.
# create key-pair on a clientssh-keygen -t rsa -b 4096 -C "name and surname"Generating public/private rsa key pair.
Enter file in which to save the key (/home/adrian/.ssh/id_rsa): /home/adrian/.ssh/p-tech
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/adrian/.ssh/p-tech
Your public key has been saved in /home/adrian/.ssh/p-tech.pub
The key fingerprint is:
SHA256:IPtApVZ/8o6mCY3lKSvcfEtkD6wzHJ0LzKeHFm3qbxs adrian@G02PLXN05963
The key's randomart image is:
+---[RSA 4096]----+
| o || + . ||= . o . ||= * o + || O % S . || . ^ = o || . o& E + . ||oooOo=|| .o+*o |+----[SHA256]-----+
To generate a passphrase you can use the following command in a separate CLI window
hexdump -vn16 -e'4/4 “%08X” 1 “\n”’ /dev/urandom
List the key-pair
1
2
3
adrian@linux:~> ll ~/.ssh/p-tech*
-rw------- 1 adrian adrian 3.4K Apr 1 16:44 /home/adrian/.ssh/p-tech
-rw-r--r-- 1 adrian adrian 745 Apr 1 16:44 /home/adrian/.ssh/p-tech.pub
The public key is saved into the ~/.ssh/authorized_keys
Provide a password
1
2
# login with the key to the serverssh -i ~/.ssh/p-tech student@IP-ADDRRESS
Provide a passphrase
Automation
Add below entries to .bashrc or .zshrc file located in your /home/user directory. First entry starts ssh agent and a second loads your private key to the agent. If you did set up a passphrase on your key it will ask for it. You can add more than one key. Bear in mind, that each time the Bash or Zsh starts aftyer a reboot or boot process of the operating system, in CLI it will ask you to provide a passphrase.