Tianhe Gao

为 Linux 服务器设置的重要安全措施

My First 5 Minutes On A Server; Or, Essential Security for Linux Servers – Sol Love

作者的安全哲学:采取一些措施,它们必须能够阻止那些最频繁的攻击方向,同时让服务器管理不至于因为过于安全而繁琐。

服务器只有两个用户:root 和部署用户。部署用户通过一段长密码获得 sudo 权限,它也是开发者登入的账户。开发者通过公匙而非密码登入,这样系统管理就变成在多个服务器之间保证 authorized_keys 文件的最新。root 用户无法通过 ssh 登入,部署用户只能从指定公司 IP 段登入。这种方法的缺点是,如果 authorized_keys 文件遭到破坏或被错误授权,需要登录到远程终端来修复它。

作者声明这并非自己的主张,只是自己的经验:

Note: I’m not advocating this as the most secure approach – just that it balances security and management simplicity for our small team. From my experience, most security breaches are caused either by insufficient security procedures or sufficient procedures poorly maintained.

改变 root 密码

密码要很长,可以记不住,但要保存在安全的地方。

我通过 Vagrant 新建了 Ubuntu 22.04:

1vagrant init generic/ubuntu2204
2vagrant up

默认情况下,无法使用 root 账户进行操作:

1su root
2# su: Authentication failure

需要设置 root 密码:

1sudo passwd root

解答来自:https://askubuntu.com/a/314098

更新包

通过包管理器更新包。Ubuntu 的更新命令:

1apt-get update
2apt-get upgrade

安装 Fail2ban

1apt-get install fail2ban

Fail2ban 是一种守护进程,用于监控登陆服务器的行为,并阻挡可疑的登陆。开箱即用。

设置登陆用户

用户名为 deploy,选择你喜欢的。

1useradd deploy
2mkdir /home/deploy
3mkdir /home/deploy/.ssh
4chmod 700 /home/deploy/.ssh

公匙授权

1vim /home/deploy/.ssh/authorized_keys

在文件中添加,你本地机器的 id_rsa.pub 文件或其他 ssh 公匙文件内容。

1chmod 400 /home/deploy/.ssh/authorized_keys
2chown deploy:deploy /home/deploy -R

测试新用户并开启 sudo 权限

1passwd deploy
2visudo

注释掉所有存在的 用户/组,并加上:

1root    ALL=(ALL) ALL
2deploy  ALL=(ALL) ALL

锁定 SSH

1vim /etc/ssh/sshd_config

修改/添加的内容:

1PermitRootLogin no
2PasswordAuthentication no
3AllowUsers deploy@(your-ip) deploy@(another-ip-if-any)

重启 ssh:

1service ssh restart

设置防火墙

1ufw allow from {your-ip} to any port 22
2ufw allow 80
3ufw allow 443
4ufw enable

开启自动安全更新

1apt-get install unattended-upgrades
2
3vim /etc/apt/apt.conf.d/10periodic

将文件更新为:

1APT::Periodic::Update-Package-Lists "1";
2APT::Periodic::Download-Upgradeable-Packages "1";
3APT::Periodic::AutocleanInterval "7";
4APT::Periodic::Unattended-Upgrade "1";
1vim /etc/apt/apt.conf.d/50unattended-upgrades

将文件更新为:

1Unattended-Upgrade::Allowed-Origins {
2        "Ubuntu lucid-security";
3//      "Ubuntu lucid-updates";
4};

安装 Logwatch 用于监控日志

1apt-get install logwatch
2
3vim /etc/cron.daily/00logwatch

添加到文件下面的内容:

1/usr/sbin/logwatch --output mail --mailto test@gmail.com --detail high

以上就是该文章的全部内容。

Hacker News 关于此问题的讨论:https://news.ycombinator.com/item?id=5316093


No notes link to this note

Welcome to tell me your thoughts via "email"
UP