在Ubuntu上配置SSH安全连接可以通过以下几个步骤来实现:
首先,确保你的Ubuntu系统是最新的:
sudo apt update && sudo apt upgrade -y
如果还没有安装OpenSSH服务器,可以使用以下命令进行安装:
sudo apt install openssh-server -y
编辑SSH配置文件 /etc/ssh/sshd_config
:
sudo nano /etc/ssh/sshd_config
找到并修改以下行,禁止root用户通过SSH直接登录:
PermitRootLogin no
为了增加安全性,可以更改SSH的默认端口(通常是22):
Port 2222
记得在防火墙中开放这个新端口。
确保以下行没有被注释掉,并且配置正确:
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
如果你更倾向于使用公钥认证,可以禁用密码认证:
PasswordAuthentication no
AllowUsers your_username
AllowUsers your_username@192.168.1.100
保存并退出编辑器后,重启SSH服务以应用更改:
sudo systemctl restart sshd
如果你使用的是UFW(Uncomplicated Firewall),可以添加规则来允许新的SSH端口:
sudo ufw allow 2222/tcp
sudo ufw reload
在本地机器上生成SSH密钥对(如果还没有):
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
按照提示操作,通常会生成 id_rsa
和 id_rsa.pub
文件。
将生成的公钥复制到远程服务器的 ~/.ssh/authorized_keys
文件中:
ssh-copy-id -p 2222 your_username@remote_host
或者手动复制:
cat ~/.ssh/id_rsa.pub | ssh -p 2222 your_username@remote_host 'mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys'
现在,你应该能够通过新的端口和安全设置连接到远程服务器:
ssh -p 2222 your_username@remote_host
通过以上步骤,你可以大大提高Ubuntu上SSH连接的安全性。