当前位置:首页 > 服务器 > 正文

ssh服务器远程登录设置


SSH (Secure Shell) 是一种安全的网络协议,用于在客户端和服务器之间建立加密的通信隧道。 它通常用于通过不安全的网络(例如 Internet)远程登录到服务器。
设置 SSH 服务器
以下是在 Linux 或 macOS 系统上设置 SSH 服务器的步骤:
1. 安装 SSH 服务器包
shell
sudo apt install openssh-server # Ubuntu/Debian
sudo yum install openssh-server # CentOS/Red Hat
sudo brew install openssh # macOS
2. 启动 SSH 服务
shell
sudo systemctl start sshd # Ubuntu/Debian
sudo systemctl enable sshd # 设置开机自启动
3. 创建 SSH 密钥
如果您还没有 SSH 密钥,请使用以下命令创建它们:
shell
ssh-keygen -t rsa -b 4096
4. 配置 SSH 配置文件
编辑文件 /etc/ssh/sshd_config 并根据需要进行配置。 以下是一些常见选项:
Port: 更改 SSH 监听的端口。
PasswordAuthentication: 启用或禁用密码身份验证。 建议禁用此选项并使用密钥。
AllowUsers: 限制允许登录的用户名。
AuthorizedKeysFile: 指定授权 SSH 密钥的文件。
5. 重启 SSH 服务
shell
sudo systemctl restart sshd
从客户端连接到 SSH 服务器
1. 使用密钥认证
shell
ssh -i ~/.ssh/id_rsa username@server_address
2. 使用密码认证
shell
ssh username@server_address
安全提示
使用强密码或 SSH 密钥。
限制 SSH 访问特定用户或 IP 地址。
禁用密码身份验证。
定期更新 SSH 服务器软件。
启用日志记录以监视登录尝试。