{"id":41,"date":"2019-09-02T10:39:41","date_gmt":"2019-09-02T08:39:41","guid":{"rendered":"http:\/\/blog.remyblom.nl\/?p=41"},"modified":"2020-11-17T10:21:21","modified_gmt":"2020-11-17T09:21:21","slug":"hardening-ubuntu-18-04-lts","status":"publish","type":"post","link":"https:\/\/blog.remyblom.nl\/?p=41","title":{"rendered":"Hardening Ubuntu 18.04 LTS"},"content":{"rendered":"\n<figure class=\"wp-block-image is-resized\"><img decoding=\"async\" src=\"https:\/\/blog.remyblom.nl\/wp-content\/uploads\/2019\/10\/ubuntu_orange_hex-1.png\" alt=\"\" class=\"wp-image-65\" width=\"734\"\/><\/figure>\n\n\n\n<p>Just got a new box setup and delivered to me by the company&#8217;s IT department. They setup a user-account with <code>sudo<\/code> privileges and included my public ssh-key. But in case you only have a root account you should create a user-account with sudo privileges yourself:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ adduser username\n$ usermod -aG sudo username<\/code><\/pre>\n\n\n\n<p>And from your local machine upload your public key:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ ssh-copy-id -i .ssh\/id_rsa user@hostname<\/code><\/pre>\n\n\n\n<p>So let&#8217;s start hardening. First let&#8217;s set a new password:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ passwd<\/code><\/pre>\n\n\n\n<p>After that I needed to set another hostname, since the one I got from IT was not what I asked for:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sudo nano hostname\n$ sudo nano hosts\n$ sudo reboot <\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">ssh &amp; sshd<\/h2>\n\n\n\n<p>In <code>\/etc\/ssh\/sshd_config<\/code> set:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PermitRootLogin no \nPasswordAuthentication no <\/code><\/pre>\n\n\n\n<p>In <code>\/etc\/ssh\/ssh_config<\/code> set:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>HashKnownHosts yes<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">firewall<\/h2>\n\n\n\n<p>Use a firewall to block all unwanted traffic to your machine. Only open up the ports you want publicly available and limit access to your ssh-port to known IP&#8217;s only. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sudo ufw allow from 10.2.0.0\/16\n$ sudo ufw allow http \n$ sudo ufw allow https\n$ sudo ufw enable\n$ sudo ufw status numbered<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">unattended updates <\/h2>\n\n\n\n<p>Make sure the package is installed and running:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sudo apt install unattended-upgrades\n$ sudo service unattended-upgrades status<\/code><\/pre>\n\n\n\n<p>Edit <code><code>\/etc\/apt\/apt.conf.d\/50unattended-upgrades<\/code><\/code> to do <strong>only<\/strong> security updates on production machines. Only out-comment the following lines:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\"${distro_id}:${distro_codename}\";\n\"${distro_id}:${distro_codename}-security\";\n\"${distro_id}ESM:${distro_codename}\";<\/code><\/pre>\n\n\n\n<p>Security updates may need dependencies from non-security origins. EMS, or extended security maintenance is for releases that have reached end of life, like 14.04 LTS.<\/p>\n\n\n\n<p>Further you can play with settings like: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Unattended-Upgrade::Mail \"your@email.com\";\nUnattended-Upgrade::MailOnlyOnError \"false\";\nUnattended-Upgrade::Remove-Unused-Kernel-Packages \"true\";\nUnattended-Upgrade::Remove-Unused-Dependencies \"true\";\nUnattended-Upgrade::Automatic-Reboot \"true\";\nUnattended-Upgrade::Automatic-Reboot-Time \"04:00\";<\/code><\/pre>\n\n\n\n<p>Doing automatic-reboots at night are at your own risk, I don&#8217;t do that on production machines, but really think it is perfectly fine on private, personal and development boxes. And I have never had anything go wrong with them, ever&#8230;<\/p>\n\n\n\n<p>Add\/edit <code>\/etc\/apt\/apt.conf.d\/20auto-upgrades<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>APT::Periodic::Update-Package-Lists \"1\";\nAPT::Periodic::Download-Upgradeable-Packages \"1\";\nAPT::Periodic::AutocleanInterval \"7\";\nAPT::Periodic::Unattended-Upgrade \"1\";<\/code><\/pre>\n\n\n\n<p>The unattended upgrades are initiated by your daily crontab, in my case this runs at 06:25 by default, which I think is a little late to also do a reboot, so I changed the time my daily crontab runs by editing <code>\/etc\/crontab<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n# m h dom mon dow user\tcommand\n17 *\t* * *\troot    cd \/ &amp;&amp; run-parts --report \/etc\/cron.hourly\n0  3\t* * *\troot\ttest -x \/usr\/sbin\/anacron || ( cd \/ &amp;&amp; run-parts --report \/etc\/cron.daily )\n47 6\t* * 7\troot\ttest -x \/usr\/sbin\/anacron || ( cd \/ &amp;&amp; run-parts --report \/etc\/cron.weekly )\n52 6\t1 * *\troot\ttest -x \/usr\/sbin\/anacron || ( cd \/ &amp;&amp; run-parts --report \/etc\/cron.monthly )<\/code><\/pre>\n\n\n\n<p>Test<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sudo unattended-upgrades --dry-run --debug<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Checkrestart<\/h2>\n\n\n\n<p>Another great way to see whether you need to reboot a machine or are just fine with reload\/restarting services is the <code>checkrestart<\/code> command. It is not on the machine by default so you install it yourself:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sudo apt install debian-goodies<\/code><\/pre>\n\n\n\n<p>I added it to my <code>.bash_profile<\/code> so everytime I log into the machine I get to see which processes still use old versions of upgraded files. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo 'Type password for checkrestart report'\nsudo checkrestart<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">More stuff&#8230;<\/h2>\n\n\n\n<p>This list is of course incomplete and could be updated and expended over time&#8230; Things like <strong>fail2ban<\/strong> or <strong>appArmor<\/strong> might be added&#8230;.<\/p>\n\n\n\n<p>But for now let&#8217;s <a href=\"https:\/\/blog.remyblom.nl\/?p=35\">install that LEMP stack<\/a>!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Just got a new box setup and delivered to me by the company&#8217;s IT department. They setup a user-account with sudo privileges and included my public ssh-key. But in case you only have a root account you should create a user-account with sudo privileges yourself: And from your local machine upload your public key: So &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/blog.remyblom.nl\/?p=41\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Hardening Ubuntu 18.04 LTS&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,3,2],"tags":[],"class_list":["post-41","post","type-post","status-publish","format-standard","hentry","category-hardening","category-ubuntu","category-vps"],"_links":{"self":[{"href":"https:\/\/blog.remyblom.nl\/index.php?rest_route=\/wp\/v2\/posts\/41","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.remyblom.nl\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.remyblom.nl\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.remyblom.nl\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.remyblom.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=41"}],"version-history":[{"count":13,"href":"https:\/\/blog.remyblom.nl\/index.php?rest_route=\/wp\/v2\/posts\/41\/revisions"}],"predecessor-version":[{"id":215,"href":"https:\/\/blog.remyblom.nl\/index.php?rest_route=\/wp\/v2\/posts\/41\/revisions\/215"}],"wp:attachment":[{"href":"https:\/\/blog.remyblom.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=41"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.remyblom.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=41"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.remyblom.nl\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=41"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}