时隔4年,重装vps记录
操作系统选择Debian 11..上一次安装的Debian 9还有一个月就LTS结束了。
初始化新用户
先用root登录到服务器
1 | ssh [email protected]_address |
创建新用户
1 | adduser username |
为新用户增加sudo权限
1 | usermod -aG sudo username |
登录新用户到服务器
1 | ssh [email protected]_address |
为新用户配置ssh证书登录
生成ssh密钥
1 | ssh-keygen |
查看~/.ssh
目录
1 | ls -l ~/.ssh/ |
包含id_rsa
和id_rsa.pub
两个文件
将公钥id_rsa.pub
重命名为authorized_keys
1 | mv ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys |
将私钥id_rsa
打印后,复制并保存到本地
1 | cat ~/.ssh/id_rsa |
删除私钥
1 | rm ~/.ssh/id_rsa |
修复文件的权限,不然无法正常工作
1 | chmod 700 ~/.ssh |
尝试使用私钥登录..
为新用户配置免密sudo
尝试切换到root
1 | sudo su - root |
去掉sudo的密码,编辑/etc/sudoers
1 | %sudo ALL=(ALL:ALL) NOPASSWD: ALL |
关掉密码登录ssh和root登录ssh权限
编辑/etc/ssh/sshd_config
注释掉 PermitRootLogin yes
1 | #PermitRootLogin yes |
修改PasswordAuthentication
1 | PasswordAuthentication no |
重启ssh服务
1 | systemctl restart sshd.service |
更新系统
1 | apt update -y && apt upgrade -y |
安装设置防火墙
安装ufw
1 | apt update |
查看ufs的应用列表
1 | ufw app list |
运行OpenSSH,并激活防火墙。
1 | ufw allow OpenSSH |
查询ufw的状态
1 | ufw status numbered |
多余的行号,可用ufw delete 行号
删掉
安装v2ray
参考:https://www.v2fly.org/guide/install.html
1 | apt install v2ray |
- 修改配置文件:
/etc/v2ray/config.json
- 重启服务:
systemctl restart v2ray.service
- 验证端口开启:
netstat -an | grep LISTEN
安装nginx
1 | apt install nginx |
过程中可以关闭ufw便于调试ufw disable
配置仅允许cloudflare流量访问nginx
1 | curl -s https://www.cloudflare.com/ips-v4 -o /tmp/cf_ips |
查询ufw的状态
1 | ufw status numbered |
配置每周自动更新
crontab -e
加入
1 | 50 19 * * 3 /usr/bin/apt update -q -y >> /var/log/apt/automaticupdates.log |
为nginx配置ssl
1 | openssl genrsa -out null.key 2048 |
修改对应的nginx站点配置
1 | listen 80; < - 原本的配置 |
开启防火墙
1 | ufw allow "WWW Secure" |