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
useradd -m -s /bin/bash youruser
passwd youruser

为新增的用户增加sudo权限

visudo

youruser  ALL=(ALL:ALL) NOPASSWD:ALL

配置ssh配置,禁止root登陆,禁止空密码登陆,然后重启ssh服务

nano /etc/ssh/sshd_config

PermitRootLogin no
PermitEmptyPasswords no #默认为no

service sshd restart

配置好后,你可以通过新增的用户ssh登陆服务器,可以通过sudo命令执行需要高权限的命令。如果希望完全切换到root账户,可以使用

sudo -i

配置ssh key

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

注意:下述部分配置会禁止ssh密码登陆,请在重启服务前将key放到正确的位置。如果你不清楚自己在做什么,建议在你能够使用非ssh手段登陆服务器的情况下进行尝试。

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

nano /etc/ssh/sshd_config

PasswordAuthentication no
PubkeyAuthentication yes

windows客户机安装git for windows后运行git bash产生密钥,产生的key存放于~.ssh目录。

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

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

最后重启ssh服务,使配置生效

systemctl restart sshd

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命令刷新这些防火墙规则。

转载于:https://www.cnblogs.com/wswind/p/9867044.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值