Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Linux Security»How To Set Up SSH Keys in Linux

    How To Set Up SSH Keys in Linux

    RahulBy RahulSeptember 1, 20215 Mins Read

    SSH or Secure Shell is a protocol utilized to enable communication between two computers and share data. It provides a password-enabled or password-less (disabled) authentication and encrypts communication between two hosts. When working with CentOS servers most of the time is spent in the terminal linked to your server via SSH.

    In this guide, we’ll be focusing on setting up SSH keys-based authentication for a CentOS 8 server. SSH keys offer a straightforward, steady technique of communicating with remote servers and are encouraged for all users.

    Creating SSH Keys in Linux

    To generate a new 2048-bit RSA key pair, open up the terminal and execute the below-given command:

    ssh-keygen 
    

    After pressing the enter, you will see the following output:

    Create SSH Keys 1

    If you press Enter, by default, it will save the key pair in the .ssh subdirectory of your_home folder. You can also provide the alternate path of where you want to save the key pair but it is recommended to use the default directory to save the key pair:

    Create SSH Keys 2

    If you have created a key pair before on your client machine then you will be prompted to overwrite it. It’s totally up to you to choose yes or no but be careful to choose the ‘y’ option. If you choose the ‘y’ option, you will not be able to use the previous key pair to log in to the server.

    Create SSH Keys 3

    Now you will be asked to provide a passphrase to add an extra layer of security that prevents unauthorized users from accessing the server. Just press Enter if you do not want to provide any passphrase:

    Create SSH Keys 4

    After providing the passphrase, you will see the following output:

    Successfully Created SSH Keys

    Now you have successfully generated an SSH key pair, to verify this run the below-given command:

    ls -l ~/.ssh/id_*.pub 
    

    List available SSH Keys

    The command given above will output the path to the file which contains the SSH key pair. If it outputs an error like “No such file or directory found” then that means that the key pair was not successfully created and you will have to repeat the process again.

    Adding the SSH Key to Remote System

    Once you have created the key pair you need to add the SSH key to the CentOS server. The fastest way is to use the ssh-copy-id method. In some cases, you might not have the ssh-copy-id method available on your local machine. In this case, you can use an alternate method to add the SSH key to the CentOS Server.

    Adding SSH Key via ssh-copy-id Command

    Your local machine will most probably have the ssh-copy-id method by default. This method will only work in case you have password-based SSH access to the server:

    ssh-copy-id [email protected]_host

    For my server I will use:

    ssh-copy-id [email protected] 
    

    ssh_copy_id command example

    The IP address is your system’s IP, type yes and press Enter, you will be prompted to enter the remote user’s password:

    Input password for ssh_copy_id

    Once the user completes all steps, the public key will be copied to the server:

    Successfuly copied public key to remote host

    Adding the SSH Key Without ssh-copy-id

    In case your local machine doesn’t have the ssh-copy-id method installed, which is highly unlikely, then you will have to run this command to add the ssh keys to the server.

    For my local server I will use:

    cat ~/.ssh/id_rsa.pub | ssh [email protected] "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys" 
    

    Copy SSH key to Remote

    How to Login to the Server using SSH keys

    Now that you have successfully added the key to the server, you should be able to login into the server without the user’s password:

    $ ssh [email protected]_ip_address

    For my server I will use the following command:

    ssh [email protected] 
    

    SSH to remote with Key

    If you didn’t set the passphrase then you can log in without authentication. It is the fastest way to log in to the server otherwise you have to pass through the authentication stage.

    Step 3 – How to Disable SSH Password Authentication

    You can add an extra security layer by disabling the password authentication for SSH. Before the beginning process, ensure that you are able to access your server without the password as a root user or as a non-root user with sudo privileges.

    To disable SSH password Authentication, the first log in to the server:

    ssh [email protected] 
    

    Now we will open and modify the SSH configuration file located at /etc/ssh/sshd_config:

    sudo nano /etc/ssh/sshd_config 
    

    After opening the configuration file make the following changes:

    PasswordAuthentication no
    

    Uncomment the above-given lines if they are commented by removing the # sign and set their value to no.

    After modification of the SSH file you need to save and close the file by pressing Ctrl + X and restart SSH service using the following command:

    sudo systemctl restart sshd 
    

    By doing all steps, password-based authentication is disabled successfully.

    Conclusion

    SSH is a secure network protocol that is used for communication between a remote server and a client; It is more secure than FTP for file transfers between a client and a server. In this article, we learned to generate SSH key pairs and set up SSH-based authentication for CentOS 8 based servers, we also learned to disable the SSH password authentication.

    security SSH
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow To Install NVM on Debian 11
    Next Article How to Install and Secure Apache on Debian11

    Related Posts

    (Resolved) userauth_pubkey: key type ssh-rsa not in PubkeyAcceptedAlgorithms

    Updated:May 10, 20221 Min Read

    Adding a New SSH Key in GitHub

    Updated:April 1, 20223 Mins Read

    Top 10 Ethical Hacking Tools in Kali Linux

    Updated:April 6, 20228 Mins Read

    How to Create SFTP Only User in Debian 11

    Updated:September 26, 20214 Mins Read

    How to Disable Strict Host Key Checking in SSH

    Updated:September 25, 20212 Mins Read

    How To Set Up SSH Tunneling

    5 Mins Read

    Leave A Reply Cancel Reply

    Recent Posts
    • How to Install JAVA on Ubuntu 22.04
    • Switching Display Manager in Ubuntu – GDM, LightDM & SDDM
    • Changing the Login Screen Background in Ubuntu 22.04 & 20.04
    • How To Install PHP (8.1, 7.4 or 5.6) on Ubuntu 22.04
    • (Resolved) Please install all available updates for your release before upgrading
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.