zl程序教程

您现在的位置是:首页 >  其他

当前栏目

openssh-server & openssh-clients

ampserver openssh
2023-09-11 14:16:17 时间

 

/etc/ssh/sshd_config Server端配置

  1. ClientAliveInterval
    Sets a timeout interval in seconds after which if no data has been received from the client,sshd will send a message through the encrypted channel to request a response from the client
    The default is 0,indicating that these messages will not be sent to the client.

  2. ClientAliveCountMax
    Sets the number of client alive messages which may be sent without sshd(8) receiving any messages back from the client.  If this threshold is reached while client alive messages are being sent,sshd will disconnect the client, terminating the session.
    Default is 3.

  3. UseDNS

    是否开启反向解析,最好设为no
  4. Banner /etc/issue.net
    显示/etc/issue.net内容,输入password前显示,无转义

  5. PrintMotd yes
    打印 /etc/motd,登录后显示

  6. PrintLastLog
    interactvie登录时,打印最后一次的登录时间
  7. DenyUsers u1 u2 => AllowUsers => DenyGroups => AllowGroups
    这个指令后面跟着一串用空格分隔的组名列表(其中可以使用"*"和"?"通配符)。默认允许所有组登录。
    如果使用了这个指令,那么将仅允许这些组中的成员登录,而拒绝其它所有组。
    这里的"组"是指"主组"(primary group),也就是/etc/passwd文件中指定的组。
    这里只允许使用组的名字而不允许使用GID。相关的 allow/deny 指令按照下列顺序处理:

  8. GatewayPorts
    是否允许远程主机连接本地转发端口,默认为 no
    sshd默认将远程转发端口绑定到loopback,这样将阻止远程主机连接到转发端口,GatewatyPorts yes可以让sshd将远程端口转发绑定到非loopback地址,这样就允许远程主机连接至本机远程转发端口


  9. HostKey
    主机私钥文件(group & others应该无任何权限)否则会出现下图错误

     

     上图中私钥文件的指纹是一致的,只不过是权限原因

    默认为:
    HostKey /etc/ssh/ssh_host_rsa_key
    #HostKey /etc/ssh/ssh_host_dsa_key
    HostKey /etc/ssh/ssh_host_ecdsa_key
    HostKey /etc/ssh/ssh_host_ed25519_key

    这个文件的内容会出现在其他主机 ~/.ssh/known_hosts中

     

     而源主机的的公钥位于/etc/ssh/ssh_host_ecdsa_key.pub文件中,格式为

     

     /etc/ssh/下面还有对应的公钥,用于身份验证
    默认使用的公钥为ecdsa格式

    !!!!!!!!!!!!!!
    默认我们都会使用ssh-keygen生成用户专属密钥对于 ~/.ssh下,用作pubkey,ssh默认也是于 ~/.ssh下寻找私钥,但是我们是可以使用 /etc/ssh/ssh_host_*_key 这些主机自带的密钥对

    ssh-copy-id -i /etc/ssh/ssh_host_ecdsa_key.pub -p 22 root@172.16.3.9
    

     

    ssh -t -i /etc/ssh/ssh_host_ecdsa_key -l root 172.16.3.9

    在进行连接的时候需要显式 使用 -i 指出私钥

     

  10. GSSAPIAuthentication
    是否允许使用基于GSSAPI的用户认证

  11. ListenAddress
    ListenAddress IP:port

    未指定port,则使用Port指令的值,可指定多条ListenAddress指令

  12. LoginGraceTime
    限制用户连接后,必须于指定时限内认证成功,默认 2m

  13. MaxAuthTries
    每个连接最大允许认证次数
    MaxAuthTries 6
  14. MaxStartups
    最多允许保持几个未认证的连接,到达限制后,将不再接受新连接,除非先前的连接认证成功 or 超出LoginGraceTime的限制
    MaxStartups 10:30:100
  15. Subsystem
    配置子系统
    Subsystem sftp /usr/libexec/openssh/sftp-server
  16. PubkeyAuthentication yes
    启用公钥认证
  17. AuthorizedKeysFile .ssh/authorized_keys

 

 

 

 

保持连接设置:

ssh连接server后,如果一段时间不操作,再此进入Terminal会卡顿,后提示

Write failed: Broken pipe

解决办法:

  • client端的配置文件 ~/.ssh/config中加入
    ServerAliveInterval 60

     

  • server端的配置文件/etc/ssh/sshd_config中加入
    ClientAliveInterval 60

     

  • 当前连接临时使用
    ssh -o ServerAliveInterval=60 user@server

     

 

 

ssh 客户端选项:

  1. -l login_name 登录名
    ssh -l root host

    但是下面情况下,-l 无效

    ssh -l user@host

     

  2. -T Disable pseudo-terminal allocation,不分配tty
    默认登录interactive shell都会分配tty,使用此选项后,不会分配tty

     

     使用w等也不会看到此登录信息

  3.  

     -t Force pseudo-terminal allocation, This can be used to execute arbitrary screen-based programs on a remote machine, which can be very userful, e.g. when implementing menu services
    强制ssh连接时分配终端,主要用于ssh -l user host 'command'时

     

     

      不加 -t 时执行一些交互式命令会提示 TERM environment variable not set 或者 terminal unkown类似的提示

    ssh -l root 172.16.3.8 -t 'watch -cen 2 ip a'


    加入t即可正常运行

     

     

     

常见问题:

  1. Remote Host Identification Has Changed!
    server端因为某种原因(重装系统,IP地址变更,DHCP,虚拟机重建,Middle Man Attack)该IP地址的公钥改变,就会发出该提示
    ssh客户端不进行公钥确认
    修改/etc/ssh/ssh_config文件
    Host *
        StrictHostKeyChecking no

    命令行

    ssh -o StrictHostKeyChecking=no host

     

  2. 防止远程主机公钥改变导致ssh连接失败
    ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null host

     

  3. ssh_exchange_identification: Connection closed by remote host

    设置了/etc/hosts.deny,在/etc/host.allow放行相应主机

    还有一种情况是server达到了最大连接数限制

    sshd:10.8.0.0/255.255.255.0:allow
    all:116.236.134.26:allow
    all:172.16.0.0/255.240.0.0:allow
    sendmail:all:allow

     

 

ssh-keygen:

OpenSSH使用的公钥格式不同于OpenSSL生成的公钥

 

 

 

我们需要使用ssh-keygen的 -y选项来生成OpenSSH专用公钥

-y This option will read a private OpenSSH format file and print OpenSSH public key to stdout

 

 

openssh第二步身份验证的时候,可以不依靠id_rsa.pub等公钥,但是当id_rsa私钥目录存在id_rsa.pub公钥时会优先使用id_rsa.pub

如果id_rsa.pub的格式不对,也不会使用,如果格式正确会使用id_rsa.pub