- 安装和配置ssh:
- 在Ubuntu上安装SSH服务:sudo apt update && sudo apt install openssh-server。
- 启动和检查SSH服务状态:sudo systemctl start ssh 和 sudo systemctl status ssh。
- 修改SSH端口以增强安全性:编辑 /etc/ssh/sshd_config 文件,例如添加 Port 2222 然后重启SSH服务:sudo systemctl restart ssh。
- 连接到远程服务器:
- 基本连接命令:ssh username@ip_address。
- 指定端口号连接:ssh -p port_number username@ip_address。
- 使用密钥登录以提高安全性:生成密钥对:ssh-keygen -t rsa,上传公钥到远程服务器:ssh-copy-id username@ip_address。
- SSH命令使用技巧:
-
使用 scp 进行安全的文件传输:
- 从本地复制到远程:scp /path/to/local/file username@ip_address:/path/to/remote/destination。
- 从远程复制到本地:scp username@ip_address:/path/to/remote/file /path/to/local/destination。
-
断开SSH连接:exit。
- 提高SSH连接的安全性和效率:
- 禁用root登录:在 /etc/ssh/sshd_config 中设置 PermitRootLogin no。
- 使用非标准端口:修改SSH配置文件中的端口设置,以减少被自动扫描的风险。
- 启用公钥认证并禁用密码认证:在SSH配置文件中设置 PubkeyAuthentication yes 和 PasswordAuthentication no。
- 定期更换SSH密钥:降低密钥被盗用的风险。
- 其他技巧:
- 使用 ufw 允许SSH流量:sudo ufw allow ssh。
- 查看SSH服务状态:sudo systemctl status ssh。
通过以上技巧,可以更高效和安全地使用Ubuntu的SSH命令行功能。