Linux 服务器运维(入门篇)

译自: Securing a Linux Server by Spenser Jones
  • 我该从哪里开始?
  • 更新服务器
  • 安装防火墙
  • Fail2ban 那些想成为黑客的人
  • 掌控全局
  • HackerNews 追踪

很少看到有人会为一台新安装的服务器立即设置保护,但我们所生活的世界使其成为一种必要。那么为什么那么多人等到最后才去设置保护呢?我也做过同样的事情,通常归结为想直接做有趣的事情。希望这篇文章能告诉你,保护一台服务器远比你想的容易,而且当攻击开始流动时,从你的堡垒往下看是相当有趣的。

这篇文章是为 Ubuntu 12.04.2 LTS 写的,但是你可以在其它 Linux 版本上做类似的事情。

我该从哪里开始?

如果服务器已经有一个公网 IP,你将希望立即锁定 root 访问权限。实际上,你需要完全锁定 SSH 访问,确保只有你能够进入。添加一个新用户,并将这个新用户添加到一个管理组(在 /etc/sudoers中预先设置对 sudo 的访问)。

## 添加用户组 admin(译者注)
$ sudo addgroup admin
Adding group 'admin' (GID 1001)
Done.

## 添加用户 spenserj(译者注)
$ sudo adduser spenserj
Adding user `spenserj' ...
Adding new group `spenserj' (1002) ...
Adding new user `spenserj' (1001) with group `spenserj' ...
Creating home directory `/home/spenserj' ...
Copying files from `/etc/skel' ...
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for spenserj
Enter the new value, or press ENTER for the default
    Full Name []: Spenser Jones
    Room Number []:
    Work Phone []:
    Home Phone []:
    Other []:
Is the information correct? [Y/n] y

## 追加用户 spenserj 到用户组 admin 中(译者注)
$ sudo usermod -a -G admin spenserj

你还需要在计算机上创建一个私钥(应为公钥,译者注),并在服务器上禁用密码身份验证。
(该公钥还要添加到服务器 .sshd 文件中,具体见文末参考链接中第三步。译者注)

## 本地主机用户主目录下新建 .ssh 文件,并将公钥追加到 authorized_keys(译者注)
$ mkdir ~/.ssh
$ echo "ssh-rsa [your public key]" > ~/.ssh/authorized_keys

PermitRootLogin no
PermitEmptyPasswords no
PasswordAuthentication no
AllowUsers spenserj

重新加载 SSH 以应用更改,然后尝试在新的会话中登录,确保一切正常。如果无法登录,你仍然可以使用原来的会话来修复问题。

## 重启 ssh(译者注)
$ sudo service ssh restart
ssh stop/waiting
ssh start/running, process 1599

更新服务器

现在只有你拥有访问服务器的权限,就不用再担心黑客会潜入,又可以正常呼吸了。你的服务器很可能有一些更新,所以现在就开始运行它们。

## 服务器更新(译者注)
$ sudo apt-get update
...
Hit http://ca.archive.ubuntu.com precise-updates/universe Translation-en_CA
Hit http://ca.archive.ubuntu.com precise-updates/universe Translation-en
Hit http://ca.archive.ubuntu.com precise-backports/main Translation-en
Hit http://ca.archive.ubuntu.com precise-backports/multiverse Translation-en
Hit http://ca.archive.ubuntu.com precise-backports/restricted Translation-en
Hit http://ca.archive.ubuntu.com precise-backports/universe Translation-en
Fetched 3,285 kB in 5s (573 kB/s)
Reading package lists... Done

## 服务器升级(译者注)
$ sudo apt-get upgrade
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages have been kept back:
  linux-headers-generic-lts-quantal linux-image-generic-lts-quantal
The following packages will be upgraded:
  accountsservice apport apt apt-transport-https apt-utils aptitude bash ...
73 upgraded, 0 newly installed, 0 to remove and 2 not upgraded.
Need to get 61.0 MB of archives.
After this operation, 151 kB of additional disk space will be used.
Do you want to continue [Y/n]? Y
...
Setting up libisc83 (1:9.8.1.dfsg.P1-4ubuntu0.6) ...
Setting up libdns81 (1:9.8.1.dfsg.P1-4ubuntu0.6) ...
Setting up libisccc80 (1:9.8.1.dfsg.P1-4ubuntu0.6) ...
Setting up libisccfg82 (1:9.8.1.dfsg.P1-4ubuntu0.6) ...
Setting up libbind9-80 (1:9.8.1.dfsg.P1-4ubuntu0.6) ...
Setting up liblwres80 (1:9.8.1.dfsg.P1-4ubuntu0.6) ...
Setting up bind9-host (1:9.8.1.dfsg.P1-4ubuntu0.6) ...
Setting up dnsutils (1:9.8.1.dfsg.P1-4ubuntu0.6) ...
Setting up iptables (1.4.12-1ubuntu5) ...
...

安装防火墙

现在运行最新的软件?好。先去建立防火墙,只允许你现在需要的东西。你可以随后添加一个例外,几分钟额外的工作不会让你崩溃。IPTables 在 Ubuntu 中是预装的,所有先给它设置一些规则。

$ sudo mkdir /etc/iptables

*filter
:INPUT DROP [0:0]
:FORWARD DROP [0:0]
:OUTPUT DROP [0:0]

# Accept any related or established connections
-I INPUT  1 -m state --state RELATED,ESTABLISHED -j ACCEPT
-I OUTPUT 1 -m state --state RELATED,ESTABLISHED -j ACCEPT

# Allow all traffic on the loopback interface
-A INPUT  -i lo -j ACCEPT
-A OUTPUT -o lo -j ACCEPT

# Allow outbound DHCP request - Some hosts (Linode) automatically assign the primary IP
#-A OUTPUT -p udp --dport 67:68 --sport 67:68 -j ACCEPT

# Outbound DNS lookups
-A OUTPUT -o eth0 -p udp -m udp --dport 53 -j ACCEPT

# Outbound PING requests
-A OUTPUT -p icmp -j ACCEPT

# Outbound Network Time Protocol (NTP) request
-A OUTPUT -p udp --dport 123 --sport 123 -j ACCEPT

# SSH
-A INPUT  -i eth0 -p tcp -m tcp --dport 22 -m state --state NEW -j ACCEPT

# Outbound HTTP
-A OUTPUT -o eth0 -p tcp -m tcp --dport 80 -m state --state NEW -j ACCEPT
-A OUTPUT -o eth0 -p tcp -m tcp --dport 443 -m state --state NEW -j ACCEPT

COMMIT

通过 iptables-apply 应用带有超时的规则集,如果丢失连接,修复规则并在继续之前重试。

$ sudo iptables-apply /etc/iptables/rules
Applying new ruleset... done.
Can you establish NEW connections to the machine? (y/N) y
... then my job is done. See you next time.

创建文件 /etc/network/if-pre-up.d/iptables,包括以下内容。这将在启动服务器时自动加载 IPTablse 规则。

#!/bin/sh
iptables-restore < /etc/iptables/rules

现在赋予它执行权限,并执行该文件以确保它正确加载。

$ sudo chmod +x /etc/network/if-pre-up.d/iptables
$ sudo /etc/network/if-pre-up.d/iptables

Fail2ban 那些想成为黑客的人

在安全性方面,Fail2ban 是我最喜欢的工具之一,因为它可以监视你的日志文件,并暂时禁止滥用资源的用户,比如强制执行 SSH 连接,或者破坏你的 Web 服务器。

## 安装 fail2ban(译者注)
$ sudo apt-get install fail2ban
[sudo] password for sjones:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
  gamin libgamin0 python-central python-gamin python-support whois
Suggested packages:
  mailx
The following NEW packages will be installed:
  fail2ban gamin libgamin0 python-central python-gamin python-support whois
0 upgraded, 7 newly installed, 0 to remove and 2 not upgraded.
Need to get 254 kB of archives.
After this operation, 1,381 kB of additional disk space will be used.
Do you want to continue [Y/n]? y
...

Fail2ban 安装了一个默认配置(/etc/fail2ban/jail.conf),但是我们希望在 /etc/fail2ban/jail.local中进行修改,所以,将其拷贝到那里。

## 拷贝配置(译者注)
sudo cp /etc/fail2ban/jail.{conf,local}
配置

将 ignoreip 行更改为你的 IP,并决定禁止 scumbags 的时间(默认为 10 分钟)。你还需要设置一个 destemail,我通常将其设为自己的电子邮件地址,然后输入 fail2ban@blocklistt.de。BlockList.de 是一个跟踪和自动报告黑客企图滥用其连接的 IP 的系统。

[DEFAULT]

# "ignoreip" can be an IP address, a CIDR mask or a DNS host
ignoreip = 127.0.0.1/8
bantime  = 600
maxretry = 3

# "backend" specifies the backend used to get files modification. Available
# options are "gamin", "polling" and "auto".
# yoh: For some reason Debian shipped python-gamin didn't work as expected
#      This issue left ToDo, so polling is default backend for now
backend = auto

#
# Destination email address used solely for the interpolations in
# jail.{conf,local} configuration files.
destemail = root@localhost,fail2ban@blocklist.de

虽然默认设置已经足够了,但是还有一些其他的设置需要检查,因此快速浏览文件到 Action 部分。

Actions

Actions 允许你对恶意活动作出反应,但默认是发出 IPTables 禁令,而我们希望它禁止并发送电子邮件。幸运的是这里有一个预先配置好的 action_wml 可以完成这项任务。

# Choose default action.  To change, just override value of 'action' with the
# interpolation to the chosen action shortcut (e.g.  action_mw, action_mwl, etc) in jail.local
# globally (section [DEFAULT]) or per specific section
action = %(action_mwl)s
Jails

为了让 Fail2ban 起作用,它需要知道监视什么。这是在配置的 Jails 部分配置的,有很多实例是预先加载和禁用的。由于到目前为止你只启用了 SSH 访问,所有我们将只启用 SSH 和 SSH-DDoS jails,但是你希望为安装在该服务器上的每个公开访问的服务添加一个新的 jails。

[ssh]

enabled  = true
port     = ssh
filter   = sshd
logpath  = /var/log/auth.log
maxretry = 6

[ssh-ddos]

enabled  = true
port     = ssh
filter   = sshd-ddos
logpath  = /var/log/auth.log
maxretry = 6
应用更改

现在我们已经配置了 Fail2ban,你将重新加载它,并确认它在向 IPTables 添加适当的规则。

$ sudo service fail2ban restart
 * Restarting authentication failure monitor fail2ban
   ...done.

$ sudo iptables -L
Chain INPUT (policy DROP)
target     prot opt source               destination
fail2ban-ssh-ddos  tcp  --  anywhere             anywhere             multiport dports ssh
fail2ban-ssh  tcp  --  anywhere             anywhere             multiport dports ssh
...
Chain fail2ban-ssh (1 references)
target     prot opt source               destination
RETURN     all  --  anywhere             anywhere

Chain fail2ban-ssh-ddos (1 references)
target     prot opt source               destination
RETURN     all  --  anywhere             anywhere

任何时候,你都可以使用sudo iptables -L列出你的规则,并在后面列出当前禁止的 IP。目前,Fail2ban 正在处理两个恶意 IP:

DROP       all  --  204.50.33.22         anywhere
DROP       all  --  195.128.126.114      anywhere

掌控全局

现在你已经锁定了一台服务器并可以开始使用了,但是这并不是你的安全之旅的终点。密切关注更新(首先在非生产环境中测试它们),始终关闭不需要的端口,定期检查日志,了解服务器内部和外部情况。

HackerNews 追踪

HackerNews 上有一些很好的评论,如果你对不同的视角和更高的安全性感兴趣,我建议通读它们。这篇文章的目的是作为一个初学者的安全指南,结束这篇文章并不意味着你的服务器就无懈可击了。使用这篇指南可以快速锁定一台新的服务器,然后根据你的特殊情况在其上进行构建。你可能想探究 IPV6 的安全性,改变你的 SSH 端口(通过隐藏),内核安全(SELinux 和 GRSecurity),跟踪系统的变化,以及全面审核(如果你的服务器曾经不安全,或已经在线很长一段时间)。服务器有数百个入口点,而且安装的每个应用程序都可能带来另一个漏洞,但是有了适当的工具,你就可以放心地上床睡觉。

参考链接:Linux服务器的初步配置流程 by 阮一峰

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值