{"id":10168,"date":"2021-09-20T18:20:55","date_gmt":"2021-09-20T18:20:55","guid":{"rendered":"https:\/\/linuxways.net\/?p=10168"},"modified":"2021-09-20T18:20:55","modified_gmt":"2021-09-20T18:20:55","slug":"linux-server-hardening-best-practices","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/centos\/linux-server-hardening-best-practices\/","title":{"rendered":"Linux Server Hardening &#8211; Best practices"},"content":{"rendered":"<p>Being an IT professional, it\u2019s challenging to get up to date with security vulnerabilities and threats. System patch updates, server, and network hardening are the most crucial factors to prevent security threats. Hardening Linux servers and network devices are important to reduce IT vulnerabilities and protect from a system compromise. In this article, we will learn some best practices used for Linux server hardening. For the demonstration purpose, CentOS\/RHEL server is being used so that some configurations might be different on other Linux distributions.<\/p>\n<h3>Update Linux Server<\/h3>\n<p>It&#8217;s necessary to keep your system up to date. There might be security patch updates for vulnerable components. Run the following command to update the system.<\/p>\n<pre>$ yum update -y<\/pre>\n<h3>Enable and configure firewall<\/h3>\n<p>Most of the Linux server comes with a firewalld package installed. Make sure that the firewalld is running in the system. In the Linux system, firewall-cmd command-line utility tool can be used to configure the firewall rules.<\/p>\n<p>Start and enable firewall service<\/p>\n<pre>$ systemctl start firewalld<\/pre>\n<pre>$ systemctl enable firewalld<\/pre>\n<p>To add specific services and ports, use the following syntax<\/p>\n<pre>$ firewall-cmd --add-service=http --permanent (Allow http service)<\/pre>\n<pre>$ firewall-cmd --add-port=8000\/tcp --permanent (Allow specific port)<\/pre>\n<p>To reflect the changes, reload the firewall.<\/p>\n<pre>$ firewall-cmd --reload<\/pre>\n<h3>Block USB drives<\/h3>\n<p>In a Linux system, USB storage can be restricted by creating a configuration files under <strong>\/etc\/modprobe.d\/ <\/strong>directory.<\/p>\n<p>Create a configuration file<\/p>\n<pre>$ touch \/etc\/modprobe.d\/usb_block.conf<\/pre>\n<pre>$ echo \u201cinstall usb-storage \/bin\/false\u201d &gt; \/etc\/modprobe.d\/usb_block.conf<\/pre>\n<h3>Remove unwanted users and groups<\/h3>\n<p>Some users and groups are already added to the system by default which is not needed. Remove such users and groups.<\/p>\n<pre>$ userdel postfix<\/pre>\n<pre>$ groupdel postfix<\/pre>\n<pre>$ userdel games<\/pre>\n<pre>$ groupdel games<\/pre>\n<p>Search of such users and groups and delete if not needed.<\/p>\n<h3>Remove unwanted packages<\/h3>\n<p>Some packages are already installed by default in the Linux system. For instance, postfix comes by default, and service starts up when the system is up. Identity such services and remove them<\/p>\n<pre>$ yum remove postfix -y<\/pre>\n<h3>Configure password policy<\/h3>\n<p>In the Linux machine, the password policy is specified in the \/etc\/login.defs file. Make changes in the password policy parameters as follows.<\/p>\n<pre>PASS_MAX_DAYS 90\r\n\r\nPASS_MIN_DAYS 1\r\n\r\nPASS_MIN_LENTH 8\r\n\r\nPASS_WARN_AGE 30<\/pre>\n<p>Example:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"887\" height=\"238\" class=\"wp-image-10169\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-340.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-340.png 887w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-340-300x80.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-340-768x206.png 768w\" sizes=\"auto, (max-width: 887px) 100vw, 887px\" \/><\/p>\n<h3>Configure SSH<\/h3>\n<p>To prevent unauthorized ssh access and attacks, make the following changes in \/etc\/ssh\/sshd_config file.<\/p>\n<pre># Set the custom ssh port\r\n\r\nPort 8022\r\n\r\n# Prevent from root login\r\n\r\nPermitRootLogin no\r\n\r\n# Restrict Empty password\r\n\r\nPermitEmptyPasswords no\r\n\r\n# Restrict host-based authentications\r\n\r\nHostbasedAuthentication no\r\n\r\nIgnoreRhosts yes\r\n\r\n# Use ssh protocol 2\r\n\r\nProtocol 2\r\n\r\n# Disable tools that have GUI\r\n\r\nX11Forwarding no<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"989\" height=\"579\" class=\"wp-image-10170\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-341.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-341.png 989w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-341-300x176.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-341-768x450.png 768w\" sizes=\"auto, (max-width: 989px) 100vw, 989px\" \/><\/p>\n<p>Check the configuration using the following command.<\/p>\n<pre>$ sshd -t<\/pre>\n<p>Restart ssh service<\/p>\n<pre>$ systemctl restart sshd<\/pre>\n<h3>Umask<\/h3>\n<p>Some files require umask hardening.<\/p>\n<pre>$ sed -i -e 's\/umask 022\/umask 027\/g' -e 's\/umask 002\/umask 027\/g' \/etc\/profile<\/pre>\n<pre>$ sed -i -e 's\/umask 022\/umask 027\/g' -e 's\/umask 002\/umask 027\/g' \/etc\/csh.cshrc<\/pre>\n<pre>$ sed -i -e 's\/umask 022\/umask 027\/g' -e 's\/umask 002\/umask 027\/g' \/etc\/init.d\/functions<\/pre>\n<pre>$ sed -i -e 's\/umask 022\/umask 027\/g' -e 's\/umask 002\/umask 027\/g' \/etc\/bashrc<\/pre>\n<h3>Disable core dump<\/h3>\n<p>Core dump stores information of an executable program. It can be used to determine why the program was aborted. Core dump also can be used to retrieve confidential information from a core file. Use the following command to disable core dump.<\/p>\n<pre>$ echo \u201c* hard core 0\u201d &gt;&gt;\/etc\/security\/limits.conf<\/pre>\n<h3>Use system auditing tools<\/h3>\n<p>The use of security tools makes it easy to identify system glitches. One of the free and open source tools is lynis which can be used to perform a regular audit of the system. Any findings are shown on the screen and also stored in the log file.<\/p>\n<p>Install the tool<\/p>\n<pre>$ yum install epel-release -y<\/pre>\n<pre>$ yum install lynis<\/pre>\n<p>Audit the system using the command below<\/p>\n<pre>$ lynis audit system<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1007\" height=\"613\" class=\"wp-image-10171\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-342.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-342.png 1007w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-342-300x183.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-342-768x468.png 768w\" sizes=\"auto, (max-width: 1007px) 100vw, 1007px\" \/><\/p>\n<p>There will be suggestions and warning results stored in the log file. Run the following command to see the result and solve accordingly.<\/p>\n<pre>$ grep Suggestion \/var\/log\/lynis.log<\/pre>\n<p>Output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1353\" height=\"340\" class=\"wp-image-10172\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-343.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-343.png 1353w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-343-300x75.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-343-1024x257.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-343-768x193.png 768w\" sizes=\"auto, (max-width: 1353px) 100vw, 1353px\" \/><\/p>\n<pre>$ grep Warning \/var\/log\/lynis.log<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"947\" height=\"91\" class=\"wp-image-10173\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-344.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-344.png 947w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-344-300x29.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/09\/word-image-344-768x74.png 768w\" sizes=\"auto, (max-width: 947px) 100vw, 947px\" \/><\/p>\n<h2>Conclusion<\/h2>\n<p>In this article, we learned some best practices to harden Linux systems. If you have any tips and tricks to secure a Linux server, do not forget to leave a comment in the box below.<\/p>","protected":false},"excerpt":{"rendered":"<p>Being an IT professional, it\u2019s challenging to get up to date with security vulnerabilities and threats. System patch updates, server, and network hardening are the most crucial factors&hellip;<\/p>","protected":false},"author":1,"featured_media":10317,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,4,5,83,165,2],"tags":[549],"class_list":["post-10168","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-centos","category-debian","category-mint","category-opensuse","category-red-hat","category-ubuntu","tag-server-security"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/10168","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=10168"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/10168\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/10317"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=10168"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=10168"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=10168"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}