Configuring Firewall and Security Settings on Your VPS
Setting up a firewall and enhancing security configurations on your VPS is crucial to protect your server from unauthorized access and potential threats. Follow these steps to configure your firewall and implement essential security settings.
Step 1: Access Your VPS via SSH
- Log in via SSH to your VPS using your root or admin credentials.
- Open an SSH client (such as Terminal on macOS/Linux or PuTTY on Windows) and enter:
- Hit Enter, then provide your password when prompted.
Step 2: Update Your System
Updating your VPS regularly helps protect it from known vulnerabilities.
- Run the following commands to update the package list and upgrade the installed packages: This command updates and applies all available security patches.
Step 3: Install and Configure a Firewall
A firewall is your first line of defense. Here, we’ll use UFW (Uncomplicated Firewall), which is easy to set up on most Linux distributions.
-
Install UFW (if it’s not already installed) by running:
-
Allow SSH access to ensure you don’t get locked out of your server:
-
Enable UFW:
You’ll be prompted to confirm enabling the firewall. Type
y
and press Enter. -
Set up additional rules as needed, for example, to allow HTTP and HTTPS traffic:
-
Check the status of your firewall to ensure rules are active:
Step 4: Disable Root SSH Login
For enhanced security, disable direct root access. This requires logging in with a different user and then escalating privileges.
- Open the SSH configuration file:
- Find the line: and change it to:
- Save the changes by pressing
CTRL + O
, thenEnter
, and close withCTRL + X
. - Restart the SSH service to apply the change:
Step 5: Set Up Fail2Ban to Protect Against Brute-Force Attacks
Fail2Ban helps secure your server by blocking IPs that show signs of malicious intent, such as repeated failed login attempts.
- Install Fail2Ban:
- Enable Fail2Ban by starting its service:
- Configure Fail2Ban (optional):
- Open the default configuration file:
- Adjust settings like
bantime
,findtime
, andmaxretry
based on your security needs. - Save and close the file, then restart Fail2Ban:
Step 6: Set Up Basic IP Tables Rules (Optional)
For advanced users, configuring IP Tables provides more fine-tuned control over your firewall.
- View current rules:
- Add rules as needed, for example, to block all incoming connections except SSH, HTTP, and HTTPS:
- Save your IP Tables rules to ensure they persist after reboot:
Step 7: Regularly Monitor and Review Security Logs
Consistently monitoring your logs helps detect any suspicious activity on your VPS.
- Use the following command to view the auth log, which tracks login attempts:
- You can also check the Fail2Ban logs for any banned IP addresses:
Step 8: Schedule Regular Security Updates
To keep your VPS secure, schedule regular updates or enable automatic updates.
- Install the unattended-upgrades package:
- Enable automatic updates: