You want to make your Linux server more secure. So you tighten SSH access, change a firewall rule, and close your terminal.
Then you try to reconnect. Nothing happens. The server is still running — but your own security changes may have locked you out.
There is a safer way to harden a public Linux server: preserve recovery access, test an independent SSH login, make one change at a time, and verify every result before moving forward.
A public Linux server has a difficult job. It must accept the connections you actually need while rejecting access that shouldn’t be allowed.
For a website, that might mean permitting public HTTPS traffic while restricting administrative SSH access. For a private application, the intended exposure might be much narrower.
The problem is that security controls can affect legitimate users, too.
A firewall doesn’t automatically know which connection belongs to the owner. SSH doesn’t know whether a disabled authentication method was the only one its administrator could use.
That’s why server hardening is not simply about making settings more restrictive. It’s about making them more restrictive without removing the access you still require.
To secure a public Linux server safely, first verify an independent recovery path and a working non-root administrative account. Test SSH public-key authentication from a fresh connection. Then inspect and validate SSH settings, review firewall policies, and make access restrictions one at a time. After each change, verify a new SSH connection before closing the previous session. Never assume that a successful configuration check guarantees remote reachability.
- 1. Why Can a Security Change Lock You Out?
- Authentication lockout
- Network lockout
- Service failure
- Administrative lockout
- 2. The Safe Order for Hardening a Public Linux Server
- 3. Before Changing Anything: Your Safety Checklist
- 🛡️ The No-Lockout Preflight
- 4. Check Your Existing SSH Configuration First
- 5. Set Up SSH Keys Before Disabling Passwords
- Step 1 — Create a key on your own computer
- Step 2 — Install the public key
- Step 3 — Test a genuinely new connection
- Step 4 — Verify administrative access
- 6. Harden SSH Authentication Carefully
- Where should these settings go?
- Validate the configuration before applying it
- Apply the change only when ready
- 7. Firewall Rules: The Most Common Remote Lockout Trap
- Inspect the current firewall first
- Review source restrictions
- What about enabling UFW?
- 8. Don’t Forget IPv6 and Provider Firewalls
- Public IPv4
- Public IPv6
- Cloud firewall
- Host firewall
- 9. A Safe Change Timeline: What Good Hardening Looks Like
- 10. What Should You Do If a New SSH Connection Fails?
- 11. Does Changing the SSH Port Improve Security?
- 12. What About Fail2ban and Automated Blocking?
- 13. A Practical Public Server Hardening Checklist
- Final Verification Checklist
- 14. Frequently Asked Questions
- 15. You Have Completed the Public Linux Server Course
- Primary Technical Sources
- A secure server is one you can still control.
1. Why Can a Security Change Lock You Out?
Imagine that your VPS accepts SSH connections over TCP port 22. You currently connect using a username and password.
You read that SSH keys are preferable to password authentication, so you immediately disable SSH passwords.
But your public key hasn’t been installed correctly. Your next login fails.
Now imagine a different scenario. Your SSH keys work, but you enable a firewall rule that blocks inbound connections to the SSH port.
SSH is configured correctly, yet the network connection cannot reach it.
Both situations have the same visible result: you can’t log in. But their causes are different.
Authentication lockout
Your connection reaches SSH, but none of the permitted authentication methods works for your account.
Network lockout
A host firewall, provider firewall, routing change or incorrect listening port prevents the SSH connection.
Service failure
An invalid SSH configuration or service problem prevents the daemon from accepting new connections.
Administrative lockout
SSH authentication succeeds, but your account lacks the permissions needed to administer or repair the server.
This distinction matters because fixing one problem doesn’t necessarily fix the others.
If a provider firewall blocks the connection, changing an SSH password won’t help. If your key isn’t authorized, opening more firewall ports won’t solve authentication.
For a systematic connection diagnosis, see Can’t SSH Into Your Linux Server? 12 Checks Before You Reboot.
2. The Safe Order for Hardening a Public Linux Server
The most important security improvement in this article isn’t a single command.
It’s the order in which you perform the work.
Do not reverse this sequence.
For example, disabling password access should be the result of successfully testing SSH keys — not the first step in setting them up.
Likewise, restrictive firewall policies should follow a verified inventory of required services and management connections.
3. Before Changing Anything: Your Safety Checklist
🛡️ The No-Lockout Preflight
Know the recovery route. Identify and test your provider’s out-of-band console or equivalent recovery access, where available.
Keep your current SSH session open. Do not use it as your only proof that a new connection will work.
Verify administrative privileges. Your intended non-root account must be able to perform the required sudo operations.
Understand existing access requirements. Record which users, automation tools, IP addresses and services depend on the current setup.
Have a recovery plan. Know which configuration changes you will reverse if verification fails.
Hosting providers may offer browser consoles, serial consoles, rescue environments or snapshots. These are not interchangeable, and their availability varies.
A snapshot may help with restoration, but it is not the same as a tested emergency management channel. A browser console may also depend on credentials or provider access you haven’t tested yet.
If SSH is your only access method and you have no reliable recovery option, postpone high-risk changes until you have a workable recovery plan.
For broader change planning, read Before You Change a Linux Server: 10 Checks to Avoid Downtime.
4. Check Your Existing SSH Configuration First
Before making restrictions, identify how SSH currently works.
On many Ubuntu and Debian installations, the relevant service is ssh.service. On many RHEL-family systems, it is sshd.service.
Start with read-only checks:
systemctl status ssh.service --no-pager
systemctl status sshd.service --no-pager
sudo ss -lntp
You may find that only one of the service names exists. Some systems also use systemd socket activation, so inspect the applicable SSH service and socket arrangement before changing ports or service behavior.
Next, examine the effective SSH settings:
sudo sshd -T | grep -E \
'^(passwordauthentication|kbdinteractiveauthentication|pubkeyauthentication|permitrootlogin) '
The sshd -T command displays effective server configuration values after parsing the main configuration and included files.
Pay attention to:
| Setting | What it controls |
|---|---|
PasswordAuthentication |
Whether SSH password authentication is permitted. |
PubkeyAuthentication |
Whether public-key authentication is permitted. |
KbdInteractiveAuthentication |
Whether keyboard-interactive authentication is allowed, which can include certain password and MFA workflows. |
PermitRootLogin |
Whether and how the root account may authenticate through SSH. |
If the server uses conditional Match blocks, the effective settings can differ by user, source address or other connection properties. Review those rules and use the appropriate sshd -T -C connection parameters when needed.
For a deeper explanation of SSH keys and authentication, see SSH Keys Explained: How Secure Server Access Works.
5. Set Up SSH Keys Before Disabling Passwords
SSH public-key authentication lets your client prove possession of a private key without transmitting that private key to the server.
The public key is installed for the account you want to access. The matching private key remains on your computer.
This section assumes you have an existing account authorized for SSH access and a working way to install its public key.
If your only login is root, establish and test a non-root administrative account before disabling root access. User creation and sudo configuration depend on the distribution and your organization’s access policy.
Step 1 — Create a key on your own computer
On a computer with OpenSSH installed, you can generate a dedicated Ed25519 key pair:
ssh-keygen -t ed25519 \
-f ~/.ssh/id_ed25519_sa_vps
Use a strong passphrase when prompted. Do not overwrite an existing key file, and never upload or share the private key.
If you already have an appropriate working SSH key, you may use that instead of generating another.
Step 2 — Install the public key
If the server currently supports a working authentication method and your local computer has ssh-copy-id, you can install the new public key for the intended account:
ssh-copy-id \
-i ~/.ssh/id_ed25519_sa_vps.pub \
YOUR_ADMIN_USER@YOUR_SERVER_IP
Replace the placeholders with your real account and server address.
Run this on your own computer, not inside an SSH session on the VPS.
If the server does not permit the authentication method needed by ssh-copy-id, use an existing authorized management route or the provider’s supported key-installation procedure. Do not temporarily weaken SSH security just to make this example work.
Step 3 — Test a genuinely new connection
Keep the original SSH session open.
Open a separate terminal on your computer and test the key while preventing password and keyboard-interactive fallback:
ssh \
-o IdentitiesOnly=yes \
-o PreferredAuthentications=publickey \
-o PasswordAuthentication=no \
-o KbdInteractiveAuthentication=no \
-o ControlMaster=no \
-o ControlPath=none \
-i ~/.ssh/id_ed25519_sa_vps \
YOUR_ADMIN_USER@YOUR_SERVER_IP
The options prevent the test from silently succeeding through a password or an existing multiplexed SSH connection.
If this login fails, stop and investigate the key installation, account permissions and SSH configuration. Do not disable password authentication yet.
Step 4 — Verify administrative access
Inside the new SSH session, run:
whoami
sudo -v
The first command confirms the current account. The second asks sudo to validate your ability to authenticate for administrative operations under the current policy.
Do not disable root SSH login until the intended non-root account can independently access the server and perform the required administrative tasks.
You should now have a working key-based SSH login from a fresh connection, a verified administrative account, and your original session still open. If any of these checks fail, do not move on to stricter authentication settings.
6. Harden SSH Authentication Carefully
Once key-based administration is working, you can consider restricting other login methods.
A common policy for a simple server administered through SSH keys is:
PubkeyAuthentication yes
PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin no
These are SSH server configuration directives, not shell commands.
They are not appropriate for every environment. For example, disabling keyboard-interactive authentication may break an existing MFA system or other authentication workflow. Do not apply this policy without first verifying all legitimate access requirements.
Where should these settings go?
On Ubuntu, OpenSSH commonly reads /etc/ssh/sshd_config and snippets under /etc/ssh/sshd_config.d/.
However, OpenSSH generally uses the first obtained value for many configuration directives. A setting in an earlier included file may take precedence over one you add later. Conditional Match blocks introduce additional considerations.
Therefore, do not blindly append these settings to the end of a file and assume they will take effect.
Inspect your existing files, make a backup, and edit the appropriate global configuration location so that it produces the intended effective settings.
Validate the configuration before applying it
sudo sshd -t
If the command reports a configuration error, do not reload or restart SSH.
When validation succeeds, inspect the effective configuration again:
sudo sshd -T | grep -E \
'^(passwordauthentication|kbdinteractiveauthentication|pubkeyauthentication|permitrootlogin) '
For environments using conditional Match rules, validate the relevant user and connection context rather than relying on the global output alone.
A successful syntax test establishes that SSH can parse the configuration. It does not guarantee that you will still be able to connect over the network.
Apply the change only when ready
On an Ubuntu installation with an active ssh.service that supports reload, you can typically use:
sudo systemctl reload ssh.service
Other distributions and SSH service arrangements may require a different procedure. Check the active service and socket configuration, particularly on systems using SSH socket activation.
Keep your original terminal open and immediately test a new SSH connection from your computer.
If the new connection fails, use the still-working session or your verified recovery console to investigate and restore the previous configuration.
7. Firewall Rules: The Most Common Remote Lockout Trap
Strong SSH authentication is important, but it doesn’t decide which remote clients can reach the service.
A firewall can enforce network restrictions independently of SSH authentication.
That is valuable, but it creates another way to accidentally block yourself.
Suppose your server currently accepts SSH traffic from any internet address. You decide to restrict it to your own public IP.
If you accidentally enter the wrong address, your new connections may be rejected.
If your internet connection uses a changing public IP, a rule that works today may prevent access after your address changes.
Never enable a restrictive firewall policy on a remote VPS until the required SSH management path and all other necessary services have been accounted for. Applying a rule successfully does not prove your remote connection will survive.
Inspect the current firewall first
On Ubuntu servers using UFW:
sudo ufw status verbose
sudo ufw status numbered
If UFW is not installed or isn’t the active firewall manager, these commands may not describe your actual filtering policy.
You may also have firewall rules configured in your cloud provider’s control panel.
Changing one layer does not automatically update the others.
Review source restrictions
UFW supports rules allowing connections from specific source addresses.
For example, the following command previews a possible rule for TCP port 22:
sudo ufw --dry-run allow proto tcp \
from YOUR_TRUSTED_PUBLIC_IP \
to any port 22
Replace YOUR_TRUSTED_PUBLIC_IP with the correct external source address before attempting to apply such a rule.
The address must represent the source seen by the server. Depending on your setup, that could be your home connection, office gateway or VPN exit point.
If the preview is appropriate and you have verified a recovery method, the corresponding rule can be added with:
sudo ufw allow proto tcp \
from YOUR_TRUSTED_PUBLIC_IP \
to any port 22
This example assumes SSH really uses TCP port 22. If it uses another port, adapt the rule accordingly.
Adding an allow rule does not necessarily make the firewall active, remove existing broader allow rules, or guarantee that the intended policy takes effect.
Check the resulting configuration and account for rule ordering, existing policies and provider-level filtering.
What about enabling UFW?
For a server where UFW is currently disabled, sudo ufw enable is a consequential operation. It may affect established SSH sessions and other services.
Do not run it as an isolated first step.
Before activation, you must know which inbound connections the server requires, configure the appropriate rules, confirm how IPv4 and IPv6 are handled, and have an independently tested recovery option.
For example, a public website may need HTTP and HTTPS access in addition to your restricted SSH management path.
Only enable the firewall when that full policy has been reviewed, and verify the outcome immediately from a new external connection.
See the official Ubuntu firewall documentation and Linux Firewall Explained for the underlying concepts.
8. Don’t Forget IPv6 and Provider Firewalls
Many VPS environments support IPv6 alongside IPv4.
Restricting one address family does not automatically mean the other is equally protected.
Similarly, a cloud firewall may be configured independently of UFW or another host firewall.
Public IPv4
Verify which services can be reached through the server’s public IPv4 path and which rules control them.
Public IPv6
Check whether IPv6 is enabled, which addresses exist, and whether the intended restrictions cover IPv6 traffic.
Cloud firewall
Review provider security groups or network firewall policies that control traffic before it reaches the VPS.
Host firewall
Inspect local filtering tools, active rules and how they interact with the provider’s network controls.
The goal is not to install every possible firewall tool. It’s to understand the controls that are actually active and avoid conflicting or poorly understood policies.
9. A Safe Change Timeline: What Good Hardening Looks Like
Let’s follow a hypothetical administrator securing a newly deployed VPS.
These times are illustrative, not results from a live experiment.
The administrator confirms the provider console is accessible and understands how to use it if SSH fails.
Current SSH settings, listening ports and firewall rules are inspected before any changes are made.
A separate terminal connects successfully using public-key authentication. The non-root account’s sudo access is checked.
The configuration is backed up and reviewed, then validated with sshd before applying the change.
The administrator confirms that key-only access still works without relying on the original session.
Required services, trusted source addresses and IPv4/IPv6 policies are checked before applying appropriate restrictions.
The administrator tests a new connection, verifies expected application access and records the final configuration.
The lesson is not that server hardening must take exactly 35 minutes.
It’s that each step has a clear purpose, an expected result and a point where you stop if something doesn’t work.
10. What Should You Do If a New SSH Connection Fails?
The first question is whether your original session still works.
If it does, keep it open. Do not make several additional changes at once.
Start by identifying where the new connection fails.
| Symptom | Possible cause | First investigation |
|---|---|---|
| Connection timed out | Firewall, routing, address or network path issue. | Check provider rules, host firewall and listening addresses. |
| Connection refused | No service accepting the connection at that destination, or an explicit network rejection. | Check SSH service status, listening port and network policy. |
| Permission denied (publickey) | Authentication key, account or authorization configuration problem. | Check the selected key, authorized_keys permissions and SSH logs. |
| SSH login works, sudo fails | Administrative privileges or sudo authentication problem. | Review account membership and sudo policy through authorized access. |
If you retain a working administrative session, inspect the last change before doing anything else.
For example, after changing SSH settings, you can run:
sudo sshd -t
sudo sshd -T | grep -E \
'^(passwordauthentication|kbdinteractiveauthentication|pubkeyauthentication|permitrootlogin) '
For firewall changes, inspect the relevant host and provider policies.
If no SSH session remains available, use the verified recovery method rather than repeatedly guessing passwords or changing unrelated settings.
Do not treat a potentially compromised server as a routine configuration problem. Preserve available evidence and follow an incident-response process. The recovery sequence in this tutorial is intended for planned administrative changes, not for proving that a system is trustworthy after a suspected breach.
11. Does Changing the SSH Port Improve Security?
You may have heard that changing SSH from port 22 to another port makes a server safer.
Moving SSH can reduce some indiscriminate scans targeting the default port. But it does not fix weak authentication, outdated software, compromised credentials or unnecessary privileges.
It also adds another possible lockout condition.
You must coordinate the listening port, host firewall, provider firewall and client configuration.
On some Ubuntu installations using systemd socket activation, SSH listening-port changes involve additional socket configuration behavior rather than simply editing one directive and restarting the service.
For beginners, changing the SSH port should not be the first hardening priority.
Verified key-based access and correctly scoped network exposure provide a stronger starting point.
12. What About Fail2ban and Automated Blocking?
Tools such as Fail2ban can react to repeated authentication failures and temporarily restrict selected sources.
They can be useful, but they add another policy layer that requires configuration, maintenance and testing.
They may also block legitimate users if thresholds are poorly chosen or if repeated errors occur behind a shared address.
Don’t use automated blocking as a replacement for strong authentication.
First establish reliable SSH access, understand your logging sources, and confirm the firewall behavior.
Then evaluate automated defenses based on the needs of your server and the operational risks they introduce.
13. A Practical Public Server Hardening Checklist
After completing your changes, review the following conditions.
Final Verification Checklist
Fresh SSH login works. You successfully connected from a new, independent terminal using the intended authentication method.
Administrative privileges work. Your authorized non-root account can perform the required maintenance tasks.
Effective SSH settings are known. You verified the actual configuration rather than assuming a file edit took effect.
Firewall rules match requirements. Management access and public application traffic behave as intended.
IPv4 and IPv6 have been reviewed. No required address family or listening endpoint was overlooked.
Recovery remains available. You know how to regain control if future policy changes fail.
Final state is documented. You recorded which settings changed and the verification results.
These checks establish a safer administrative baseline. They are not a guarantee that the server has no vulnerabilities or that every security risk has been eliminated.
Continue reviewing updates, exposed applications, user permissions and backups as part of routine maintenance.
For a broader assessment, see Linux Server Security Audit Checklist: 15 Checks to Find Hidden VPS Risks.
14. Frequently Asked Questions
Can disabling SSH password authentication lock me out?
Yes. If your account cannot authenticate using another permitted method, new SSH logins may fail. Test an independent key-based login and administrative privileges before disabling passwords.
Should I disable root SSH login?
Restricting direct root SSH access is often appropriate when a tested non-root administrative account is available. Do not disable root SSH access if it is still your only working management route.
Can enabling UFW disconnect an existing SSH session?
Yes. Enabling or modifying firewall policies can affect established connections and prevent new ones. Configure the required access rules and verify recovery options before activating restrictions.
Is SSH key authentication enough to secure a VPS?
No. SSH keys protect one aspect of authentication. Exposed services, software vulnerabilities, excessive privileges, account security and network configuration also matter.
Should I allow SSH only from my home IP address?
Source restrictions can reduce network exposure when your trusted management address is stable and known. If your public IP changes or you use multiple networks, an overly narrow rule may block legitimate access. A managed VPN or other controlled access architecture may be more appropriate.
Does a successful sshd -t check mean I cannot be locked out?
No. The command validates SSH configuration syntax and certain prerequisites. It does not test external routing, provider firewall rules, account authorization or a real login from your computer.
What if I have already lost SSH access?
Use the hosting provider’s recovery console or other verified out-of-band management method if available. Investigate the latest SSH, service or firewall change. Avoid assuming that a reboot will solve the issue.
15. You Have Completed the Public Linux Server Course
Over three lessons, we’ve followed a Linux server through three connected questions.
First, we learned why receiving a public IP address can make network services discoverable even without a published domain.
Second, we investigated unfamiliar SSH authentication attempts and learned why a rejected login is not proof of compromise.
Finally, we’ve developed a safer way to protect remote access: establish recovery options, verify authentication, change access rules deliberately, and test the result.
The core principle is simple:
Make your server harder for unauthorized clients to access — without making it impossible for authorized administrators to manage.
Primary Technical Sources
- Ubuntu Server — OpenSSH Server — SSH key configuration, testing, and server administration.
- OpenSSH — sshd_config(5) — Authentication directives and configuration precedence.
- OpenSSH — sshd(8) — Server validation and effective configuration testing.
- Ubuntu Server — Firewall Documentation — UFW rules and source restrictions.
- Ubuntu Manual — ufw(8) — Firewall commands and remote management considerations.
A secure server is one you can still control.
Strong authentication, carefully scoped network access and a tested recovery plan work together. None replaces the others.
The safest hardening process is deliberate: understand your current access, verify the alternative, apply one change, and confirm the result before continuing.








