Ubuntu SSH 服务配置指南:从安装到安全加固
Ubuntu SSH Service Configuration Guide: From Installation to Security Hardening
SSH(Secure Shell)是一种网络协议,用于安全地远程访问和管理 Linux 系统。Ubuntu 默认可能没有安装 SSH 服务器,需要手动安装和配置。本文详细介绍如何在 Ubuntu 上安装、配置和加固 SSH 服务,包括基本使用、密钥认证、安全配置和故障排查等完整内容。
一、SSH 简介 SSH(Secure Shell)是一种加密的网络传输协议,可以在不安全的网络中为网络服务提供安全的传输环境。SSH 最常见的用途是远程登录系统,但也可以用于端口转发、文件传输等。
1.1 SSH 组件 SSH 分为两个主要组件:
openssh-client :SSH 客户端,用于连接到远程服务器
openssh-server :SSH 服务器,用于接受远程连接
二、安装 SSH 2.1 安装 SSH 客户端 如果只需要连接到其他服务器,只需安装客户端:
1 2 3 4 5 6 7 8 sudo apt-get updatesudo apt-get install -y openssh-clientssh -V
2.2 安装 SSH 服务器 如果需要允许其他机器连接到本机,需要安装服务器:
1 2 3 4 5 6 7 8 sudo apt-get updatesudo apt-get install -y openssh-serverssh -V
2.3 检查 SSH 服务状态 1 2 3 4 5 6 7 8 9 10 sudo systemctl status sshsudo systemctl status sshdps -e | grep ssh
三、SSH 服务管理 3.1 启动和停止服务 1 2 3 4 5 6 7 8 9 10 11 sudo systemctl start sshsudo systemctl stop sshsudo systemctl restart sshsudo systemctl reload ssh
3.2 设置开机自启 1 2 3 4 5 6 7 8 sudo systemctl enable sshsudo systemctl disable sshsudo systemctl is-enabled ssh
3.3 使用 service 命令(旧版本 Ubuntu) 对于较旧版本的 Ubuntu,可以使用 service 命令:
1 2 3 4 5 6 7 sudo service ssh startsudo service ssh stopsudo service ssh restartsudo service ssh status
四、SSH 配置文件 4.1 配置文件位置 SSH 服务器的主配置文件位于:
SSH 客户端的配置文件位于:
1 2 ~/.ssh/config /etc/ssh/ssh_config
4.2 查看和编辑配置 1 2 3 4 5 6 7 8 sudo nano /etc/ssh/sshd_configsudo vim /etc/ssh/sshd_confignano ~/.ssh/config
4.3 测试配置文件 修改配置后,务必测试配置文件语法:
五、基本使用 5.1 基本连接 1 2 3 4 5 6 7 8 9 10 11 12 ssh username@hostname ssh username@ip_address ssh root@192.168.1.100 ssh user@example.com ssh -p 2222 username@hostname
5.2 首次连接 首次连接到服务器时,会提示确认服务器指纹:
1 2 3 The authenticity of host '192.168.1.100 (192.168.1.100)' can't be established. ECDSA key fingerprint is SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
输入 yes 后,服务器指纹会被保存到 ~/.ssh/known_hosts 文件中。
5.3 使用别名简化连接 方法一:使用 SSH 配置文件 创建或编辑 ~/.ssh/config 文件:
添加以下内容:
1 2 3 4 5 6 7 8 9 10 11 Host dev-server HostName 192.168.1.100 User root Port 22 IdentityFile ~/.ssh/id_rsa Host production HostName example.com User deploy Port 2222 IdentityFile ~/.ssh/production_key
然后就可以使用别名连接:
1 2 ssh dev-server ssh production
方法二:使用 Shell 别名 在 ~/.bashrc 或 ~/.zshrc 中添加:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 nano ~/.bashrc nano ~/.zshrc alias login-dev="ssh root@192.168.1.100" alias login-prod="ssh deploy@example.com" source ~/.bashrcsource ~/.zshrclogin-dev login-prod
5.4 执行远程命令 1 2 3 4 5 6 7 8 9 10 11 ssh username@hostname "command" ssh root@192.168.1.100 "date" ssh root@192.168.1.100 "df -h" ssh username@hostname "command1 && command2"
5.5 文件传输 使用 SCP 1 2 3 4 5 6 7 8 scp local_file.txt username@hostname:/remote/path/ scp username@hostname:/remote/path/file.txt ./ scp -r local_directory username@hostname:/remote/path/
使用 SFTP 1 2 3 4 5 6 7 8 9 10 11 sftp username@hostname put local_file.txt get remote_file.txt ls lls cd remote_directory lcd local_directory exit
使用 rsync 1 2 3 4 5 rsync -avz local_directory/ username@hostname:/remote/path/ rsync -avz username@hostname:/remote/path/ local_directory/
六、SSH 密钥认证 6.1 生成 SSH 密钥对 1 2 3 4 5 6 7 8 ssh-keygen -t rsa -b 4096 -C "your_email@example.com" ssh-keygen -t ed25519 -C "your_email@example.com" ssh-keygen -t rsa -b 4096 -f ~/.ssh/my_key -C "your_email@example.com"
生成过程中会提示:
密钥保存位置(默认 ~/.ssh/id_rsa)
密码短语(passphrase,可选但推荐)
6.2 复制公钥到服务器 方法一:使用 ssh-copy-id(推荐) 1 2 3 4 5 6 7 8 ssh-copy-id username@hostname ssh-copy-id -i ~/.ssh/my_key.pub username@hostname ssh-copy-id -p 2222 username@hostname
方法二:手动复制 1 2 3 4 5 6 7 8 9 10 11 12 13 cat ~/.ssh/id_rsa.pubssh username@hostname mkdir -p ~/.sshchmod 700 ~/.sshecho "your_public_key" >> ~/.ssh/authorized_keyschmod 600 ~/.ssh/authorized_keys
6.3 使用密钥登录 配置好密钥后,可以直接使用密钥登录:
1 2 3 4 5 ssh username@hostname ssh -i ~/.ssh/my_key username@hostname
6.4 配置 SSH Agent 1 2 3 4 5 6 7 8 9 10 11 eval "$(ssh-agent -s) " ssh-add ~/.ssh/id_rsa ssh-add -l ssh-add -D
在 ~/.bashrc 或 ~/.zshrc 中添加自动启动 SSH Agent:
1 2 3 4 5 if [ -z "$SSH_AUTH_SOCK " ]; then eval "$(ssh-agent -s) " ssh-add ~/.ssh/id_rsa 2>/dev/null fi
七、SSH 安全配置 7.1 基本安全设置 编辑 /etc/ssh/sshd_config:
1 sudo nano /etc/ssh/sshd_config
推荐的安全配置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 PermitRootLogin no PermitRootLogin prohibit-password AllowUsers username1 username2 AllowGroups sshusers DenyUsers baduser DenyGroups badgroup Port 2222 PasswordAuthentication no PubkeyAuthentication yes MaxAuthTries 3 ClientAliveInterval 300 ClientAliveCountMax 2 PermitEmptyPasswords no X11Forwarding no AllowTcpForwarding no
7.2 应用配置更改 1 2 3 4 5 6 7 8 sudo sshd -tsudo systemctl reload sshsudo systemctl restart ssh
7.3 使用 fail2ban 防止暴力破解 1 2 3 4 5 6 7 8 9 sudo apt-get updatesudo apt-get install -y fail2bansudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.localsudo nano /etc/fail2ban/jail.local
在 [sshd] 部分配置:
1 2 3 4 5 6 7 [sshd] enabled = true port = sshlogpath = /var/log/auth.logmaxretry = 3 bantime = 3600 findtime = 600
1 2 3 4 5 6 7 8 9 sudo systemctl start fail2bansudo systemctl enable fail2bansudo fail2ban-client status sshdsudo fail2ban-client status sshd
7.4 防火墙配置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 sudo apt-get install -y ufwsudo ufw allow 22/tcpsudo ufw allow 2222/tcpsudo ufw enable sudo ufw status
八、高级用法 8.1 SSH 端口转发 本地端口转发 1 2 3 4 ssh -L 8080:localhost:80 username@hostname
远程端口转发 1 2 ssh -R 8080:localhost:80 username@hostname
动态端口转发(SOCKS 代理) 1 2 3 4 ssh -D 1080 username@hostname
8.2 SSH 隧道 1 2 3 4 5 6 ssh -N -L 3306:localhost:3306 username@hostname
8.3 保持连接活跃 1 2 3 4 5 6 7 Host * ServerAliveInterval 60 ServerAliveCountMax 3 ssh -o ServerAliveInterval=60 username@hostname
8.4 压缩传输 1 2 3 4 5 6 ssh -C username@hostname Host * Compression yes
九、故障排查 9.1 无法连接到服务器 1 2 3 4 5 6 7 8 9 10 11 12 13 sudo systemctl status sshsudo netstat -tlnp | grep :22sudo ss -tlnp | grep :22sudo ufw statussudo tail -f /var/log/auth.log
9.2 连接超时 1 2 3 4 5 6 7 8 ssh -o ConnectTimeout=30 username@hostname ssh -v username@hostname
9.3 权限问题 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ls -la ~/.sshchmod 700 ~/.sshchmod 600 ~/.ssh/authorized_keyschmod 600 ~/.ssh/id_rsachmod 644 ~/.ssh/id_rsa.pub
9.4 密钥认证失败 1 2 3 4 5 6 7 8 9 sudo tail -f /var/log/auth.logssh -o PreferredAuthentications=password username@hostname
9.5 查看 SSH 连接信息 1 2 3 4 5 6 7 8 who w sudo netstat -tnpa | grep :22
十、最佳实践 10.1 安全建议
✅ 禁用 root 登录 :创建普通用户,使用 sudo 提升权限
✅ 使用密钥认证 :禁用密码认证,只使用密钥
✅ 更改默认端口 :减少自动化攻击
✅ 限制登录用户 :只允许必要的用户登录
✅ 使用 fail2ban :防止暴力破解攻击
✅ 定期更新系统 :保持系统和 SSH 软件最新
✅ 监控日志 :定期检查 /var/log/auth.log
✅ 使用强密码 :如果必须使用密码认证
10.2 性能优化 1 2 3 UseDNS no GSSAPIAuthentication no
10.3 备份配置 1 2 3 4 5 sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.backupcp -r ~/.ssh ~/.ssh.backup
十一、总结 通过本文的介绍,您已经了解了:
✅ SSH 安装 :客户端和服务器的安装方法
✅ 服务管理 :启动、停止、重启 SSH 服务
✅ 基本使用 :连接、执行命令、文件传输
✅ 密钥认证 :生成密钥、配置密钥登录
✅ 安全配置 :加固 SSH 服务器安全
✅ 高级用法 :端口转发、隧道、代理
✅ 故障排查 :常见问题和解决方法
SSH 是 Linux 系统管理的重要工具,正确配置和使用 SSH 可以大大提高工作效率和系统安全性。
十二、相关参考