SSH

/etc/ssh/sshd_config

UseDNS no
Port 6NNNN
ClientAliveInterval 60
ClientAliveCountMax 3
PermitRootLogin without-password
GatewayPorts clientspecified
AllowTcpForwarding yes

~/.ssh/config

GSSAPIAuthentication no
StrictHostKeyChecking no
Compression yes
ServerAliveInterval 60
ServerAliveCountMax 2

ssh proxy

参考 ProxyJump 选项,可以实现跳转,跳板机可以多个,ssh client 配置以本地为准,dns 以跳板机为准。

最佳实践:搞一台跳板机,并设置好 /etc/hosts,公钥分别部署到各机器,就可以通过跳板机中转登录到各机器。

优点:

限制 ip 登录

最灵活的方式是编辑 authorized_keys 中公钥的选项,参考 https://en.wikibooks.org/wiki/OpenSSH/Client_Configuration_Files,如:

from="*.2.3,111.111.*,10.0.0.0/8" ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDHWrD5V10X9SWTYnfIGQcglQmjiCK6KyMDKyTgwb3Vx+Dfpa5EiRFerAKzxM+IiL8lQmUVKzoRk5rl36iQMRCcN5QNQJCtuhJSos94OMWotfvspIqhIuRig8S7CI7SNgvPRkzup8a/J78EQAr40ukjKRlQzJkuWtD22RhqdpPZRrF/JNE+8m/+sHt0UyVJ3GD0dcBDO7vrfGukjSZ3XqM+CDi/5xBgOOG0PR3K0VWQjtuqQoJbkFBGAFwZqTFk32nF7RzE+JyyUE0TPnfrNaNBCFJQcr9jr5icaGgEY7J5R2B55FwwDAPK6qfJ1P4srEm9zcL1g3LtZjMwIm2hx9a1 tc@box

批量部署

管理很多服务器的各个用户的登录公钥时,可以考虑生成 shell 脚本再执行覆盖 authorized_keys 的方式,注意需要用 root 权限。 一般跳板机保留所有服务器的 root 权限,并执行这种批量部署脚本。 参考:https://github.com/lwzm/deploy-ssh-keys

sshfs

yum install sshfs
chmod +s $(which sshfs)  # 方便普通用户

# 挂载
sshfs HOST:DIR MNT

# 如果需要符号链接可用
sshfs HOST:DIR MNT -o follow_symlinks

# 卸载
fusermount -u test/mnt

macOS 也可以用,但要首先

brew cask install osxfuse

systemd

有的系统使用 systemd 来替代 sshd 的监听功能,也就是 sshd.socket,若还没摸透,可以考虑不用它。

systemctl mask --now sshd.socket
systemctl enable sshd.service
systemctl restart sshd.service

尝试使用 sshd.socket

systemctl enable sshd.socket
systemctl disable sshd.service
cd /etc/systemd/system && cp sockets.target.wants/sshd.socket .
# find and edit sshd.socket -> ListenStream=NN
systemctl daemon-reload
systemctl restart sshd.socket

参考:https://coreos.com/os/docs/latest/customizing-sshd.html

socks5

参见 shadowsocks