Red Hat System Administration I
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
rootuser over thesshservice.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
workstationmachine, log in to theserveramachine as thestudentuser.Switch to the
production1user on theserveramachine. Enterredhatas the password.Generate passphrase-less SSH keys for the
production1user on theserveramachine.[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
production1user on theserverbmachine.[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
production1user can successfully log in to theserverbmachine with the SSH keys.Configure the
sshdservice onserverbto prevent users from logging in as therootuser. Useredhatas therootpassword.Switch to the
rootuser on theserverbmachine.[production1@serverb ~]$
su -Password:redhat[root@serverb ~]#Set the
PermitRootLoginparameter tonoin the/etc/ssh/sshd_configfile and reload thesshdservice. Edit the active uncommented parameter and not a commented example....output omitted... PermitRootLogin
no...output omitted... [root@serverb ~]#systemctl reload sshd.serviceOpen another terminal on the
workstationmachine and log in to theserveramachine as theproduction1user. Fromservera, try to log in to theserverbmachine as therootuser. This command should fail, because you disabled SSHrootuser login.The command exited after three failed attempts to log in to the
serveramachine as therootuser. By default, thesshcommand 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.Note
For course convenience, the password-less
rootlogin is already configured betweenworkstationandserverain 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
sshdservice on theserverbmachine to allow users to authenticate with SSH keys only, rather than with their passwords.Return to the first terminal with the
rootactive shell on theserverbmachine. Set thePasswordAuthenticationparameter tonoin the/etc/ssh/sshd_configfile and reload thesshdservice. 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
production1active shell on theserveramachine, and try to log in to theserverbmachine as theproduction2user. This command should fail, because SSH keys are not configured for theproduction2user, and thesshdservice on theserverbmachine does not allow the use of passwords for authentication.[production1@servera ~]$
ssh production2@serverbproduction2@serverb: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).Note
For more granularity, you can use the explicit
sshcommand-o PubkeyAuthentication=noand-o PasswordAuthentication=yesoptions. With these options, you can override thesshcommand defaults, and confidently establish whether the preceding command failed based on the settings that you adjusted in the/etc/ssh/sshd_configfile in the preceding step.Return to the first terminal with the
rootactive shell on theserverbmachine. Verify thatPubkeyAuthenticationis enabled in the/etc/ssh/sshd_configfile.[root@serverb ~]$
cat /etc/ssh/sshd_config...output omitted... #PubkeyAuthentication yes ...output omitted...The
PubkeyAuthenticationline 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
production1active shell on theserveramachine, and try to log in to theserverbmachine as theproduction1user. This command should succeed, because SSH keys are configured for theproduction1user to log in to theserverbmachine from theserveramachine.[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 ~]$exitReturn to the
workstationsystem as thestudentuser.[root@serverb ~]#
exitlogout [production1@serverb ~]$exitlogout Connection to serverb closed. [production1@servera ~]$exitlogout [student@servera ~]$exitlogout Connection to servera closed. [student@workstation ~]$