RHCSA Rapid Track
In this exercise, you will configure a user to use key-based authentication for SSH.
Outcomes
You should be able to:
Generate an SSH key pair without passphrase protection.
Generate an SSH key pair with passphrase protection.
Authenticate using both passphrase-less and passphrase-protected SSH keys.
Log in to workstation as student using student as the password.
On workstation, run lab ssh-configure start to start the exercise.
This script creates the necessary user accounts.
[student@workstation ~]$lab ssh-configure start
From
workstation, open an SSH session toserverbasstudent.[student@workstation ~]$ssh student@serverb...output omitted...[student@serverb ~]$Use the su command to switch to the
operator1user onserverb. Useredhatas the password ofoperator1.[student@serverb ~]$su - operator1Password:redhat[operator1@serverb ~]$Use the ssh-keygen command to generate 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+xXhOqsJQQLzBNUldb+jJbyrCZQBERI operator1@serverb.lab.example.com The key's randomart image is: +---[RSA 2048]----+ |E+*+ooo . | |.= o.o o . | |o.. = . . o | |+. + * . o . | |+ = X . S + | | + @ + = . | |. + = o | |.o . . . . | |o o.. | +----[SHA256]-----+Use the ssh-copy-id command to send the public key of the SSH key pair to
operator1onservera. Useredhatas the password ofoperator1onservera.[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. ECDSA key fingerprint is SHA256:ERTdjooOIrIwVSZQnqD5or+JbXfidg0udb3DXBuHWzA.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 keysoperator1@servera's password:redhatNumber of key(s) added: 1 Now try logging into the machine, with: "ssh 'operator1@servera'" and check to make sure that only the key(s) you wanted were added.Execute the hostname command on
serveraremotely using SSH without accessing the remote interactive shell.[operator1@serverb ~]$ssh operator1@servera hostnameservera.lab.example.comNotice that the preceding ssh command did not prompt you for a password because it used the passphrase-less private key against the exported public key to authenticate as
operator1onservera. This approach is not secure, because anyone who has access to the private key file can log in toserveraasoperator1. The secure alternative is to protect the private key with a passphrase, which is the next step.Use the ssh-keygen command to generate another set of SSH keys with passphrase-protection. Save the key as
/home/operator1/.ssh/key2. Useredhatpassas the passphrase of the private key.Warning
If you do not specify the file where the key gets saved, the default file (
/home/) is used. You have already used the default file name when generating SSH keys in the preceding step, so it is vital that you specify a non-default file, otherwise the existing SSH keys will be overwritten.user/.ssh/id_rsa[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:OCtCjfPm5QrbPBgqbEIWCcw5AI4oSlMEbgLrBQ1HWKI operator1@serverb.lab.example.com The key's randomart image is: +---[RSA 2048]----+ |O=X* | |OB=. | |E*o. | |Booo . | |..= . o S | | +.o o | |+.oo+ o | |+o.O.+ | |+ . =o. | +----[SHA256]-----+Use the ssh-copy-id command to send the public key of the passphrase-protected key pair to
operator1onservera.[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 into the machine, with: "ssh 'operator1@servera'" and check to make sure that only the key(s) you wanted were added.Notice that the preceding ssh-copy-id command did not prompt you for a password because it used the public key of the passphrase-less private key that you exported to
serverain the preceding step.Execute the hostname command on
serveraremotely with SSH without accessing the remote interactive shell. Use/home/operator1/.ssh/key2as the identity file. Specifyredhatpassas the passphrase, which you set for the private key in the preceding step.[operator1@serverb ~]$ssh -i .ssh/key2 operator1@servera hostnameEnter passphrase for key '.ssh/key2':redhatpassservera.lab.example.comNotice that the preceding ssh command prompted you for the passphrase you used to protect the private key of the SSH key pair. This passphrase protects the private key. Should an attacker gain access to the private key, the attacker cannot use it to access other systems because the private key itself is protected with a passphrase. The ssh command uses a different passphrase than the one for
operator1onservera, requiring users to know both.You can use ssh-agent, as in the following step, to avoid interactively typing in the passphrase while logging in with SSH. Using ssh-agent is both more convenient and more secure in situations where the administrators log in to remote systems regularly.
Run ssh-agent in your Bash shell and add the passphrase-protected private key (
/home/operator1/.ssh/key2) of the SSH key pair to the shell session.[operator1@serverb ~]$eval $(ssh-agent)Agent pid 21032[operator1@serverb ~]$ssh-add .ssh/key2Enter passphrase for .ssh/key2:redhatpassIdentity added: .ssh/key2 (operator1@serverb.lab.example.com)The preceding eval command started ssh-agent and configured this shell session to use it. You then used ssh-add to provide the unlocked private key to ssh-agent.
Execute the hostname command on
serveraremotely without accessing a remote interactive shell. Use/home/operator1/.ssh/key2as the identity file.[operator1@serverb ~]$ssh -i .ssh/key2 operator1@servera hostnameservera.lab.example.comNotice that the preceding ssh command did not prompt you to enter the passphrase interactively.
Open another terminal on
workstationand open an SSH session toserverbasstudent.[student@workstation ~]$ssh student@serverb...output omitted...[student@serverb ~]$On
serverb, use the su command to switch tooperator1and invoke an SSH connection toservera. Use/home/operator1/.ssh/key2as the identity file to authenticate using the SSH keys.Use the su command to switch to
operator1. Useredhatas the password ofoperator1.[student@serverb ~]$su - operator1Password:redhat[operator1@serverb ~]$Open an SSH session to
serveraasoperator1.[operator1@serverb ~]$ssh -i .ssh/key2 operator1@serveraEnter passphrase for key '.ssh/key2':redhatpass...output omitted...[operator1@servera ~]$Notice that the preceding ssh command prompted you to enter the passphrase interactively because you did not invoke the SSH connection from the shell that you used to start ssh-agent.
Exit all the shells you are using in the second terminal.
Log out of
servera.[operator1@servera ~]$exitlogout Connection to servera closed.[operator1@serverb ~]$Exit the
operator1andstudentshells onserverbto return to thestudentuser's shell onworkstation.[operator1@serverb ~]$exitlogout[student@serverb ~]$exitlogout Connection to serverb closed.[student@workstation ~]$Close the second terminal on
workstation.[student@workstation ~]$exit
Log out of
serverbon the first terminal and conclude this exercise.From the first terminal, exit the
operator1user's shell onserverb.[operator1@serverb ~]$exitlogout[student@serverb ~]$The exit command caused you to exit the
operator1user's shell, terminating the shell session where ssh-agent was active, and return to thestudentuser's shell onserverb.Exit the
studentuser's shell onserverbto return to thestudentuser's shell onworkstation.[student@serverb ~]$exitlogout Connection to serverb closed.[student@workstation ~]$