Securing a Linux server is essential for protecting your data, applications, and network infrastructure. Whether you operate a VPS, a dedicated server, or a business-critical server hosting environment, following security best practices can reduce the risk of unauthorized access and security incidents.
This guide covers practical steps to secure a Linux server, including applying software updates, strengthening passwords, changing the SSH port, configuring a firewall, and using SSH key authentication.
Introduction
To improve Linux server security, follow these recommended practices:
- Keep the Linux kernel and software packages updated.
- Use strong passwords and change them when necessary.
- Change the default SSH port to reduce exposure to automated scans.
- Enable a firewall and block unnecessary ports.
- Use SSH key authentication instead of password-based SSH login.
- Disable remote root login and monitor server activity.
Server security requires ongoing maintenance. Regular updates, access control, and monitoring are important whether you manage your own infrastructure or use a hosting provider for Linux server hosting.
Keep the Linux Kernel and Software Updated
Installing security patches promptly is one of the most important steps in protecting a Linux server. Updates address known vulnerabilities, fix software bugs, and improve system stability.
Use the appropriate package manager for your Linux distribution to update installed software.
CentOS and compatible distributions:
sudo yum updateOn newer distributions that use DNF, run:
sudo dnf upgradeUbuntu and Debian:
sudo apt update
sudo apt upgradeThe apt update command refreshes the package index, while apt upgrade installs available package updates.
Review the proposed changes before confirming an upgrade, particularly on production servers. Some kernel or system-library updates may require a reboot before they take effect.
Keeping your operating system and applications updated is a fundamental part of securing any server hosting environment. If your hosting provider offers server management services, confirm which updates are handled by the provider and which remain your responsibility.
Change Passwords Regularly and Use Strong Credentials
Use strong, unique passwords for Linux user accounts, especially accounts with administrative privileges. Avoid reusing passwords across servers or services.
A strong password should be difficult to guess and should not contain predictable personal information. A password manager can help you generate and store unique credentials securely.
Change a password immediately if you suspect it has been exposed, shared improperly, or compromised. Routine password changes may also be required by your organization's security policy.
For instructions, refer to How to Change Passwords for Users on the Linux Server.
Password security is an important part of server administration, but it should be combined with other controls, such as SSH key authentication, restricted access, and firewall rules.
Change the SSH Port
SSH commonly uses TCP port 22 by default. Changing the SSH port can reduce the number of automated scans and login attempts appearing in your logs, although it does not replace authentication controls or firewall protection.
For instructions, refer to How to Change the SSH Port.
Before changing the port, make sure the new port is allowed by the server firewall and any network-level firewall. Update your SSH client configuration accordingly, and verify that you can connect through the new port before closing your existing session.
For example, if you change the SSH port to 2222, connect using:
ssh -p 2222 username@server_ipA hosting provider may also apply network-level security rules, so check the applicable firewall settings before making changes.
Enable the Firewall and Block Unused Ports
Enable a firewall and configure it to allow only the network traffic required by your applications and administration tools.
For example, a Linux server may need to allow:
- SSH on the configured SSH port.
- HTTP on TCP port
80if it hosts a website. - HTTPS on TCP port
443if it serves encrypted web traffic. - Other application ports only when they are required.
On Ubuntu, you can use UFW to manage firewall rules. For example, if SSH uses the default port:
sudo ufw allow 22/tcp
sudo ufw enable
sudo ufw statusIf SSH uses a custom port, replace 22 with the actual port before enabling the firewall.
Important: Configure the required SSH access rule before enabling or modifying the firewall, especially when working remotely. Otherwise, you may lock yourself out of the server. Firewall commands and configuration methods vary by distribution, so use the appropriate procedure for your operating system.
Restricting unnecessary network access reduces the server's exposed attack surface. For production hosting and other internet-facing workloads, review firewall rules periodically and remove access that is no longer needed.
Use SSH Key Authentication Instead of Passwords
SSH key authentication provides a stronger alternative to password-based SSH login when configured correctly. It uses a public-private key pair: the public key is installed on the server, while the private key remains with the client.
Before disabling password authentication, complete the following steps:
- Generate an SSH key pair on your local computer.
- Add the public key to the appropriate user's
~/.ssh/authorized_keysfile on the Linux server. - Test key-based login in a new SSH session.
- Confirm that you can access the server using the key before changing the SSH authentication settings.
Once key-based access has been verified, update the SSH server configuration.
Open the configuration file:
sudo vi /etc/ssh/sshd_configFind or configure the following settings:
PubkeyAuthentication yes
PasswordAuthentication no
PermitRootLogin noThese settings enable public-key authentication, disable SSH password authentication, and prevent direct remote login as the root user. Ensure there are no conflicting active settings in the configuration or included files.
Important: Do not disable password authentication until you have confirmed that SSH key login works. Keep your current SSH session open and establish a second working session so that you can recover if the configuration is incorrect.
Before applying the changes, validate the SSH configuration:
sudo sshd -tIf the command reports no errors, restart or reload the SSH service using the service name appropriate for your distribution. On many Ubuntu and Debian systems, use:
sudo systemctl reload sshOn many CentOS and RHEL-based systems, use:
sudo systemctl reload sshdIf reloading is unsupported in your environment, use the appropriate service restart command after validating the configuration. Keep the existing session open until you have successfully tested a new connection.
Combining SSH key authentication, restricted root access, firewall rules, and timely software updates helps protect Linux servers against common attack methods. These controls are valuable for both self-managed infrastructure and server hosting services, regardless of the hosting provider or server location.