日期:2014-05-16  浏览次数:20779 次

如何保证Linux服务器的安全

 很少见有人马上为一台新安装的服务器做安全措施,然而我们生活所在的这个社会使得这件事情是必要的。不过为什么仍旧这么多人把它拖在最后?我已经做了相同的事情,它常常可以归结为想要马上进入有趣的东西。希望这篇文章将向大家展示,确保服务器安全没有你想得那样难。在攻击开始后,俯瞰你的“堡垒”会相当享受的。

?

这篇文章为Ubuntu 12.04.2 LTS而写,你也可以在任何其他Linux分发版上做相同的事情。

?

?

我从哪儿开始?

  如果服务器已经有了一个公有IP,你会希望立即锁定 root 访问。事实上,你得锁定整个ssh访问,并确保只有你可以访问。增加一个新用户,把它加入admin组(在/etc/sudoers预配置以拥有sudo访问权限)。

?

$ 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,并且在服务器上禁用讨厌的密码验证。

?

?

$ mkdir ~/.ssh
$ echo "ssh-rsa [your public key]" > ~/.ssh/authorized_keys

?/etc/ssh/sshd_config

?

?

PermitRootLogin no
PermitEmptyPasswords no
PasswordAuthentication no
AllowUsers spenserj

?重新加载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

?/etc/iptables/rules

?

?