In this lab, you set up key-based authentication for users, and disable direct login as root and password authentication for all users for the OpenSSH service on one of your servers.
Outcomes
Authenticate with SSH keys.
Prevent users from directly logging in as the root user over the ssh service.
Prevent users from logging in to the system with SSH password-based authentication.
As the student user on the workstation machine, use the lab command to prepare your system for this exercise.
This command prepares your environment and ensures that all required resources are available.
[student@workstation ~]$ lab start ssh-review
Instructions
From the workstation machine, log in to the servera machine as the student user.
Switch to the production1 user on the servera machine.
Enter redhat as the password.
Generate passphrase-less SSH keys for the production1 user on the servera machine.
[production1@servera ~]$ssh-keygenGenerating public/private rsa key pair. Enter file in which to save the key (/home/production1/.ssh/id_rsa):EnterCreated directory '/home/production1/.ssh'. Enter passphrase (empty for no passphrase):EnterEnter same passphrase again:EnterYour identification has been saved in /home/production1/.ssh/id_rsa. Your public key has been saved in /home/production1/.ssh/id_rsa.pub. The key fingerprint is: SHA256:MCQ8nXClDFSlJV0i5IouUzlzFrbsdz+jO8ZIMeSTOuQ production1@servera.lab.example.com The key's randomart image is: +---[RSA 3072]----+ | o==B==.. | | oB+*.. | | o+B. | | =.+*o | | *o*. +S | | o *E . | |o . .o.o. | | o ...+.o | | .o+.o | +----[SHA256]-----+
Send the public key of the SSH key pair to the production1 user on the serverb machine.
[production1@servera ~]$ssh-copy-id production1@serverb/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/production1/.ssh/id_rsa.pub" The authenticity of host 'serverb (172.25.250.11)' can't be established. ED25519 key fingerprint is SHA256:h/hEJa/anxp6AP7BmB5azIPVbPNqieh0oKi4KWOTK80. Are you sure you want to continue connecting (yes/no)?yes/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys production1@serverb's password:redhatNumber of key(s) added: 1 Now try logging in to the machine, with: "ssh 'production1@serverb'" and check to make sure that only the key(s) you wanted were added.
Verify that the production1 user can successfully log in to the serverb machine with the SSH keys.
Configure the sshd service on serverb to prevent users from logging in as the root user.
Use redhat as the root password.
Switch to the root user on the serverb machine.
[production1@serverb ~]$su -Password:redhat[root@serverb ~]#
Set the PermitRootLogin parameter to no in the /etc/ssh/sshd_config file and reload the sshd service.
Edit the active uncommented parameter and not a commented example.
...output omitted... PermitRootLoginno...output omitted... [root@serverb ~]#systemctl reload sshd.service
Open another terminal on the workstation machine and log in to the servera machine as the production1 user.
From servera, try to log in to the serverb machine as the root user.
This command should fail, because you disabled SSH root user login.
The command exited after three failed attempts to log in to the servera machine as the root user.
By default, the ssh command prefers to use SSH keys for authentication, and if it does not find the necessary keys of the user, then it requests the user's password for authentication.
For course convenience, the password-less root login is already configured between workstation and servera in the classroom environment.
However, this configuration is highly insecure, and is not recommended for any production environment.
[student@workstation ~]$ssh production1@servera...output omitted... [production1@servera ~]$ssh root@serverbroot@serverb's password:redhatPermission denied, please try again. root@serverb's password:redhatPermission denied, please try again. root@serverb's password:redhatroot@serverb: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password). [production1@servera ~]$
Configure the sshd service on the serverb machine to allow users to authenticate with SSH keys only, rather than with their passwords.
Return to the first terminal with the root active shell on the serverb machine.
Set the PasswordAuthentication parameter to no in the /etc/ssh/sshd_config file and reload the sshd service.
Edit the active uncommented parameter and not a commented example.
...output omitted...
PasswordAuthentication no
...output omitted...
[root@serverb ~]# systemctl reload sshdGo to the second terminal with the production1 active shell on the servera machine, and try to log in to the serverb machine as the production2 user.
This command should fail, because SSH keys are not configured for the production2 user, and the sshd service on the serverb machine does not allow the use of passwords for authentication.
[production1@servera ~]$ ssh production2@serverb
production2@serverb: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).For more granularity, you can use the explicit ssh command -o PubkeyAuthentication=no and -o PasswordAuthentication=yes options.
With these options, you can override the ssh command defaults, and confidently establish whether the preceding command failed based on the settings that you adjusted in the /etc/ssh/sshd_config file in the preceding step.
Return to the first terminal with the root active shell on the serverb machine.
Verify that PubkeyAuthentication is enabled in the /etc/ssh/sshd_config file.
[root@serverb ~]$ cat /etc/ssh/sshd_config
...output omitted...
#PubkeyAuthentication yes
...output omitted...The PubkeyAuthentication line is commented.
Commented lines indicate the default values of a parameter.
The public key authentication of SSH is active by default, as the commented line indicates.
Return to the second terminal with the production1 active shell on the servera machine, and try to log in to the serverb machine as the production1 user.
This command should succeed, because SSH keys are configured for the production1 user to log in to the serverb machine from the servera machine.
[production1@servera ~]$ ssh production1@serverb
...output omitted...
[production1@serverb ~]$Exit and close the extra terminal.
[production1@serverb ~]$exitlogout Connection to serverb closed. [production1@servera ~]$exitlogout [student@workstation ~]$exit
Return to the workstation system as the student user.
[production1@serverb ~]$exitlogout Connection to serverb closed. [production1@servera ~]$exitlogout [student@servera ~]$exitlogout Connection to servera closed. [student@workstation ~]$
This concludes the section.