Ubuntu 下 SSH 的安装与配置
1. 安装与运行
sudo apt update # 更新源
sudo apt install openssh-server # 安装 ssh 服务
sudo service ssh start # 启动服务
sudo service ssh stop # 停止服务
sudo service ssh restart # 重启服务
2. SSH 连接服务器
# 使用 ssh 登录远程服务器, -p port 指定端口(小写 p)
ssh [-p port] (username)@(ip|host)
# 使用 scp 复制文件或文件夹到指定服务器
# -P port 指定端口 P 为大写
# -p 保留原文件的修改时间、访问时间和访问权限(小写 p)
# -r 递归复制整个目录
scp [-P port] [-r|-p] source1 (username)@(ip|host):(path)
3. 配置文件
/etc/ssh/ssh_config(客户端配置)
| 选项 | 默认值 | 说明 |
|---|---|---|
| Host | * | “*” 表示所有计算机 |
| ForwardAgent | no | 是否经过验证代理转发给远程计算机 |
| ForwardX11 | no | X11 连接是否自动重定向 |
| RSAAuthentication | yes | 是否使用 RSA 算法进行安全验证 |
| PasswordAuthentication | yes | 是否使用口令验证 |
| BatchMode | no | 设为 yes 时禁止交互式输入口令(脚本场景) |
| CheckHostIP | yes | 是否查看 IP 地址防止 DNS 欺骗 |
| StrictHostKeyChecking | no | 是否拒绝主机密匙变化的连接 |
| IdentityFile | ~/.ssh/identity | 从哪个文件读取 RSA 验证标识 |
| Port | 22 | 连接远程主机的端口 |
/etc/ssh/sshd_config(服务端配置)
| 选项 | 默认值 | 说明 |
|---|---|---|
| Port | 22 | SSH 监听端口(可设多个) |
| Protocol | 2,1 | SSH 协议版本 |
| ListenAddress | 0.0.0.0 | 监听网卡 |
| PidFile | /var/run/sshd.pid | PID 文件位置 |
| LoginGraceTime | 600 | 登录超时(秒) |
| HostKey | /etc/ssh/ssh_host_rsa_key | SSHv2 RSA 私钥 |
| HostKey | /etc/ssh/ssh_host_dsa_key | SSHv2 DSA 私钥 |
| KeyRegenerationInterval | 3600 | v1 公钥重建间隔(秒) |
| ServerKeyBits | 768 | server key 长度 |
| SyslogFacility | AUTH | 日志类别 |
| LogLevel | INFO | 日志级别 |
| PermitRootLogin | no | 是否允许 root 登入(建议 no) |
| StrictModes | yes | 主机密匙改变后拒绝连接 |
| PubkeyAuthentication | yes | 是否允许 PublicKey(v2) |
| AuthorizedKeysFile | .ssh/authorized_keys | 公钥存放文件 |
| IgnoreRhosts | yes | 是否忽略 ~/.ssh/.rhosts |
| PasswordAuthentication | yes | 是否启用密码验证 |
| PermitEmptyPasswords | no | 是否允许空密码登入 |
| X11Forwarding | yes | 是否转发 X11 |
| PrintMotd | no | 登入后是否显示信息 |
| KeepAlive | yes | 发送 KeepAlive 保活 |
| MaxStartups | 10 | 同时允许多少未登入连接 |
| DenyUsers / AllowUsers | * | 黑白名单用户 |
建议在
sshd_config.d目录中创建自定义配置文件来配置,不直接修改默认配置文件。
4. 问题排查
no route to host:通常是防火墙没做好对应端口的配置,设置下防火墙即可。
参考:O无为学长O - 简书
原文链接:https://www.ssssmy.com/notes/ubuntu-xia-ssh-de-an-zhuang-yu-pei-zhi