sshd
ssh 连接时卡一段时间
解决方法:修改服务器 sshd 配置文件 /etc/ssh/sshd_config,增加(或修改为)以下几项配置:
GSSAPIAuthentication no
UseDNS no
允许 root 直接登录
/etc/ssh/sshd_config
PasswordAuthentication yes
PermitRootLogin yes
或者用 PermitRootLogin without-password 只允许 root 用户以公钥验证方式登录。修改后需要重启 ssh 服务生效。
Oracle Cloud 等服务商的 VPS 创建时设置公钥会在 /root/.ssh/authorized_keys 里写入
no-port-forwarding,no-agent-forwarding,no-X11-forwarding,command="echo 'Please login as the user \"opc\" rather than the user \"root\".';echo;sleep 10" ssh-rsa ooxxxxxx55524242442……
将 "ssh-rsa" 前面的所有内容删掉(同时修改 sshd_config )即可正常以 root 登录。
ssh
公钥登录
ssh-keygen
~/.ssh# ls -lha .
total 24K
drwx------ 2 root root 4.0K Jun 23 03:08 .
drwx------ 11 root root 4.0K Jun 23 03:08 ..
-rw------- 1 root root 2.5K Jun 23 03:08 authorized_keys
-rw------- 1 root root 2.6K Oct 28 2020 id_rsa
-rw-r--r-- 1 root root 566 Oct 28 2020 id_rsa.pub
-rw-r--r-- 1 root root 3.3K May 13 07:43 known_hosts
注意 ~/.ssh 目录以及其中的 id_rsa, authorized_keys 等文件权限必须与上面显示的一致。id_rsa.pub 是本机(当前用户)的 ssh公钥。
ssh-copy-id 自动将本机的 id_rsa.pub 添加到远程 ssh 服务器 ~/.ssh/authorized_keys 文件末尾。
ssh host command
在远程 ssh 服务器上直接运行命令(并将远程输出 output 到本地 stdout)。
ssh -t 192.168.1.1 'ls -lh'
- 如果在非交互(nont-interactive) shell 环境执行,必须配置好服务器的公钥登录。
- -tt : 在运行命令前分配伪终端会话。Force pseudo-terminal allocation。
- 如果是 -t (只有 1个 t),那么只有当 ssh 命令本身在终端(tty)里执行时才会为远程终端分配 tty(比如通过 systemd / cronjob 等方式自动运行的 ssh 命令本身没有 tty)。
- -t / -tt 主要用于 ssh -t host cmd 执行一些 screen 类型命令。
- OpenWrt 默认带的 Dropbear 里的 ssh client 只支持 -t, 不支持 -tt (当作 -t 对待)。如果需要可以从 opkg 安装完整版的 openssh-client。
- 测试发现,OpenWrt (默认带的 dropbear 里的) ssh host command, 如果加了 -t / -tt 参数,那么只能在交互式终端里执行。如果在 OpenWrt 的 crontab 里执行 ssh -t host command (服务器已经配置好了公钥登录),会直接失败,根本不会在远程执行命令(所以说 busybox / dropbear 这种精简版的东西全是坑!!)。
如果远程 ssh 服务器是 Linux (openssh server + bash 标准环境):
- ssh host command 对于远程服务器属于 "none-interactive (none-login) shell"。不管是否加 -t 或 -tt 参数。并且即使 ssh host command 在本地的 tty (交互式终端)里执行也是如此。
ssh client
options
-o ServerAliveInterval=10
: (ssh2 only) Keepalive秒数。 Sets a timeout interval in seconds after which if no data has been received from the server, ssh will send a message through the encrypted channel to request a response from the server. The default is 0, indicating that these messages will not be sent to the server. This option applies to protocol version 2 only.