Ssh
打开
sudo apt update
sudo apt install openssh-server
启动
sudo systemctl start ssh
自动开启
sudo systemctl enable ssh
状态
sudo systemctl status ssh
防火墙
sudo ufw allow ssh
#或者
sudo ufw allow 22/tcp
配置
/etc/ssh/sshd_config
- Port 2222
- PasswordAuthentication yes
连接
ssh user@192.168.1.100 -p 2222
密钥(可选)
如果已经在本地机器上生成了SSH密钥对,并且将公钥添加到了目标服务器的 ~/.ssh/authorized_keys 文件中,你可以使用密钥认证而不需要输入密码。
#生成SSH密钥对
ssh-keygen -t rsa -b 4096
#将公钥复制到目标服务器
ssh-copy-id -i ~/.ssh/id_rsa.pub user@192.168.1.100
#使用密钥连接
ssh user@192.168.1.100
权限一定要对
# 确保目录和文件权限正确
mkdir -p ~/.ssh
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
# 如果authorized_keys文件不存在,创建它
touch ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
禁止root登录
sudo nano /etc/ssh/sshd_config
# 完全禁止root登录(最安全)
# PermitRootLogin no
# 允许root使用密码登录(不安全)
# PermitRootLogin yes
# 只允许root使用密钥认证(推荐)
# PermitRootLogin prohibit-password
# 禁止 root 通过 SSH 登录
PermitRootLogin no
# 确保密码认证已正确设置(根据需要)
PasswordAuthentication yes # 或 no(如果使用密钥)
# 非root用户
# PasswordAuthentication no
# PubkeyAuthentication yes
# AllowUsers user1 user2 # 指定允许的用户
sudo systemctl restart ssh