ssh的常用实现

  1. dropbear
  2. openssh

ssh的一些注意点

  1. ~/.ssh/authorized_keys只能被当前用户读取和写入。 chmod 600 /root/.ssh/authorized_keys

ssh的其他实现

  1. 使用golang实现ssh,sshd

常见问题

  1. ssh进docker容器中,docker环境变量全部丢失。
    使用如下命令 env $(cat /proc/1/environ |tr '\0' '\n' | xargs) bash , 开启子进程

ssh一些常见命令和用法

# 通过跳板机登录
ssh -t jumperserver "ssh -i ~/.ssh/id_rsa root@172.19.171.24"
# 通过多机跳板
ssh -J user@jump1:port1 -J user@jump2 user@targethost

sshconfig

在~/.ssh/config设置如下内容

Host 122server
  HostName 47.97.245.150
  User root
  IdentityFile ~/.ssh/id_rsa

Host 122a10
  HostName 172.19.174.158
  User root
  ForwardAgent no
  ProxyJump 122server



  #keep one connection in 72hour,72h只需要输入一次密码
  #ControlPersist 72h


  #这个配置在公网因为安全原因请谨慎关闭
  StrictHostKeyChecking=no
  TCPKeepAlive=yes
  CheckHostIP=no
  UserKnownHostsFile /dev/null
  GSSAPIAuthentication=no

   # 可选:压缩(节省带宽)
  Compression yes
  # 关键:启用保活
  ServerAliveInterval 30
  # 可选:复用连接(避免频繁握手)
  ControlMaster auto
  ControlPath /tmp/%r@%h:%p
  ControlPersist 600          # 断开后保持 socket 10 分钟

  # LocalForward <本地端口> <远程主机>:<远程端口>
  # LocalForward localhost:8082 127.0.0.1:8081
  # LocalForward localhost:8083 127.0.0.1:8081

  # RemoteForward 是把本地的端口映射到服务器上
  # RemoteForward [服务端ip地址:服务端端口] [本地服务的ip地址:本地服务的端口]
  # RemoteForward 需要在服务器的配置文件 /etc/ssh/sshd_config 上开启 GatewayPorts yes
  # RemoteForward 8080 localhost:80

  # 动态端口转发(类似 VPN 功能)
  # DynamicForward 1080
  # ProxyCommand ssh -i ~/.ssh/id_rsa -W %h:%p 126server

Host 10.5.*.*, 10.*.*.*
  Port 22
  User root
  ProxyJump 122server


include ssh_config.d/*.conf

sshuttle 穷人的vpn

pip3 install sshuttle
#  -l 是转发所有的ip都可以用这个地址出去,docker环境用这个
#        --latency-buffer-size 32768 是默认值,大概能支持300k左右速度,设置延迟控制中使用的缓冲区的大小。默认值为32768。更改 此选项允许在没有的延迟和带宽之间进行折衷
# --no-latency-control 完全不限速  需要关注ipv4保留地址。
sshuttle -v -l 127.0.0.1:12345  --dns --no-latency-control  --daemon  --exclude=10.0.0.0/8 --exclude=100.64.0.0/10 --exclude=127.0.0.0/8 --exclude=172.16.0.0/12 --exclude=192.168.0.0/16  -r root@8.219.194.200 0/0