,

Top 50 Linux Commands for Scuring Linux Server

Posted by

Limited Time Offer!

For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!

Enroll Now

Securing a Linux server requires a deep understanding of essential commands that help mitigate vulnerabilities, manage user access, and enforce system integrity. Here’s a comprehensive list of the top 50 Linux commands for securing a Linux server, organized into categories for better readability. Iโ€™ll cover each command with an explanation of its purpose and usage to give you an in-depth guide on securing your server.

User Management and Access Control

  1. adduser
    • Creates a new user account with a home directory and initial configurations. Run adduser [username].
  2. usermod
    • Modifies a user account. Useful for changing user groups, shell, and other settings: usermod -aG [group] [username].
  3. passwd
    • Changes user passwords. It can also set password expiration policies with options like -e (expire): passwd [username].
  4. sudo
    • Provides superuser privileges to a user. Control sudo permissions through the /etc/sudoers file for fine-grained access.
  5. lastlog
    • Displays the last login times of all users, useful for identifying suspicious logins.
  6. who
    • Shows who is currently logged into the system: who -a.
  7. chage
    • Configures password aging policies for a user, enforcing periodic password changes: chage -E [expire_date] [username].
  8. faillog
    • Displays failed login attempts to monitor unauthorized access attempts: faillog -u [username].

Filesystem Permissions and Ownership

  1. chmod
    • Changes file or directory permissions: chmod 700 [filename].
  2. chown
    • Changes file or directory ownership: chown user:group [filename].
  3. umask
    • Sets default permissions for newly created files and directories. Use umask 027 for more restrictive permissions.
  4. lsattr
    • Lists file attributes, useful for finding immutable or restricted files: lsattr [filename].
  5. chattr
    • Changes file attributes to make files immutable (useful for critical system files): chattr +i [filename].
  6. find
    • Searches for files with specific permissions, users, or groups: find / -type f -perm 777.
  7. getfacl
    • Views Access Control List (ACL) permissions for files: getfacl [filename].
  8. setfacl
    • Sets ACLs for users or groups on files: setfacl -m u:username:rwx [filename].

Network Security

  1. iptables
    • Configures the firewall for packet filtering and traffic control: iptables -A INPUT -p tcp --dport 22 -j ACCEPT.
  2. firewalld
    • Manages firewall rules dynamically. Use firewall-cmd to add/remove firewall rules: firewall-cmd --zone=public --add-port=22/tcp --permanent.
  3. nmap
    • Scans open ports on a network to detect vulnerabilities: nmap -sS [target_ip].
  4. netstat
    • Displays network connections and listening ports: netstat -tuln.
  5. ss
    • A faster alternative to netstat for checking socket statistics: ss -tuln.
  6. tcpdump
    • Captures network packets to monitor traffic: tcpdump -i eth0.
  7. ufw
    • Uncomplicated Firewall, simplifying iptables for common use: ufw enable.
  8. fail2ban
    • Monitors log files for failed login attempts and blocks IP addresses. Configuration is in /etc/fail2ban/jail.conf.

Process and Resource Monitoring

  1. ps
    • Lists active processes, essential for detecting unauthorized processes: ps aux.
  2. top
    • Displays real-time resource usage, monitoring for unusual activity.
  3. htop
    • An enhanced version of top with a more user-friendly interface.
  4. kill
    • Terminates suspicious processes by their process ID (PID): kill -9 [PID].
  5. lsof
    • Lists open files and the processes using them: lsof -i.
  6. strace
    • Traces system calls of a process to identify vulnerabilities in real-time: strace -p [PID].

Logging and Auditing

  1. journalctl
    • Views and manages systemd logs: journalctl -u ssh.service.
  2. logrotate
    • Manages log rotation for various system logs, configured in /etc/logrotate.conf.
  3. auditctl
    • Configures auditing rules for monitoring file access or command execution. Useful for compliance and security.
  4. ausearch
    • Searches through audit logs for specific events: ausearch -c '[command]'.
  5. auditd
    • A daemon that manages and logs events specified by auditctl rules.
  6. cat /var/log/auth.log
    • Reads the authentication log file, showing login attempts.
  7. cat /var/log/syslog
    • Reads the system log for broader logging information on errors and activities.

Security Updates and Maintenance

  1. apt-get update && apt-get upgrade
    • Installs security patches and updates on Debian-based systems.
  2. yum update
    • Updates packages for Red Hat-based systems.
  3. unattended-upgrades
    • Configures automatic updates to keep your system secure with critical patches.
  4. rkhunter
    • Scans for rootkits and other vulnerabilities on the system.
  5. chkrootkit
    • Another tool for rootkit detection: chkrootkit.

7. SSH Configuration

  1. sshd_config
    • Configures the SSH daemon. Secure it by editing /etc/ssh/sshd_config (e.g., disable root login with PermitRootLogin no).
  2. ssh-keygen
    • Generates SSH key pairs for secure password-less authentication.
  3. ssh-copy-id
    • Installs public keys on remote hosts to allow key-based SSH login: ssh-copy-id [user@host].
  4. scponly
    • Restricts users to SCP/SFTP commands only, enhancing secure file transfers.

Disk and File Integrity Monitoring

  1. aide
    • A tool that checks for changes to files and directories, useful for integrity verification: aide --check.
  2. tripwire
    • Another file integrity checker that creates a snapshot of your systemโ€™s filesystem.
  3. md5sum
    • Checksums files for data integrity verification.
  4. sha256sum
    • Similar to md5sum, but uses SHA-256 for a more secure hash: sha256sum [filename].

What are the Tips for Linux Server Security?

1. Disable Unnecessary Services

Unused services are potential vulnerabilities, as each open service represents a possible entry point for attackers. By identifying and disabling unnecessary services, you reduce the system’s exposure to threats and conserve server resources. Begin by listing all active services and then decide which ones are essential to your server’s functions. For example, if you identify services like FTP that are not required, disabling them will help secure the system. After disabling a service, itโ€™s best to stop it from running to prevent it from restarting until itโ€™s genuinely needed.


2. Limit Sudo Access

The sudo privilege grants users elevated access to execute commands as a superuser, which can pose a security risk if widely distributed. By limiting sudo privileges to essential users only, you ensure a smaller attack surface and reduce the chances of accidental or malicious misuse. You can manage sudo access by editing the sudoers configuration file and defining permissions specific to each user or group. Additionally, itโ€™s beneficial to assign users to a dedicated group with sudo access rather than granting it individually. This practice makes it easier to manage and audit privileges. Configuring sudo permissions for specific commands only adds another layer of security by allowing users to perform certain tasks without unrestricted access.


3. Regular Backups

Backups are essential to ensure data integrity and availability. In case of data loss, corruption, or a security breach, backups allow you to restore your system to its previous state, reducing downtime and data loss. Regular backups should ideally be automated to maintain consistency. Tools like rsync allow for incremental backups, meaning only modified files are updated, saving time and storage space. Setting up automated backup schedules with cron jobs enables your server to run backups at regular intervals, such as daily or weekly, based on your requirements. Itโ€™s also beneficial to store backups offsite or in a secure cloud location, protecting them from potential local threats.


4. Limit Open Ports

Every open port on your server is a pathway for external connections, which could be exploited if not properly managed. By limiting open ports to only those that are essential, you significantly reduce the network attack surface. Regularly review open ports to ensure they align with your server’s purpose, and close any unnecessary ones. You can monitor open ports by listing network services and reviewing which ports are open. To secure your server, implement firewall rules to block or restrict access to specific ports. For instance, allowing only SSH traffic while blocking other unused ports makes your system less accessible to unauthorized users. Regularly review and update firewall rules to maintain a minimal and secure port configuration.


5. Check System Integrity

File integrity monitoring is crucial for detecting unauthorized changes to files and directories. Tools like AIDE (Advanced Intrusion Detection Environment) and Tripwire help monitor the filesystem by creating a baseline database that represents a known good state of critical files. By periodically comparing the current state of files with this baseline, you can detect and respond to unauthorized modifications or tampering, which is often an indication of malicious activity. Set up regular integrity checks with these tools to automatically scan your system and notify you of any discrepancies. This proactive approach ensures you quickly detect security issues that could otherwise go unnoticed.


Continuous Monitoring and Proactive Security

Maintaining a secure Linux server requires a continuous commitment to monitoring and updating security practices. Periodically audit user privileges, keep services and packages updated, and ensure your firewall and backup configurations are aligned with best practices. Centralized logging and monitoring solutions, such as Syslog servers or Security Information and Event Management (SIEM) systems, allow for effective, scalable, and comprehensive server security management. These tools help identify unusual patterns and system anomalies, enabling you to proactively respond to threats and safeguard your Linux server.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x