{"id":259,"date":"2023-01-19T09:01:07","date_gmt":"2023-01-19T05:01:07","guid":{"rendered":"https:\/\/kidds.co.za\/?p=259"},"modified":"2023-01-19T09:01:07","modified_gmt":"2023-01-19T05:01:07","slug":"linux-server-security-tips-and-best-practices","status":"publish","type":"post","link":"https:\/\/kidds.co.za\/index.php\/2023\/01\/19\/linux-server-security-tips-and-best-practices\/","title":{"rendered":"Linux Server Security Tips and Best Practices"},"content":{"rendered":"\n<p>checklist for SysAdmins<\/p>\n\n\n\n<p id=\"3442\">This article includes a collection of commands and best practices that you can use to improve the security of your Linux servers (RHEL\/CentOS)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"fe6e\">1. Remove insecure tools (FTP, telnet, rlogin, rsh, etc.) and use only secure alternatives (SCP, SSH, sftp, rsync, etc.)<\/h3>\n\n\n\n<p id=\"27fa\">When choosing data communication tools, use only the secure and encrypted tools and remove the rest of the tools from the server.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ yum erase xinetd ypserv tftp-server telnet-server rsh-server<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"916c\">2. Enable firewall\/Iptables<\/h3>\n\n\n\n<p id=\"a07e\">Iptables allows you to configure the IP packet filter rules of the Linux kernel firewall. To get the maximum advantage, you may need some advanced knowledge on setting up these rules. The following are few examples that you can try.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ service iptables start\/\/ Force SYN packets check<br>$ iptables -A INPUT -p tcp ! --syn -m state --state NEW -j DROP\/\/ Drop XMAS packets<br>$ iptables -A INPUT -p tcp --tcp-flags ALL ALL -j DROP\/\/ Drop null packets<br>$ iptables -A INPUT -p tcp --tcp-flags ALL NONE -j DROP\/\/ Drop incoming packets with fragments<br>$ iptables -A INPUT -f -j DROP<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"a4b6\">3. Disable unwanted services and daemons<\/h3>\n\n\n\n<p id=\"11d3\">You may not need services like AutoFS, NFS, FTP, HTTP, NIS, telnetd, sendmail most of the time. You can remove\/disable them.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ chkconfig \u2013list | grep \u20183:on\u2019<br>$ service serviceName stop<br>$ chkconfig serviceName off$ yum remove packageName<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"c374\">4. Audit installed packages and remove unwanted ones regularly<\/h3>\n\n\n\n<p id=\"3e8e\">A smaller number of packages always results in a smaller security threat surface, which means fewer potential threats at the end of the day. Therefore, keep only the necessary packages and clean up the rest.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ yum list installed<br>$ yum list packageName<br>$ yum remove packageName<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4427\">5. Audit listening network ports and block unwanted ones regularly<\/h3>\n\n\n\n<p id=\"5211\">From time to time, you might open ports for different services and leave them out later. This is very risky, so you must always be conscious of your open ports at any given time and make sure that unwanted ones are blocked.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ List open ports of own server<br>$ netstat -tulpn<br>$ ss -tulpn\/\/ List open ports of any server in network <br>$ nmap -sT -O localhost<br>$ nmap -sT -O server.example.com\/\/ Use chkconfig to disable corresponding services<br>$ chkconfig \u2013list | grep \u20183:on\u2019<br>$ service serviceName stop<br>$ chkconfig serviceName off<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"556d\">6. Audit user accounts and disable the unwanted ones regularly<\/h3>\n\n\n\n<p id=\"a17f\">Only add the minimum number of user accounts needed and disable the rest.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ Lock password (add ! in the second field of the file \/etc\/passwd, password authentication will fail because of this, but other authentication methods like SSH keys will still work)<br>$ usermod -L userName<br>$ passwd -l userName\/\/ Unlock password (i.e. revert the effect of above command)<br>$ passwd -u userName\/\/ Expire account <br>$ chage -E0 userName\/\/ Change default shell to non-interactive, so user won't get any login shell<br>$ usermod -s \/sbin\/nologin userName\/\/ Verify locked user account (*LK* flag in the output indicates that the account is locked)<br>$ passwd --status userName<br>userName <strong>*LK*<\/strong> 2021-04-05 0 45 7 -1 (Password set, SHA512 crypt.)\/\/ Verify password and account expiration<br>$ chage -l userName<br>Last password change: Jan 19, 2021<br>Password expires: Jan 02, 2022<br>Password inactive: never<br>Account expires: Jan 02, 2022<br>Minimum number of days between password change: 0<br>Maximum number of days between password change: 45<br>Number of days of warning before password expires: 7\/\/ Verify non-interactive shell<br>$ grep ^userName \/etc\/passwd<br>userName:x:1000:1000:,,,:\/home\/userName:\/sbin\/nologin<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"8c54\">7. Enable SELinux<\/h3>\n\n\n\n<p id=\"bf4a\">Security-enhanced Linux (SELinux) is provided by the kernel as an access control security mechanism.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ View current status<br>$ sestatus<br>$ system-config-selinux<br>$ getenforce\/\/ Enable SELinux (using command)<br>$ setenforce enforcing<br>$ setenforce 1\/\/ Enable SELinux (by editing config file)<br>$ vi \/etc\/selinux\/config<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2ae3\">8. Turn off IPv6<\/h3>\n\n\n\n<p id=\"b135\">Unless you use IPv6 for a specific reason, you can disable it.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ vi \/etc\/sysconfig\/networkNETWORKING_IPV6=no<br>IPV6INIT=no<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"8670\">9. Make \/boot directory a read-only<\/h3>\n\n\n\n<p id=\"32eb\">By default, the&nbsp;<code>\/boot<\/code>&nbsp;directory allows both read and write access. Since it includes the Linux kernel and its related files, you must protect them from unauthorized and unintended modifications.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ vi \/etc\/fstabLABEL=\/boot     \/boot     ext2     defaults,ro     1 2<\/pre>\n\n\n\n<p id=\"53ba\">As a result, you may not be able to upgrade the kernel in the future. In such a scenario, you can temporarily revert this change.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1e28\">10. Disable ICMP broadcast requests and harden \/etc\/sysctl.conf<\/h3>\n\n\n\n<p id=\"9ecb\"><a href=\"https:\/\/en.wikipedia.org\/wiki\/Ping_(networking_utility)\" rel=\"noreferrer noopener\" target=\"_blank\">Ping<\/a>&nbsp;is the most fundamental way of checking whether a server is reachable over a network. Ping operates by sending ICMP (Internet Control Message Protocol) echo request packets to the target host and waiting for an ICMP echo reply. It\u2019s used for testing and troubleshooting only. So when there\u2019s no such need, you must keep it disabled. Also, it is best to harden&nbsp;<code>\/etc\/sysctl.conf<\/code>&nbsp;with the below configurations.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ vi \/etc\/sysctl.conf# Ignore ICMP request<br>net.ipv4.icmp_echo_ignore_all = 1# Ignore Broadcast request<br>net.ipv4.icmp_echo_ignore_broadcasts = 1<br>net.ipv4.icmp_ignore_bogus_error_messages=1# Turn on execshield<br>kernel.exec-shield=1<br>kernel.randomize_va_space=1# Enable IP spoofing protection<br>net.ipv4.conf.all.rp_filter=1# Make sure spoofed packets get logged<br>net.ipv4.conf.all.log_martians = 1# Disable IP source routing<br>net.ipv4.conf.all.accept_source_route=0\/\/ Load new changes<br>$ sysctl -p<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"579d\">11. Strengthen password policy<\/h3>\n\n\n\n<p id=\"9abf\">Never allow user accounts with empty passwords.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ Check for empty password accounts<br>$ cat \/etc\/shadow | awk -F: \u2018($2==\u201d\u201d){print $1}\u2019<\/pre>\n\n\n\n<p id=\"ecbb\">Also, force strong passwords using tools like pam_cracklib (PAM = pluggable authentication modules).<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ vi \/etc\/pam.d\/system-authpassword requisite pam_cracklib.so retry=3 minlen=8 lcredit=1 ucredit=2 dcredit=2 ocredit=1\/\/ Notation<br>retry = retry attempts for a user to pick a good password before the passwd program aborts<br>lcredit = lower-case<br>ucredit = upper-case<br>dcredit = numeric (digit)<br>ocredit = non-alphanumeric (other)<br>minlen = minimum length<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"7988\">12. Monitor suspicious user activities<\/h3>\n\n\n\n<p id=\"a903\">If you notice suspicious user activities or have to deal with difficult users with different trust levels, you can collect information about their activities and processes for post audits using&nbsp;<code>psacct<\/code>&nbsp;or acct tools.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ yum install psacct\/\/ Enable<br>$ chkconfig psacct on<br>$ \/etc\/init.d\/psacct start<br>$ \/etc\/init.d\/psacct status\/\/ Find day-wise login info for a user<br>$ ac -d userName\/\/ Find last executed commands by a user<br>$ lastcomm userName\/\/ Find unsuccessful login attempts<br>$ faillog -u userName<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"efb5\">13. Disable root login and password authentication for SSH<\/h3>\n\n\n\n<p id=\"8b28\">Root user is available in most Linux servers by default and hackers often try to get a login as root by guessing its password. Therefore, it\u2019s recommended to disable the root account. First, ensure that you have an admin user account so that you can execute root-level commands later with the&nbsp;<code>sudo<\/code>&nbsp;special privileges access command. Then you can safely disable the root login.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ vi \/etc\/ssh\/sshd_configPermitRootLogin no<\/pre>\n\n\n\n<p id=\"b6ff\">Also, you can disable password authentication in favour of a secure method, like SSH public-private key authentication.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ vi \/etc\/ssh\/sshd_configPasswordAuthentication no<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2884\">14. Disable shutdown\/restart on three-finger salute (Ctrl+Alt +Del)<\/h3>\n\n\n\n<p id=\"7fa5\">You can define how a Linux system must react to a three-finger salute, but since someone can mistakenly also run it, it is better to disable it on always running servers.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ vi \/etc\/inttab\/\/ Comment-out the line starting with `ca::ctrlaltdel:`<br># ca::ctrlaltdel:\/sbin\/shutdown -t3 -r now<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4373\">15. Remove desktops<\/h3>\n\n\n\n<p id=\"e887\">You really don\u2019t need to run desktops on a dedicated server. Disabling them increases not only performance but also security (fewer bloatware\/packages = fewer security troubles).<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ yum groupremove \"X Window System\"<br>$ yum groupremove \"GNOME Desktop\"<br>$ yum groupremove \"KDE Plasma Workspaces\"<br>$ yum groupremove \"Server with GUI\"<br>$ yum groupremove \"MATE Desktop\"<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1223\">16. Fix permissions on world-writable files and no-owner files<\/h3>\n\n\n\n<p id=\"5f9a\">Find files with loose permissions and set correct user and group permissions.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ Find all world writable and sticky bits set files<br>$ find \/dir -xdev -type d \\( -perm -0002 -a ! -perm -1000 \\) -print\/\/ Find all files not owned by any user or group<br>$ find \/dir -xdev \\( -nouser -o -nogroup \\) -prin<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"ae51\">17. Remove unwanted files\/scripts\/directories regularly and keep the server clean<\/h3>\n\n\n\n<p id=\"b9fa\">If you create a temporary file\/script\/folder, make sure you remove it soon. Do not stack a huge load of old file data in a server because there can be a threat hidden somewhere.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"6fc6\">18. Keep servers up to date<\/h3>\n\n\n\n<p id=\"a66f\">Always apply the latest security updates, releases, patches, etc. to your servers.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ Update all packages<br>$ yum updates\/\/ Update security-related packages<br>$ yum update --security\/\/ Check available updates<br>$ yum check-update<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"925b\">19. Collect Syslog regularly<\/h3>\n\n\n\n<p id=\"f63c\">Syslog helps you understand most suspicious behaviours and errors in services and packages. Collect syslog information proactively for potential investigation of security incidents in the future.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"c014\">20. Set up periodic backups and secure offsite mount points for important files\/directories<\/h3>\n\n\n\n<p id=\"6728\">In the event of an intrusion or ransomware attacks, to ensure that critical data remains accessible, various backup strategies can be used. As a rule of thumb, backups are maintained at least as three copies, with at least two of them stored at separate locations, and with one location being offsite (the 3\u20132\u20131 backup rule).<\/p>\n\n\n\n<p id=\"00b6\">Tools like rsync can help with backing up data in Linux and with many data-copying and backup-related features.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ yum install rsync<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"eec4\">21. Configure intrusion prevention tools at the network level<\/h3>\n\n\n\n<p id=\"1536\">Tools like&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Fail2ban\" rel=\"noreferrer noopener\" target=\"_blank\">Fail2Ban<\/a>&nbsp;can alter firewall rules to ban any address that has attempted to log in a specific number of times. Also, it can be used to spot and address authentication failure patterns, with support for email alerts too.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ yum install fail2ban<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"a2b5\">22. Perform security audits regularly<\/h3>\n\n\n\n<p id=\"b7eb\">Tools like auditd can help to collect basic system audit information and keep a record on the disk, for instance, of predefined rules in&nbsp;<code>\/etc\/audit.rules<\/code>.<\/p>\n\n\n\n<p id=\"7463\">However, it is always industry best practice to run security audits using specialized commercial tools too, especially when it\u2019s an enterprise environment. With these audits, misconfiguration, expired policies, pending updates, and rare threats can be revealed.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>checklist for SysAdmins This article includes a collection of commands and best practices that you can use to improve the security of your Linux servers (RHEL\/CentOS) 1. Remove insecure tools (FTP, telnet, rlogin, rsh, etc.) and use only secure alternatives (SCP, SSH, sftp, rsync, etc.) When choosing data communication tools, use only the secure and [&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,2],"tags":[],"class_list":["post-259","post","type-post","status-publish","format-standard","hentry","category-general-server-stuff","category-linux"],"_links":{"self":[{"href":"https:\/\/kidds.co.za\/index.php\/wp-json\/wp\/v2\/posts\/259","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=259"}],"version-history":[{"count":1,"href":"https:\/\/kidds.co.za\/index.php\/wp-json\/wp\/v2\/posts\/259\/revisions"}],"predecessor-version":[{"id":260,"href":"https:\/\/kidds.co.za\/index.php\/wp-json\/wp\/v2\/posts\/259\/revisions\/260"}],"wp:attachment":[{"href":"https:\/\/kidds.co.za\/index.php\/wp-json\/wp\/v2\/media?parent=259"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kidds.co.za\/index.php\/wp-json\/wp\/v2\/categories?post=259"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kidds.co.za\/index.php\/wp-json\/wp\/v2\/tags?post=259"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}