1. 更新软件包(必做)
sudo apt update
2. 安装 OpenSSH Server
sudo apt install -y openssh-server
3. 启动 SSH 并设置开机自启
# 立即启动 + 开机自启
sudo systemctl enable --now ssh
4. 检查是否正常运行
sudo systemctl status ssh
看到 active (running) 表示成功。
5. 放行防火墙(Ubuntu 默认有 ufw)
sudo ufw allow ssh
# 或写端口:sudo ufw allow 22/tcp
6. 查看本机 IP(用于远程连接)
hostname -I
# 或
ip a
拿到类似:192.168.x.x
7. 从另一台机器连接
ssh 你的用户名@上面的IP
# 例:ssh ubuntu@192.168.1.100
常用管理命令
sudo systemctl restart ssh # 重启
sudo systemctl stop ssh # 停止
sudo systemctl disable ssh # 取消开机自启
安全建议(可选)
sudo nano /etc/ssh/sshd_config
推荐改几项:
Port 2222 # 改端口(可选)
PermitRootLogin no # 禁止 root 直接登录
PasswordAuthentication no # 禁用密码(只用密钥)
改完重启:
sudo systemctl restart ssh
