【荐】25 Hardening Security Tips for Linux Servers

本文提供了一套详尽的25步指南,旨在帮助您安全地配置Linux系统,从物理安全到软件更新,全面覆盖核心安全措施。包括设置GRUB密码保护、最小化安装包、使用SSH加密连接、定期更新系统、限制cron作业、禁用USB设备、启用SELinux等,确保您的Linux服务器免受黑客攻击。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Everybody says that Linux is secure by default and agreed to some extend (It’s debatable topics). However, Linux has in-built security model in place by default. Need to tune it up and customize as per your need which may help to make more secure system. Linux is harder to manage but offers more flexibility and configuration options.

Securing a system in a 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 & tricks to secure your Linux system. Hope, below tips & tricks will help you some extend to secure your system.

1. Physical System Security

Configure the BIOS to disable booting from CD/DVDExternal DevicesFloppy Drive inBIOS. Next, enable BIOS password & also protect GRUB with password to restrict physical access of your system.

  1. Set GRUB Password to Protect Linux Servers

2. Disk Partitions

It’s important to have different partitions to obtain higher data security in case if any disaster happens. By creating different partitions, data can be separated and grouped. When an unexpected accident occurs, only data of that partition will be damaged, while the data on other partitions survived. Make sure you must have following separate partitions and sure that third party applications should be installed on separate file systems under /opt.

/
/boot
/usr
/var
/home
/tmp
/opt

3. Minimize Packages to Minimize Vulnerability

Do you really want all sort of services installed?. It’s recommended to avoid installing useless packages to avoid vulnerabilities in packages. This may minimize risk that compromise of one service may lead to compromise of other services. Find and remove or disable unwanted services from the server to minimize vulnerability. Use the ‘chkconfig‘ command to find out services which are running on runlevel 3.

# /sbin/chkconfig --list |grep '3:on'

Once you’ve find out any unwanted service are running, disable them using the following command.

# chkconfig serviceName off

Use the RPM package manager such as “yum” or “apt-get” tools to list all installed packages on a system and remove them using the following command.

# yum -y remove package-name
# sudo apt-get remove package-name
  1. 5 chkconfig Command Examples
  2. 20 Practical Examples of RPM Commands
  3. 20 Linux YUM Commands for Linux Package Management
  4. 25 APT-GET and APT-CACHE Commands to Manage Package Management

4. Check Listening Network Ports

With the help of ‘netstat‘ networking command you can view all open ports and associated programs. As I said above use ‘chkconfig‘ command to disable all unwanted network services from the system.

# netstat -tulpn
  1. 20 Netstat Commands for Network Management in Linux

5. Use Secure Shell(SSH)

Telnet and rlogin protocols uses plain text, not encrypted format which is the security breaches. SSH is a secure protocol that use encryption technology during communication with server.

Never login directly as root unless necessary. Use “sudo” to execute commands. sudo are specified in /etc/sudoers file also can be edited with the “visudo” utility which opens in VIeditor.

It’s also recommended to change default SSH 22 port number with some other higher level port number. Open the main SSH configuration file and make some following parameters to restrict users to access.

# vi /etc/ssh/sshd_config
Disable root Login
PermitRootLogin no
Only allow Specific Users
AllowUsers username
Use SSH Protocol 2 Version
Protocol 2
  1. 5 Best Practices to Secure and Protect SSH Server

6. Keep System updated

Always keep system updated with latest releases patches, security fixes and kernel when it’s available.

# yum updates
# yum check-update

7. Lockdown Cronjobs

Cron has it’s own built in feature, where it allows to specify who may, and who may not want to run jobs. This is controlled by the use of files called /etc/cron.allow and/etc/cron.deny. To lock a user using cron, simply add user names in cron.deny and to allow a user to run cron add in cron.allow file. If you would like to disable all users from using cron, add the ‘ALL‘ line to cron.deny file.

# echo ALL >>/etc/cron.deny
  1. 11 Cron Scheduling Examples in Linux

8. Disable USB stick to Detect

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 ‘/etc/modprobe.d/no-usb‘ and adding below line will not detect USB storage.

install usb-storage /bin/true

9. Turn on SELinux

Security-Enhanced Linux (SELinux) is a compulsory access control security mechanism provided in the kernel. Disabling SELinux means removing security mechanism from the system. Think twice carefully before removing, if your system is attached to internet and accessed by the public, then think some more on it.

SELinux provides three basic modes of operation and they are.

  1. Enforcing: This is default mode which enable and enforce the SELinux security policy on the machine.
  2. 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 term of troubleshooting SELinuxrelated issues.
  3. DisabledSELinux is turned off.

You can view current status of SELinux mode from the command line using ‘system-config-selinux‘, ‘getenforce‘ or ‘sestatus‘ commands.

# sestatus

If it is disabled, enable SELinux using the following command.

# setenforce enforcing

It also can be managed from ‘/etc/selinux/config‘ file, where you can enable or disable it.

10. Remove KDE/GNOME Desktops

There is no need to run X Window desktops like KDE or GNOME on your dedicated LAMPserver. You can remove or disable them to increase security of server and performance. To disable simple open the file ‘/etc/inittab‘ and set run level to 3. If you wish to remove it completely from the system use the below command.

# yum groupremove "X Window System"

11. Turn Off IPv6

If you’re not using a IPv6 protocol, then you should disable it because most of the applications or policies not required IPv6 protocol and currently it doesn’t required on the server. Go to network configuration file and add followings lines to disable it.

# vi /etc/sysconfig/network
NETWORKING_IPV6=no
IPV6INIT=no

12. Restrict Users to Use Old Passwords

This is very useful if you want to disallow users to use same old passwords. The old password file is located at /etc/security/opasswd. This can be achieved by using PAM module.

Open ‘/etc/pam.d/system-auth‘ file under RHEL / CentOS / Fedora.

# vi /etc/pam.d/system-auth

Open ‘/etc/pam.d/common-password‘ file under Ubuntu/Debian/Linux Mint.

# vi /etc/pam.d/common-password

Add the following line to ‘auth‘ section.

auth        sufficient    pam_unix.so likeauth nullok

Add the following line to ‘password‘ section to disallow a user from re-using last 5 password of his or her.

password   sufficient    pam_unix.so nullok use_authtok md5 shadow remember=5

Only last 5 passwords are remember by server. If you tried to use any of last 5 old passwords, you will get an error like.

Password has been already used. Choose another.

13. How to Check Password Expiration of User

In Linux, user’s passwords are stored in ‘/etc/shadow‘ file in encrypted format. To check password expiration of user’s, you need to use ‘chage‘ command. It displays information of password expiration details along with last password change date. These details are used by 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.

#chage -l username

To change password aging of any user, use the following command.

#chage -M 60 username
#chage -M 60 -m 7 -W 7 userName
Parameters
  1. -M Set maximum number of days
  2. -m Set minimum number of days
  3. -W Set the number of days of warning

14. Lock and Unlock Account Manually

The lock and unlock features are very useful, instead of removing an account from the system, you can lock it for an week or a month. To lock a specific user, you can use the follow command.

# passwd -l accountName

Note : The locked user is still available for root user only. The locking is performed by replacing encrypted password with an (!) string. If someone trying to access the system using this account, he will get an error similar to below.

# su - accountName
This account is currently not available.

To unlock or enable access to an locked account, use the command as. This will remove (!) string with encrypted password.

# passwd -u accountName

15. Enforcing Stronger Passwords

A number of users use soft or weak passwords and their password might be hacked with adictionary based or brute-force attacks. The ‘pam_cracklib‘ module is available in PAM(Pluggable Authentication Modules) module stack which will force user to set strong passwords. Open the following file with an editor.

Read Also:

# vi /etc/pam.d/system-auth

And add line using credit parameters as (lcreditucreditdcredit and/or ocreditrespectively lower-case, upper-case, digit and other)

/lib/security/$ISA/pam_cracklib.so retry=3 minlen=8 lcredit=-1 ucredit=-2 dcredit=-2 ocredit=-1

16. Enable Iptables (Firewall)

It’s highly recommended to enable Linux firewall to secure unauthorised access of your servers. Apply rules in iptables to filters incomingoutgoing and forwarding packets. We can specify the source and destination address to allow and deny in specific udp/tcp port number.

  1. Basic IPTables Guide and Tips

17. Disable Ctrl+Alt+Delete in Inittab

In most Linux distributions, pressing ‘CTRL-ALT-DELETE’ will takes your system to reboot process. So, it’s not a good idea to have this option enabled at least on production servers, if someone by mistakenly does this.

This is defined in ‘/etc/inittab‘ file, if you look closely in that file you will see a line similar to below. By default line is not commented out. We have to comment it out. This particular key sequence signalling will shut-down a system.

# Trap CTRL-ALT-DELETE
#ca::ctrlaltdel:/sbin/shutdown -t3 -r now

18. Checking Accounts for Empty Passwords

Any account having an empty password means its 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 password, use the following command.

# cat /etc/shadow | awk -F: '($2==""){print $1}'

19. Display SSH Banner Before Login

It’s always a better idea to have an legal banner or security banners with some security warnings before SSH authentication. To set such banners read the following article.

  1. Display SSH Warning Message to Users

20. Monitor User Activities

If you are dealing with lots of users, then its important to collect the information of each user activities and processes consumed by them and analyse them at a later time or in case if any kind of performance, security issues. But how we can monitor and collect user activities information.

There are two useful tools called ‘psacct‘ and ‘acct‘ are used for monitoring user activities and processes on a system. These tools runs in a system background and continuously tracks each user activity on a system and resources consumed by services such as ApacheMySQL,SSHFTP, etc. For more information about installation, configuration and usage, visit the below url.

  1. Monitor User Activity with psacct or acct Commands

21. Review Logs Regularly

Move logs in dedicated log server, this may prevents intruders to easily modify local logs. Below are the Common Linux default log files name and their usage:

  1. /var/log/message – Where whole system logs or current activity logs are available.
  2. /var/log/auth.log – Authentication logs.
  3. /var/log/kern.log – Kernel logs.
  4. /var/log/cron.log – Crond logs (cron job).
  5. /var/log/maillog – Mail server logs.
  6. /var/log/boot.log – System boot log.
  7. /var/log/mysqld.log – MySQL database server log file.
  8. /var/log/secure – Authentication log.
  9. /var/log/utmp or /var/log/wtmp : Login records file.
  10. /var/log/yum.log: Yum log files.

22. Important file Backup

In a production system, it is necessary to take important files backup and keep them in safety vault, remote site or offsite for Disasters recovery.

23. NIC Bonding

There are two types of mode in NIC bonding, need to mention in bonding interface.

  1. mode=0 – Round Robin
  2. mode=1 – Active and Backup

NIC Bonding helps us to avoid single point of failure. In NIC bonding, we bond two or moreNetwork Ethernet Cards together and make one single virtual Interface where we can assign IP address to talk with other servers. Our network will be available in case of one NIC Card is down or unavailable due to any reason.

Read Also : Create NIC Channel Bonding in Linux

24. Keep /boot as read-only

Linux kernel and its related files are in /boot directory which is by default as read-write. Changing it to read-only reduces the risk of unauthorized modification of critical boot files. To do this, open “/etc/fstab” file.

# vi /etc/fstab

Add the following line at the bottom, save and close it.

LABEL=/boot     /boot     ext2     defaults,ro     1 2

Please note that you need to reset the change to read-write if you need to upgrade the kernel in future.

25. Ignore ICMP or Broadcast Request

Add following line in “/etc/sysctl.conf” file to ignore ping or broadcast request.

Ignore ICMP request:
net.ipv4.icmp_echo_ignore_all = 1

Ignore Broadcast request:
net.ipv4.icmp_echo_ignore_broadcasts = 1

Load new settings or changes, by running following command

#sysctl -p

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 discussion for improvement.

<think>我们正在讨论如何在Kali Linux上加强GRUB的安全性。GRUB是系统的引导加载程序,保护GRUB对于防止未经授权的系统访问至关重要。 我们将采取以下步骤来加强GRUB的安全性: 1. 设置GRUB密码:防止未经授权的用户修改启动参数(例如进入单用户模式)。 2. 加密GRUB配置文件:防止用户查看或修改GRUB配置。 3. 限制GRUB菜单:在启动时隐藏菜单,除非按下按键,并设置超时时间。 4. 设置文件权限:确保GRUB配置文件只能由root用户读写。 注意:在操作前备份重要文件(如/etc/grub.d/目录下的文件以及/boot/grub/grub.cfg)。 步骤1:生成GRUB密码的哈希值 使用`grub-mkpasswd-pbkdf2`命令生成一个PBKDF2加密的密码。 步骤2:编辑/etc/grub.d/00_header文件,添加密码保护 在00_header文件中添加超级用户和普通用户的密码设置(如果需要普通用户启动但无法修改,可以设置普通用户密码,但通常只需要超级用户密码)。 步骤3:更新GRUB配置 运行`update-grub`命令来应用更改。 步骤4:设置GRUB配置文件权限 确保/boot/grub/grub.cfg和/etc/grub.d/下的文件只有root可读写。 步骤5:调整GRUB菜单显示设置(可选) 通过编辑/etc/default/grub文件,设置GRUB_TIMEOUT=0(立即启动默认项)和GRUB_HIDDEN_TIMEOUT=0(隐藏菜单),然后更新GRUB。 下面详细说明: 1. 生成密码哈希: 在终端运行: grub-mkpasswd-pbkdf2 输入密码后,会得到一行以“grub.pbkdf2.sha512”开头的哈希字符串,复制它。 2. 编辑/etc/grub.d/00_header文件: 在文件末尾(在最后一行`fi`之后)添加以下内容(假设超级用户名为`root`,密码哈希为上面生成的字符串): cat << EOF set superusers="root" password_pbkdf2 root <这里替换为你的哈希字符串> EOF 注意:如果希望普通用户能够启动系统但不能修改启动参数,可以添加普通用户(例如`user`)和密码,但通常我们只设置超级用户密码,然后允许普通用户启动而不需要密码(通过配置菜单项)。为了安全,我们通常只设置超级用户。 3. 更新GRUB配置: update-grub 4. 设置文件权限: sudo chmod 600 /etc/grub.d/* sudo chmod 600 /boot/grub/grub.cfg 5. 隐藏GRUB菜单(可选): 编辑/etc/default/grub: GRUB_TIMEOUT=0 GRUB_HIDDEN_TIMEOUT=0 GRUB_HIDDEN_TIMEOUT_QUIET=true 然后运行`update-grub`。 注意:在设置密码后,启动时按下`e`键编辑启动项会要求输入用户名和密码(只有超级用户才能修改)。 但是,请注意:Kali Linux默认使用root用户,因此上述超级用户名设置为root是合适的。 另外,为了防止通过救援模式绕过,还应该考虑对BIOS/UEFI设置密码,并禁用从外部设备启动。 警告:如果忘记GRUB密码,将无法修改启动参数,这可能导致无法进入系统(除非通过Live CD修改配置文件)。请务必记住密码。 现在,我们将按照上述步骤编写具体命令和配置。 注意:在Kali Linux中,GRUB的配置文件通常位于/boot/grub/grub.cfg,而配置脚本位于/etc/grub.d/,全局配置文件是/etc/default/grub。 具体操作: 1. 生成密码哈希(示例,实际使用请替换): # grub-mkpasswd-pbkdf2 Enter password: Reenter password: PBKDF2 hash of your password is grub.pbkdf2.sha512.10000.9CA4611006FE...(很长一串) 2. 编辑00_header: sudo nano /etc/grub.d/00_header 在文件末尾(在`fi`之后)添加: cat << EOF set superusers="root" password_pbkdf2 root grub.pbkdf2.sha512.10000.9CA4611006FE...(替换为你的哈希) EOF 保存并退出。 3. 更新GRUB: sudo update-grub 4. 修改权限: sudo chmod 600 /etc/grub.d/* sudo chmod 600 /boot/grub/grub.cfg 5. (可选)隐藏菜单: sudo nano /etc/default/grub 修改以下行: GRUB_TIMEOUT=0 GRUB_HIDDEN_TIMEOUT=0 GRUB_HIDDEN_TIMEOUT_QUIET=true 然后运行: sudo update-grub 完成以上步骤后,重启系统,尝试在GRUB菜单出现时按下`e`键,会提示输入用户名和密码(输入root和设置的密码)才能编辑。 注意:在Kali Linux中,默认情况下可能没有安装GRUB的完整工具,如果`grub-mkpasswd-pbkdf2`命令不存在,可能需要安装`grub-common`包(通常已经安装)。 另外,为了进一步安全,可以设置BIOS/UEFI密码并加密硬盘。 引用:GRUB手册[^1],Kali Linux文档[^2] 相关问题: 1. 如何重置GRUB密码? 2. 除了GRUB密码,还有哪些方法可以保护Linux启动过程? 3. 如何配置GRUB以使用加密的磁盘?</think>### 加强 Kali Linux 的 GRUB 安全性指南 GRUB 是系统启动的关键组件,保护 GRUB 可防止未授权访问(如修改内核参数进入单用户模式)。以下是关键加固步骤: #### 1. **设置 GRUB 密码**(核心防护) ```bash sudo grub-mkpasswd-pbkdf2 # 生成密码哈希 ``` - 执行命令后输入密码,将输出类似: `grub.pbkdf2.sha512.10000.9CA4611006FE...` - 编辑配置文件: ```bash sudo nano /etc/grub.d/40_custom ``` - 添加以下内容(替换 `<hash>` 为生成的哈希): ```conf set superusers="root" password_pbkdf2 root <hash> ``` - 更新 GRUB: ```bash sudo update-grub ``` **效果**:启动时按 `e` 编辑启动项需输入密码[^1]。 #### 2. **配置文件权限加固** ```bash sudo chmod 700 /boot/grub/grub.cfg sudo chown root:root /boot/grub/grub.cfg ``` - 禁止非 root 用户读取配置,防止密码哈希泄露。 #### 3. **禁用恢复模式**(可选) - 编辑 `/etc/default/grub`: ```conf GRUB_DISABLE_RECOVERY="true" ``` - 更新配置: ```bash sudo update-grub ``` #### 4. **缩短启动超时时间** - 在 `/etc/default/grub` 中修改: ```conf GRUB_TIMEOUT=3 # 建议3秒内 ``` - 更新 GRUB 防止菜单长时间暴露。 #### 5. **UEFI 安全启动启用** ```bash sudo mokutil --enable-validation # 启用安全启动验证 sudo update-grub ``` - 需在 UEFI 设置中同步启用 Secure Boot[^2]。 #### 验证步骤 1. 重启系统,尝试编辑 GRUB 菜单(按 `e`) 2. 系统应提示输入用户名 (`root`) 和密码 3. 成功拦截未授权访问即配置生效 > ⚠️ **重要提醒**: > - 丢失密码需通过 Live USB 挂载分区重置 > - 定期更新 GRUB:`sudo apt upgrade grub2-common` > - 完整磁盘加密 (LUKS) 可提供额外保护层[^3] --- ### 相关问题 1. GRUB 密码丢失后如何恢复访问权限? 2. 如何配置 GRUB 以支持全磁盘加密 (LUKS)? 3. UEFI Secure Boot 与 GRUB 安全有何关联? [^1]: GRUB Manual: Password protection [^2]: Kali Docs: UEFI Secure Boot [^3]: Kali Hardening Guide: Disk Encryption
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值