ubuntu 22.04 安全加固
检查端口被占用解决
lsof -i :3000
kill -9 ididid
ssh访问控制,多次失败登录即封掉IP,防止暴力破解
/var/log/auth.log # for ubuntu
https://www.cnblogs.com/panblack/p/secure_ssh_auto_block.html
nginx 屏蔽ip
https://www.cnblogs.com/fuanyu/p/14601836.html
cd /etc/nginx
touch ip.black
ip.black
deny 192.168.41.218;
deny 192.168.41.217;
清除指令历史记录
rm -rf ~/.bash_history
history -c
查看内存使用情况
free -h
防火墙设置
ufw
- 先查看列表
ufw app list
- 开启防火墙
ufw enable
ufw reset # 重置防火墙
- 开启指定服务
# 比如开启OpenSSH
ufw allow OpenSSH # 默认开启的是22端口
# 开启nginx http
ufw allow "Nginx HTTP"
ufw status # 查看状态
注意: 修改ssh端口后,需要执行ufw allow xxx/tcp
!!!!!!!!!!!!
注意: allow OpenSSH 或修改ssh端口 都要执行 service ssh restart
!!!!!!!!!!!!
SSH
apt install openssh-server 查看日志:
cat /var/log/auth.log
限制ssh root登录
- 先创建非root账号
adduser testuser
usermod -aG sudo testuser
passwd root # for edit root account password
su - username # for switch to normal account
userdel testuser # for delete account
- 修改配置
vi /etc/ssh/sshd_config # 此文件同时可以修改ssh端口
此文件同时可以修改ssh 端口
注释掉:
PermitRootLogin prohibit-password
改成
PermitRootLogin no
注意: 需要重启ssh service ssh restart
!!!!!!!
使用密钥登录(并禁止密码登录)
要使用 SSH 建立安全连接,您需要生成一对公钥和私钥。它们协同工作来验证和加密您与远程服务器的通信。
有两种常用的生成认证密钥的算法:
- RSA – SSH RSA 密钥被认为是高度安全的,因为它通常具有较大的密钥大小,通常为 2048 或 4096 位。它还与旧操作系统更兼容。
ssh-keygen -t rsa -b 4096
# 一路回车即可
注意:这会冲掉您之前的git ssh!!!
- Ed25519 – 一种更现代的算法,具有较小的 256 位标准密钥大小。由于其强大的加密特性,它与 RSA 密钥一样安全、高效。兼容性较低,但较新的操作系统支持它。
ssh-keygen -t ed25519
# 一路回车即可
本地计算机创建密钥对
见上面的两种方式
上传生成的公钥到服务器上
- RSA 方式
ssh-copy-id -i ~/.ssh/id_rsa.pub user@host-ip-address
- Ed25519 方式
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@host-ip-address
# 或者指定端口
ssh-copy-id -p 12345 -i ~/.ssh/id_ed25519.pub user@host-ip-address
提示如下:
验证:通过ssh命令即可直接免密登录
上传的公钥在这里:
也可以把密码登录方式禁用 (虽然安全,但是请慎用)
风险就是你本机的私钥不要变更,否则你将无法登录到服务器
vi /etc/ssh/sshd_config
# PasswordAuthentication no
记得重启ssh
sudo systemctl restart ssh
登陆失败锁定 (慎用,操作不当可能自己也登陆不了)
登录失败超过一定次数,限制登录 确认faillock是否存在,使用:
ls /lib/x86_64-linux-gnu/security/pam_faillock.so
- 客户端登录
vi /etc/pam.d/login
第2行添加
auth required pam_faillock.so preauth silent audit deny=5 unlock_time=130
auth [default=die] pam_faillock.so authfail deny=5 unlock_time=130
auth sufficient pam_faillock.so authsucc unlock_time=130
# even_deny_root 也限制root用户;
# deny 设置普通用户和root用户连续错误登陆的最大次数,超过最大次数,则锁定该用户;
# unlock_time 设定普通用户锁定后,多少时间后解锁,单位是秒;
# root_unlock_time 设定root用户锁定后,多少时间后解锁,单位是秒;
- ssh密码登录
vi /etc/pam.d/sshd
第2行添加
auth required pam_faillock.so preauth silent audit deny=5 unlock_time=130
auth [default=die] pam_faillock.so authfail deny=5 unlock_time=130
auth sufficient pam_faillock.so authsucc unlock_time=130
# even_deny_root 也限制root用户;
# deny 设置普通用户和root用户连续错误登陆的最大次数,超过最大次数,则锁定该用户;
# unlock_time 设定普通用户锁定后,多少时间后解锁,单位是秒;
# root_unlock_time 设定root用户锁定后,多少时间后解锁,单位是秒;
重启sshd
systemctl restart sshd.service
# or
# service ssh restart
- 其他命令
service ssh status
sudo pam_tally2 --user admin # 查看admin用户登录失败次数
sudo pam_tally2 --user admin --reset # 解锁admin用户
mysql 配置
配置密码/配置远程权限
先配置mysql和root密码 https://www.digitalocean.com/community/tutorials/how-to-install-mysql-on-ubuntu-22-04#step-2-configuring-mysql
再配置远程权限 https://www.digitalocean.com/community/tutorials/how-to-allow-remote-access-to-mysql
sudo mysql_secure_installation
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'newpassword';
# mysql -u root -p # for connection
# 远程连mysql
CREATE USER 'sammy'@'37.1.210.141' IDENTIFIED BY 'newpassword';
GRANT CREATE, ALTER, DROP, INSERT, UPDATE, DELETE, SELECT, REFERENCES, RELOAD on *.* TO 'sammy'@'37.1.210.141' WITH GRANT OPTION;
update user set host = '%' where user = 'root' and host = 'localhost';
FLUSH PRIVILEGES;
exit
vi /etc/mysql/mysql.conf.d/mysqld.cnf
# 此文件也可以修改端口
# 将bind-address = 127.0.0.1
# 改成
# bind-address = 0.0.0.0
# performance_schema = off # 可减少服务器内存消耗
sudo systemctl restart mysql
sudo ufw allow 3306
ufw status
# test connect outside
mysql -u root -h 37.1.210.141 -p
mysql -u root -h 37.1.210.141 -P3063 -p # 如果修改了端口需要指定,比如3063
sudo ufw allow 3063 # 开启3063端口
ufw deny 3306 # 禁用3306端口
service ssh restart