要保障Ubuntu SSH的安全,可以采取以下措施:
/etc/ssh/sshd_config
文件,將端口設置為非標準端口(如2222),以減少被自動掃描工具發現的幾率。Port 2222
sudo systemctl restart sshd
/etc/ssh/sshd_config
中設置:PermitRootLogin no
sudo systemctl restart sshd
ssh-keygen -t rsa -b 4096
~/.ssh/authorized_keys
文件中:ssh-copy-id user@server_ip
/etc/ssh/sshd_config
文件,確保以下行沒有被注釋掉:PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
sudo systemctl restart sshd
sudo ufw allow 2222/tcp
sudo ufw enable
sudo apt get install fail2ban
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.locals
sudo nano /etc/fail2ban/jail.local
在 [ssh]
部分進行如下配置:[ssh]
enabled = true
port = 2222
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
bantime = 600
重啟Fail2Ban服務:sudo systemctl restart fail2ban
sudo apt update
sudo apt upgrade
通過以上步驟,可以顯著提高Ubuntu SSH的安全性,保護系統和數據不受未經授權的訪問和攻擊。