Securing Your Ubuntu Server: Essential Firewall and SSH Configurations

Security Is the Most Important Aspect of Server Operations
Hello.
In our previous session, we learned how to operate multiple services on a single Ubuntu server using Reverse Proxy.
Your server is now ready to run actual services.
However, as soon as you expose your server to the internet, it becomes vulnerable to numerous automated attacks.
Even without running special services, any internet-connected server will experience constant login attempts and port scans.
In this session, we'll apply essential security configurations to operate your Ubuntu server more securely.
Why Is Server Security Important?
Many beginners think "My server is small and personal, so it should be fine."
However, attackers don't target specific servers but automatically scan the entire internet for vulnerable systems.
Common attacks include:
Brute force SSH password attacks
Web vulnerability scanning
Malware installation attempts
Admin page exploration
Port scanning
Therefore, basic security configurations must be applied once your server is exposed to the internet.
What Is UFW?
Ubuntu provides a built-in firewall tool called UFW (Uncomplicated Firewall).
UFW simplifies using complex iptables rules.
In simple terms, it
"allows only specified ports and blocks everything else"
functionality.
It's the most commonly used firewall on Ubuntu due to its beginner-friendly interface.
Checking UFW Installation
UFW is pre-installed on most Ubuntu systems.
To check, enter this command:
sudo ufw status
If not yet activated, it will show
Status: inactive
status.
Allow SSH First
You must allow SSH before enabling the firewall.
If not, remote connections will be blocked.
sudo ufw allow OpenSSH
Or
sudo ufw allow 22/tcp
can be used.
It's crucial to always allow SSH first.
Allowing HTTP and HTTPS
If operating a web server, you need to allow ports 80 and 443.
In Nginx, this is simply done with
sudo ufw allow 'Nginx Full'
This command allows both
HTTP
HTTPS
simultaneously.
Enabling the Firewall
Now enable the firewall:
sudo ufw enable
To check current settings, run
sudo ufw status
Only allowed ports will be listed.
What Is Fail2Ban?
Fail2Ban is a program that automatically blocks IPs making repeated failed login attempts.
For example, if one IP fails to log in dozens of times,
it will be blocked from accessing the server for a certain period.
It's a useful security tool applicable to various services beyond just SSH.
Installing Fail2Ban
You can install Fail2Ban on Ubuntu using apt:
sudo apt install fail2ban -y
After installation, start the service:
sudo systemctl enable fail2ban
Then
sudo systemctl start fail2ban
to activate it.
Checking Fail2Ban Status
Verify if it's running properly:
sudo systemctl status fail2ban
If normal, you'll see the
active (running)
status.
Enhancing SSH Security
After Ubuntu installation, SSH can be used with default settings,
but minor adjustments significantly improve security.
Common methods include:
Restricting root login
Using strong passwords
Implementing public key authentication
Removing unnecessary users
Checking access logs
These are standard practices in professional environments.
Restricting Root Login
Direct login to the administrator account is not recommended.
Instead, create a regular user account and use sudo only when necessary for specific tasks.
It's best to avoid direct root logins whenever possible.
Should You Change the SSH Port?
Many people change port 22 to another number.
Changing ports can reduce some automated attacks, but
it's not a complete security solution by itself.
More important are
Strong passwords
Public key authentication
Fail2Ban
Regular updates
applied together.
What Is Public Key Authentication?
A login method using encrypted keys instead of passwords.
No password entry is needed, providing high security.
Many enterprise servers use public key authentication.
Beginners can learn this later, but it's recommended for long-term use.
Server Updates Are Part of Security
Security isn't just about installing programs.
Maintaining your operating system in the latest state is equally important.
Regularly run
sudo apt update
followed by
sudo apt upgrade
to apply the latest security patches.
Avoid Installing Unnecessary Programs
The more unnecessary programs installed, the higher the security risks.
On production servers,
it's best to install only necessary programs.
Develop a habit of stopping or removing unused services.
Maintaining Log Checking Habits
When server issues occur, logs should be checked first.
For example,
Login failures
Web server errors
System errors
are typically recorded in logs.
Checking logs regularly is crucial for production servers.
In the next session, we'll explore log analysis as well.
Backups Are the Ultimate Security Measure
No matter how well security is configured,
accidents and failures can still occur.
Therefore, it's recommended to regularly back up
Websites
Databases
Configuration files
Backups enable quick recovery from most issues.
What We Learned Today
In this session, we applied essential security configurations for Ubuntu servers including UFW firewall setup, Fail2Ban installation, and SSH hardening techniques commonly used in production environments.
Secure operation is as important as server setup itself.
Applying basic security measures can effectively block most automated attacks.
Key Takeaways
UFW is Ubuntu's built-in firewall tool.
Always allow SSH before enabling the firewall.
Nginx Full rule allows both HTTP and HTTPS simultaneously.
Fail2Ban automatically blocks repeated failed login attempts.
Direct root logins are not recommended.
Maintaining the latest system updates is important for security.
Regular backups are the ultimate security measure.
Next Session Preview
11th Session: Service Auto-Start (systemd), Log Analysis, and Backup & Disaster Recovery
In the next session, we'll learn how to automatically start services on Ubuntu Server, analyze logs, and implement backup and disaster recovery techniques used in real-world scenarios.
FAQ
Why is it important to allow SSH before enabling the firewall?
Allowing SSH first ensures you can still remotely access your server after activating the firewall. Without this step, you risk being locked out of your server permanently.
What ports should I open for a web server running Nginx?
For Nginx, you need to allow both port 80 (HTTP) and port 443 (HTTPS). The 'Nginx Full' rule in UFW handles this automatically.
Is changing the SSH port from 22 a good security practice?
Changing the SSH port can reduce some automated attacks but is not a complete security solution. It's more important to use strong passwords, public key authentication, Fail2Ban, and regular updates.
Korean Source
This article is an English localization of a Korean-language report from BlogScore. Original: [Ubuntu 서버 구축] 10강. 방화벽(UFW), Fail2Ban, SSH 보안 설정으로 서버 안전하게 운영하기