
This guide describes some best security practices for a new Rocky Linux server. You'll learn how to:
You should have a local workstation with OpenSSH installed like Windows 10 or higher, macOS, or Linux.
If you don't have an SSH key, you should create one and add it to your Vultr account before you deploy the server.
Open a terminal session on you local workstation.
Use ssh-keygen to generate a new SSH key.
-t option specifies the key type. ed25519 is recommended because it is more secure than RSA. If you prefer RSA or DSA keys, change id_ed25519 to id_rsa or id_dsa.-C flag specifies the comment for the key. This is optional. Adding your email address is a good idea.-f flag specifies the file to save the key to. This is optional. The default name for ed25519 format is ~/.ssh/id_ed25519. You may want to use another name to identify the key, like shown above.This creates a private/public keypair. Assuming you named your key as shown above, the keypair is in the ~/.ssh/ folder and named:
You should keep the private key secret. You'll upload the *.pub file to Vultr and install it on the server.
Log in to the Vultr Console.
Click your name in the upper-right, then select SSH Keys from the dropdown menu.
Click the blue "plus sign" button to add a key.
Enter a descriptive name for the key.
View your public key in your terminal. It should look something like this:
Copy the output string into the SSH Key field. It looks something like this:
Click the Add SSH Key button to add it to your account.
In the Vultr Console, deploy a new Rocky Linux cloud server. Make sure to select your SSH key in the Vultr Console while deploying, and Vultr will automatically install that key for the root user.
This guide uses 192.0.2.123 as the server's IP address in all the following examples.
From your local terminal, SSH to the server as root:
If your private key is not in the standard location, use the -i parameter, like this:
If everything is working properly, you'll log in without being prompted for a password.
It's a good security practice to do normal maintenance tasks as a standard user, and use sudo for commands that need higher access. For most purposes, you shouldn't routinely log in as root. Here's how to do that:
Create a new user account.
Specify a strong password for the new user.
Add the new user to the wheel group with usermod.
Check the sudoers file with visudo.
Look for the wheel group. Remove the comment if the line is disabled. It should look like this when you are ready to save the file.
Save and exit vi. Type Esc, then ColonWQ, then Enter.
Note: The visudo utility performs syntax checking before committing your edits to the file. A malformed sudoers file can break your system. Never edit /etc/sudoers directly. For example, if you make an error, you'll see this when exiting visudo.
Switch to the new user to test the sudo access.
Verify you are the new user with whoami, then test sudo access with sudo whoami, which should return root.
Exit and close the terminal session.
Open a new terminal on your local workstation, then use ssh-copy-id to add the SSH key to the new standard user. This copies the public SSH key to the new user's authorized_keys file.
Never copy your private key to the server.
Log in to the server as the new user. If everything is working properly, you'll log in without being prompted for a password.
It's best practice to disable SSH for the root account, and to disable password-based SSH access.
Open the SSH server configuration file:
Disable SSH for root. Look for the PermitRootLogin line and change the directive to no.
Disable SSH password authentication. Change the PasswordAuthentication directive to no.
With this setting, you must use SSH keys for authentication.
Save and close the file.
Activate the changes by restarting SSH:
From this point on, you must log in as your standard user. Use sudo for commands that require greater access.
Changing the default SSH port is an optional task. Some administrators recommend this to reduce the number of port scanning attempts on the server. To change it:
Edit the SSH server configuration file.
Look for the Port 22 line, uncomment it, and change the port number to a port of your choice. Like this:
The port should be:
Save and close the file.
Notify SELinux about this change so that it will allow SSH to bind to the new port.
Verify that the port notification was successful.
Configure the firewall to allow traffic via the new port.
All SSH traffic will go through the new port. Remove the default SSH service assignment from the firewall.
Restart the firewall and SSH.
Open a new terminal session on your local workstation to test the connection. Use the -p option to specify the new SSH port.
FirewallD, the firewall application for Rocky Linux 8, is installed and active. View the status using:
The last line in that output says that zone drifting is an insecure configuration that should be disabled. In a zone-based firewall like FirewallD, it's possible, depending on the configuration, for traffic configured to pass through one zone to also pass through another zone it's not intended for. This is called Zone Drifting. Disabling it is highly recommended by the developers of FirewallD.
To disable it:
Open the firewall configuration file.
Look for the AllowZoneDrifting line (at the end of the file) and change the directive to no.
Save and close the file.
Restart the firewall.
These basic steps greatly increase the security of your system. To learn more about Rocky Linux 8 and Firewalld, see these resources:
0 Comments
Be the first to comment and share your perspective with the community.