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.
-
Open the terminal application on your Linux machine.
-
Create a
.ssh
directory in your home directory by running the following command:1
mkdir ~/.ssh
-
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.
-
Check your username’s group by running the following command:
1
groups
-
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. -
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. -
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
-
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:
-
Open a terminal and run the following command to start the key generation process:
1
ssh-keygen
-
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. -
You will then be prompted to enter a passphrase for the key. Enter a strong passphrase and press Enter.
-
You will be asked to confirm the passphrase. Enter the same passphrase again and press Enter.
-
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
Have you ever wondered how to securely connect to a Linux machine? Check out this tutorial on setting up SSH for Linux.https://t.co/JW2SoIEaP8#linux #ssh #virtualmachines #homelab #sysadmin
— rcdevops (@rcdevops) March 23, 2024