Everybody says that Linux is secure by default, and to some extent, this is agreed upon (it’s a debatable topic). However, Linux has an in-built security model in place by default.
You need to tune and customize it according to your needs, which can help in making the system more secure. Linux is more challenging to manage but offers greater flexibility and configuration options.
Securing a system in production from the hands of hackers and crackers is a challenging task for a System Administrator. This is our first article related to “How to Secure Linux box” or “Hardening a Linux Box“.
In this post, we’ll explain 25 useful tips and tricks to secure your Linux system. Hope, below tips and tricks will help you some extend to secure your system.
1. Physical System Security – Setting a GRUB Password
One effective way to enhance security is by setting a GRUB password, which is a boot loader used by most Linux distributions to load the operating system when the computer starts up.
By setting a GRUB password, you add an extra layer of defense against unauthorized users who might attempt to tamper with or gain unauthorized access to your system.
Log into your Linux server and open the GRUB configuration file, which is located in different paths as shown.
- For Ubuntu/Debian: /etc/default/grub
- For CentOS/RHEL: /etc/grub2.cfg or /boot/grub/grub.cfg
Next, use a text editor like nano or vi to edit this file with root privileges (sudo).
Look for the line that starts with GRUB_CMDLINE_LINUX or similar and append GRUB_PASSWORD=<password> to the end of this line.
After editing the configuration file, update GRUB to apply the changes.
Restart your server to apply the GRUB password.
2. Creating Different Partitions for Higher Data Security
It’s important to have different partitions to obtain higher data security in case any disaster happens. By creating different partitions, data can be separated and grouped.
When an unexpected accident occurs, only data from that partition will be damaged, while the data on other partitions will survive. Make sure you have the following separate partitions and that third-party applications should be installed on separate file systems under /opt.
Most Linux distributions allow you to create and configure partitions during the installation process using the guided or manual partitioning options.
Post-installation, you can use tools like fdisk, parted, or graphical tools like GParted to create and manage partitions.
Example using fdisk:
Follow the prompts to create new partitions and assign them to the appropriate file systems.
3. Minimize Packages to Minimize Vulnerability: Remove Unwanted Services
One of the key strategies in securing a Linux system is to minimize the number of installed packages and running services. Each package and service can potentially introduce vulnerabilities, so keeping your system lean and efficient is a crucial step in hardening your server.
Start by identifying the packages and services that are not needed for your server’s specific function, which can be done using package management tools such as dpkg or rpm and service management utilities.
Once you have identified the unnecessary packages, you can remove them using your package manager such as apt or yum.
After removing the unwanted packages, the next step is to disable and stop services that are not needed.
4. Check Listening Network Ports in Linux
Monitoring and managing network ports is a crucial aspect of securing a Linux system and knowing which ports are open and listening can help you identify potential vulnerabilities and ensure that only necessary services are accessible.
To check network ports, we will use netstat or ss command-line tools, which provide various network-related information, including open ports and listening services.
5. Use Secure Shell (SSH) for Enhanced Security
Secure Shell (SSH) is a widely used protocol that offers a secure way to access and manage your Linux servers. However, to maximize security, there are several best practices you should follow, such as disabling root login, allowing only specific users, using SSH protocol 2, and changing the default SSH port.
Disabling root login forces users to log in with their user accounts, providing better accountability and reducing the risk of unauthorized access.
Find the line that says PermitRootLogin and change its value to no:
Restricting SSH access to specific users adds a layer of security by ensuring that only authorized users can log in.
Add a line at the end of the file to specify the allowed users:
Changing the default SSH port (22) to a non-standard port can help reduce the number of automated attacks on your server.
Restart the SSH service to apply the changes:
6. Keep Your System Up-to-Date
Regularly update your Linux distribution and all installed packages to the latest security patches and bug fixes using your system default package manager, such as apt for Debian-based distributions or yum for Red Hat-based systems.
7. Managing Cron Job Permissions
Cron is a powerful utility in Unix-like operating systems that allows users to schedule jobs to run at specific intervals.
However, there might be situations where you need to control who can or cannot create and run cron jobs on your system. Cron has built-in features to manage these permissions using the /etc/cron.allow and /etc/cron.deny files.
To allow specific users to use cron, edit /etc/cron.allow file and the usernames of the users you want to deny, one per line.
To deny specific users to use cron, edit /etc/cron.deny file and the usernames of the users you want to allow, one per line.
To completely disable all users from using cron, you can add the ALL line to the /etc/cron.deny file.
8. Disable USB Storage Detection
Many times it happens that we want to restrict users from using USB stick in systems to protect and secure data from stealing.
Create a file ‘nano /etc/modprobe.d/no-usb.conf‘ and adding the below line will not detect USB storage.
After creating the blacklist file, update the initramfs (initial RAM filesystem) to ensure the blacklisted module is not loaded during the boot process:
Reboot your system for the changes to take effect.
9. Turn on SELinux Protection
Security-Enhanced Linux (SELinux) is a compulsory access control security mechanism provided in the kernel. Disabling SELinux means removing the security mechanism from the system. Think twice carefully before removing it, if your system is attached to the internet and accessed by the public, then think some more about it.
SELinux provides three basic modes of operation and they are.
- Enforcing: This is the default mode that enables and enforces the SELinux security policy on the machine.
- Permissive: In this mode, SELinux will not enforce the security policy on the system, only warn and log actions. This mode is very useful in terms of troubleshooting SELinux-related issues.
- Disabled: SELinux is turned off.
You can view the current status of SELinux mode from the command line using ‘system-config-selinux‘, ‘getenforce‘, or ‘sestatus‘ commands.
If it is disabled, enable SELinux using the following command.
It also can be managed from the ‘/etc/selinux/config‘ file, where you can enable or disable it.
10. Removing X Desktops on a Linux Server
There is no need to run X Window desktops like KDE, GNOME, or XFCE on your dedicated LAMP server. You can remove or disable them to increase the security of the server and performance.
Before removing any desktop environment, identify which one is installed on your server.
Once identified, you can remove the desktop environment along with any associated packages.
Remove GNOME Desktop Environment:
Remove KDE Plasma Desktop Environment:
Remove Xfce Desktop Environment:
If your server also has the X Server installed, which is the display server system for GUIs, you can remove it to further reduce potential security risks.
Ensure that the server boots into a text-based console rather than starting a graphical desktop environment.
After removing the desktop environment and optionally the X Server, reboot your server to apply the changes.
11. Disabling IPv6 Protocol on a Linux Server
If you’re not using an IPv6 protocol, then you should disable it because most of the applications or policies do not require IPv6 protocol and currently, it isn’t required on the server.
You need to edit the network configuration file to disable IPv6.
Add the following lines to the end of the file:
Next, apply the changes to the running system.
Verify that IPv6 is disabled by checking the network interfaces and configuration.
You should see only IPv4 addresses assigned to your network interfaces. IPv6 addresses should not be present.
12. Restricting Users from Using Old Passwords on Linux
This is very useful if you want to disallow users to use the same old passwords. The old password file is located at /etc/security/opasswd. This can be achieved by using the PAM module.
Open the ‘/etc/pam.d/system-auth‘ file under RHEL / CentOS / Fedora.
Open the ‘/etc/pam.d/common-password‘ file under Ubuntu/Debian/Linux Mint.
Add the following line to the ‘auth‘ section.
Add the following line to the ‘password‘ section to disallow a user from re-using the last 5 passwords of his or her.
Only the last 5 passwords are remembered by the server. If you try to use any of the last 5 old passwords, you will get an error like.
13. How to Check Password Expiration of User
In Linux, the user’s passwords are stored in a ‘/etc/shadow‘ file in an encrypted format. To check the password expiration of users, you need to use the ‘chage‘ command, which displays information on password expiration details along with the last password change date. These details are used by the system to decide when a user must change his/her password.
To view any existing user’s aging information such as expiry date and time, use the following command.
To change the password aging of any user, use the following command.
Break down of the command:
- -M Set the maximum number of days
- -m Set the minimum number of days
- -W Set the number of days of warning
14. Lock and Unlock the Account Manually
The lock and unlock features are very useful, instead of removing an account from the system, you can lock it for a week or a month. To lock a specific user, you can use the following command.
Note : The locked user is still available for the root user only. The locking is performed by replacing the encrypted password with an (!) string. If someone tries to access the system using this account, he will get an error similar to below.
To unlock or enable access to a locked account, use the command as. This will remove the (!) string with an encrypted password.
15. Enforcing Stronger Passwords
Many users use soft or weak passwords and their passwords might be hacked with dictionary-based or brute-force attacks.
The ‘pam_cracklib‘ module is available in the PAM (Pluggable Authentication Modules) module stack which will force users to set strong passwords.
Open the following file with an editor.
And add a line using credit parameters such as (lcredit, ucredit, dcredit, and/or ocredit respectively lower-case, upper-case, digit, and other)
16. Enabling and Configuring Firewalls: firewalld and ufw
Firewalls are essential for securing Linux servers by controlling incoming and outgoing network traffic based on predetermined security rules. There are two most widely used firewall solutions for Linux are firewalld for RHEL-based distributions and ufw for Debian-based systems.
If firewalld or ufw is not already installed on your system, you can install it using the package manager.
Install Firewalld:
Install Ufw:
17. Disabling Ctrl+Alt+Delete on Linux
In most Linux distributions, pressing ‘CTRL-ALT-DELETE’ will take your system to the reboot process. So, it’s not a good idea to have this option enabled at least on production servers, if someone mistakenly does this.
To disable Ctrl+Alt+Delete, create or edit the override file for the Ctrl+Alt+Delete key combination.
Add the following lines to the override file to disable the key combination:
After making changes, reload the systemd configuration and mask the ctrl-alt-del.target ensures that it cannot be triggered:
18. Checking Accounts for Empty Passwords on Linux
Any account having an empty password means it’s opened for unauthorized access to anyone on the web and it’s a part of security within a Linux server. So, you must make sure all accounts have strong passwords and no one has any authorized access. Empty password accounts are security risks and that can be easily hackable.
To check if there were any accounts with empty passwords, use the following command.
19. Display SSH Banner Before Login
Displaying a banner message before the SSH login prompt can be a useful way to provide legal notices, warnings, or information to users attempting to access your Linux server.
To set such banners, you need to create a text file that contains the message you want to display.
Add your banner message:
Next, you need to configure the SSH server to display the banner before the login prompt.
Find and modify the Banner directive:
After making these changes, restart the SSH service to apply the new configuration.
20. Monitor User Activities on Linux
If you are dealing with lots of users, then it is important to collect the information of each user’s activities and processes consumed by them and analyze them at a later time or in case of any kind of performance, or security issues. But how we can monitor and collect user activity information.
There are two useful tools called ‘psacct‘ and ‘acct‘ are used for monitoring user activities and processes on a system. These tools run in a system background and continuously track each user activity on a system and resources consumed by services such as Apache, MySQL, SSH, FTP, etc.
For more information about installation, configuration, and usage, visit the below url.
- Monitor User Activity with psacct or acct Commands
21. Monitor Linux Logs Regularly
Reviewing logs on a regular basis is an important part of managing and securing a Linux system, as logs provide detailed records of system events, user activities, and potential security incidents.
By regularly checking these logs (usually stored in the /var/log directory), you can identify issues early, respond to security threats, and ensure the system runs smoothly.
- /var/log/message – Where whole system logs or current activity logs are available.
- /var/log/auth.log – Authentication logs.
- /var/log/kern.log – Kernel logs.
- /var/log/cron.log – Crond logs (cron job).
- /var/log/maillog – Mail server logs.
- /var/log/boot.log – System boot log.
- /var/log/mysqld.log – MySQL database server log file.
- /var/log/secure – Authentication log.
- /var/log/utmp or /var/log/wtmp : Login records file.
- /var/log/yum.log: Yum log files.
22. Backup Files in Linux Using rsync
Backing up files in Linux using rsync is an efficient and reliable method, as it synchronizes files and directories between two locations, making it perfect for backups.
To back up files on the local system, use the following command:
To back up files on the remote system, use the following command:
23. NIC Bonding
In Linux, NIC Bonding is a feature that allows you to combine multiple network interfaces into a single bonded interface to improve network reliability, redundancy, and performance.
Below guides, you’ll find a simple explanation of how NIC Bonding works in Linux, including configuration details for the two common modes.
- How to Create NIC Teaming or Bonding in CentOS 8 / RHEL 8
- A Beginner’s Guide to Creating Network Bonding and Bridging in Ubuntu
24. Keeping /boot as Read-Only in Linux
The /boot directory in Linux contains essential files needed to boot the operating system, such as the kernel, initial ramdisk (initrd), and bootloader configuration files.
Ensuring that /boot is mounted as read-only can enhance system security and integrity by preventing unauthorized modifications to these critical files.
To do this, open “/etc/fstab” file.
Add the following line at the bottom, save, and close it.
Please note that you need to reset the change to read-write if you need to upgrade the kernel in the future.
25. Ignoring ICMP or Broadcast Requests in Linux
In Linux, you can configure your system to ignore ICMP (Internet Control Message Protocol) or broadcast requests to enhance security and reduce unwanted network traffic.
Open the /etc/sysctl.conf file using a text editor:
Add or modify the following line to set the ICMP echo ignore flag:
Apply the changes:
26. Implement Intrusion Detection and Prevention
To enhance network security, install and configure an Intrusion Detection System (IDS) or Intrusion Prevention System (IPS) to monitor traffic and detect potential attacks.
IDS options like Snort and Suricata analyze network packets for suspicious activity and provide alerts. Snort offers flexible, rule-based detection, while Suricata supports multi-threaded processing and advanced protocols.
For a proactive defense, consider Fail2ban, which detects and responds to suspicious behavior by blocking offending IPs. Each tool can be configured to suit your specific security needs, providing robust protection against intrusions and helping maintain network integrity.
If you’ve missed any important security or hardening tip in the above list, or you’ve any other tip that needs to be included in the list. Please drop your comments in our comment box. TecMint is always interested in receiving comments, suggestions as well as discussions for improvement.
No comments:
Post a Comment