Generating and Exchanging SSH Keys for Secure Access

Hands-On Lab

 

Photo of Terrence Cox

Terrence Cox

Senior Vice President of Content

Length

00:15:00

Difficulty

Beginner

Understanding the creation and exchange of SSH keys is a key concept to grasp as a new system administrator. In this lab, we will generate keys on two systems using the ssh-keygen utility and learn how to exchange and verify the keys with a remote system using ssh-copy-id and associated key files on each. At the end of this lab, you will understand how to create secure keys for remote access, how to exchange them, and where to store them on each system involved in the chain.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Generating and Exchanging SSH Keys for Secure Access

Introduction

Understanding the creation and exchange of SSH keys is a key concept to grasp as a new system administrator. In this lab, we will generate keys on two systems using the ssh-keygen utility and learn how to exchange and verify the keys with a remote system using ssh-copy-id and associated key files on each. At the end of this lab, you will understand how to create secure keys for remote access, how to exchange them, and where to store them on each system involved in the chain.

Create the SSH Keys on Server 1 and Server 2

Create the Key on Server 1

  1. In your terminal, log in to Server 1.
    ssh cloud_user@<SERVER1_PUBLIC_IP>
  2. List the contents of the current directory.
    ls -la
  3. Change to the .ssh directory.
    cd .ssh
  4. List the contents of the .ssh directory.
    ls -la
  5. Generate a key for Server 1.
    ssh-keygen
  6. Press Enter at the next three prompts.
  7. List the contents of the .ssh directory again.
    ls -la
  8. List the contents of the id_rsa.pub file.
    cat id_rsa.pub
  9. Copy the output of this command to your clipboard.

Create the Key on Server 2

  1. Log in to Server 2.
    ssh cloud_user@<SERVER2_PUBLIC_IP>
  2. Change to the .ssh directory.
  3. List the contents of the .ssh directory.
    ls -la
  4. Install the nano text editor.
    sudo yum install nano
  5. Enter your password at the prompt.
  6. Open the authorized_keys file in nano.
    nano authorized_keys
  7. Add the key we just generated to the file.
  8. Press Ctrl + X.
  9. Press Y then Enter to save the changes.

Exchange the SSH Keys between Server 1 and Server 2

  1. In your Server 2 terminal window, create a new key.
    ssh-keygen
  2. Press Enter for the next three prompts.
  3. List the contents of the current directory.
    ls -la
  4. List the contents of the id_rsa.pub file.
    cat id_rsa.pub
  5. Copy the output of this command to your clipboard.
  6. Type exit to log out of Server 2.
  7. Install nano.
    sudo yum install nano
  8. Type y to continue.
  9. List the contents of the current directory.
    ls -la
  10. Open the authorized_keys file in nano.
    nano authorized_keys
  11. Add the key we just generated to the file.
  12. Press Ctrl + X.
  13. Press Y then Enter to save the changes.

Test the Configuration

  1. Attempt to log in to Server 2 from Server 1 without a password.
    ssh cloud_user@<SERVER2PUBLIC_IP> 
  2. Attempt to log in to Server 1 from Server 2 without a password.
    ssh cloud_user@<SERVER1PUBLIC_IP> 

Conclusion

Congratulations, you've successfully completed this hands-on lab!