Post

Setting Up SSH on Linux

Introduction –

SSH (Secure Shell) is a cryptographic network protocol for secure data communication. It is used to securely connect to remote servers and execute commands or transfer files between them. In this tutorial, we will show you how to set up SSH on Linux.

Prerequisites

Before we begin, you should have:

  • A Linux machine (we will be using Red Hat Enterprise Linux in this tutorial)
  • A user account with sudo privileges

Steps

Follow the steps below to set up SSH on your Linux machine.

  1. Open the terminal application on your Linux machine.

  2. Create a .ssh directory in your home directory by running the following command:

    1
    
    mkdir ~/.ssh
    
  3. Create an empty known_hosts file in the .ssh directory by running the following command:

    1
    
    touch ~/.ssh/known_hosts
    

    This file will be used to store the public keys of remote servers that you connect to.

  4. Check your username’s group by running the following command:

    1
    
    groups
    
  5. Change the ownership of the .ssh directory and its contents to your user account by running the following command:

    1
    
    sudo chown -R <username>:<group> ~/.ssh
    

    Replace <username> with your Linux user account name and <group> with your username’s primary group.

  6. Set the correct permissions on the .ssh directory by running the following command:

    1
    
    chmod 700 ~/.ssh
    

    This will ensure that only you have read, write, and execute permissions on the .ssh directory.

  7. Change the ownership of the known_hosts file to your user account by running the following command:

    1
    
    sudo chown <username>:<group> ~/.ssh/known_hosts
    
  8. Set the correct permissions on the known_hosts file by running the following command:

    1
    
    chmod 600 ~/.ssh/known_hosts
    

    This will ensure that only you have read and write permissions on the known_hosts file.

Generating SSH keys

You can generate a new SSH key with a password on RHEL using the ssh-keygen command. Here are the steps:

  1. Open a terminal and run the following command to start the key generation process:

    1
    
    ssh-keygen
    
  2. You will be prompted to enter the file in which to save the key. Press Enter to accept the default location (/home/<your_username>/.ssh/id_rsa) or specify a different location if you prefer.

  3. You will then be prompted to enter a passphrase for the key. Enter a strong passphrase and press Enter.

  4. You will be asked to confirm the passphrase. Enter the same passphrase again and press Enter.

  5. The key pair will be generated and saved in the specified location. The public key (id_rsa.pub) can be shared with remote servers or services to enable passwordless authentication.

Once the new SSH key pair has been generated, you can add the public key to the remote service, such as your GitHub account.

Here is the complete list of commands needed:

1
2
3
4
5
6
7
8
9
10
11
mkdir ~/.ssh
touch ~/.ssh/known_hosts
groups
sudo chown -R <username>:<group> ~/.ssh
chmod 700 ~/.ssh
sudo chown <username>:<group> ~/.ssh/known_hosts
chmod 600 ~/.ssh/known_hosts
ssh-keygen

chmod 600 ~/.ssh/<private_key>
ssh-add ~/.ssh/<private_key>



Featured Tweet

This post is licensed under Apache License 2.0 by the author.