码迷,mamicode.com
首页 > 系统相关 > 详细

Linux 系统安全配置 Debian => 禁止root SSH登陆+配置SSH Key+配置iptables

时间:2018-10-28 21:58:32      阅读:307      评论:0      收藏:0      [点我收藏+]

标签:分配   lis   读者   ash   导入   res   passwd   real   不清楚   

Linux 系统安全配置 Debian => 禁止root SSH登陆+配置SSH Key+配置iptables

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

本文读者需要有一定的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

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生成,产生的key存放于C:\Users\..\.ssh目录。生成命令为:

ssh-keygen

将客户端的公钥,拷贝到服务器的对应用户的.ssh目录

scp ./id_rsa.pub  youruser@ip:~/.ssh/authorized_keys

如果有多个客户机,可以使用cat命令添加多个公钥

cat id_rsa.pub > ~/.ssh/authorized_keys

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

service sshd restart

iptables与ip6tables配置

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

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

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

Linux 系统安全配置 Debian => 禁止root SSH登陆+配置SSH Key+配置iptables

标签:分配   lis   读者   ash   导入   res   passwd   real   不清楚   

原文地址:https://www.cnblogs.com/wswind/p/9867044.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!