博客 – 伯乐在线
首页最新文章在线课程业界开发 IT技术 设计 创业IT职场投稿更多 »
伯乐在线 > 首页 > 所有文章 > IT技术 > 如何保证Linux服务器的安全
如何保证Linux服务器的安全
2013/06/29 | 分类: IT技术 | | 标签: Linux, 服务器
分享到: 99
less即学即用
iOS-动画进阶
洪大师带你解读Symfony2框架
HTML5存储
本文由 伯乐在线 - 贾朝藤 翻译。未经许可,禁止转载!
英文出处:Spenser Jones。欢迎加入翻译小组。
很少见有人马上为一台新安装的服务器做安全措施,然而我们生活所在的这个社会使得这件事情是必要的。不过为什么仍旧这么多人把它拖在最后?我也做过相同的事,这通常可以归结为我们想要马上去折腾那些有趣的东西。希望这篇文章将向大家展示,确保服务器安全没有你想得那样难。在攻击开始后,俯瞰你的“堡垒”,也相当享受。
这篇文章为 Ubuntu 12.04.2 LTS 而写,你也可以在任何其他 Linux 分发版上做相同的事情。
我从哪儿开始?
如果服务器已经有了一个公有IP,你会希望立即锁定 root 访问。事实上,你得锁定整个ssh访问,并确保只有你可以访问。增加一个新用户,把它加入admin组(在/etc/sudoers预配置以拥有sudo访问权限)。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$ sudo addgroup admin
Adding group 'admin' (GID 1001)
Done.
$ 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
$ sudo usermod -a -G admin spenserj
你也将希望在你电脑上创建一个私有key,并且在服务器上禁用讨厌的密码验证。
1
2
$ mkdir ~/.ssh
$ echo "ssh-rsa [your public key]" > ~/.ssh/authorized_keys
/etc/ssh/sshd_config
1
2
3
4
PermitRootLogin no
PermitEmptyPasswords no
PasswordAuthentication no
AllowUsers spenserj
重新加载SSH,使用修改生效,之后尝试在一个新会话中登陆来确保所有事情正常工作。如果你不能登陆,你将仍然拥有你的原始会话来做修改。
1
2
3
$ sudo service ssh restart
ssh stop/waiting
ssh start/running, process 1599
更新服务器
既然你是访问服务器的唯一用户,你就不用担心黑客鬼鬼祟祟进入,再次正常呼吸。当有一些针对你服务器的更新时,正是修补的机会,所以动手吧,就现在。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
$ 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里预装了,所以去设置一些规则吧。
1
$ sudo mkdir /etc/iptables
/etc/iptables/rules
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
*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 命令为规则集生效。如果你丢失连接,修补你的规则,在继续之前再试一下
1
2
3
4
$ 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,然后写入下面内容。当你启动服务器的时候,将自动载入你的iptables规则。
/etc/network/if-pre-up.d/iptables
1
2
#!/bin/sh
iptables-restore < /etc/iptables/rules
现在给它执行权限,执行文件,以确保它正常载入
本回答被提问者和网友采纳