一、vps基本信息
执行: curl -s ipinfo.io 命令,可以查看服务器ip、地区、ASN、时区等信息
root@localhost:~# curl -s ipinfo.io
结果如下:
root@localhost:~# curl -s ipinfo.io
{
"ip": "xxxx",
"city": "xxxxx",
"region": "xxx",
"country": "CN",
"loc": "xxxx",
"org": "ASxxxxxx",
"postal": "xxxxxxx",
"timezone": "Asia/Shanghai",
"readme": "https://ipinfo.io/missingauth"
}root@localhost:~#
二、更新补丁
Ubuntu执行:apt update && apt upgrade -y 命令更新补丁
安装unattended-upgrades:apt install unattended-upgrades -y && dpkg-reconfigure -plow
unattended-upgrades
重新连接服务器后,自动检查安全更新:systemctl status unattended-upgrades –no-pager
红帽系统执行:yum update -y
三、用户
一般情况下,不要使用root用户远程登录。使用普通用户登录,然后将普通用户加入sudo组(Ubuntu系统)或whell组(红帽系统)。
sudo 命令用于给普通用户提供额外的权限来执行指定的命令,语法格式为“sudo [参数] 命令”。
使用 sudo 命令能够给普通用户提供额外的权限来完成原本只有 root 管理员才能完成的任务
下面演示的是Ubuntu系统将用户添加到sudo组:
方法一:
创建普通用户:useradd 用户名。注意:由于没有添加参数,所以不会创建用户家目录和指定默认shell,所以加上参数-m和-s 指定默认shell,即:useradd -m -s /bin/bash 用户名。这样创建的用户才可以正常使用。
加入sudo组:usermod -aG sudo 用户名,参数-a追加用户组,-G组名
检查用户:id 用户名
查看sudo权限:sudo -l -U 用户名
例如:
root@localhost:~# useradd test
root@localhost:~# usermod -aG sudo test
root@localhost:~# id test
uid=1000(test) gid=1000(test) groups=1000(test),27(sudo)
root@localhost:~# sudo -l -U test
User test may run the following commands on localhost:
(ALL : ALL) ALL
root@localhost:~#
切换到test用户:
root@localhost:~# su - test
由于创建test用户的时候,没有创建家目录,提示如下:

输入exit,回到root用户,创建test用户家目录:/home/test
root@localhost:~# mkdir /home/test
root@localhost:~# chown test:test /home/test
root@localhost:~# chmod 755 /home/test
并指定默认shell为/bin/bash:
root@localhost:~# usermod -s /bin/bash test
重新切换到test用户:
root@localhost:~# su - test
test@localhost:~$
这样就正常了。所以,一般情况下,在创建用户的时候使用-m和-s参数比较方便。不用后续自行创建家目录和指定shell。
方法二:
创建用户的时候加入sudo组:useradd -G sudo 用户名
例如:
root@localhost:~# useradd -G sudo test01
root@localhost:~# id test01
uid=1001(test01) gid=1001(test01) groups=1001(test01),27(sudo)
root@localhost:~#
sudo配置文件:/etc/sudoers
对于创建新用户且需要加入sudo组的话,最好用以下命令:useradd -G sudo -m -s /bin/bash 用户名。例如:
root@localhost:~# useradd -G sudo -m -s /bin/bash test02
root@localhost:~# passwd test02 #设置test用户密码
New password:
Retype new password:
passwd: password updated successfully
root@localhost:~#
创建好用户后,设置用户密码。使用root用户登录执行:passwd 用户名,设置用户密码。或者使用命令:echo “密码” | passwd –stdin 用户名
不过此种方式会显示密码,安全性不高。
四、使用密钥登录vps服务器
五、查看运行中的服务
查看运作中的服务命令:systemctl –type=service –state=running
关闭并取消开机启动服务:systemctl disable –now 服务名称
六、安装fail2ban
Ubuntu安装fail2ban:apt install fail2ban -y
CentOS系统:yum install fail2ban -y
配置fail2ban:vim /etc/fail2ban/jail.d/sshd.local
内容如下:
[sshd]
enabled = true
backend = systemd
maxretry = 5 #10分钟内允许失败5次
findtime = 10m #10分钟
bantime = 1h #5次失败,禁用1小时
七、内核调优
内核参数文件:/etc/sysctl.conf
部分内核参数:
# 1. 虚拟内存:优先使用物理内存,90% 物理内存满后才用 Swap
vm.swappiness = 10
# 2. BBR 拥塞控制:提升高延迟/跨国网络吞吐量
net.core.default_qdisc = fq
net.ipv4.tcp_congestion_control = bbr
# 3. TCP 缓冲区优化:最大上限 4MB~8MB,平衡速度与内存占用,防止小内存爆机
net.ipv4.tcp_rmem = 4096 87380 4194304
net.ipv4.tcp_wmem = 4096 65536 4194304
net.core.rmem_max = 8388608
net.core.wmem_max = 8388608
# 4. 连接队列:设为 2048,兼容低配 CPU(1C/2C),防止高并发丢包
net.core.somaxconn = 2048
net.ipv4.tcp_max_syn_backlog = 2048
# 5. 连接回收与网络探测
net.ipv4.tcp_fin_timeout = 15
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_mtu_probing = 1
# 6. 系统安全与进程限制加固
kernel.pid_max = 65536
fs.protected_fifos = 1
fs.protected_hardlinks = 1
fs.protected_regular = 2
fs.protected_symlinks = 1
编辑配置文件保存后,执行命令:sysctl -p
其他参数解析:部分Linux内核参数 – 我的Linux