Limit server privileges and network access
🎯 Introduction
Successful SSH login and a running Node.js service do not establish that a server is secure. Attackers may repeatedly guess account passwords, and an endpoint intended only for local access may be exposed publicly.
This article addresses three questions: who can log in, what they can do afterward, and which ports external clients can reach. You will inspect privileges, safely disable SSH password authentication, and restrict access with a firewall.
These steps target an Ubuntu practice machine. Before changing SSH or firewall settings, keep the current session open and confirm that you can recover through the cloud or virtual machine console. Do not apply them directly to an unattended production server.
🧱 Regular accounts, sudo, and service accounts
Why avoid root for everyday administration? It can change almost everything on the system, so a mistaken command can have far-reaching effects.
A better approach is to log in with a regular account and use sudo for temporary administrator privileges when changing system configuration.
On Ubuntu, run:
whoami
id
sudo -v
whoami: Display the current login account.id: Display group memberships. Ubuntu administrator accounts commonly belong to thesudogroup.sudo -v: Validate the account's sudo authorization; this may ask for the current account's password.
Another kind of account is dedicated to running applications. For example, the next deployment exercise creates linuxnotes to run Node.js rather than accept SSH logins. This helps ensure that an application problem does not automatically grant administrator privileges.
If you completed the earlier systemd article and created linux-notes.service, inspect its configured account:
systemctl show linux-notes.service -p User
For example, User=linuxnotes means systemd is configured to run the service as that account. If the service does not exist yet, a unit-not-found message is expected; check again after the next deployment exercise.
Account privileges must work together with file permissions: administrators should control application code, and only accounts that need secrets should read configuration containing passwords or tokens. The next article provides practical commands.
🔐 Verify key authentication before disabling passwords
SSH is the entry point for remote administration. It authenticates users with an account password or a public/private key pair: the public key goes on the server and the private key stays on your computer.
Disabling password login reduces password-guessing risk, but verify that key authentication actually works first. Otherwise, you may lock yourself out.
Step 1: Verify key authentication in a new terminal
Keep the current SSH session open and run this in a new Mac terminal:
ssh -o PreferredAuthentications=publickey -o PasswordAuthentication=no linux-lab
linux-lab is the connection alias configured in the SSH article. Substitute your own alias for a cloud server if necessary.
After login, run whoami and sudo -v on Ubuntu. Unlocking a private key with a passphrase differs from entering a server account password. A password prompt from sudo -v does not mean SSH used password authentication.
Step 2: Edit the SSH server configuration
Create a configuration snippet on Ubuntu:
sudo vim /etc/ssh/sshd_config.d/00-linux-course.conf
Enter:
PubkeyAuthentication yes
PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin no
These settings enable public key authentication, disable SSH account password authentication, disable keyboard-interactive authentication, and prohibit direct root login. Disabling remote root login does not prevent regular administrators from using sudo.
Step 3: Validate and reload
sudo sshd -t
sudo sshd -T | grep -E '^(pubkeyauthentication|passwordauthentication|kbdinteractiveauthentication|permitrootlogin) '
sshd -t checks syntax: no output and an exit status of 0 usually mean validation passed. The second command shows the parsed SSH configuration. Expect:
pubkeyauthentication yes
passwordauthentication no
kbdinteractiveauthentication no
permitrootlogin no
Why inspect more than the new file? SSH may read other files, and some options use the first value read. If the server uses Match rules for particular users or source addresses, also evaluate the actual login conditions rather than relying only on ordinary sshd -T output.
After confirming the settings, run:
sudo systemctl reload ssh.service
Keep the original SSH window open. Connect again with a key from a new Mac terminal and verify sudo. If it fails, use the retained session or console to revert the new configuration file, run sudo sshd -t, and reload SSH.
🛡️ Restrict externally accessible ports with a firewall
SSH determines who may log in; a firewall determines which network connections may enter.
Cloud servers commonly have two layers:
- Cloud security group: Controls which traffic reaches the server at the cloud network layer.
- Host firewall: Further controls inbound traffic on Ubuntu itself; the commands below use
ufw.
For example, SSH uses 22/tcp, while Node.js listens only on 127.0.0.1:3000. Administrators need external SSH access but do not need direct access to 3000. 127.0.0.1 refers to the current machine itself, not a public address usable from any computer.
Step 1: Check the SSH port and current rules
sudo ss -ltnp | grep sshd
sudo ufw status verbose
First identify the actual SSH listening port. If it differs from 22, replace the port in the allow rule below. If another firewall management system is already in use, verify its setup before layering on or replacing rules.
Step 2: Enable UFW on a dedicated practice machine
This example assumes SSH on port 22. If ufw: command not found appears, install it with sudo apt update && sudo apt install ufw first.
sudo ufw allow 22/tcp
sudo ufw default deny incoming
sudo ufw enable
sudo ufw status verbose
Allowing SSH before enabling the firewall reduces lockout risk. Keep the original session open, connect from another Mac terminal, and verify login.
For a cloud server, also inspect its security group in the cloud console. Restrict SSH sources to your administration IP or a trusted subnet. If your IP changes, first ensure reliable console recovery. Do not copy someone else's IP configuration.
Step 3: Check whether the service is externally exposed
If the earlier Node.js service is running, execute:
sudo ss -ltnp 'sport = :3000'
Expect 127.0.0.1:3000. 0.0.0.0:3000 means the program listens on all IPv4 interfaces. That alone does not prove internet accessibility, but requires further inspection of firewall and security group rules.
If the service does not exist yet, verify this after the next deployment exercise.
Reference: Ubuntu firewall documentation
❓ Knowledge check
Q1: Why might I still see a password prompt after disabling SSH password login?
It may ask for the passphrase that unlocks your private key on the Mac, or the account password required by sudo after login. Neither authenticates the SSH login with a server account password.
Q2: Why is a port unreachable even though the cloud security group allows it?
The security group is only one check. Also verify the Ubuntu firewall, whether the program is listening, and whether its address is 127.0.0.1 or an externally reachable interface.
🧪 Exercise
Perform a security check on a dedicated practice machine:
- Use
whoami,id, andsudo -vto confirm appropriate administrator access. - Inspect the four SSH authentication settings with
sudo sshd -Tand verify key login from a new Mac window. - Inspect inbound rules with
sudo ufw status verbose. If Node.js is deployed, also check port3000's listening address withss.
Success criteria: Explain who may log in, which ports are allowed, and whether Node.js listens directly for external traffic, identifying the command supporting each conclusion. You do not need to open additional ports for this exercise.
🎉 You can now inspect and restrict server access using account privileges, SSH authentication, and firewall rules!
