Technical Anshu
Tutorials Blog
Internet Exposure

Expose SFTP to the Internet Only With Keys and Fail2ban

The moment a router port points at your server, every scanning bot on earth finds it within hours. SFTP can face that safely - SSH is built for hostile networks - but only after three defences are in place, not before.

By Technical AnshuUbuntu Server8 minute read
Watch the complete Ubuntu SFTP server setup step by step in the video

Let the first option be said out loud: if the transfers can stay on the local network, or travel over a VPN your office already has, do that instead - a door nobody can find needs no guard. If remote staff genuinely must reach the server directly, then forward the port only after the checklist below is completely done. Partially done is the same as not done, because the scanners do not grade on effort.

Terminal - who fail2ban has already locked out
$ sudo fail2ban-client status sshd
Currently banned: 3
Total banned: 41
Banned IP list: 203.0.113.9 198.51.100.4 ...
Within days of going public, this list is never empty. The addresses are examples - the steady count is the honest part.
1

Rule One - Keys Only, No Exceptions

Password login that was merely risky on a LAN is indefensible on the internet, where guessing runs at machine speed around the clock. Complete the key login guide in this series - every user on keys, PasswordAuthentication no verified from an outside connection - before the port forward exists. Also disable direct root login: the setting PermitRootLogin no removes the one account name every scanner tries first.

2

Rule Two - Install Fail2ban

Fail2ban reads the login log and temporarily bans addresses that keep failing - turning a million guesses into a handful.

  1. sudo apt install fail2ban
  2. Create /etc/fail2ban/jail.local with an [sshd] section containing enabled = true - editing jail.local, never the shipped jail.conf, so updates do not erase your settings
  3. sudo systemctl restart fail2ban
  4. Check it is watching with sudo fail2ban-client status sshd

The defaults already ban an address after a few failed logins for a cooling-off period, and repeat offenders earn longer bans. It is a seatbelt, not a wall - with keys only, the guesses it interrupts had no chance anyway, which is exactly the layered position you want.

3

Rule Three - Updates Are Part of the Defence

An internet facing server runs public facing software, so its patch level is a security control, not housekeeping. Enable automatic security updates with sudo apt install unattended-upgrades and let it handle the SSH stack between your manual reviews. The exposed server you forget for a year is the one that ends up in someone else's botnet - set the updates, then set a monthly reminder to look at the logs and banned list from this guide.

4

Forward the Port Last, Then Verify From Outside

  1. Give the server a fixed local address first, so the forward never drifts to another machine
  2. On the router, forward the external port to the server's port 22 (or your changed port from the firewall guide)
  3. Test from a genuinely outside network - a phone on mobile data with Wi-Fi off is the classic honest test
  4. Confirm the fingerprint on that first outside connection matches the server's real one, as in the Windows connection guide
  5. Within the first week, read the auth log once. Watching the failed attempts arrive - and fail - is how you know the three rules above are doing their job
🔴 The honest summary: keys decide who can get in, fail2ban decides how often strangers may knock, and updates decide whether the lock itself stays sound. Skip one and the other two are carrying weight they were not designed for.

Watch the Full Video

See the whole Ubuntu SFTP setup done live, with the exact clicks and commands, in the Technical Anshu video.

Watch Full Video
Frequently Asked Questions

How fast will scanners find my newly forwarded port?

Typically within hours, sometimes minutes. Automated scanning of the entire internet address space is continuous. This is normal and expected - it is why the defences go in before the forward, not after the first scare.

Does fail2ban replace key login?

No, and the order matters. Keys make guessing futile; fail2ban merely reduces how much guessing reaches the door and keeps your logs readable. A password protected server with fail2ban is still one leaked or weak password away from entry.

Someone legitimate got banned by fail2ban. How do I free them?

Check the banned list with the status command from this guide, then unban their address with sudo fail2ban-client set sshd unbanip followed by the address. If it keeps happening, their client is probably retrying with a wrong saved password - fix the client, not the ban rules.

This guide is based on the Technical Anshu Ubuntu SFTP server video - Stuck on a step? Ask in the video comments and you will get a reply.