\n
sudo vi \/etc\/ssh\/sshd_config<\/p>\n<\/div><\/div>\n\n\n\n
We need to find the line where the PermitRootLogin<\/strong> is and change it to no:<\/p>\n\n\n\n <\/figure>\n\n\n\nNext, we would like to allow only certain users to log in to our server via SSH:<\/p>\n\n\n\n <\/figure>\n\n\n\nMake sure that you already have real users that will be allowed with appropriate permissions so you don’t get locked out.<\/p>\n\n\n\n
Save the configuration file and restart the SSH service:<\/p>\n\n\n\n
\n
sudo systemctl restart sshd<\/p>\n<\/div><\/div>\n\n\n\n
Now, you should be able to log in with your user only. Here is an example where we try to log in with our second user jumpcloud2<\/strong>:<\/p>\n\n\n\n <\/figure>\n\n\n\nWe can see that permission is denied even when we enter the correct password.<\/p>\n\n\n\n
Step 4: Set up SSH keys and disable password login<\/h2>\n\n\n\n In order to enhance the security further we can disable login with a password and use the SSH key.<\/p>\n\n\n\n
This method eliminates the risk of brute force attacks on passwords and ensures that only users with the corresponding private keys can access the system. Let’s go through the steps to achieve this.<\/p>\n\n\n\n
If you don’t have an SSH key pair on your local machine, you need to generate one. <\/p>\n\n\n\n
Open a terminal on your local machine and use the following command:<\/p>\n\n\n\n
\n
ssh-keygen -t rsa<\/p>\n<\/div><\/div>\n\n\n\n
This command will prompt you to choose a location to save the keys (usually ~\/.ssh\/id_rsa and ~\/.ssh\/id_rsa.pub) and set an optional passphrase for added security.<\/p>\n\n\n\n <\/figure>\n\n\n\nOnce you generate your SSH key pair, you need to copy the public key (jumpcloud_rockylinux.pub) to the remote server. You can use the ssh-copy-id command to do this. <\/p>\n\n\n\n
In our case we will run the following command:<\/p>\n\n\n\n
\n
ssh-copy-id -p 2222 -i ~\/.ssh\/jumpcloud_rockylinux.pub jumpcloud@194.195.240.58<\/p>\n<\/div><\/div>\n\n\n\n
You will get a similar output:<\/p>\n\n\n\n <\/figure>\n\n\n\nAlso, this command will prompt you to enter your user password on the remote server. Once you provide the password, the public key will be copied to the ~\/.ssh\/authorized_keys<\/strong> file on the server.<\/p>\n\n\n\nBefore logging into the server, we would need to change the permissions to our key file so they don’t have 644 by default, and we will assign them permissions with the value 400.<\/p>\n\n\n\n
We can do so by running the following command in our local terminal:<\/p>\n\n\n\n
\n
chmod 400 ~\/.ssh\/jumpcloud_rockylinux<\/p>\n<\/div><\/div>\n\n\n\n
Next, we will connect with our server:<\/p>\n\n\n\n
\n
ssh -i ~\/.ssh\/jumpcloud_rockylinux -p 2222 jumpcloud@194.195.240.58<\/p>\n<\/div><\/div>\n\n\n\n
Let’s break down the commands:<\/p>\n\n\n\n
\nThe -i<\/strong> option will load and use the private key that we’ve generated and give the exact path where it is stored.<\/li>\n\n\n\nThe -p<\/strong> option defines that we are trying to connect to our server via non-default 2222 port. If we don’t use this option we will get a connection refused error.<\/li>\n\n\n\nThe last part, jumpcloud@194.195.240.58, defines the username and IP address of our server. <\/li>\n<\/ul>\n\n\n\nYou should be able to log in without entering a password because the server is now configured to use SSH keys for authentication.<\/p>\n\n\n\n <\/figure>\n\n\n\nIn order for us to disable password logging and use only SSH keys we need to edit our configuration file again:<\/p>\n\n\n\n