写在前面

  • 记录一下日常的操作
  • 其实有很多一键脚本
  • 聊以慰藉吧

客户端使用 secureCRT 和 finallshell,各有所长,粘贴命令的时候,finallshell 会错乱,有个啥方法处理的,忘记了,有知道留言告知,多谢

1、初始化升级

1
apt update && apt full-upgrade -y && apt autoremove && apt autoclean

这一步的主要目的是为了升级内核版本。

安装常用软件

1
apt install sudo htop git wget curl screen emacs vim podman ufw net-tools -y

安装常用软件,这里的 podman 和 docker 是类似的。

开启 bbr

1
2
3
4
echo "net.core.default_qdisc=fq" >> /etc/sysctl.conf
echo "net.ipv4.tcp_congestion_control=bbr" >> /etc/sysctl.conf

sysctl -p

2、安全性

添加非 root 用户(普通用户)

目的是为了避免直接使用 root 登录,避免使用密码登录,增加安全性

这里假如使用 ittinker 作为非 root 用户

1
2
3
adduser ittinker

usermod -aG sudo ittinker

使用密钥登录并禁止 22 端口

生成证书文件

mac 下直接用命令行就可以,使用 secureCRT 也可以生成(Tools - Create Public Key…),一路 continue,可以不设置 Passphrase,也就是不设置密码,格式选 OpenSSH Key format(new)。
它会生成 2 个文件,一个 pub 文件上传到服务器,一个在本地。

设置文件权限

为了使用它,需要先设置文件权限

1
2
3
4
5
mkdir /home/ittinker/.ssh
touch /home/ittinker/.ssh/authorized_keys
chown -R ittinker:ittinker /home/richardson/.ssh/
chmod 600 /home/ittinker/.ssh/authorized_keys
chmod 700 /home/ittinker/.ssh

上传证书文件

文件上传的方法很多,secureCRT 可以用 sftp(File-Connect FTP Session),finalshell 可以直接拖进去。目的是为了让 pub 文件的内容放到上面创建的 authorized_keys 里面去。

1
cat ~/ittinker.pub > ~/.ssh/authorized_keys

直接用 vim 编辑文件也是可以的,主要是担心换行符等问题,所以上传文件

设置仅允许证书登录,端口改为非 22

1
2
3
4
5
6
7
8
9
10
11
vim /etc/ssh/sshd_config

PubkeyAuthentication yes # yes表示允许密钥登陆
AuthorizedKeysFile .ssh/authorized_keys .ssh/authorized_keys2 # 指定密钥的文件位置
PasswordAuthentication no # 不允许使用密码登陆,等测试密钥登陆成功了再修改此条,以防无法登陆
Port 22000 # 把默认的22端口改了
PermitRootLogin no # 禁止root用户登陆
Protocol 2 # 使用更安全的 SSH2 协议
MaxAuthTries 3 # 最大认证尝试次数
ClientAliveInterval 300 # 客户端超时时间
ClientAliveCountMax 2 # 超时检测次数

安全起见,建议分步执行,密码登录和证书登录都保留,等测试没问题了再关闭

重启服务

1
systemctl restart ssh

设置防火墙

1
2
3
ufw allow 22000
ufw allow 80
ufw allow 443

启动防火墙(inactive 表示没启动)

1
2
3
4
root@racknerd-6c704ab:~# ufw enable
Command may disrupt existing ssh connections. Proceed with operation (y|n)? y
Firewall is active and enabled on system startup

检查防火墙状态

1
2
3
4
5
6
7
8
9
10
11
root@racknerd-7a8b74:~# ufw status
Status: active

To Action From
-- ------ ----
22000 ALLOW Anywhere
80 ALLOW Anywhere
443 ALLOW Anywhere
22000 (v6) ALLOW Anywhere (v6)
80 (v6) ALLOW Anywhere (v6)
443 (v6) ALLOW Anywhere (v6)

测试连通性

1
2
nc -v 192.210.228.220 80
nc -v 192.210.228.220 81

nc 命令一般不用,太麻烦了。用它是为了看看防火墙生效没,该关闭的端口都关闭了没

登录失败过多限制 ip

多次连接或登录失败的疑似入侵系统的行为时,就自动屏蔽其 IP 地址,禁止其再次尝试,以确保系统安全。

1
2
3
4
5
sudo apt-get -y install fail2ban 
cd /etc/fail2ban
sudo cp jail.conf jail.local

sudo systemctl restart fail2ban

仅允许 cf 访问

如果整站都在 CF 后面,安全起见可以只允许 CF 访问 80/443 端口,可以使用如下脚本配置。脚本会从 https://www.cloudflare.com/ips-v4 和 https://www.cloudflare.com/ips-v6 获取 CF 的 IP 范围,添加到 UFW 规则中并 reload UFW。还会配置每周一零点的定时任务,刷新 IP 段。

1
2
3
4
wget -O ~/.cloudflare-ufw.sh https://gist.githubusercontent.com/Xm798/12560579ce11f62027ea8da1fae37456/raw/b07ac8cfe09badf02fc70e2d8bc2da68cabbda50/cloudflare-ufw.sh
chmod +x ~/.cloudflare-ufw.sh
~/.cloudflare-ufw.sh
(crontab -l 2>/dev/null; echo "0 0 * * 1 /root/.cloudflare-ufw.sh > /dev/null 2>&1") | crontab -

3、安装常用软件