{"id":512,"date":"2021-11-02T20:41:36","date_gmt":"2021-11-02T20:41:36","guid":{"rendered":"https:\/\/andrejacobs.org\/?p=512"},"modified":"2022-04-11T20:22:59","modified_gmt":"2022-04-11T20:22:59","slug":"installing-ubuntu-server-20-04-part-2","status":"publish","type":"post","link":"https:\/\/andrejacobs.org\/linux\/installing-ubuntu-server-20-04-part-2\/","title":{"rendered":"Installing Ubuntu Server 20.04 \u2013 part 2"},"content":{"rendered":"\n
Photo by Gabriel Heinzer<\/a> on Unsplash<\/a><\/p>\n\n\n\n Following on from part 1<\/a> the plan for today is:<\/p>\n It is crucial that a server receives security updates and in this section I will install "automatic" updates for Ubuntu. For more information see Cyberciti.biz’s post<\/a>.<\/p>\n <\/p>\n The server will not be receiving email but it still needs to be able to send email to me for alerts and notifications. I will setup the server to relay email via another mail server.<\/p>\n The objective is to relay email from the server to another mail server for example: smtp.server.org at port 465 and to use TLS.<\/p>\n <\/p>\n Now that we have email delivery setup, it is a good time to setup a cron job that can monitor CPU temperature and send me an email when things get to hot.<\/p>\n The script works on two temperature levels. If a CPU Core temperature goes above WARN_TEMP then an email will be sent out. If a temperature goes above CRIT_TEMP then an email will be sent out and the system be shutdown after 10 seconds.<\/p>\n In a later section I will cover how to tune the power down for hard disk idle times.<\/p>\n <\/p>\n <\/p>\n In this section I will be installing various tools used to monitor the server and detect potential intrusions.<\/p>\n rkhunter is great and it not only checks for known rootkits but it also does various other things including checking if files were modified.<\/p>\n Logwatch will scan log files and send you a report. This is handy to pick up when things start going bad.<\/p>\nOverview<\/h2>\n
\n
Automatic updates<\/h2>\n
\n
$ sudo apt -y update && sudo apt -y upgrade\n<\/code><\/pre>\n
\n
$ sudo apt install unattended-upgrades apt-listchanges bsd-mailx\n\n# Turn it on\n$ sudo dpkg-reconfigure -plow unattended-upgrades\n<\/code><\/pre>\n
\n
$ sudo vi \/etc\/apt\/apt.conf.d\/50unattended-upgrades\n\nUnattended-Upgrade::Mail "user@somewhere.com";\n\n$ sudo vi \/etc\/apt\/listchanges.conf\n\nemail_address=user@somewhere.com\n<\/code><\/pre>\n
\n
$ sudo unattended-upgrades --dry-run\n<\/code><\/pre>\n
\n
$ sudo etckeeper commit "Configured automatic updates"\n<\/code><\/pre>\n
Email delivery<\/h2>\n
\n
$ sudo apt install postfix\n<\/code><\/pre>\n
\n
\n
$ sudo cp \/usr\/share\/postfix\/main.cf.debian \/etc\/postfix\/main.cf\n$ sudo vi \/etc\/postfix\/main.cf\n\n# Add this to the bottom of the file\n# Replace SERVER with the mail server to use e.g. smtp.server.org\n# Replace PORT with the tcp port e.g 465\n\nrelayhost = [SERVER]:PORT\nsmtp_use_tls=yes\nsmtp_sasl_auth_enable = yes\nsmtp_sasl_password_maps = hash:\/etc\/postfix\/sasl_passwd\nsmtp_tls_CAfile = \/etc\/ssl\/certs\/ca-certificates.crt\nsmtp_sasl_security_options = noanonymous\nsmtp_tls_wrappermode = yes\nsmtp_tls_security_level = encrypt\n# Uncomment the following lines to debug any issues\n#debug_peer_list=SERVER\n#debug_peer_level=4\n<\/code><\/pre>\n
\n
$ sudo vi \/etc\/postfix\/sasl_passwd\n\n[SERVER]:PORT username:password\n\n$ sudo postmap \/etc\/postfix\/sasl_passwd\n# Ensure permissions are correct\n$ sudo chown root \/etc\/postfix\/sasl_passwd*\n$ sudo chmod 0600 \/etc\/postfix\/sasl_passwd*\n$ ls -la \/etc\/postfix\/\n\n-rw------- 1 root root 66 Nov 2 19:52 sasl_passwd\n-rw------- 1 root root 12288 Nov 2 19:54 sasl_passwd.db\n<\/code><\/pre>\n
\n
# I will be monitoring the logs for a bit, so now is a good time to start\n# a tmux session\n$ tmux\n\n# Split the window into two vertical panes: Ctrl + b %\n# Switch to the left pane: Ctrl + b o\n# Restart postfix\ntmux$ sudo systemctl restart postfix\n\n# Switch to the other pane: Ctrl + b o\n# Start monitoring the log\ntmux$ sudo tail -f \/var\/log\/mail.log\n<\/code><\/pre>\n
\n
# To install the mail program\ntmux$ sudo apt install mailutils\n\n# Back in the left hand tmux pane (not the one monitoring the log)\ntmux$ echo "The quick brown fox" | mail -s "Testing mail setup" USER@MAIL.COM\n\n# You should receive the email shortly\n# Test that root can also send email\ntmux$ sudo su\ntmux$ echo "The quick brown fox" | mail -s "Testing mail setup" USER@MAIL.COM\n<\/code><\/pre>\n
\n
# Enable debugging\n$ sudo vi \/etc\/postfix\/main.cf\n\n# Uncomment the lines you added previously\n\t\t\ndebug_peer_list=SERVER\ndebug_peer_level=4\n\n# Restart postfix and monitor the logs (Follow the tmux steps from earlier)\n$ sudo systemctl restart postfix\n$ sudo tail -f \/var\/log\/mail.log\n<\/code><\/pre>\n
\n
$ sudo crontab -e\n\nMAILTO=user@mail.com\n<\/code><\/pre>\n
\n
$ sudo etckeeper commit "Setup email delivery"\n<\/code><\/pre>\n
Monitor CPU temperature<\/h2>\n
\n
$ sudo apt install lm-sensors\n<\/code><\/pre>\n
\n
$ sudo sensors-detect\n$ sudo service kmod start\n\n# I chose the default option for 99% of the questions, just press ENTER\n# However this one, I answered yes so that the coretemp module can be loaded at reboots.\nDo you want to add these lines automatically to \/etc\/modules? (yes\/NO) yes\n<\/code><\/pre>\n
\n
$ sudo sensors\n\nacpitz-acpi-0\nAdapter: ACPI interface\ntemp1: +27.8\u00b0C (crit = +119.0\u00b0C)\ntemp2: +29.8\u00b0C (crit = +119.0\u00b0C)\n\ncoretemp-isa-0000\nAdapter: ISA adapter\nPackage id 0: +28.0\u00b0C (high = +80.0\u00b0C, crit = +100.0\u00b0C)\nCore 0: +26.0\u00b0C (high = +80.0\u00b0C, crit = +100.0\u00b0C)\nCore 1: +28.0\u00b0C (high = +80.0\u00b0C, crit = +100.0\u00b0C)\n<\/code><\/pre>\n
\n
$ sudo mkdir -p \/root\/scripts\n$ sudo vi \/root\/scripts\/cputempmon.sh\n<\/code><\/pre>\n
#!\/bin\/bash\n# Monitor CPU temperature and take action if needed\n# Andr\u00e9 Jacobs 14\/05\/2014\n# note: running on cron every minute, * * * * * \/....\/cputempmon.sh\n\nEMAIL=user@somewhere.com\nWARN_TEMP=70\nCRIT_TEMP=90\n\nwarn_flag=false\ncrit_flag=false\n\nfor line in `sensors | grep -oP "Core\\s+\\d:\\s+\\+?(\\K\\d+)"`; do\n if [ ${line} -ge ${WARN_TEMP} ]; then\n warn_flag=true\n fi\n\n if [ ${line} -ge ${CRIT_TEMP} ]; then\n crit_flag=true\n fi\ndone\n\nif [ "${crit_flag}" = true ]; then\n \/bin\/echo "CRITICAL: CPU temperature >= ${CRIT_TEMP}" | \/usr\/bin\/mail -s "[Server] CRITICAL CPU Temperature!" ${EMAIL}\n sleep 10\n \/sbin\/shutdown -h now\n exit\nfi\n\nif [ "${warn_flag}" = true ]; then\n \/bin\/echo "WARNING: CPU temperature >= ${WARN_TEMP}" | \/usr\/bin\/mail -s "[Server] WARNING CPU Temperature" ${EMAIL}\nfi\n<\/code><\/pre>\n
\n
$ sudo chmod 700 \/root\/scripts\/cputempmon.sh\n$ sudo chmod 700 \/root\/scripts\/\n$ sudo chown -R root:root \/root\/scripts\n<\/code><\/pre>\n
\n
$ sudo crontab -e\n\n# Monitor CPU temperature every minute\n* * * * * \/root\/scripts\/cputempmon.sh\n<\/code><\/pre>\n
\n
$ sysbench --test=cpu --cpu-max-prime=1000000000 --threads=4 run\n<\/code><\/pre>\n
Tuning power usage<\/h2>\n
\n
$ sudo apt install powertop\n<\/code><\/pre>\n
\n
tab<\/code> to switch tabs and the values to be tweaked can be found under the
Tunables<\/code> tab. You can press
enter<\/code> to allow powertop to tweak a setting.<\/li>\n<\/ul>\n
$ sudo powertop\n<\/code><\/pre>\n
\n
\n
Security monitoring<\/h2>\n
Rootkit hunting with rkhunter<\/h3>\n
\n
$ sudo apt install rkhunter\n<\/code><\/pre>\n
\n
$ sudo vi \/etc\/rkhunter.conf\n\n# Send email notifications\nMAIL-ON-WARNING="user@somewhere.com"\n<\/code><\/pre>\n
\n
$ sudo rkhunter --check --sk\n\n... # All is ok except this warning\nChecking for hidden files and directories [ Warning ]\n\n# Check the log file to see what is being picked up\n$ sudo less \/var\/log\/rkhunter.log\n...\n[19:55:13] Warning: Hidden directory found: \/etc\/.git\n[19:55:13] Warning: Hidden file found: \/etc\/.etckeeper: ASCII text\n[19:55:13] Warning: Hidden file found: \/etc\/.gitignore: ASCII text\n\n# Note: If you configured the email reporting then you should have\n# also received an email from rkhunter warning you.\n<\/code><\/pre>\n
\n
$ sudo vi \/etc\/rkhunter.conf\n\nALLOWHIDDENDIR=\/etc\/.git\nALLOWHIDDENFILE=\/etc\/.gitignore\nALLOWHIDDENFILE=\/etc\/.etckeeper\n\n# Perform the check again\n$ sudo rkhunter --check --sk\n<\/code><\/pre>\n
\n
\/etc\/cron.daily\/rkhunter<\/code> that will be run daily. However the config file for this also need to be updated.<\/li>\n<\/ul>\n
$ sudo vi \/etc\/default\/rkhunter\n\nCRON_DAILY_RUN="true"\nREPORT_EMAIL="user@somewhere.com"\n# This will ensure that rkhunter's database is updated when apt packages are installed or updated.\nAPT_AUTOGEN="true"\n\n# Do a cron test run\n$ sudo \/etc\/cron.daily\/rkhunter\n<\/code><\/pre>\n
\n
sudo rkhunter --update<\/code> . However this will now fail because mirrors and the WEB_CMD has been disabled for good reason. It is better to update these through automatic updates and apt. The internet is full of examples showing how to edit the .conf file but with no real explanation as to what it is doing.<\/li>\n
$ sudo rkhunter --propupd\n<\/code><\/pre>\n
\n
$ sudo rkhunter --propupd\n$ sudo etckeeper commit "Configured rkhunter"\n<\/code><\/pre>\n
Log monitoring with logwatch<\/h3>\n
\n
$ sudo apt install logwatch\n\n# You will also need the following directory or the cron job will fail\n$ sudo mkdir \/var\/cache\/logwatch\n\n# NOTE: By now etckeeper should be doing auto commits\n[main 3e5c81f] committing changes in \/etc made by "apt install logwatch"\n# Aswell as rkhunter gets updated\n[ Rootkit Hunter version 1.4.6 ]\nFile updated: searched for 180 files, found 142\n<\/code><\/pre>\n
\n
$ sudo logwatch | less\n<\/code><\/pre>\n
\n
$ sudo logwatch --mailto user@somewhere.com --output mail --format html --range 'between -7 days and today'\n<\/code><\/pre>\n
\n
# Copy the base config file so we can override it inside of \/etc\/\n$ sudo cp \/usr\/share\/logwatch\/default.conf\/logwatch.conf \/etc\/logwatch\/conf\/\n$ sudo vi \/etc\/logwatch\/conf\/logwatch.conf\n\n# Configure to send an HTML email\nOutput = mail\nFormat = html\nMailTo = user@somewhere.com\n\n# Initially I want as much info to be reported on until the system is tweaked\n# then it will go back to Low\nDetail = High\n<\/code><\/pre>\n