Change SSH Port on Agent Host
Complete procedure for changing the default SSH port (22) to a custom port on the Hostinger VPS (Agent Host), including server-side and client-side changes, firewall updates, and verification.
Prerequisites:
- Current SSH access to the VPS (via
ssh hostinger-vps)sudoaccess on the VPS- Your local machine’s SSH config at
~/.ssh/config- Cloudflare Tunnel configured for hermes.ducatillon.net (CF Tunnel bypasses local firewall — HTTP/HTTPS services are unaffected)
- 10-15 minutes
- Pick a port number before starting (e.g.
2468). Use a port between 1024 and 65535 that is not in use. This guide usesPORTas placeholder — replace it with your chosen number.
Overview
Section titled “Overview”On Ubuntu 24.04, sshd is socket-activated by default — systemd listens on port 22 via ssh.socket and spawns sshd on demand. Changing sshd_config alone is not enough; the socket unit still binds to port 22. You must disable socket activation so sshd listens directly on the new port.
Steps:
- VPS — Edit
sshd_configto set the new port - VPS — Disable socket activation, restart sshd
- VPS — Open new port in UFW, close port 22
- Local — Update your SSH config to use the new port
Step 1: Verify current state
Section titled “Step 1: Verify current state”On the VPS:
# Confirm port 22 is activesudo ss -tlnp | grep ssh
# Check UFW rulessudo ufw status verbose
# Check sshd_configgrep -E '^#?Port' /etc/ssh/sshd_config
# Check socket activation statussystemctl is-active ssh.socketExpected: port 22 listening, UFW allows 22/tcp, #Port 22 commented out, ssh.socket active.
Step 2: Edit sshd_config
Section titled “Step 2: Edit sshd_config”On the VPS:
sudo nano /etc/ssh/sshd_configFind the line:
#Port 22Uncomment it and change the port number:
Port PORTSave and exit (Ctrl+O, Enter, Ctrl+X).
Step 3: Disable socket activation and restart sshd
Section titled “Step 3: Disable socket activation and restart sshd”On the VPS:
# Stop and disable the socket unit (it hardcodes port 22)sudo systemctl stop ssh.socketsudo systemctl disable ssh.socket
# Restart sshd — it now listens directly on the new portsudo systemctl restart sshVerify:
sudo ss -tlnp | grep sshExpected: only :PORT should be listening, :22 should be gone.
Important: If you only run
systemctl restart sshwithout disablingssh.socket, the socket still binds to port 22 and your config change has no effect.
Step 4: Update UFW
Section titled “Step 4: Update UFW”On the VPS:
# Allow the new portsudo ufw allow PORT/tcp comment 'SSH custom port'
# Remove port 22 (one command removes both IPv4 and IPv6)sudo ufw delete allow 22/tcp
# Verifysudo ufw statusExpected: only PORT/tcp should appear as ALLOW IN.
Step 5: Test the new port (from your local machine)
Section titled “Step 5: Test the new port (from your local machine)”On your local machine, open a new terminal (do not close your current VPS session yet):
ssh -p PORT hermes@srv1715245.hstgr.cloudOr using your SSH host alias with an override:
ssh -p PORT hostinger-vpsIf this connects successfully, the new port works.
If it does NOT work, debug from your existing VPS session:
# Check sshd is listening on the new portsudo ss -tlnp | grep PORT
# Check UFWsudo ufw status
# Check sshd config is validsudo sshd -t
# Check ssh logssudo journalctl -u ssh --since "5 minutes ago"Step 6: Update local SSH config
Section titled “Step 6: Update local SSH config”On your local machine, edit ~/.ssh/config:
Host hostinger-vps User hermes HostName srv1715245.hstgr.cloud Port PORT IdentityFile ~/.ssh/hostinger_vps_ed25519 RequestTTY forceKey change: added Port PORT line.
Verify it works:
# Final test from local machine (close old SSH sessions first)ssh hostinger-vpsFinal state checklist
Section titled “Final state checklist”On the VPS:
# Only new port listeningsudo ss -tlnp | grep ssh# Expected: only :PORT
# UFW only allows new portsudo ufw status# Expected: PORT/tcp ALLOW, no 22/tcp
# sshd_config has only the new portgrep '^Port' /etc/ssh/sshd_config# Expected: Port PORT
# Socket activation disabledsystemctl is-active ssh.socket# Expected: inactiveOn your local machine: ssh hostinger-vps connects on the new port.
What is NOT affected
Section titled “What is NOT affected”- Hermes gateway (local service, not reachable from internet directly)
- Dashboard (9119) (behind Cloudflare Tunnel, bypasses local firewall entirely)
- Cloudflare Tunnel (outbound-only connection, no inbound port changes needed)
- Any other local services (they bind to 127.0.0.1 behind the tunnel)
The only thing that changes is how you SSH into the box from your local machine.