{"id":8480,"date":"2022-10-10T19:43:13","date_gmt":"2022-10-10T19:43:13","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=8480"},"modified":"2022-10-10T19:43:13","modified_gmt":"2022-10-10T19:43:13","slug":"setting-up-ubuntu-server-firewall","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/setting-up-ubuntu-server-firewall\/","title":{"rendered":"Setting up Ubuntu Server firewall"},"content":{"rendered":"<p>UFW is used as the firewall on Linux and in my case on Ubuntu server. UFW comes with a UI, but we&#8217;re going to use this on a headless server (hence no UI being used).<\/p>\n<p><strong>Status and enabling\/disabling the firewall<\/strong><\/p>\n<p>Simply run the following to check whether your firewall is active or not<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nsudo ufw status\r\n<\/pre>\n<p>To enable the firewall simply use the following<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nsudo ufw enable\r\n<\/pre>\n<p>Use <em>disable<\/em> to disable the firewall (as you probably guessed). <\/p>\n<p>Once enabled run the <em>status<\/em> command again and you should see a list showing which ports we have defined rules for and these will show whether to ALLOW or REJECT connections to port. For example <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nTo                         Action      From\r\n--                         ------      ----\r\n22\/tcp                     ALLOW       Anywhere\r\n80\/tcp                     ALLOW       Anywhere\r\n443\/tcp                    ALLOW       Anywhere\r\n80                         ALLOW       Anywhere\r\n<\/pre>\n<p><strong>Allow and reject access<\/strong><\/p>\n<p>We can allow access to a port, reject access to ports and reject outgoing traffic on ports. When we allow, reject incoming or reject outgoing access we&#8217;re creating firewall rules.<\/p>\n<p>To allow access to SSH, for example we do the following<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nsudo ufw allow 22\r\n<\/pre>\n<p>This will allow tcp and udp access, but we can be more precise and just allow tcp by using<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nsudo ufw allow 22\/tcp\r\n<\/pre>\n<p>As you can see from the previous output from the <em>status<\/em> option, we&#8217;ve enabled 22\/tcp already.<\/p>\n<p>To reject access to a port we use <em>reject<\/em>. <\/p>\n<p><em>Note: If you&#8217;re access your server using SSH you probably don&#8217;t want to reject access to port 22, for obvious reasons, i.e. port 22 is used by SSH and this will block your access via SSH.<\/em><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nsudo ufw reject 80\r\n<\/pre>\n<p><strong>Application profiles<\/strong><\/p>\n<p>UFW includes application profiles which allow us to enable predefined lists of permissions<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nsudo ufw app list\r\n<\/pre>\n<p>The applications listed from this command can also be seen by listing \/etc\/ufw\/applications.d, so for example on my system I have a file name openssh-server, if you open this with nano (or your preferred editor), you&#8217;ll see an INI file format, for example <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n&#x5B;OpenSSH]\r\ntitle=Secure shell server, an rshd replacement\r\ndescription=OpenSSH is a free implementation of the Secure Shell protocol.\r\nports=22\/tcp\r\n<\/pre>\n<p>We can also use <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nsudo ufw app info OpenSSH\r\n<\/pre>\n<p><em>Replacing OpenSSH with the name of the application profile you want to view<\/em><\/p>\n<p>As you can see, if our application profiles are just INI files, then you can create your own file and place it into the aforementioned folder and make it available to UFW. Once you&#8217;ve created your file you&#8217;ll need to tell UFW to load the application definitions using<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nsudo ufw app update MyApp\r\n<\/pre>\n<p><em>Replace MyApp with your application name in the above.<\/em><\/p>\n<p>Ofcourse once we have these profiles we can allow, reject etc. using the application name, i.e.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nsudo ufw allow OpenSSH\r\n<\/pre>\n<p><strong>Logging<\/strong><\/p>\n<p>By default logging is disabled, we can turn it on using<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nsudo ufw logging on\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>UFW is used as the firewall on Linux and in my case on Ubuntu server. UFW comes with a UI, but we&#8217;re going to use this on a headless server (hence no UI being used). Status and enabling\/disabling the firewall Simply run the following to check whether your firewall is active or not sudo ufw [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[140,307],"tags":[],"class_list":["post-8480","post","type-post","status-publish","format-standard","hentry","category-ubuntu","category-ufw"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8480","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=8480"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8480\/revisions"}],"predecessor-version":[{"id":9531,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8480\/revisions\/9531"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=8480"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=8480"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=8480"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}