Hardening Linux: Practical Commands to Boost System Security
This article presents a comprehensive guide on strengthening Linux systems by applying console restrictions, password policies, sudo notifications, SSH hardening, Tripwire intrusion detection, firewalld and iptables firewall management, compiler restrictions, immutable files, and SELinux tools such as aureport and sealert.
This article discusses how to harden a Linux system using a collection of practical security commands.
Console Security
Limit root login to specific terminals by editing the security file under /etc/ and allowing only one console for root, while requiring other users to operate without root privileges.
Password Lifecycle
Enforce password expiration using chage or by setting defaults in /etc/login.defs:
$ chage -M 20 likegeeks$ chage likegeeksPASS_MAX_DAYS 20 PASS_MIN_DAYS 0 PASS_WARN_AGE 5Sudo Notification
Configure sudo to send email notifications by adding to /etc/sudoers:
mailto [email protected]mail_always onSSH Tuning
Modify /etc/ssh/sshd_config to improve SSH security:
Port 5555PermitRootLogin noPasswordAuthentication no PermitEmptyPasswords noUseDNS yesGSSAPIAuthentication noServerAliveInterval 15 ServerAliveCountMax 3 TCPKeepAlive yesClientAliveInterval 30 ClientAliveCountMax 5AllowUsers user1 user2AllowGroup group1 group2Enable two‑factor authentication with Google Authenticator:
$ yum install google-authenticator$ google-authenticatorauth required pam_google_authenticator.soChallengeResponseAuthentication yes$ systemctl restart sshdTripwire Intrusion Detection
Install Tripwire from EPEL, set up keys, customize the policy, and initialize the database:
$ wget http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-9.noarch.rpm$ rpm -ivh epel-release-7-9.noarch.rpm$ sudo yum install tripwire$ tripwire-setup-keyfiles/etc/tripwire/twpol.txt$ tripwire --update-policy --secure-mode low /etc/tripwire/twpol.txt$ tripwire --init$ tripwire --checkFirewalld
Use firewalld for dynamic firewall management without restarting services:
$ firewall-cmd --state$ firewall-cmd --get-zones$ firewall-cmd --set-default-zone=$ firewall-cmd --zone= --list-all$ firewall-cmd --get-services$ firewall-cmd --zone= --add-service= SERVICE$ firewall-cmd --zone= --remove-service= SERVICE$ firewall-cmd --zone= --list-ports$ firewall-cmd --zone= --add-port= PORT/PROTOCOL$ firewall-cmd --zone= --remove-port= PORT/PROTOCOL$ firewall-cmd --zone= --add-forward-port= ...$ firewall-cmd --zone= --remove-forward-port= ...Reverting to iptables
If preferred, disable firewalld and install iptables services:
$ systemctl disable firewalld$ systemctl stop firewalld$ yum install iptables-services$ touch /etc/sysconfig/iptables$ touch /etc/sysconfig/ip6tables$ systemctl start iptables$ systemctl start ip6tables$ systemctl enable iptables$ systemctl enable ip6tablesRestricting Compilers
Prevent attackers from using gcc by limiting its permissions:
$ rpm -q --filesbypkg gcc | grep 'bin'$ groupadd compilerGroup$ chown root:compilerGroup /usr/bin/gcc$ chmod 0750 /usr/bin/gccImmutable Files
Make critical files immutable with chattr so even root cannot modify them:
$ chattr +i /myscript$ chattr -i /myscriptManaging SELinux with Aureport
Generate SELinux audit reports using aureport:
$ aureport --avc$ aureport -x$ aureport -au -i$ aureport -au --summary -i --failed$ aureport -au --summary -i --successUsing Sealert
Install and run sealert to interpret SELinux audit logs:
$ yum install setools$ sealert -a /var/log/audit/audit.logThe article concludes that these techniques collectively enhance Linux system security, though many more hardening measures remain to be explored.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
