Linux系统设置
配置 SSH 服务
ssh-copy-id -i ~/.ssh/id_rsa.pub root@服务器ip
配置SSH配置文件/etc/ssh/sshd_config
sudo cp /etc/ssh/sshd_config ~ (备份,复原时使用)
sudo vi /etc/ssh/sshd_config
| 选项 | 含义 |
|---|---|
| Port | 默认22(可修改) |
| Protocol 2 | ssh 协议使用新版的 |
| PermitRootLogin no | 不允许 root 登录 |
| PermitEmptyPasswords no | 不允许空密码登录 |
| PasswordAuthentication no | 使用密码授权登录 |
| RSAAuthentication yes | 使用RSA算法进行安全验证 |
| PubkeyAuthentication yes | 允许公钥认证 |
| UseDNS no | 禁用DNS反向解析 会加快速度 |
| SyslogFacility AUTHPRIV | 记录用户登录信息 |
设置完成之后,保存退出重启SSHD
sudo service ssh restart
或者
sudo /etc/init.d/ssh restart
创建管理员账户(可选)
配置登录用户 ,假如用户名为deploy
useradd -d /home/deploy -s /bin/bash -m deploy
为新创建的用户,设置密码
passwd deplay
然后给deploy用户配置权限
sudo vi /etc/sudoers
找到下面的这一行
root ALL=(ALL:ALL) ALl
deplay ALL=(ALL) NOPASSWD: ALL
NOPASSWD表示,在切换sudo的时候不需要输入密码,根据自己喜好来设置
最后保存退出即可。
运行环境配置
检查服务器的区域设置。
locale
如果结果不是 en_US.UTF-8,建议都设成它。
sudo locale-gen en_US en_US.UTF-8 en_CA.UTF-8
sudo dpkg-reconfigure locales
然后,更新软件
sudo apt-get update
sudo apt-get upgrade
安全设置
ufw 防火墙
sudo ufw allow from [ip] to any port [端口] 仅特定ip可以访问此端口
sudo ufw allow 80 允许80端口访问
sudo ufw allow 443 允许443端口访问
sudo ufw disable 禁用ufw
sudo ufw enable 启用ufw
iptables
iptables -I INPUT -s *.*.*.* -j DROP 封停ip
iptables -D INPUT -s *.*.*.* -j DROP 解封ip
logwatch
- 安装logwatch apt-get install logwatch
- 编辑任务, 发送log到指定邮箱
vim /etc/cron.daily/00logwatch
#!/bin/bash
# Check if removed-but-not-purged
test -x /usr/share/logwatch/scripts/logwatch.pl || exit 0
# execute
/usr/sbin/logwatch --output mail --mailto [邮箱] --detail high
# Note: It's possible to force the recipient in above command
# Just pass --mailto address@a.com instead of --output mail
sendmail
安装 sendmail
sudo apt-get install sendmail
sudo apt-get install sendmail-cf
如果mail命令不存在,安装mailutils
sudo apt-get install mailutils
查看是否启动
ps aux | grep sendmail
编辑配置文件, sudo vim /etc/mail/sendmail.mc
修改监听地址, DAEMON_OPTIONS('Family=inet, Name=MTA-v4, Port=smtp, Addr=0.0.0.0')dnl</span>
如果提示 hostname 不合格, 修改它
查看 hostname `hostname`
修改 hostname `hostname [新值]`
发送邮件
mail [邮箱地址]
主题
内容
Ctrl-D结束
Fail2Ban
对于 Debian / Ubuntu,使用 APT-GET 命令或 APT 命令安装。
sudo apt install fail2ban
配置Fail2Ban
默认情况下,Fail2Ban 将所有配置文件保存在 /etc/fail2ban/ 目录中。 主配置文件是 jail.conf,它包含一组预定义的过滤器。 所以,不要编辑该文件,这是不可取的,因为只要有新的更新,配置就会重置为默认值。
只需在同一目录下创建一个名为 jail.local 的新配置文件,并根据您的意愿进行修改。
cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
默认情况下,大多数选项都已经配置的很完美了,如果要启用对任何特定 IP 的访问,则可以将 IP 地址添加到 ignoreip 区域,对于多个 IP 的情况,用空格隔开 IP 地址。
配置文件中的 DEFAULT 部分包含 Fail2Ban 遵循的基本规则集,您可以根据自己的意愿调整任何参数。
# nano /etc/fail2ban/jail.local
[DEFAULT]
ignoreip = 127.0.0.1/8 192.168.1.100/24
bantime = 600
findtime = 600
maxretry = 3
destemail = mhecy98@gmail.com
ignoreip:本部分允许我们列出 IP 地址列表,Fail2Ban 不会禁止与列表中的地址匹配的主机
bantime:主机被禁止的秒数
findtime:如果在最近 findtime 秒期间已经发生了 maxretry 次重试,则主机会被禁止
maxretry:是主机被禁止之前的失败次数
如何配置服务
Fail2Ban 带有一组预定义的过滤器,用于各种服务,如 ssh、apache、nginx、squid、named、mysql、nagios 等。 我们不希望对配置文件进行任何更改,只需在服务区域中添加 enabled = true这一行就可以启用任何服务。 禁用服务时将 true 改为 false 即可。
# SSH servers
[sshd]
enabled = true
port = ssh
logpath = %(sshd_log)s
backend = %(sshd_backend)s
enabled: 确定服务是打开还是关闭。
port:指明特定的服务。 如果使用默认端口,则服务名称可以放在这里。 如果使用非传统端口,则应该是端口号。
logpath:提供服务日志的位置
backend:指定用于获取文件修改的后端。
重启 Fail2Ban
进行更改后,重新启动 Fail2Ban 才能生效。
[For SysVinit Systems]
# service fail2ban restart
[For systemd Systems]
# systemctl restart fail2ban.service
验证 Fail2Ban iptables 规则
你可以使用下面的命令来确认是否在防火墙中成功添加了Fail2Ban iptables 规则。
# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
f2b-apache-auth tcp -- anywhere anywhere multiport dports http,https
f2b-sshd tcp -- anywhere anywhere multiport dports 1234
ACCEPT tcp -- anywhere anywhere tcp dpt:1234
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
Chain f2b-apache-auth (1 references)
target prot opt source destination
RETURN all -- anywhere anywhere
Chain f2b-sshd (1 references)
target prot opt source destination
RETURN all -- anywhere anywhere
查看启用的监狱列表,请运行以下命令。
# fail2ban-client status
Status
|- Number of jail: 2
`- Jail list: apache-auth, sshd
通过运行以下命令来获取禁止的 IP 地址。
# fail2ban-client status ssh
Status for the jail: ssh
|- filter
| |- File list: /var/log/auth.log
| |- Currently failed: 1
| `- Total failed: 3
`- action
|- Currently banned: 1
| `- IP list: 192.168.1.115
`- Total banned: 1
要从 Fail2Ban 中删除禁止的 IP 地址,请运行以下命令。
# fail2ban-client set ssh unbanip 192.168.1.115
Portsentry
下载源代码
weget https://www.itkylin.com/download/portsentry-1.2.tar.gz
解压、编译安装portsentry
tar -xpf portsentry-1.2.tar.gz
cd portsentry_beta
make linux
make install
编辑配置文件
sudo vim /usr/local/psionic/portsentry/portsentry.conf
找到RESOLVE_HOST,将其修改为RESOLVE_HOST = 0
启动PortSentry的TCP/UDP高级秘密扫描侦测模式
/usr/local/psionic/portsentry/portsentry -atcp
/usr/local/psionic/portsentry/portsentry -audp
设置随系统启动自动运行
执行如下命令
sudo vim /etc/rc.local
将下面这两条命令写进去
/usr/local/psionic/portsentry/portsentry -atcp
/usr/local/psionic/portsentry/portsentry -audp
查询阻止的恶意扫描
portsentry阻档的所有恶意扫描记录会实时写入到/var/log/syslog文件,所以我们只需要用tail命令就能实时的查询已阻止的恶意扫描,命令如下
tail -f /var/log/syslog
被阻止的IP将会放入/etc/hosts.deny,若是有误判,可以编辑此文件删除即可。你也可以编辑/usr/local/psionic/portsentry/portsentry.ignore此文件,将误判的IP或要忽略的IP加入portsentry.ignore文件,它就相当于是白名单。
现在,你会感觉你的服务器基本上已经安全了,可以惬意的喝杯咖啡吧!