Linux Hardening
Linux Hardening
BLOG ARTICLES
(/resource-center)
Most people assume that Linux is already secure, but imagine that your laptop is stolen (or
yours) without first being hardened. A thief could easily use the default password and user on
Kali to breach your device.
I’ll share all of the necessary steps to make your Linux host secure, including how I use
penetration testing and Kali Linux (https://www.pluralsight.com/courses/penetration-
testing-automation-using-python-kali-linux) to get the job done. It’s important to know
that there are so many Linux distributions
(https://en.wikipedia.org/wiki/List_of_Linux_distributions) (AKA distros) and each one will
differ from the command line perspective, but the logic is the same. Use the following tips to
harden your own Linux box.
2. BIOS protection
4. Disk partitioning
Machine name
IP address
Mac address
Name of the person who is doing the hardening (most likely you)
Date
Asset Number (If you’re working for a company, then you need to include the asset number
that your company uses for tagging hosts
2. BIOS protection
You need to protect the BIOS (http://www.linfo.org/bios.html) of the host with a password
so the end-user won’t be able to change and override the security settings in the BIOS; it’s
important to keep this area protected from any changes. Each computer manufacturer has a
different set of keys to enter the BIOS mode, then it’s a matter of finding the configuration
where you set the administrative password.
Next, you need to disable the booting from external media devices (USB/CD/DVD). If you omit
(/)
to change this setting, anyone can use a USB stick that contains a bootable OS and can
access your OS data.
The latest servers’ motherboards have an internal web server where you can access them
remotely. Make sure to change the default password of the admin page or disable it if it’s
possible.
In the image below, choose the third option from the list: Guided-use entire disk and set up
encrypted LVM (https://wiki.ubuntu.com/Lvm) (logical volume manager).
4. Disk partitioning
(/) (availability)
Backups have so many advantages in case of a damaged system, bugs in the OS update. For
important servers, the backup needs to be transferred offsite in case of a disaster. Backup
needs to be managed as well. For example, how long will you keep the old backups? When do
you need to backup your system (every day, every week …)?
/boot
/usr
/home
/tmp
/var
/opt
Portioning disks gives you the opportunity of performance and security in case of a system
error. In the picture below, you can see the option of how to separate partitions in Kali Linux
during the installation.
(/)
When you finish editing the file, you need to set the owner by executing the following
command:
Set permission on the /etc/grub.conf file to read and write for root only:
#nano /etc/modprobe.d/blacklist.conf
When the file opens, then add the following line at the end of the file (save and close):
blacklist usb_storage
#nano /etc/rc.local
modprobe -r usb_storage
exit 0
Remember that disabling unnecessary services will reduce the attack surface, so it is
important to remove the following legacy services if you found them installed on the Linux
server:
Telnet server
RSH server
(/)
NIS server
TFTP server
TALK server
PermitRootLogin no
The list can go on and on, but these should be enough to start with. For example, some
companies add banners to deter attackers and discourage them from continuing further. I
encourage you to check the manual of the SSH to understand all the configurations
(https://www.ssh.com/ssh/sshd_config/) in this file.
Here are some additional options that you need to make sure exist in the “sshd_config” file:
Protocol2
IgnoreRhosts to yes
HostbasedAuthentication no
PermitEmptyPasswords no
X11Forwarding no
MaxAuthTries 5
Ciphers aes128-ctr,aes192-ctr,aes256-ctr
ClientAliveInterval 900
ClientAliveCountMax 0
UsePAM yes
Finally, set the permissions on the sshd_config file so that only root users can change its
contents:
Disabled: Turned-off
Permissive: Prints warnings
(/)
#nano /etc/selinux/config
SELINUX=enforcing
I strongly recommend using the Linux Firewall by applying the iptable rules and filtering all the
incoming, outgoing and forwarded packets. Configuring your iptables rules will take some time,
but it’s worth the pain.
Another password policy that should be forced is strong passwords. The PAM module
(https://en.wikipedia.org/wiki/Pluggable_authentication_module) offers a pam_cracklib
that protects your server from dictionary and brute-force attacks. To accomplish this task,
open the file /etc/pam.d/system-auth using any text editor and add the following line:
Linux will hash the password to avoid saving it in cleartext so, you need to make sure to define
a secure password hashing algorithm SHA512.
Another interesting functionality is to lock the account after five failed attempts. To make this
happen, you need to open the file “/etc/pam.d/password-auth” and add the following lines:
auth required pam_env.so
(/)
auth required pam_faillock.so preauth audit silent deny=5 unlock_time=604800
auth [success=1 default=bad] pam_unix.so
auth [default=die] pam_faillock.so authfail audit deny=5 unlock_time=604800
auth sufficient pam_faillock.so authsucc audit deny=5 unlock_time=604800
auth required pam_deny.so
We’re not done yet; one additional step is needed. Open the file “/etc/pam.d/system-auth” and
make sure you have the following lines added:
After five failed attempts, only an administrator can unlock the account by using the following
command:
Also, another good practice is to set the password to expire after 90 days, to accomplish this
task you need to:
The next tip for enhancing the passwords policies is to restrict access to the su command by
setting the pam_wheel.so parameters in “/etc/pam.d/su”:
The final tip for passwords policy is to disable the system accounts for non-root users by using
the following bash script:
#!/bin/bash
(/)
for user in `awk -F: '($3 < 500) {print $1 }' /etc/passwd`; do
if [ $user != "root" ]
then
/usr/sbin/usermod -L $user
if [ $user != "sync" ] && [ $user != "shutdown" ] && [ $user != "halt" ]
then /usr/sbin/usermod -s /sbin/nologin $user
fi
fi
done
RECOMMENDED ARTICLES
Support Community
Contact (/contact) Guides (https://www.pluralsight.com/guides)
Help Center Teach (/teach)
(https://help.pluralsight.com/hc/en-us) Partner with Pluralsight (/partners)
IP Allowlist Affiliate Partners (/affiliate)
(https://help.pluralsight.com/hc/en- Pluralsight One (https://www.pluralsightone.org/)
us/articles/24428176323476) Authors (/authors)
Site Map
(https://www.pluralsight.com/sitemap.xml)
Download Pluralsight (/product/downloads)
Skills Plans (/pricing/skills)
A Cloud Guru Plans (/cloud-guru/pricing)
Flow Plans (/pricing/flow)
Professional Services
(/product/professional-services)
Company Industries
About Us (/about) Education (/industries/public-sector/higher-
Careers (/careers) education)
Newsroom (/newsroom) Financial Services (FSBI) (/industries/finance)
Resources (/resources) Healthcare (/industries/healthcare)
Insurance (/industries/insurance)
(/)
Non-Profit (https://www.pluralsightone.org/)
Public Sector (/industries/public-sector)
Newsletter
Sign up with your email to join our mailing list.
Email Address:*
Submit