使用Ubuntu进行SSH远程管理时,有一些技巧可以帮助你更高效、安全地管理服务器。以下是一些常用的技巧:
ssh-keygen -t rsa -b 4096
ssh-copy-id user@remote_host
/etc/ssh/sshd_config文件,找到并修改以下行:PasswordAuthentication no
然后重启SSH服务:sudo systemctl restart sshd
~/.ssh/config文件:Host myserver
HostName remote_host
User username
IdentityFile ~/.ssh/id_rsa
Port 22
这样你可以通过ssh myserver直接连接到服务器。本地端口转发:
ssh -L 8080:localhost:80 user@remote_host
这会将远程服务器的80端口转发到本地的8080端口。
远程端口转发:
ssh -R 8080:localhost:80 user@remote_host
这会将本地的80端口转发到远程服务器的8080端口。
~/.ssh/config中添加别名:Host myalias
HostName remote_host
User username
IdentityFile ~/.ssh/id_rsa
这样你可以通过ssh myalias连接到服务器。ServerAliveInterval:
在~/.ssh/config中添加:Host *
ServerAliveInterval 60
这会每60秒发送一个保持连接的信号,防止连接超时。eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa
ssh -J user@jump_host user@remote_host
这会先连接到jump_host,然后再从jump_host连接到remote_host。sshpass自动输入密码(不推荐用于生产环境,安全性较低):sshpass -p 'your_password' ssh user@remote_host
sudo tail -f /var/log/auth.log
Ansible、Puppet或Chef等工具来管理SSH配置和自动化任务。通过这些技巧,你可以更高效、安全地使用Ubuntu进行SSH远程管理。记得定期更新系统和软件,以确保安全。