ssh链接多路复用

发布时间 2023-12-27 22:18:39作者: ishmaelwanglin

前言

SSH是安全的加密协议,用于远程连接Linux服务器 ;SSH的默认端口是22,安全协议版本是SSH2 ;服务器端主要包含2个服务功能:SSH连接和SFTP服务 器;客户端包含ssh连接命令和远程拷贝scp命令等

为什么要用链接的多路复用?

当启用了多路复用:

  1. 首次SSH到服务器时,OpenSSH将启动主连接,OpenSSH创建一个UNIX 套接字(控制套接字,Control socket)维持与远程服务器的连接;
  2. 当下一次SSH到服务器时,OpenSSH将会使用已建立的控制套接字与服务器通信,而不再是重新建立新的TCP连接,减少了重复建立tcp连接的开
    销;

配置项

# ~/.ssh/config
Host * #
    Protocol 2  # ssh proto 2
    ServerAliveInterval 60 #
    ServerAliveCountMax 30 # ssh60sserversshdkeep-alive30server
    ControlMaster auto        #autoSSH
    ControlPersist 10m # ,SSH
    ControlPath ~/.ssh/connect-%r@%h:%p.socket  # sshunix socketrhp
    Compression no  #
    TCPKeepAlive no # TCPssh
    ForwardAgent yes #

这部分选项参数也可以通过-o 在连接时指定 或者直接修改/etc/ssh/ssh_config

sshd心跳:

服务端sshd如果要发起心跳是ClientAliveInterval ClientAliveCountMax

代理转发:

假设当前有两台服务器A和B,你从自己的电脑远程登录到服务器A,这时候想直接在服务器A上面登录到服务器B,但是服务器A上面 没有能够登录到服务器B的key,此时只要开启代理转发功能,将自己电脑的key跟随ssh过程一起发送到服务器A,就可以从服务器A 登录到服务器B了。

如何验证自己的配置是否生效呢?我们可以通过ssh-add -L命令来查看当前主机所拥有的key。

ControlPath的标识符

            %%    A literal ‘%’.
            %C    Shorthand for %l%h%p%r.
            %d    Local user's home directory.
            %h    The remote hostname.
            %i    The local user ID.
            %L    The local hostname.
            %l    The local hostname, including the domain name.
            %n    The original remote hostname, as given on the command line.
            %p    The remote port.
            %r    The remote username.
            %u    The local username.

Multiplexer (多路复用) 操作

# -O
# check
ssh -O check <user>@<host>
# forward
ssh -O forward <user>@<host>
# cancel
ssh -O cancel <user>@<host>
# exit MasterMaster
ssh -O exit <user>@<host>
# stop
ssh -O stop <user>@<host>
[root@centos7 ~]# ssh -O check root@10.211.55.50
Master running (pid=27828)
[root@centos7 ~]# ssh -O stop root@10.211.55.50
Stop listening request sent.
[root@centos7 ~]# ssh -O check root@10.211.55.50
Control socket connect(/root/.ssh/connection-root@10.211.55.50:22): No such file or directory
[root@centos7 ~]# ssh -O exit 10.211.55.50
Exit request sent.

使用SSH复用连接

建立复用链接

要建立多路复用连接,我们必须使用分别对应于ControlMaster和ControlPath的-M和-S参数。 因此,在启动SSH命令之后,我们将添加-M以开始使用多路 复用,并添加-S以及要使用的路径。

创建"Master"连接

ssh -M -N -f user@ServerIP [-o ServerAliveInterval=30]

复用"Master"连接

ssh -S ~/.ssh/connection-root@10.211.55.50:22 user@ServerIP <command>

终止复用SSH连接

要结束多路复用服务器,我们必须执行-O参数。 结束服务器1的示例如下:

ssh 10.211.55.50 -O stop -S ~/.ssh/connection-root@10.211.55.50:22

输入此命令后,会删除掉ControlPath即ssh master的socket文件,阻止新的多路复用连接,但对已建立的连接,不会被中断,直到 所有连接都完成为止。

还可以配置ControlPersist的值限制,以便在时间到之后连接自动结束连接。

SSH连接优化

  1. 使用特定的SSH身份验证方法
    加速ssh连接的另一种方法是对所有ssh连接使用给定的身份验证方法. 完成之后,使用上面的ssh_config文件(全局或用户特定)中的PreferredAuthentications指令。 这个指令定义了客户端应该尝试认证方法的顺序(你可以
    指定一个命令分隔列表来使用多个方法)。
    PreferredAuthentications=publickey
    SSH认证方法 或者,从命令行使用以下语法。
    # ssh -o "PreferredAuthentications=publickey" [email protected]
    如果您更喜欢被认为不安全的密码认证,请使用它。
    # ssh -o "PreferredAuthentications=password" [email protected]
  2. Client关闭GSSAPIAuthentication
    /etc/ssh/ssh_config
    GSSAPIAuthentication no
    StrictHostKeyChecking no
    
  3. Server端禁用DNS查找
    默认情况下,sshd守护程序查找远程主机名,并检查远程IP地址的解析主机名是否映射到完全相同的IP地址。 这可能导致连接建立或会话创建延迟。
    UseDNS指令控制上述功能; 要禁用它,请在/etc/ssh/sshd_config文件中搜索并取消注释。 如果没有设置,则添加该字段和值: UseDNS no,
    GSSAPIAuthentication 大多数情况下使用密码验证或者秘钥验证所以关闭GSSAPI验证即可.
  4. Server其他优化
    server/etc/hostsiphostname
    server/etc/resolv.conf
    server/etc/nsswitch.confhostshosts: files
    

六、ssh命令参数

-1ssh1
-2ssh2
-4IPv4
-6IPv6
-A
-a
-bip[-b bind_address]
-C ( stdin, stdout, stderr  X11  TCP/IP  ).  gzip(1) , ,  ``level  CompressionLevel .  modem ,  . .  Compression .
-c: cipher_spec , , .  Ciphers [-c cipher_spec]
-D: port  `` . ,  socket  port , , , .  SOCKS4 ,  SOCKS4 .  root . . [-D [bind_address:]port]
-e: ch|^ch|none  pty  escape  (: `~' ) . escape , escape  (`.' ) ,  control-Z (suspend),  escape  .  ``none
escape , .[-e escape_char]
-Fssh[-F configfile]
-fssh
-g
-I: smartcard_device (smartcard). , ,  RSA .
-i[-i identity_file]
-l[-l login_name]
-L: port:host:hostport (). ,  socket  port , , ,  host  hostport . .  root . IPv6 : port/host/hostport  [-L
[bind_address:]port:host:hostport]
-m: mac_spec, , ,  MAC() (message authentication code).  MACs .  [-m mac_spec]
-n:  stdin  /dev/null ( stdin ). .  X11 . , ssh -n shadows.cs.hut.fi emacs  shadows.cs.hut.fi  emacs,  X11 . . (
, ;  -f .)
-N
-p[-p port]
-q.
-R: port:host:hostport(). ,  socket  port , , ,  host  hostport . .  root  . IPv6 : port/host/hostport   [-R

[bind_address:]port:host:hostport]
        ECS10022
        ssh -NfR 10022:local.gaox.site:22 root@gaox.net -p22
-s: .  SSH2 ,  ( sftp)SSH. .
-t: . (screen-based), , .  -t ,  .
-T: .
-v: .  . , .  -v . .
-XX11
-xX11
-yX11
-O: ctl_cmd
-S: ctl_path
-W: host:port
-w: local_tun[:remote_tun]
-o:
-o option
AddressFamily
BatchMode
BindAddress
ChallengeResponseAuthentication
CheckHostIP
Cipher
Ciphers
ClearAllForwardings
Compression
CompressionLevel
ConnectionAttempts
ConnectTimeout
ControlMaster
ControlPath
DynamicForward
EscapeChar
ExitOnForwardFailure
ForwardAgent
ForwardX11
ForwardX11Trusted
GatewayPorts
GlobalKnownHostsFile
GSSAPIAuthentication
GSSAPIDelegateCredentials
HashKnownHosts
Host
HostbasedAuthentication
HostKeyAlgorithms
HostKeyAlias
HostName
IdentityFile
IdentitiesOnly
IPQoS
KbdInteractiveDevices
KexAlgorithms
LocalCommand
LocalForward
LogLevel
MACs
NoHostAuthenticationForLocalhost
NumberOfPasswordPrompts
PasswordAuthentication
PermitLocalCommand
PKCS11Provider
Port
PreferredAuthentications
Protocol
ProxyCommand
PubkeyAuthentication
RekeyLimit
RemoteForward
RequestTTY
RhostsRSAAuthentication
RSAAuthentication
SendEnv
ServerAliveInterval
ServerAliveCountMax
StrictHostKeyChecking
TCPKeepAlive
Tunnel
TunnelDevice
UsePrivilegedPort
User
UserKnownHostsFile
VerifyHostKeyDNS
VisualHostKey

XAuthLocation
ssh sftp
       1.WindowLinux
         wincp  filezip
         sftp -->ssh
        samba
       2.sftp
         sftp -oPort=22 root@192.168.25.137
         put /etc/hosts /tmp
         get /etc/hosts /home/omd

密码自动输入组件sshpass

使用该软件可以自动输入server的密码
sh -p password -f filename
example: sshpass -p "password" ssh user@serverIP command

ssh链接多路复用+ sshpass即形成了 批量 快速连接servers的方式,也是ansible采用的方法

ssh服务安全

1、用密钥登录,不用密码登陆
2、牤牛阵法:解决SSH安全问题
- 防火墙封闭SSH,指定源IP限制(局域网、信任公网)
- 开启SSH只监听本地内网IP(ListenAddress 172.16.1.61)
3、尽量不给服务器外网IP
4、最小化(软件安装-授权)
5、给系统的重要文件或命令做一个指纹
6、给他锁上 chattr +i +a