关于Linux服务器安全的9条建议

来源:互联网 发布:windows update被禁用 编辑:程序博客网 时间:2024/05/29 11:13

任何重要的系统都不能忽视服务器安全,尤其在公有云中。网上有关这方面的小建议和教程有很多很多,这里我们只看几个基础的、通用的最佳做法。

完成系统配置后应当执行的几项安全措施

以Ubuntu 16.04为例:

1、更新内核版本

当然了,不能盲目更新,但对新部署的服务器来说,使用最新版内核一般是无害的,并且能提高系统安全系数。通常人们会建议我们禁用未使用的服务,但我选择信任发行商,我相信他们会做出正确的选择,决定哪些应当默认安装或禁用。

apt-get –y update

2、重置root密码

访问虚拟机的web控制台需要root密码,当SSH(Secure Shell,安全外壳协议)不能正常工作时,例如有奇怪的防火墙设置阻止你的操作,系统发生严重内核错误,机器神秘重启。

root_pwd="DevOpsDennyChangeMe1"echo "root:$root_pwd" | chpasswd

3、加强SSHD服务安全

只允许通过密钥文件登录SSH,这样黑客就无法通过破解你的密码轻易入侵。ssh监听端口改用其他(默认为22端口),这样可以避免恼人的ssh登录尝试。

# Disable ssh by passwordsed -i 's/^#PasswordAuthentication yes/PasswordAuthentication no/g' \      /etc/ssh/sshd_configsed -i 's/PasswordAuthentication yes/PasswordAuthentication no/g' \     /etc/ssh/sshd_configgrep PasswordAuthentication /etc/ssh/sshd_config# Use another ssh portsshd_port="2702"sed -i "s/^Port 22/Port $sshd_port/g" /etc/ssh/sshd_configgrep "^Port " /etc/ssh/sshd_config# Restart sshd to take effectservice ssh restart

4、通过防火墙限制恶意访问

这也许是你应当执行的最重要的安全措施。

# Have a clean start with iptablesiptables -F; iptables -Xecho 'y' | ufw resetecho 'y' | ufw enableufw default deny incomingufw default deny forward# Allow traffic of safe portsufw allow 22,80,443/tcp# Allow traffic from certain portufw allow 2702/tcp# Allow traffic from trusted ipufw allow from 52.74.151.55

5、给命令历史记录添加时间戳

这样就可以看到在什么时间执行了什么命令。

echo export HISTTIMEFORMAT=\"%h %d %H:%M:%S \" >> /root/.bashrc

6、生成SSH密钥对

绝不要在整个服务器中共享同一个ssh密钥对!

exec ssh-agent bash# General new key pairssh-keygen# Load key pairssh-add

7、密切关注var/log

使用logwatch(https://www.howtoforge.com/tutorial/logwatch-installation-on-debian-and-ubuntu/)进行自动化检测与分析,这是一个非常有用的Perl脚本,可以生成系统日志活动的每日报告。主要关注以下几个日志文件:

  • /var/log/kern.log
  • /var/log/syslog
  • /var/log/ufw.log
  • /var/log/auth.log
  • /var/log/dpkg.log
  • /var/log/aptitude
  • /var/log/boot.log
  • /var/log/cron.log
  • /var/log/mailog
apt-get install -y logwatch# Full check. Takes several minuteslogwatch --range ALL# Only check log of Todaylogwatch --range Today# Check log for last weeklogwatch --range "between -7 days and -1 days"

8、使用第三方安全检查工具

并非所有人都是或者会成为安全专家,不妨尝试一些可信赖的多功能的工具,lynis(https://cisofy.com/lynis/)就是这样一个方便且直接的工具,仅包含一个bash文件。

apt-get install -y lynis# Run lynis to check security issueslynis -c

9、适当备份数据

时刻都要保留一个“B计划”,作为最后的手段,在另一台服务器上做一个可快速恢复的系统备份是非常可行的。

  • 原文链接:http://www.dennyzhang.com/linux_security/#more-4078
  • 翻译:zhangrj
  • 译文链接:http://www.icoder.top/blog/index.php/2016/09/22/9-useful-tips-for-linux-server-security/

转载请保留上述信息。

0 0