How To Use ufw Firewall In Ubuntu (allow port from IP)
hey everyone this is tony teaches tech i’m tony and i’m going to show you how to use the ufw firewall on ubuntu and deviant systems and if you’re not familiar ufw stands for uncomplicated firewall i’ll let you be the judge of that if you think it is uncomplicated or not i personally think it is based on the syntax alone but to demonstrate the functionality you can do a lot with this but to demonstrate the functionality i want to by the end of this tutorial show you how to lock down all external connections to the to the server itself um except for ssh over port 22 um over the tcp protocol from a specific ip address.

Of How Ufw Works So Let’s Go Ahead
so that sounds like a pretty complicated thing to do but it’s actually very straightforward one single command that we can execute and we’ll build up to that throughout the tutorial just to have a solid foundational understanding of how ufw works so let’s go ahead and get on into the tutorial here i’m logged in via ssh to two separate ubuntu servers this one at this ip address ending in 196 on the left and this one over here on the right ending an ip address or the ip address ending in 115. so we’re going to install ufw over here on the left hand side and we can do that with apt install ufw in case you don’t.
already have it i already have it on my system so nothing has to happen here so let’s first do a ufw status and we’ll see that the firewall is not active right now it’s inactive but just so we’re all on the same page a good way to i guess set some defaults here is to disallow all incoming connections and allow all outgoing connections so that’s like a very strict firewall and we’ll we’ll open up we’ll break that fire while we’ll let some holes through the firewall throughout the tutorial here so but let’s just start with that so we can do ufw default deny incoming and ufw default allow outgoing okay so very very simple to.
do that and right now if we do ufw status again we’re still inactive so we need to actually activate the firewall um but before we activate the firewall i just want to show you that i can ssh into this from an external system so we can do that with ssh root at 23.92.26.196 password and there we go we logged in welcome to the server we’ll we’ll see this message pop up anytime you log into the remote server here so let’s get out of here and let’s do ufw enable to actually enable the uh the firewall and it’s saying this command might just disrupt the existing ssh connection over here on the left hand side i.

Again via Ssh from the External Connection We’ll
never seen it actually do that but i’m sure it’s possible but i’m going to take my chances and enable a firewall anyway okay so the firewall is active and enabled on this uh on system startup so we don’t have to worry about it now um let’s go ahead and try to log in again via ssh from the external connection we’ll do hit enter and it’s just going to hang there because all incoming connections are blocked based on our default rules that we just applied so that um that is working that is exactly what it’s supposed to do so we’ll have to break out of this here ctrl c to get back to the terminal session.
now let’s let’s open up a hole in the firewall to allow ssh connections and we can do that really simply with ufw allow ssh and you don’t even have to know the port of ssh to open up that port but you could also do ufw allow 22 in this case you can specify the port or the application that usually typically runs on that port so with uh with allowing that we can try to ssh in again from the external connection from the external server i should say and we can do that and we see right away that it’s asking us for the password so that’s a good sign and now welcome to the server we.
are logged into the remote server via ssh so that firewall rule has been added and allows us um that incoming connection so let’s let’s back out of that again and if we don’t for whatever reason want that rule anymore we can do a ufw delete um and what do you want to delete we want to delete the fact that we’re allowing ssh so it’s going to delete that rule again over here we can test it out and it’s not prompting us for a password because port 22 the ssh port is blocked so we’ll back out of that again okay so one thing that is i think is really cool if you have a static ip.

Something Like Ufw Allow from Okay
address that you’re working from you want to block all ssh connections unless it’s coming from a specific ip address and we can do that again with this fairly straightforward syntax we can do something like ufw allow from okay and then the ip address that we want to allow from is over here so 192.53.124.115. make sure i get that right 115. um and we’ll say to any port 22.

