Loading

Debian/Linux系统安全配置教程

  • 禁止root SSH登陆
  • 配置SSH Key
  • 配置iptables

当我们安装完Linux系统作为服务器后,总有一系列的安全配置需要进行。特别是当你的服务器Ip是对外网开放的话。全世界有很多不怀好意的人,不断试图穷举你的root密码,尝试登陆。那么为Linux服务器增加一些安全措施,是很有必要的。

本文读者需要有一定的linux基础,有一定的网络与英语基础。知道如何使用nano/vim编辑器。不过总体而言,本文是为初级用户编写。

系统更新

在配置前更新一下系统

apt update
apt upgrade -y

配置sudo

新增一个用户,用于替代root进行登陆

apt install sudo -y

#编辑sudo配置
visudo 
#确保包含以下内容
%sudo   ALL=(ALL:ALL) ALL

#添加用户
useradd -m -s /bin/bash youruser
#设置用户密码
passwd youruser

为新增的用户加入sudo用户组

usermod -aG sudo youruser

配置ssh配置,限制root账户的ssh登陆权限,然后重启ssh服务

nano /etc/ssh/sshd_config

#禁止root账户远程登录
PermitRootLogin no
#or
#禁止root用户使用密码远程登录
PermitRootLogin prohibit-password
service ssh restart

配置好后,需要确认:
1.你可以通过新增的用户通过ssh登陆服务器
2.可以通过sudo命令执行需要高权限的命令

如果希望切换到root账户,可以使用:

sudo -i

配置ssh key

配置ssh key后,可以通过公钥私钥的验证方法验证模式,验证用户身份。这样的验证方式更加安全,便捷。配置成功后,ssh登陆服务器无需再繁琐地输入密码。

windows客户机安装git for windows后运行git bash产生密钥,产生的key存放于~.ssh目录。
客户机生成/查看秘钥的命令:

#生成密钥对
ssh-keygen
#查看公钥
cat ~/.ssh/id_rsa.pub

将客户端的公钥,写入服务器的对应用户的~/.ssh/authorized_keys文件,如果有多个客户机,可以换行添加多个公钥。
服务器添加公钥命令:

mkdir .ssh && chmod 700 .ssh
touch .ssh/authorized_keys && chmod 600 .ssh/authorized_keys
cat > .ssh/authorized_keys << EOF
<粘贴id_rsa.pub秘钥内容>
EOF

注意:下述部分配置会禁止ssh密码登陆,请在重启服务前将key放到正确的位置。如果你不能使用秘钥登陆成功并成功使用sudo,建议在能够使用非ssh手段登陆服务器的情况下再进行尝试。

下述配置用于禁止密码登陆,仅用公钥验证登陆。

nano /etc/ssh/sshd_config

#完全禁止ssh密码登陆
PasswordAuthentication no

重启ssh服务,使配置生效

service ssh restart

iptables与ip6tables配置

更多关于iptables配置信息,可参考:

https://wiki.debian.org/iptables
https://wiki.debian.org/DebianFirewall

nano /etc/iptables.rules
*filter

# Allows all loopback (lo0) traffic and drop all traffic to 127/8 that doesn't use lo0
-A INPUT -i lo -j ACCEPT
-A INPUT ! -i lo -d 127.0.0.0/8 -j REJECT

# Accepts all established inbound connections
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# Allows all outbound traffic
# You could modify this to only allow certain traffic
-A OUTPUT -j ACCEPT

# Allows HTTP and HTTPS connections from anywhere (the normal ports for websites)
#-A INPUT -p tcp -s yourip --dport yourport -j ACCEPT
#-A INPUT -p tcp -s yourip -m state --state NEW  --dport yourport -j ACCEPT
#-A INPUT -p tcp --dport yourport -j ACCEPT
#-A INPUT -p udp --dport yourport -j ACCEPT

# Allows SSH connections 
# The --dport number is the same as in /etc/ssh/sshd_config
-A INPUT -p tcp -m state --state NEW --dport 22 -j ACCEPT

# Now you should read up on iptables rules and consider whether ssh access 
# for everyone is really desired. Most likely you will only allow access from certain IPs.

# Allow ping
#  note that blocking other types of icmp packets is considered a bad idea by some
#  remove -m icmp --icmp-type 8 from this line to allow all kinds of icmp:
#  https://security.stackexchange.com/questions/22711
-A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT

# log iptables denied calls (access via 'dmesg' command)
#-A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7

# Reject all other inbound - default deny unless explicitly allowed policy:
-A INPUT -j REJECT
-A FORWARD -j REJECT

COMMIT
nano /etc/ip6tables.rules
*filter

-P INPUT DROP
-P FORWARD DROP
-P OUTPUT ACCEPT

-A INPUT -i lo -j ACCEPT
-m state --state ESTABLISHED,RELATED -A INPUT -j ACCEPT
-A INPUT -p icmpv6 -j ACCEPT

COMMIT

使配置生效可使用命令

iptables-restore < /etc/iptables.rules
ip6tables-restore < /etc/ip6tables.rules

开机启动时,导入iptables规则

nano /etc/network/if-pre-up.d/iptables

#!/bin/bash
/sbin/iptables-restore < /etc/iptables.rules
/sbin/ip6tables-restore < /etc/ip6tables.rules

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

保存当前的iptables rules可使用命令

iptables-save > /etc/iptables.rules

查看防火墙规则

iptables -L -n -v
ip6tables -L -n -v

至此,安全配置已完成,你可以在上述配置中添加自己的防火墙规则,例如哪些Ip能够访问服务器,哪些端口能够开放。然后使用iptables-restore命令刷新这些防火墙规则。

posted @ 2018-10-28 20:45  wswind  阅读(1011)  评论(0编辑  收藏  举报