Configuring Key-Based Authentication

45 minutes
  • 4 Learning Objectives

About this Hands-on Lab

A strong password is typically composed of upper-case and lower-case letters, numbers, and special characters. You can even create a 20-character password and, in all likelihood, nobody will ever guess it. But do you really want to type in a 20-character password every time you need to access the server? Maybe you might, but most people really do not want to. Therefore, in this lab, we will configure SSH key-based authentication, which is far more secure and you do not have to type in any passwords when you log in to the server. Furthermore, when key-based authentication is configured, most people forget to disable password-based authentication altogether. We will not forget to do this. Finally, we will also disable root-based login as an extra security precaution.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Generate a Key Pair on the Client for SSH

Note: For this lab, the use of a standalone terminal app with ssh support is best as the Instant Terminal does not permit port 61613. The instance does take a minute or so to be ready to be connected to via ssh. Please use port 61613 when connecting to this lab.

ssh-keygen
Copy the Newly Created Key Over to the Server
  1. Open your newly created key (replace <PATH_TO_PUBLIC_KEY> with your path):

    vim <PATH_TO_PUBLIC_KEY>
  2. Select all and copy.

  3. Open /home/cloud_user/.ssh/authorized_keys:

    vim /home/cloud_user/.ssh/authorized_keys
  4. Paste in the public key.

  5. Save and quit:

    ESC
    :wq
    ENTER
Configure SSH to Not Allow Root Login or Password-Based Authentication and to Allow Key-Based Authentication
  1. Open the file:

    sudo vim /etc/ssh/sshd_config
  2. Make the following changes:

    PermitRootLogin no
    PasswordAuthentication no
    ChallengeResponseAuthentication no
    PubkeyAuthentication yes
  3. Save and quit:

    ESC
    :wq
    ENTER
  4. Restart the sshd service:

    sudo systemctl restart sshd
Test the New Configuration by Logging Out and Back in with the Newly Created Key
  1. Log out:

    exit
  2. Log in with the newly created key (replace <PATH_TO_PRIVATE_KEY> with your path):

    ssh cloud_user@SERVER_IP -p 61613 -i <PATH_TO_PRIVATE_KEY>

Additional Resources

Note: For this lab, the use of a standalone terminal app with ssh support is best as the Instant Terminal does not permit port 61613. The instance does take a minute or so to be ready to be connected to via ssh. Please use port 61613 when connecting to this lab.

In all honesty, password-based authentication via SSH should only be used during the initial steps of configuring your server, but one of the first things you should do during that configuration is set up a key-based authentication and completely deny password-based authentication.

In order for us to achieve this, we will need to change a few parameters in the configuration file of SSH, generate a public-private key on the client side, and then copy the public key over to the server so we can log in with our key alone and not the password.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?