Ever left your front door unlocked by accident? ๐ Imagine doing the same with your server. Leaving an open port is like inviting trouble straight into your digital home. Itโs not just a minor oopsieโitโs a full-blown security risk.
Cybercriminals are always on the prowl, looking for weak spots to exploit. Brute force attacks, data breaches, and unauthorized access are just a few of the nightmares you could face. And trust us, no one wants to deal with that kind of mess.
But donโt worry, weโve got your back. By the end of this guide, youโll be a security ninja, ready to lock down your connection like a pro. Think of it as adding extra cheese to your pizzaโit just makes everything better.
Key Takeaways
- Open ports are like unlocked doorsโeasy targets for hackers.
- Brute force attacks and data breaches are real risks.
- Securing your server is essential for protecting sensitive data.
- This guide will help you lock down your connection effectively.
- Think of security as an upgradeโit makes everything better.
Understanding the Risks of Exposed SSH Ports
Port 22 is like a neon sign for cybercriminals. ๐จ Itโs the default entry point for ssh connections, making it a prime target for hackers. Why? Because itโs predictable, and predictability is a hackerโs best friend.

Why SSH Ports Are Targeted
Automated bots are constantly scanning for open ports, especially port 22. These bots are like digital vultures, looking for weak spots to exploit. A recent example showed 7 attack attempts in just 2 minutes from the same IP address. Thatโs relentless!
Default configurations make servers low-hanging fruit. Hackers know that many admins donโt change the default settings, leaving the door wide open for brute force attacks. Itโs like leaving your keys in the ignitionโsomeoneโs bound to take advantage.
Common Vulnerabilities
One of the biggest mistakes? Leaving password authentication enabled. Weak passwords are an open invitation for unauthorized access. Another issue is failing to monitor logs. Attackers often leave traces, like repeated SYN packets, which can indicate a brute force attempt.
Imagine the 3am nightmare scenario: an attacker gets through. They could install malware, steal sensitive data, or even shut down your ssh server. The fallout? A mess you donโt want to deal with. For more insights on securing your setup, check out this guide on SSH vulnerabilities.
How to Identify an Exposed SSH Port
Ever wondered if your server is shouting โcome on inโ to hackers? ๐จ Identifying open ports is like playing digital detectiveโitโs all about spotting the clues before trouble arrives. Letโs dive into the tools and commands thatโll help you uncover any vulnerabilities.

Using Network Scanning Tools
First up, nmap. This powerful tool scans your network for open ports and services. Run a quick scan with the command nmap -sV your-server-ip. If you see port 22 listed, itโs time to take action. Pro tip: If the scan shows other unnecessary ports open, close them ASAP.
Another handy tool is ss. Use ss -tuln to list all listening ports. Look for port 22 in the outputโif itโs there, your server might be exposed. For a deeper dive, check out this guide on check TCP port usage.
Checking SSH Configuration
Next, inspect your ssh configuration file. Open it with sudo nano /etc/ssh/sshd_config. If you spot Port 22 in the file, sound the alarms! Change it to a non-default port to reduce risk.
Hereโs a quick checklist to spot red flags:
- Port 22 is listed in your config file.
- Unnecessary ports are open and listening.
- The SSH service is running on the default port.
By following these steps, youโll know exactly where your server stands. Stay vigilant, and keep those doors locked! ๐
Changing the Default SSH Port
Default settings are a hackerโs best friendโtime to break up that relationship. ๐ซ Sticking with port 22 is like rolling out the welcome mat for cybercriminals. Letโs upgrade your serverโs security by changing the default port.

Selecting a New Port Number
Picking a new port number is like choosing a secret hideoutโmake it hard to guess. Avoid common ports like 80 or 443, as theyโre often targeted. Instead, opt for numbers between 49152 and 65535. These are less likely to clash with other services.
Pro tip: Pick a number youโll remember, like your birth year plus 50,000. Just make sure itโs not something obvious, like 12345. ๐ฏ
Editing the SSH Configuration File
Now, letโs tweak the configuration file. Open it with sudo nano /etc/ssh/sshd_config. Look for the line that says Port 22 and replace it with your new port number. Save the file and exit.
Before you celebrate, double-check your work. A typo could lock you out of your own ssh service. Hereโs a quick safety checklist:
- Ensure the new port is open in your firewall.
- Test the connection before closing the current session.
- Keep a backup of the original config fileโjust in case.
By following these steps, youโll make your server a fortress. Hackers wonโt know what hit them! ๐
Adjusting Firewall Settings
Think of your firewall as the bouncer at a clubโonly the right guests get in. ๐ช Once youโve changed your ssh port, itโs time to update your firewall settings. This ensures only authorized traffic can access your server via the new port.

Allowing Traffic on the New Port
First, open the new port in your firewall. If youโre using UFW, the command is simple: sudo ufw allow [new-port-number]. This tells the firewall to let traffic through the new door. ๐
Pro tip: Always test your rules with a burner device. Be your own hacker and ensure everything works as expected. ๐
Using UFW for Firewall Management
UFW (Uncomplicated Firewall) is your best friend for managing firewall settings. Start by checking the status with sudo ufw status. This shows which ports are open and where the traffic is flowing.
For Hostinger users, hPanel makes it even easier. Navigate to the firewall section and add your new port. Itโs like building your digital fortress wall ๐ฐโstrong and secure.
Remember the 5-second rule: Never apply firewall changes without testing. A small mistake could lock you out of your vps. Stay safe, stay smart! ๐
Restarting the SSH Service
Ever felt like your server needs a quick refresh? ๐ Restarting the ssh service is the IT equivalent of turning it off and on again. Itโs simple, effective, and often solves lingering issues. Plus, itโs not a breakupโyour server will thank you later.

Using Systemctl to Restart SSH
To restart the ssh service, use the sudo systemctl command. Hereโs how:
sudo systemctl restart ssh
This command tells your server to stop and start the service. Itโs quick, painless, and ensures everything runs smoothly. Pro tip: Always keep two SSH sessions open during this process. If something goes wrong, youโll still have access.
Verifying the Service Status
After restarting, verify the status with:
sudo systemctl status ssh
Look for the word โactiveโ in the output. If you see โfailedโ or โinactive,โ donโt panic. Check the logs for errors and try restarting again. Hereโs a quick guide to interpreting the status messages:
- Active (running): Everythingโs good to go. ๐
- Active (exited): The service started but stopped. Investigate further.
- Failed: Something went wrong. Check the logs for details.
| Distro | Systemctl Command | Service Command |
|---|---|---|
| Ubuntu | sudo systemctl restart ssh | sudo service ssh restart |
| CentOS | sudo systemctl restart sshd | sudo service sshd restart |
| Debian | sudo systemctl restart ssh | sudo service ssh restart |
By following these steps, youโll keep your ssh service running like a well-oiled machine. Stay sharp, and happy restarting! ๐ง
Testing the New SSH Port
Time to put your new setup to the testโletโs see if itโs hacker-proof! ๐ต๏ธโโ๏ธ After all the changes, itโs crucial to ensure everything works as planned. No one wants to celebrate prematurely only to find out the doorโs still wide open.

Connecting via SSH with the New Port
First, letโs connect server using the new port. Open your terminal and use the following ssh command:
ssh username@your-server-ip -p [new-port-number]
If the connection works, youโre golden! ๐ If not, donโt panic. Double-check the port number and firewall settings. Pro tip: Keep a backup session open just in case things go sideways.
Using ss and netstat Commands
Next, letโs verify the port is active. Use the ss or netstat commands to check:
ss -tuln | grep [new-port-number]
Or:
netstat -tuln | grep [new-port-number]
If you see the new port listed, itโs working! ๐ If not, revisit your configuration file and firewall rules. Hereโs a quick troubleshooting checklist:
- Ensure the new port is open in the firewall.
- Check for typos in the SSH config file.
- Restart the SSH service if needed.
Pro tip: Celebrate with a small dance if it works! ๐ And if it doesnโt, donโt worryโweโve got your back with an emergency recovery plan.
Enhancing SSH Security with Key-Based Authentication
Passwords are like old flip phonesโtime for an upgrade. ๐ While theyโve served us well, relying solely on them for ssh connections is like using a wooden lock on a bank vault. Itโs time to level up your serverโs security with key-based authentication. Trust us, itโs the upgrade your digital life deserves.

Key-based authentication replaces passwords with a pair of cryptographic keys. Think of it as a VIP pass for your serverโonly those with the right key get in. No more guessing games for hackers. ๐ซ
Generating SSH Keys
Ready to get fancy? Letโs generate your ssh keys. Open your terminal and type:
ssh-keygen -t rsa -b 4096
This command creates a pair of keys: a private one (your secret) and a public one (the VIP pass). Store your private key safelyโlike you would with cat videos. ๐ฑ Pro tip: Add a passphrase for an extra layer of security.
Hereโs a quick comparison of key generation methods across operating systems:
| OS | Command | Key Location |
|---|---|---|
| Linux | ssh-keygen -t rsa | ~/.ssh/id_rsa |
| macOS | ssh-keygen -t ed25519 | ~/.ssh/id_ed25519 |
| Windows | ssh-keygen -t rsa -b 4096 | C:\Users\YourUser\.ssh\id_rsa |
Disabling Password Authentication
Now that youโve got your keys, itโs time to lock the door on passwords. Open your ssh configuration file with:
sudo nano /etc/ssh/sshd_config
Find the line that says PasswordAuthentication yes and change it to no. Save the file and restart the SSH service. ๐
Hereโs why this matters: Password-only servers are like horror stories waiting to happen. Brute force attacks, data breaches, and sleepless nightsโnone of itโs worth the risk. Upgrade to key-based authentication and sleep like a baby. ๐ด
Pro tip: Backup your keys like cat videosโirreplaceable! ๐พ And if youโre feeling extra fancy, add 2FA for the ultimate security flex. ๐
Implementing Fail2Ban for Brute Force Protection
Imagine your server as a nightclubโFail2Ban is the bouncer keeping the troublemakers out. ๐ถ๏ธ This powerful tool blocks repeated failed login attempts, making brute force attacks a thing of the past. Itโs like having a digital bodyguard for your server management.

Installing and Configuring Fail2Ban
First, letโs get Fail2Ban up and running. Install it with:
sudo apt install fail2ban -y
Next, copy the default configuration file:
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
Now, tweak the settings in jail.local to protect your ssh connections. Hereโs a quick guide:
- Set
enabled = truefor SSH. - Specify the port (e.g.,
port = 22000). - Set
maxretry = 5to ban after 5 failed attempts. - Adjust
bantime = 600to ban for 10 minutes.
Restart Fail2Ban to apply the changes:
sudo systemctl restart fail2ban
Monitoring Fail2Ban Logs
Keep an eye on Fail2Banโs activity by checking the logs. Use this command to see the status:
sudo fail2ban-client status sshd
Hereโs what to look for:
| Log Entry | Meaning |
|---|---|
| Banned IP | An IP was blocked for suspicious activity. |
| Failed Attempts | Number of failed login tries before banning. |
| Ban Duration | How long the IP is banned. |
Pro tip: Customize ban messages to make hackers cry. ๐ญ For more advanced firewall tips, check out this guide on blocking brute force attacks.
With Fail2Ban, your server is a fortress. Hackers wonโt stand a chance! ๐
Regularly Updating Your Server
Your server is like a plantโignore it, and itโll wither away. ๐ฑ Regular updates are the digital vitamins that keep your server healthy and secure. Without them, youโre just asking for trouble. Think of it as skipping your morning coffeeโnothing good comes from it. โ

Applying Security Patches
Security patches are like band-aids for your server. They fix vulnerabilities before hackers can exploit them. To apply them, use the command:
sudo apt-get update && sudo apt-get upgrade
This ensures your server gets the latest fixes. Pro tip: Schedule these updates during off-peak hours to avoid disruptions. ๐
Automating Updates
Why manually update when you can automate it? Install unattended-upgrades with:
sudo apt-get install unattended-upgrades
Then, configure it to run automatically. Itโs like setting a reminder for your serverโs health check. ๐ฉบ
Hereโs why automation matters:
- Consistency is keyโupdates happen on time, every time.
- Reduces human errorโno more forgetting to update.
- Keeps your vps secure without constant attention.
Pro tip: Treat your server like a Tamagotchiโneglect it, and it dies. ๐ฃ Stay on top of updates, and your ssh connections will thank you. ๐
Monitoring SSH Access Logs
Think of your server logs as a detectiveโs notebookโevery clue matters. ๐ Theyโre your first line of defense against suspicious activity. By keeping an eye on these logs, you can spot trouble before it spirals out of control.
Whether itโs failed login attempts or unusual traffic, your logs tell the story. The trick? Knowing how to read them like a pro. Letโs dive into the world of log analysis and alerts.
Analyzing Log Files
Start by checking /var/log/auth.log. This file is your go-to for ssh activity. Look for patterns like repeated failed logins or unfamiliar IP addresses. These are red flags that scream โhacker alert!โ ๐จ
Hereโs a quick guide to reading your logs:
- Failed logins: Indicate brute force attempts.
- Successful logins: Verify theyโre from trusted sources.
- Unusual timestamps: Late-night logins? Suspicious.
Pro tip: Make log reading your morning coffee ritual. โ Itโs the best way to stay ahead of threats.
Setting Up Alerts
Donโt wait for trouble to find youโset up alerts instead. Tools like logwatch and fail2ban can notify you of suspicious activity in real-time. Think of them as your digital watchdogs. ๐
Hereโs how to get started:
- Install
logwatchand configure it to email daily summaries. - Set
fail2banto trigger alerts after multiple failed attempts. - Create thresholds for โoh noโ momentsโlike 10 failed logins in 5 minutes.
Pro tip: Customize your alerts to match your serverโs traffic patterns. This way, youโll catch the bad guys without drowning in notifications. ๐
Conclusion
Youโve just leveled up your serverโs defense gameโnice work! ๐ก๏ธ By securing your ssh connection and changing the default port, youโve made your setup hacker-resistant. But remember, security is an ongoing process, not a one-time fix.
Donโt let complacency creep in. Regularly monitor your server, update configurations, and stay vigilant. Share this guide with that one friend whoโs still using port 22โtheyโll thank you later. ๐
Ready to take the next step? Download our security audit checklist and keep your connection rock-solid. Your server is now hacker-proofโฆ until next weekโs vulnerabilities! ๐
FAQ
Why is changing the default SSH port important?
Changing the default port reduces the risk of automated attacks and brute force attempts, making your server less of a target.
How do I check if my SSH port is exposed?
Use network scanning tools like Nmap or check your serverโs configuration file to verify if the port is open to external traffic.
Whatโs the best way to select a new SSH port number?
Choose a port between 1024 and 65535, avoiding well-known ports. Ensure itโs not already in use by another service.
How do I edit the SSH configuration file?
Open the file located at `/etc/ssh/sshd_config`, find the `Port` line, and update it with your new port number. Save and exit.
How do I adjust firewall settings for the new SSH port?
Use `UFW` or `iptables` to allow traffic on the new port. For example, with UFW, run `sudo ufw allow [new_port]/tcp.
How do I restart the SSH service after making changes?
Use the command `sudo systemctl restart sshd` to restart the service and apply your new settings.
How can I test if the new SSH port is working?
Connect to your server using the new port with the command `ssh -p [new_port] user@server_address. Verify with `ss` or `netstat.
Why should I use key-based authentication?
SSH keys are more secure than passwords, as theyโre harder to crack and protect against brute force attacks.
How do I generate SSH keys?
Run `ssh-keygen` on your client machine, follow the prompts, and copy the public key to your server using `ssh-copy-id.
What is Fail2Ban, and how does it help?
Fail2Ban monitors logs for failed login attempts and blocks suspicious IPs, adding an extra layer of security against brute force attacks.
How do I install and configure Fail2Ban?
Install it with `sudo apt install fail2ban`, then customize the configuration file at `/etc/fail2ban/jail.local` to suit your needs.
Why is updating my server regularly important?
Regular updates ensure you have the latest security patches, reducing vulnerabilities and keeping your server secure.
How can I automate server updates?
Use tools like `unattended-upgrades` on Debian-based systems or `cron` jobs to automate the update process.
How do I monitor SSH access logs?
Check the logs at `/var/log/auth.log` for SSH activity. Set up alerts using tools like `logwatch` or `syslog` for real-time monitoring.