什么是 OpenSSH?
OpenSSH(Open Secure Shell)是一套用于安全远程登录、命令执行和文件传输的开源工具集。它基于 SSH(Secure Shell)协议,广泛应用于 Linux、Unix、macOS 以及现代 Windows 系统中。
主要功能
- ssh:安全远程登录到其他计算机。
- scp / sftp:安全地复制或传输文件。
- ssh-keygen:生成用于身份验证的公私钥对。
- ssh-agent:管理私钥,避免重复输入密码。
安装与启动
Ubuntu/Debian:
sudo apt update && sudo apt install openssh-server
CentOS/RHEL:
sudo yum install openssh-server
启动服务:
sudo systemctl start sshd sudo systemctl enable sshd
安全配置建议
- 禁用 root 登录:
PermitRootLogin no - 使用密钥认证代替密码:
PasswordAuthentication no - 更改默认端口(可选):
Port 2222 - 限制允许登录的用户:
AllowUsers user1 user2
配置文件路径通常为:/etc/ssh/sshd_config,修改后需重启服务生效。
常见命令示例
- 远程登录:
ssh username@hostname - 使用指定端口:
ssh -p 2222 user@host - 生成密钥对:
ssh-keygen -t ed25519 - 复制公钥到服务器:
ssh-copy-id user@host