RHCSA Rapid Track
Configure a user to use key-based authentication for SSH.
Outcomes
Generate an SSH key pair without passphrase protection.
Generate an SSH key pair with passphrase protection.
Authenticate with both passphrase-less and passphrase-protected SSH keys.
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-configure
Instructions
Log in to the
serverbmachine as thestudentuser.[student@workstation ~]$
ssh student@serverb...output omitted... [student@serverb ~]$Switch to the
operator1user on theserverbmachine. Useredhatas the password.[student@serverb ~]$
su - operator1Password:redhat[operator1@serverb ~]$Generate a set of SSH keys. Do not enter a passphrase.
[operator1@serverb ~]$
ssh-keygenGenerating public/private rsa key pair. Enter file in which to save the key (/home/operator1/.ssh/id_rsa):EnterCreated directory '/home/operator1/.ssh'. Enter passphrase (empty for no passphrase):EnterEnter same passphrase again:EnterYour identification has been saved in /home/operator1/.ssh/id_rsa. Your public key has been saved in /home/operator1/.ssh/id_rsa.pub. The key fingerprint is: SHA256:JainiQdnRosC+xXh operator1@serverb.lab.example.com The key's randomart image is: +---[RSA 3072]----+ |E+*+ooo . | |.= o.o o . | |o.. = . . o | |+. + * . o . | |+ = X . S + | | + @ + = . | |. + = o | |.o . . . . | |o o.. | +----[SHA256]-----+Send the public key of the SSH key pair to the
operator1user on theserveramachine, withredhatas the password.[operator1@serverb ~]$
ssh-copy-id operator1@servera/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/operator1/.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. This key is not known by any other names Are you sure you want to continue connecting (yes/no/[fingerprint])?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 operator1@servera's password:redhatNumber of key(s) added: 1 Now try logging in to the machine, with: "ssh 'operator1@servera'" and check to make sure that only the key(s) you wanted were added.Execute the
hostnamecommand on theserveramachine remotely by using thesshcommand without accessing the remote interactive shell.[operator1@serverb ~]$
ssh operator1@servera hostnameservera.lab.example.comThe preceding
sshcommand does not prompt you for a password, because it uses the passphrase-less private key against the exported public key to authenticate as theoperator1user on theserveramachine.This approach is not secure, because anyone who has access to the private key file can log in to the
serveramachine as theoperator1user.In a following step in this exercise, you make your private key more secure by encrypting it and protecting access to it by adding a passphrase.
Generate another set of SSH keys with the default name and without a passphrase, and overwrite the previously generated SSH key files. Try to connect to the
serveramachine by using the new SSH keys. Thesshcommand asks for a password, because it cannot authenticate with the SSH key. Run again thesshcommand with the-v(verbose) option to verify it.Send the new public key of the SSH key pair to the
operator1user on theserveramachine, to replace the previous public key. Useredhatas the password for theoperator1user on theserveramachine. Execute thehostnamecommand on theserveramachine remotely by using thesshcommand without accessing the remote interactive shell, to verify that it works again.Again, generate another set of SSH keys with the default name and without a passphrase, and overwrite the previously generated SSH key files.
[operator1@serverb ~]$
ssh-keygenGenerating public/private rsa key pair. Enter file in which to save the key (/home/operator1/.ssh/id_rsa):Enter/home/operator1/.ssh/id_rsa already exists. Overwrite (y/n)?yEnter passphrase (empty for no passphrase):EnterEnter same passphrase again:EnterYour identification has been saved in /home/operator1/.ssh/id_rsa Your public key has been saved in /home/operator1/.ssh/id_rsa.pub ...output omitted...Try to connect to the
serveramachine by using the new SSH keys. Thesshcommand asks for a password, because it cannot authenticate with the SSH key. Press Ctrl+c to exit from thesshcommand when it prompts for a password. Run again thesshcommand with the-v(verbose) option to verify it. Press again Ctrl+c to exit from thesshcommand when it prompts for a password.[operator1@serverb ~]$
ssh operator1@servera hostnameoperator1@servera's password: ^C[operator1@serverb ~]$ssh -v operator1@servera hostnameOpenSSH_8.7p1, OpenSSL 3.0.1 14 Dec 2021 debug1: Reading configuration data /etc/ssh/ssh_config debug1: Reading configuration data /etc/ssh/ssh_config.d/01-training.conf ...output omitted... debug1: Next authentication method: publickeydebug1: Offering public key: /home/operator1/.ssh/id_rsa RSA SHA256:ad597Zf64xckV26xht8bjQbzqSPuOXQPXksGEWVsP80debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password debug1: Trying private key: /home/operator1/.ssh/id_dsa debug1: Trying private key: /home/operator1/.ssh/id_ecdsa debug1: Trying private key: /home/operator1/.ssh/id_ecdsa_sk debug1: Trying private key: /home/operator1/.ssh/id_ed25519 debug1: Trying private key: /home/operator1/.ssh/id_ed25519_sk debug1: Trying private key: /home/operator1/.ssh/id_xmssdebug1: Next authentication method: passwordoperator1@servera's password: ^CSend the new public key of the SSH key pair to the
operator1user on theserveramachine, to replace the previous public key. Useredhatas the password for theoperator1user on theserveramachine. Execute thehostnamecommand on theserveramachine remotely by using thesshcommand without accessing the remote interactive shell, to verify that it works again.[operator1@serverb ~]$
ssh-copy-id operator1@servera...output omitted... operator1@servera's password:redhatNumber of key(s) added: 1 Now try logging in to the machine, with: "ssh 'operator1@servera'" and check to make sure that only the key(s) you wanted were added. [operator1@serverb ~]$ssh operator1@servera hostnameservera.lab.example.com
Generate another set of SSH keys with passphrase-protection. Save the key as
/home/operator1/.ssh/key2. Useredhatpassas the passphrase of the private key.[operator1@serverb ~]$
ssh-keygen -f .ssh/key2Generating public/private rsa key pair. Enter passphrase (empty for no passphrase):redhatpassEnter same passphrase again:redhatpassYour identification has been saved in .ssh/key2. Your public key has been saved in .ssh/key2.pub. The key fingerprint is: SHA256:OCtCjfPm5QrbPBgqb operator1@serverb.lab.example.com The key's randomart image is: +---[RSA 3072]----+ |O=X* | |OB=. | |E*o. | |Booo . | |..= . o S | | +.o o | |+.oo+ o | |+o.O.+ | |+ . =o. | +----[SHA256]-----+Send the public key of the passphrase-protected key pair to the
operator1user on theserveramachine. The command does not prompt you for a password, because it uses the public key of the passphrase-less private key that you exported to theserveramachine in the preceding step.[operator1@serverb ~]$
ssh-copy-id -i .ssh/key2.pub operator1@servera/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: ".ssh/key2.pub" /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 Number of key(s) added: 1 Now try logging in to the machine, with: "ssh 'operator1@servera'" and check to make sure that only the key(s) you wanted were added.Execute the
hostnamecommand on theserveramachine remotely by using thesshcommand. Use the/home/operator1/.ssh/key2key as the identity file. Specifyredhatpassas the passphrase, which you set for the private key in the preceding step.The command prompts you for the passphrase that you used to protect the private key of the SSH key pair. If an attacker gains access to the private key, then the attacker cannot use it to access other systems, because a passphrase protects the private key itself. The
sshcommand uses a different passphrase from theoperator1user on theserveramachine, and so users must know both passphrases.[operator1@serverb ~]$
ssh -i .ssh/key2 operator1@servera hostnameEnter passphrase for key '.ssh/key2':redhatpassservera.lab.example.comUse the
ssh-agentprogram, as in the following step, to avoid interactively typing the passphrase when logging in with SSH. Using thessh-agentprogram is both more convenient and more secure when the administrators log in to remote systems regularly.Run the
ssh-agentprogram in yourBashshell, and add the passphrase-protected private key (/home/operator1/.ssh/key2) of the SSH key pair to the shell session.The command starts the
ssh-agentprogram and configures the shell session to use it. Then, you use thessh-addcommand to provide the unlocked private key to thessh-agentprogram.[operator1@serverb ~]$
eval $(ssh-agent)Agent pid 1729 [operator1@serverb ~]$ssh-add .ssh/key2Enter passphrase for .ssh/key2:redhatpassIdentity added: .ssh/key2 (operator1@serverb.lab.example.com)Execute the
hostnamecommand on theserveramachine remotely without accessing a remote interactive shell. Use the/home/operator1/.ssh/key2key as the identity file.The command does not prompt you to enter the passphrase interactively.
[operator1@serverb ~]$
ssh -i .ssh/key2 operator1@servera hostnameservera.lab.example.comOpen another terminal on the
workstationmachine and log in to theserverbmachine as thestudentuser.[student@workstation ~]$
ssh student@serverb...output omitted... [student@serverb ~]$On the
serverbmachine, switch to theoperator1user and remotely log in to theserveramachine. Use the/home/operator1/.ssh/key2key as the identity file to authenticate by using the SSH keys.Use the
sucommand to switch to theoperator1user. Useredhatas the password for theoperator1user.[student@serverb ~]$
su - operator1Password:redhat[operator1@serverb ~]$Log in to the
serveramachine as theoperator1user.The command prompts you to enter the passphrase interactively, because you do not invoke the SSH connection from the same shell where you started the
ssh-agentprogram.[operator1@serverb ~]$
ssh -i .ssh/key2 operator1@serveraEnter passphrase for key '.ssh/key2':redhatpass...output omitted... [operator1@servera ~]$
Exit and close all extra terminals, and return to the
workstationmachine.Exit and close extra terminal windows. The
exitcommands leave theoperator1user's shell; terminate the shell session wheressh-agentis active; and return to thestudentuser's shell on theserverbmachine.[operator1@servera ~]$
exitlogout Connection to servera closed. [operator1@serverb ~]$Return to the
workstationsystem as thestudentuser.[operator1@serverb ~]$
exitlogout [student@serverb ~]$exitlogout Connection to serverb closed. [student@workstation ~]$