Setting Up SSH Authentication
In this step, you will learn how to set up SSH key-based authentication. This allows you to log in to remote servers without typing a password every time.
For SSH key authentication to work, you need to copy your public key to the server's ~/.ssh/authorized_keys
file. In a real-world scenario, you would use the ssh-copy-id
command to do this:
## Example for reference (Do not run this)
## ssh-copy-id username@remote_host
Since we don't have a real remote server for this lab, we'll simulate this process by creating a local authorized_keys
file.
First, let's create the .ssh
directory if it doesn't already exist:
mkdir -p ~/.ssh
The mkdir -p
command creates the directory if it doesn't exist and does nothing if it already exists.
Now, let's create or append to the authorized_keys
file:
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
This command appends your public key to the authorized_keys
file. In a real environment, this file would be on the remote server.
Let's verify the content of the authorized_keys
file:
cat ~/.ssh/authorized_keys
You should see your public key in the output, which should look similar to:
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQ... (long string of characters) ...labex@hostname
Finally, let's make sure the authorized_keys
file has the correct permissions:
chmod 600 ~/.ssh/authorized_keys
This sets the permissions to read and write for the owner only, which is a security requirement for the authorized_keys
file.