1. 设置 root 新密码
设置复杂密码,可以使用密码生成器生成一个复杂密码1
# passwd root
2. 创建普通用户并设置用户密码1
# adduser {username}
3. 添加普通用户到 sudoers
组
执行 visudo
编辑 /etc/sudoers
文件1
2# apt-get install sudo
# visudo
添加 {username} ALL=(ALL:ALL) ALL
到 root ALL=(ALL:ALL) ALL
的下一行
保存并退出,切换到新用户使用 sudo
命令执行特权操作1
2$ su {username}
$ sudo iptables -L
4. 使用公钥登录
本地生成 RSA 密钥对1
ssh-keygen -b 4096 -t rsa
通过 ssh-copy-id
复制公钥到远程服务器 authorized_keys 文件里面1
ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
接下来输入用户密码,密码正确会看到类似以下提示:1
2
3
4Number of key(s) added: 1
Now try logging into the machine, with: "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.
再次登录远程服务器可以看到复制的公钥1
2$ ssh {username}@your-server-ip
$ cat ~/.ssh/authorized_keys
5. 配置 SSH
修改 ssh 端口1
2# vi /etc/ssh/sshd_config
Port 22 > Port 26530
禁止 root 用户通过终端登录1
2# vi /etc/ssh/sshd_config
PermitRootLogin yes > PermitRootLogin no
禁止使用密码登录1
2# vi /etc/ssh/sshd_config
PasswordAuthentication yes > PasswordAuthentication no
使用 SSH Protocol 21
2# vi /etc/ssh/sshd_config
Protocol 2
限制可登录用户1
2# vi /etc/ssh/sshd_config
AllowUsers {username}
不允许空闲会话,设置空闲会话退出时间区间1
2
3# vi /etc/ssh/sshd_config
ClientAliveInterval 600 # (Set to 600 seconds = 10 minutes)
ClientAliveCountMax 0
禁止基于主机认证1
2# vi /etc/ssh/sshd_config
HostbasedAuthentication no
禁止用户 ~/.rhosts
文件1
2# vi /etc/ssh/sshd_config
IgnoreRhosts yes
禁止空密码登录1
2# vi /etc/ssh/sshd_config
PermitEmptyPasswords no
删除 rlogin 和 rsh 二进制文件,软链接到 SSH:1
2
3
4# find /usr -name rsh
/usr/bin/rsh
# rm -f /usr/bin/rsh
# ln -s /usr/bin/ssh /usr/bin/rsh
重启 ssh 服务1
sudo systemctl restart sshd
6. 配置防火墙1
sudo apt-get install ufw
设置 UFW 开机启动1
2sudo systemctl start ufw
sudo systemctl enable ufw
设置默认规则1
2sudo ufw default allow outgoing
sudo ufw default deny incoming
允许 ssh 端口1
sudo ufw allow 26530
防火墙初始状态可能是 Status: inactive
,这时可以手动开启防火墙1
sudo ufw enable
类似的,禁用 UFW’s 规则1
sudo ufw disable
开启 UFW 日志1
sudo ufw logging on
UFW 日志路径为 /var/log/ufw.log
7. 配置无人值守升级1
$ sudo apt-get install unattended-upgrades apt-listchanges
默认配置只自动升级安全更新
此外,还支持自动升级指定工具,例如:vim。详见1
# sudo vi /etc/apt/apt.conf.d/50unattended-upgrades
手动运行测试一遍1
sudo unattended-upgrade -d
8. Fail2Ban
使用 Fail2Ban 保护 SSH 登录1
$ sudo apt-get install fail2ban
安装完成 fail2ban-server 服务会自动启动。
Fail2ban 配置文件规则是 .local
优先于 .conf
,我们复制 .conf
文件为 .local
文件:1
2$ sudo cp /etc/fail2ban/fail2ban.conf /etc/fail2ban/fail2ban.local
$ sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
9. Logwatch 监控日志1
$ sudo apt-get install logwatch
Logwatch 支持发送日志到指定邮箱地址
配置1
$ sudo vi /usr/share/logwatch/default.conf/logwatch.conf
改 MailTo = root
为 MailTo = [email protected]
10. 设置时区和安装 NTP 服务
设置时区,在列表中选择:Asia -> 回车 -> Shanghai 回车即可。1
$ sudo dpkg-reconfigure tzdata
安装 ntp 服务1
$ sudo apt-get install ntp
配置 ntp,并替换 ntp 服务器1
2
3
4
5$ sudo vi /etc/ntp.conf
server 0.cn.pool.ntp.org
server 1.cn.pool.ntp.org
server 2.cn.pool.ntp.org
server 3.cn.pool.ntp.org
11. 加固共享内存1
sudo vi /etc/fstab
在最后一行添加 tmpfs /run/shm tmpfs defaults,noexec,nosuid 0 0
,重启服务器即可生效。
12. 加固网络层
防止传入数据包的源路由,以及记录异常 IP,sudo vi /etc/sysctl.conf
编辑内核参数配置文件,添加以下参数: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
35# IP Spoofing protection
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.rp_filter = 1
# Ignore ICMP broadcast requests
net.ipv4.icmp_echo_ignore_broadcasts = 1
# Disable source packet routing
net.ipv4.conf.all.accept_source_route = 0
net.ipv6.conf.all.accept_source_route = 0
net.ipv4.conf.default.accept_source_route = 0
net.ipv6.conf.default.accept_source_route = 0
# Ignore send redirects
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
# Block SYN attacks
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_syn_backlog = 2048
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 5
# Log Martians
net.ipv4.conf.all.log_martians = 1
net.ipv4.icmp_ignore_bogus_error_responses = 1
# Ignore ICMP redirects
net.ipv4.conf.all.accept_redirects = 0
net.ipv6.conf.all.accept_redirects = 0
net.ipv4.conf.default.accept_redirects = 0
net.ipv6.conf.default.accept_redirects = 0
# Ignore Directed pings
net.ipv4.icmp_echo_ignore_all = 1
保存文件,执行 sudo sysctl -p
使配置生效。
13. 预防 IP 欺骗
sudo vi /etc/host.conf
编辑 DNS 域名解析配置文件,你可以看到一行:1
multi on
改成以下:1
nospoof on
相关链接
(完)