Technical Anshu
Tutorials Blog
Firewall Rules

UFW Firewall Setup for SFTP and Changing the SSH Port Safely

The firewall decides who can even knock. UFW makes the rule a one-liner - and the famous port change deserves a fair hearing: it quiets the noise, it is not armour, and done in the wrong order it locks you out.

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

Ubuntu's UFW is the friendly face of the system firewall, and SFTP needs exactly one opening in it. This guide adds that rule, shows how to restrict it to your office network only, and then deals honestly with the most repeated piece of SSH advice on the internet: moving the port.

Terminal - UFW rules for the SFTP server
$ sudo ufw allow OpenSSH
$ sudo ufw enable
$ sudo ufw status
22/tcp ALLOW Anywhere # OpenSSH
22/tcp (v6) ALLOW Anywhere (v6) # OpenSSH
The OpenSSH profile opens exactly the one port SFTP needs. Status output shown is an example.
1

Open Port 22 With the Profile, Not a Guess

  1. sudo ufw allow OpenSSH - the profile knows the port, so the rule stays correct even in writing
  2. sudo ufw enable if the firewall is not already on - do this while you still have a working session, and confirm the allow rule exists first, or the firewall will politely cut your own connection
  3. sudo ufw status verbose to read back the result: default deny incoming, your one rule listed

UFW is a front end, not the firewall itself - the rules it writes are what matter, and status is how you prove they exist instead of believing they do.

2

Restrict the Rule to Your Own Network

On a LAN server, "anywhere" is wider than necessary. You can allow only your office range:

  1. sudo ufw allow from 192.168.1.0/24 to any port 22 proto tcp, adjusting the range to your network
  2. Then remove the broad rule with sudo ufw delete allow OpenSSH
  3. Test from inside the range and, if you can, confirm a device outside it is refused

This one change makes internet-style scanning irrelevant to a local server - the door only exists for your own street.

3

The Port Change, Graded Honestly

Moving SSH from 22 to something like 2222 has exactly one real benefit: your logs go quiet, because the endless automated scans aim at the standard port. That is genuinely pleasant and makes real events easier to spot.

What it does not do: stop a determined attacker, who scans all ports anyway, or fix weak passwords, which fail on any port. Treat it as noise reduction, never as the security itself. Keys are the security; the port is the volume knob.

4

If You Change the Port, This Order or Lockout

  1. In /etc/ssh/sshd_config set Port 2222 - keep your current session open
  2. Open the new port before restarting: sudo ufw allow 2222/tcp
  3. sudo systemctl restart ssh
  4. From a new window, connect with the port stated: sftp -P 2222 [email protected] (in sftp and ssh the flag is a capital P)
  5. Only after that succeeds, close port 22: sudo ufw delete allow OpenSSH
  6. Tell every user the new port the same day - every saved client profile on every desk still points at 22

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

I enabled UFW and my SSH session died. What happened?

The allow rule was missing or added after enabling. The firewall blocked your own connection along with everyone else's. Recovery needs console access to the machine; add the OpenSSH rule first, then enable.

Is port 22 itself unsafe?

No. A port is just a number; what listens behind it and how it authenticates decides safety. A key-only SSH on port 22 is stronger than a password-based SSH on an obscure port.

Do I also need to open UDP or extra ports for SFTP?

No. SFTP uses TCP port 22 only - one of its practical advantages over old FTP, which needed extra data connections.

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.