SSH是什么,为什么要用它
SSH(Secure Shell)是你远程管理Linux服务器的主要方式。简单说,就是通过加密通道在你的电脑上操作远程服务器的终端。
我管理的所有服务器,全部通过SSH连接。密码登录只在初次配置时用,之后全部切换成密钥认证,更安全也更方便。
基本连接方式
密码登录
最基础的连接方式:
1
2
3
4
5
6
7
8
| $ ssh [email protected]
The authenticity of host '203.0.113.10' can't be established.
ED25519 key fingerprint is SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '203.0.113.10' (ED25519) to the list of known hosts.
[email protected]'s password:
Welcome to Ubuntu 22.04.3 LTS
root@myserver:~#
|
第一次连接会提示确认指纹,输入yes后以后就不会再问了。
指定端口连接
如果服务器修改了默认SSH端口(强烈建议修改):
| 参数 | 作用 | 示例 |
|---|
-p | 指定端口 | -p 2222 |
-i | 指定密钥文件 | -i ~/.ssh/mykey |
-v | 调试模式 | 连接有问题时用 |
-o | 临时设置选项 | -o ConnectTimeout=5 |
密钥认证(强烈推荐)
密码登录有被暴力破解的风险。密钥认证既安全又免去每次输密码的麻烦。
第一步:生成密钥对
1
2
3
4
5
6
7
8
9
10
| # 在本地电脑执行(不是服务器上)
$ ssh-keygen -t ed25519 -C "my-server-key"
Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/user/.ssh/id_ed25519):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/user/.ssh/id_ed25519
Your public key has been saved in /home/user/.ssh/id_ed25519.pub
The key fingerprint is:
SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx my-server-key
|
提示:推荐使用ed25519算法,比传统的RSA更安全更高效。passphrase建议设一个,相当于给钥匙加了个保险柜。
第二步:上传公钥到服务器
1
2
3
4
5
6
| $ ssh-copy-id -i ~/.ssh/id_ed25519.pub [email protected]
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/user/.ssh/id_ed25519.pub"
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '[email protected]'"
and check to make sure that only the key(s) you wanted were added.
|
第三步:测试无密码登录
1
2
3
4
| $ ssh [email protected]
Welcome to Ubuntu 22.04.3 LTS
root@myserver:~#
# 直接就进来了,不需要输密码
|
第四步:禁用密码登录
确认密钥登录正常后,禁用密码登录:
1
| $ sudo vim /etc/ssh/sshd_config
|
修改以下配置:
1
2
3
| PasswordAuthentication no
PubkeyAuthentication yes
PermitRootLogin prohibit-password
|
重启SSH服务:
1
| $ sudo systemctl restart sshd
|
警告:修改SSH配置前,务必保持当前连接不断开,另开一个终端测试能否正常登录。否则一旦配置出错,你可能被锁在外面!
SSH Config配置文件
每次输IP和端口太麻烦?用config文件简化:
1
2
3
4
5
6
7
8
9
10
11
| Host myserver
HostName 203.0.113.10
User root
Port 2222
IdentityFile ~/.ssh/id_ed25519
Host webserver
HostName 198.51.100.20
User deploy
Port 22
IdentityFile ~/.ssh/id_ed25519
|
配置好之后,直接用别名连接:
1
2
3
4
5
| $ ssh myserver
root@myserver:~#
$ ssh webserver
deploy@webserver:~$
|
| Config选项 | 作用 | 建议值 |
|---|
HostName | 服务器IP或域名 | 按实际填写 |
User | 登录用户名 | 非root用户更安全 |
Port | SSH端口 | 建议改非22端口 |
IdentityFile | 私钥路径 | 不同服务器用不同密钥 |
ServerAliveInterval | 心跳间隔 | 60(防断连) |
ServerAliveCountMax | 最大重试次数 | 3 |
SSH端口转发
本地端口转发
把远程服务器的端口映射到本地,比如访问远程MySQL:
1
2
3
| # 将远程服务器的3306端口映射到本地3307
$ ssh -L 3307:localhost:3306 [email protected]
# 然后本地用 mysql -h 127.0.0.1 -P 3307 就能连上远程数据库
|
动态端口转发(SOCKS代理)
SCP和SFTP传文件
SCP传文件
常见问题排查
连接超时
1
2
3
4
5
6
7
| $ ssh -v [email protected]
# -v 参数显示详细日志,帮你定位问题
# 常见原因:
# 1. 防火墙没开SSH端口 → 检查 ufw/iptables
# 2. SSH服务没启动 → systemctl status sshd
# 3. IP地址错了 → 确认服务器IP
|
Permission denied
1
2
3
4
5
| # 检查密钥权限(这是最常见的坑)
$ chmod 700 ~/.ssh
$ chmod 600 ~/.ssh/id_ed25519
$ chmod 644 ~/.ssh/id_ed25519.pub
$ chmod 600 ~/.ssh/authorized_keys # 服务器上
|
连接经常断开
在~/.ssh/config中添加:
1
2
3
| Host *
ServerAliveInterval 60
ServerAliveCountMax 3
|
安全加固建议
- 修改默认端口:把22改成其他端口,减少扫描攻击
- 禁用密码登录:只允许密钥认证
- 禁止root直接登录:用普通用户登录后
sudo提权 - 安装fail2ban:自动封禁暴力破解IP
- 定期更新:
apt update && apt upgrade
关于防火墙的详细配置,可以参考Linux防火墙配置教程。想了解更多服务器安全和用户管理,推荐阅读Linux用户与权限管理。
总结
SSH是Linux运维的生命线。掌握密钥认证和config配置后,你的效率会提升一大截。我的工作流是:每台新服务器拿到手,第一件事就是配密钥、改端口、禁密码登录。养成这个习惯,能省去很多安全隐患。