Red Hat System Administration I
Disable direct logins as root and disable password-based authentication for the OpenSSH service on one of your servers.
Outcomes
Disable direct logins as
rootoverssh.Disable password-based authentication for remote users to connect over SSH.
As the student user on the workstation machine, use the lab command to prepare your system for this exercise.
This command ensures that all required resources are available.
[student@workstation ~]$ lab start ssh-customize
Instructions
From
workstation, open an SSH session to theserverbmachine as thestudentuser.[student@workstation ~]$
ssh student@serverb[student@serverb ~]$Use the
sucommand to switch to theoperator2user on theserverbmachine. Useredhatas the password for theoperator2user.[student@serverb ~]$
su - operator2Password:redhat[operator2@serverb ~]$Use the
ssh-keygencommand to generate SSH keys. Do not enter any passphrase for the keys.[operator2@serverb ~]$
ssh-keygenGenerating public/private rsa key pair. Enter file in which to save the key (/home/operator2/.ssh/id_rsa):EnterCreated directory '/home/operator2/.ssh'. Enter passphrase (empty for no passphrase):EnterEnter same passphrase again:EnterYour identification has been saved in /home/operator2/.ssh/id_rsa. Your public key has been saved in /home/operator2/.ssh/id_rsa.pub. The key fingerprint is: SHA256:LN5x1irX0OWxgyd/qhATNgZWOtLUj16EZkM1JHkCR+I operator2@serverb.lab.example.com The key's randomart image is: +---[RSA 3072]----+ | *=+ | | = =O.o. | | . Eo=B o | | o +.=o+ o | | . S..= = | | . o +. + . | | . o + . . .| | o . o | | ... | +----[SHA256]-----+Use the
ssh-copy-idcommand to send the public key of the SSH key pair to theoperator2user on theserveramachine. Useredhatas the password for theoperator2user onservera.[operator2@serverb ~]$
ssh-copy-id operator2@servera/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/operator2/.ssh/id_rsa.pub" The authenticity of host 'servera (172.25.250.10)' 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 operator2@servera's password:redhatNumber of key(s) added: 1 Now try logging into the machine, with: "ssh 'operator2@servera'" and check to make sure that only the key(s) you wanted were added.Confirm that you can successfully log in to the
serveramachine as theoperator2user with the SSH keys.Open an SSH session to the
serveramachine as theoperator2user.[operator2@serverb ~]$
ssh operator2@servera...output omitted... [operator2@servera ~]$The preceding
sshcommand used SSH keys for authentication.Log out of the
serveramachine.[operator2@servera ~]$
exitlogout Connection to servera closed.
Confirm that you can successfully log in to the
serveramachine as therootuser withredhatas the password.Open an SSH session to the
serveramachine as therootuser withredhatas the password.[operator2@serverb ~]$
ssh root@serveraroot@servera's password:redhat...output omitted... [root@servera ~]#The preceding
sshcommand used the password of the superuser for authentication, because SSH keys do not exist for the superuser.Log out of the
serveramachine.[root@servera ~]#
exitlogout Connection to servera closed. [operator2@serverb ~]$
Confirm that you can successfully log in to the
serveramachine as theoperator3user withredhatas the password.Open an SSH session to the
serveramachine as theoperator3user withredhatas the password.[operator2@serverb ~]$
ssh operator3@serveraoperator3@servera's password:redhat...output omitted... [operator3@servera ~]$The preceding
sshcommand used the password of theoperator3user for authentication, because SSH keys do not exist for theoperator3user.Log out of the
serveramachine.[operator3@servera ~]$
exitlogout Connection to servera closed. [operator2@serverb ~]$
Configure the
sshdservice on theserveramachine to prevent users from logging in as therootuser. Useredhatas the password of the superuser when required.Open an SSH session to the
serveramachine as theoperator2user with the SSH keys.[operator2@serverb ~]$
ssh operator2@servera...output omitted... [operator2@servera ~]$On the
serveramachine, switch to therootuser. Useredhatas the password for therootuser.[operator2@servera ~]$
su -Password:redhat[root@servera ~]#Set
PermitRootLogintonoin the/etc/ssh/sshd_configfile and reload thesshdservice. You can use thevim /etc/ssh/sshd_configcommand to edit the configuration file of thesshdservice....output omitted... PermitRootLogin no ...output omitted... [root@servera ~]#
systemctl reload sshdOpen another terminal on
workstation, and open an SSH session to theserverbmachine as theoperator2user. From theserverbmachine, try to log in to theserveramachine as therootuser. This command should fail, because you disabled therootuser login over SSH in the preceding step.Note
For your convenience, password-less login is already configured between
workstationandserverbin the classroom environment.[student@workstation ~]$
ssh operator2@serverb...output omitted... [operator2@serverb ~]$ssh root@serveraroot@servera's password:redhatPermission denied, please try again. root@servera's password:redhatPermission denied, please try again. root@servera's password:redhatroot@servera: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).By default, the
sshcommand attempts to authenticate with key-based authentication first, and if that method fails, then with password-based authentication.
Configure the
sshdservice on theserveramachine to allow users to authenticate with SSH keys only, rather than with their passwords.Return to the first terminal with the
rootuser's active shell on theserveramachine. Set thePasswordAuthenticationparameter tonoin the/etc/ssh/sshd_configfile and reload thesshdservice. You can use thevim /etc/ssh/sshd_configcommand to edit the configuration file of thesshdservice....output omitted... PasswordAuthentication no ...output omitted... [root@servera ~]#
systemctl reload sshdGo to the second terminal with the
operator2user's active shell on theserverbmachine, and try to log in to theserveramachine as theoperator3user. This command should fail, because SSH keys are not configured for theoperator3user, and thesshdservice on theserveramachine does not allow the use of passwords for authentication.[operator2@serverb ~]$
ssh operator3@serveraoperator3@servera: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).Note
For more granularity, you can use the explicit
-o PubkeyAuthentication=noand-o PasswordAuthentication=yesoptions with thesshcommand. You can then override thesshcommand's defaults and confidently determine that the preceding command fails based on the settings that you adjusted in the/etc/ssh/sshd_configfile in the preceding step.Return to the first terminal with the
rootuser's active shell on theserveramachine. Verify thatPubkeyAuthenticationis enabled in the/etc/ssh/sshd_configfile. You can use thevim /etc/ssh/sshd_configcommand to view the configuration file of thesshdservice....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
operator2user's active shell on theserverbmachine, and try to log in to theserveramachine as theoperator2user. This command should succeed, because the SSH keys are configured for theoperator2user to log in to theserveramachine from theserverbmachine.[operator2@serverb ~]$
ssh operator2@servera...output omitted... [operator2@servera ~]$From the second terminal, exit the
operator2user's shell on both theserveraandserverbmachines.[operator2@servera ~]$
exitlogout Connection to servera closed. [operator2@serverb ~]$exitlogout Connection to serverb closed. [student@workstation ~]$Close the second terminal on the
workstationmachine.[student@workstation ~]$
exitFrom the first terminal, exit the
rootuser's shell on theserveramachine.[root@servera ~]#
exitlogoutFrom the first terminal, exit the
operator2user's shell on both theserveraandserverbmachines.[operator2@servera ~]$
exitlogout Connection to servera closed. [operator2@serverb ~]$exitlogout [student@serverb ~]$Log out of
serverb, and return to thestudentuser's shell onworkstation.[student@serverb ~]$
exitlogout Connection to serverb closed. [student@workstation ~]$