{"id":278,"date":"2023-01-23T09:03:19","date_gmt":"2023-01-23T05:03:19","guid":{"rendered":"https:\/\/kidds.co.za\/?p=278"},"modified":"2023-01-23T09:03:19","modified_gmt":"2023-01-23T05:03:19","slug":"basics-steps-after-new-ubuntu-server-install","status":"publish","type":"post","link":"https:\/\/kidds.co.za\/index.php\/2023\/01\/23\/basics-steps-after-new-ubuntu-server-install\/","title":{"rendered":"Basics steps after new Ubuntu Server install"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Keep System Up-To-Date<\/h3>\n\n\n\n<p>An extremely crucial part of hardening any system is to ensure that it is always kept up-to-date. Doing this will keep any known bugs or vulnerabilities patched if one exists. The following commands are ways to update an Ubuntu system:<br><code><strong>apt-get update &amp;&amp; apt-get upgrade<\/strong><\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Accounts<\/h3>\n\n\n\n<p>A good place to start when dealing with any operating system\u2019s security is to ensure that user accounts are locked down.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Ensure Only root Has UID of 0<\/h3>\n\n\n\n<p>Accounts that have a UID set to 0 have the highest access to a system. In most cases, this should only be the \u201croot\u201d account. Using the below command will list all accounts with a UID of 0:<br><code><strong>awk -F: '($3==\"0\"){print}' \/etc\/passwd<\/strong><\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Check for Accounts with Empty Passwords<\/h3>\n\n\n\n<p>Accounts that have no password essentially have no security. The command below will print all accounts that have an empty password:<\/p>\n\n\n\n<p><code><strong>cat \/etc\/shadow | awk -F: '($2==\"\"){print $1}'<\/strong><\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Lock Accounts<\/h3>\n\n\n\n<p>In addition, you can use the command below to lock any accounts (prepends a ! to the user\u2019s password hash):<\/p>\n\n\n\n<p><code><strong>passwd -l accountName<\/strong><\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Adding New User Accounts<\/h3>\n\n\n\n<p>It is best practice to keep the use of the root account to a minimum. To do this, add a new account that will be primarily used with the command below:<\/p>\n\n\n\n<p><code><strong>adduser accountName<\/strong><\/code><\/p>\n\n\n\n<p>This will automatically create a user with the default configuration defined in \u2018\/etc\/skel\u2019.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Sudo Configuration<\/h3>\n\n\n\n<p>The Sudo package allows a regular user to run commands in an elevated context. This means a regular user can run commands normally restricted to the root account. Often, this is the ideal way of making system configurations or running elevated commands; not by using the root account. The configuration file for Sudo is in \/etc\/sudoers, however it can only be edited by using the \u201cvisudo\u201d command. There are many different configuration options for that limit the use of Sudo to certain users, groups, Ips, and commands. The general configuration format is below:<\/p>\n\n\n\n<p><code><strong>%www ALL=(ALL)NOPASSWD:\/bin\/cat,\/bin\/ls<\/strong><\/code><\/p>\n\n\n\n<p><strong>%www<\/strong>&nbsp;\u2013 All users of the www group<\/p>\n\n\n\n<p><strong>ALL=<\/strong>&nbsp;\u2013 From any Host\/IP<\/p>\n\n\n\n<p><strong>(ALL)<\/strong>&nbsp;\u2013 can run as any user<\/p>\n\n\n\n<p><strong>NOPASSWD<\/strong>&nbsp;\u2013 No password required (omit to require a password)<\/p>\n\n\n\n<p><strong>:\/bin\/cat,\/bin\/ls<\/strong>&nbsp;\u2013 Commands able to run as sudo. In this case, \u201ccat\u201d and \u201cls\u201d<\/p>\n\n\n\n<p>To run any elevated command, simply place \u201csudo\u201d in front of it as a properly configured user.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">IpTables<\/h3>\n\n\n\n<p>IpTables are essentially your operating system\u2019s firewall. IpTables are extremely powerful in controlling the network traffic going into and out of your server. While I will give some very basic example configurations below, it is recommended that any person looking into hardening their Ubuntu OS do research into IpTables implementation. Be careful it is easy to lock yourself out of SSH which will be painful on a cloud install. If you have access to a static ip then the SSH option is hardened by allowing only access form that ip with the addition of -s x.x.x.x or a range using -s x.x.x.0\/24<\/p>\n\n\n\n<p>Running the commands below will configured your box to allow inbound connections only on ports 80, 22, and the loopback interface and drop all other packets (configure to your own server\u2019s needs):<\/p>\n\n\n\n<p><code>iptables -A INPUT -p tcp -m tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT<\/code><\/p>\n\n\n\n<p><code>iptables -A INPUT -p tcp -m tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT<\/code><\/p>\n\n\n\n<p><code>iptables -A INPUT -I lo -j ACCEPT<\/code><\/p>\n\n\n\n<p><code>iptables -A INPUT -j DROP<\/code><\/p>\n\n\n\n<p>(Or use&nbsp;<code>iptables -P INPUT DROP<\/code>&nbsp;to automatically drop all packets without a rule)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">SSH<\/h3>\n\n\n\n<p>It goes without saying that all services hosted on your server should be adequately configured and locked down; however since SSH is almost always going to be running on your server, it is essential to lock it down as much as possible. The SSH service configuration file can be found at \u2018\/etc\/ssh\/sshd_config\u2019.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Disable root Login<\/h3>\n\n\n\n<p>This configuration will limit SSH only to users other than root. Find and ensure the line for \u201cPermitRootLogin\u201d exists and looks like the one below:<\/p>\n\n\n\n<p><code>PermitRootLogin no<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Allow Specific Users<\/h3>\n\n\n\n<p>This line will allow you to specify which users can log into the SSH service:<\/p>\n\n\n\n<p><code>AllowUsers accountName<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Change Default Port From 22<\/h3>\n\n\n\n<p>This line will specify which port to host the SSH service on. It is recommended to change this to a non-default high port number. (Remember to fix your IpTables accordingly!)<\/p>\n\n\n\n<p><code>Port 22222<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Disable Empty Passwords<\/h3>\n\n\n\n<p>This line ensures that no users can login with an empty password. This adds a nice layer of security if there is a user without a password set:<\/p>\n\n\n\n<p><code>PermitEmptyPasswords no<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Restart Service<\/h3>\n\n\n\n<p>As always, after making changes to a service be sure to restart it!<\/p>\n\n\n\n<p><code>service ssh restart<\/code><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Additional Tips and Tricks<\/h2>\n\n\n\n<p>In addition to the server hardening tips above, below are some useful things to remember when hardening an Ubuntu server:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Display All Current Connections, Listening Services, and Processes Handling Them<\/h3>\n\n\n\n<p>The below command can be an Ubuntu sysadmin\u2019s best friend, it will list all current connections and listening services on a system along with the processes and PIDs for each connection:<\/p>\n\n\n\n<p><code>netstat -tulpn<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Display Services and Their Status<\/h3>\n\n\n\n<p>The command below will list all services on the system and their status:<\/p>\n\n\n\n<p><code>service --status-all<\/code><\/p>\n\n\n\n<p>Use grep to specify only the running services:<\/p>\n\n\n\n<p><code>service --status-all | grep \"[ + ]\"<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Check for Rootkits<\/h3>\n\n\n\n<p>The package \u201crkhunter\u201d is useful for doing a quick scan of your system for any known rootkits:<\/p>\n\n\n\n<p><code>apt-get install rkhunter<\/code><\/p>\n\n\n\n<p><code>rkhunter -C<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Common Configuration File Locations<\/h3>\n\n\n\n<p>Below are configuration file locations for just a few common services:<\/p>\n\n\n\n<p>\/etc\/apache\/apache2.conf #Apache 2<\/p>\n\n\n\n<p>\/etc\/ssh\/sshd_config #SSH Server<\/p>\n\n\n\n<p>\/etc\/mysql\/mysql.cnf #MySQL<\/p>\n\n\n\n<p>\/var\/lib\/mysql\/ #This entire directory contains all of the database in MySQL<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Log Locations<\/h3>\n\n\n\n<p>Below are the common default log locations:<\/p>\n\n\n\n<p>\/var\/log\/message \u2014 Where whole system logs or current activity logs are available.<\/p>\n\n\n\n<p>\/var\/log\/auth.log \u2014 Authentication logs.<\/p>\n\n\n\n<p>\/var\/log\/kern.log \u2014 Kernel logs.<\/p>\n\n\n\n<p>\/var\/log\/cron.log \u2014 Crond logs (cron job).<\/p>\n\n\n\n<p>\/var\/log\/maillog \u2014 Mail server logs.<\/p>\n\n\n\n<p>\/var\/log\/boot.log \u2014 System boot log.<\/p>\n\n\n\n<p>\/var\/log\/mysqld.log \u2014 MySQL database server log file.<\/p>\n\n\n\n<p>\/var\/log\/secure \u2014 Authentication log.<\/p>\n\n\n\n<p>\/var\/log\/utmp or \/var\/log\/wtmp \u2014 Login records file.<\/p>\n\n\n\n<p>\/var\/log\/apt \u2014 Apt package manager logs<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Keep System Up-To-Date An extremely crucial part of hardening any system is to ensure that it is always kept up-to-date. Doing this will keep any known bugs or vulnerabilities patched if one exists. The following commands are ways to update an Ubuntu system:apt-get update &amp;&amp; apt-get upgrade Accounts A good place to start when dealing [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,4,2],"tags":[],"class_list":["post-278","post","type-post","status-publish","format-standard","hentry","category-general-server-stuff","category-iptables","category-linux"],"_links":{"self":[{"href":"https:\/\/kidds.co.za\/index.php\/wp-json\/wp\/v2\/posts\/278","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kidds.co.za\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kidds.co.za\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kidds.co.za\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kidds.co.za\/index.php\/wp-json\/wp\/v2\/comments?post=278"}],"version-history":[{"count":1,"href":"https:\/\/kidds.co.za\/index.php\/wp-json\/wp\/v2\/posts\/278\/revisions"}],"predecessor-version":[{"id":279,"href":"https:\/\/kidds.co.za\/index.php\/wp-json\/wp\/v2\/posts\/278\/revisions\/279"}],"wp:attachment":[{"href":"https:\/\/kidds.co.za\/index.php\/wp-json\/wp\/v2\/media?parent=278"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kidds.co.za\/index.php\/wp-json\/wp\/v2\/categories?post=278"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kidds.co.za\/index.php\/wp-json\/wp\/v2\/tags?post=278"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}