x# 这里以 openEuler 22.03 (LTS-SP3) 升级为例tar -xf openssh-portable-V_9_9_P1.tar.gz -C /usr/local/cd /usr/local/openssh-portable-V*./configure --prefix=/usr/local/openssh # 本例无需升级 OpenSSLmake && make install# 修改启动项# vi /usr/lib/systemd/system/sshd.servicesshd=/usr/local/openssh/sbin/sshdsed -i 's#ExecStart=/usr/sbin/sshd \(.*\)#ExecStart='${sshd}' \1#' /usr/lib/systemd/system/sshd.service# 修改配置文件# vi /usr/local/openssh/etc/sshd_configsed -i 's/^#PermitRootLogin prohibit-password/PermitRootLogin yes/' /usr/local/openssh/etc/sshd_config# 重启systemctl daemon-reloadsystemctl restart sshdsystemctl status sshd/usr/local/openssh/bin/ssh -V
xxxxxxxxxx# 版本号ssh -Vssh user@hostnamessh user@hostname -p60022ssh -i i.pem user@hostname -p60022# 切换sudo、执行多条命令ssh -i i.pem root@10.37.176.121 "sudo -i sh -c 'cd /tmp && pwd'"
SSH 使用直接 TTY 访问来确保密码确实是由交互式键盘用户发出的,sshpass 提供了非交互式的方式来验证 SSH 会话。
sshpass 在一个专用 tty 中运行 SSH,让它误以为从交互用户那里获得了密码。
xxxxxxxxxxsshpass -p password ssh user@hostname -p60022
ssh 无密码登录要使用公钥与私钥。linux下可以用用ssh-keygen生成公钥/私钥对。
有机器A(192.168.1.155),B(192.168.1.181)。现想A通过ssh免密码登录到B。
1.在A机下生成公钥/私钥对。
xxxxxxxxxxssh-keygen -t rsa -P ''
-P表示密码,-P '' 就表示空密码,也可以不用-P参数,这样就要三车回车,用-P就一次回车。
这将生成一对密钥文件:~/.ssh/id_rsa(私钥)和 ~/.ssh/id_rsa.pub(公钥)。
2.使用 ssh-copy-id 将A机生成的公钥复制到远程主机的 ~/.ssh/authorized_keys 文件中。
执行该命令需要输入远程主机的密码。
xxxxxxxxxxssh-copy-id username@remote_hostssh-copy-id root@192.168.1.181
脚本中为了跳过交互模式,可以将远程主机的 SSH 主机密钥(Host Key)追加到本地的 ~/.ssh/known_hosts 文件中。
xxxxxxxxxxssh-keyscan -p 2222 -t rsa remote_host1 remote_host2ssh-keyscan remote_host | grep ssh-rsa >> ~/.ssh/known_hostsssh-keyscan 192.168.1.181 | grep ssh-rsa >> ~/.ssh/known_hosts
3.为了实现通过ssh登陆本机,将A机生成的公钥复制到本机的 ~/.ssh/authorized_keys 文件中。同样将 SSH 主机密钥追加到 known_hosts 文件中。
xxxxxxxxxxcat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keyschmod 600 ~/.ssh/authorized_keysssh-keyscan 192.168.1.155 | grep ssh-rsa >> ~/.ssh/known_hosts
4.A机登录B机。
xxxxxxxxxxssh username@remote_hostssh root@192.168.1.181ssh root@192.168.1.155
想让A,B机无密码互登录,那B机以上面同样的方式配置即可。
免密登录一节涉及 SSH 主机密钥 和 SSH 公钥。
区别如下:
| 关键点 | SSH 主机密钥 | SSH 公钥 |
|---|---|---|
| 用途 | 用于验证服务器(主机)的身份 | 用于用户身份验证(客户端验证) |
| 生成地点 | 由服务器生成,存储在服务器上 | 由用户生成,存储在客户端和服务器的 authorized_keys 中 |
| 类型 | 一对密钥:公钥和私钥(通常是 RSA、DSA、ECDSA 或 Ed25519) | 一对密钥:公钥和私钥(通常是 RSA、DSA、ECDSA 或 Ed25519) |
| 公钥存储位置 | 服务器上的 /etc/ssh/ 或类似目录 | 用户的 ~/.ssh/authorized_keys 文件 |
| 作用 | 用于服务器验证客户端是否连接到正确的服务器 | 用于验证用户是否具有访问权限 |
计算 SSH 主机密钥指纹
xxxxxxxxxx# 保存公钥到文件echo "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILAuWtZi/AFlZqlxcmQ0PnNSY2/FsTtEhWV6Jw0L2XWS" > id_ed25519.pub# 计算 SHA-256 Fingerprintssh-keygen -lf id_ed25519.pub# 计算 MD5 Fingerprintssh-keygen -lf id_ed25519.pub -E md5
从 SSH 私钥 提取公钥
xxxxxxxxxx# 假设私钥文件名为 id_rsassh-keygen -y -f id_rsassh-keygen -y -f id_rsa > id_rsa.pub
xxxxxxxxxxvi ~/.ssh/configHost myserve # 进行连接的别名HostName remoteipUser rootPort 22identityfile ~/.ssh/mykey.pemcat > ~/.ssh/config <<EOFHost h1 h2 h3 kafkaidentityfile ~/CoachMAQAEMRCluster_KeyPair.pemEOF
xxxxxxxxxx[root@RHEL ~]# ssh 192.168.1.102The authenticity of host '192.168.1.102 (192.168.1.102)' can't be established.RSA key fingerprint is f9:67:d3:98:6d:0b:8a:54:da:d4:de:c8:db:fc:17:4b.Are you sure you want to continue connecting (yes/no)? yesWarning: Permanently added '192.168.1.102' (RSA) to the list of known hosts.root@192.168.1.102's password:
解决方法:
xxxxxxxxxxcat >> /etc/ssh/ssh_config << EOFStrictHostKeyChecking noEOF
或者在使用ssh连接远程主机时加上 -o StrictHostKeyChecking=no 的选项,如下:
xxxxxxxxxxssh -o StrictHostKeyChecking=no 192.168.xxx.xxx
用OpenSSH的人都知道ssh会把你每个你访问过计算机的公钥(public key)都记录在~/.ssh/known_hosts。当下次访问相同计算机时,OpenSSH会核对公钥。如果公钥不同,OpenSSH会发出警告,避免你受到DNS Hijack之类的攻击。 SSH对主机的public_key的检查等级是根据StrictHostKeyChecking变量来配置的。默认情况下,StrictHostKeyChecking=ask。简单说下它的三种配置值:
原因排查,以ssh -v调试模式远程登录
xxxxxxxxxx[root@RHEL ~]# ssh 192.168.1.102 -vOpenSSH_5.3p1, OpenSSL 1.0.0-fips 29 Mar 2010debug1: Reading configuration data /etc/ssh/ssh_configdebug1: Applying options for *debug1: Connecting to 192.168.1.102 [192.168.1.102] port 22.debug1: Connection established.debug1: permanently_set_uid: 0/0debug1: identity file /root/.ssh/identity type -1debug1: identity file /root/.ssh/identity-cert type -1debug1: identity file /root/.ssh/id_rsa type -1debug1: identity file /root/.ssh/id_rsa-cert type -1debug1: identity file /root/.ssh/id_dsa type -1debug1: identity file /root/.ssh/id_dsa-cert type -1debug1: Remote protocol version 2.0, remote software version OpenSSH_5.3debug1: match: OpenSSH_5.3 pat OpenSSH*debug1: Enabling compatibility mode for protocol 2.0debug1: Local version string SSH-2.0-OpenSSH_5.3debug1: SSH2_MSG_KEXINIT sentdebug1: SSH2_MSG_KEXINIT receiveddebug1: kex: server->client aes128-ctr hmac-md5 nonedebug1: kex: client->server aes128-ctr hmac-md5 nonedebug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sentdebug1: expecting SSH2_MSG_KEX_DH_GEX_GROUPdebug1: SSH2_MSG_KEX_DH_GEX_INIT sentdebug1: expecting SSH2_MSG_KEX_DH_GEX_REPLYdebug1: Host '192.168.1.102' is known and matches the RSA host key.debug1: Found key in /root/.ssh/known_hosts:1debug1: ssh_rsa_verify: signature correctdebug1: SSH2_MSG_NEWKEYS sentdebug1: expecting SSH2_MSG_NEWKEYSdebug1: SSH2_MSG_NEWKEYS receiveddebug1: SSH2_MSG_SERVICE_REQUEST sentdebug1: SSH2_MSG_SERVICE_ACCEPT received --卡这里很久debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,passworddebug1: Next authentication method: publickeydebug1: Trying private key: /root/.ssh/identitydebug1: Trying private key: /root/.ssh/id_rsadebug1: Trying private key: /root/.ssh/id_dsadebug1: Next authentication method: passwordroot@192.168.1.102's password:
解决方法:修改服务器配置
xxxxxxxxxxcat >> /etc/ssh/sshd_config << EOFUseDNS noEOFservice sshd restart
xxxxxxxxxx[root@RHEL ~]# ssh -o StrictHostKeyChecking=no -v 192.168.1.102OpenSSH_5.3p1, OpenSSL 1.0.0-fips 29 Mar 2010debug1: Reading configuration data /etc/ssh/ssh_configdebug1: Applying options for *debug1: Connecting to 192.168.1.102 [192.168.1.102] port 22.debug1: Connection established.debug1: permanently_set_uid: 0/0debug1: identity file /root/.ssh/identity type -1debug1: identity file /root/.ssh/identity-cert type -1debug1: identity file /root/.ssh/id_rsa type -1debug1: identity file /root/.ssh/id_rsa-cert type -1debug1: identity file /root/.ssh/id_dsa type -1debug1: identity file /root/.ssh/id_dsa-cert type -1debug1: Remote protocol version 2.0, remote software version OpenSSH_5.3debug1: match: OpenSSH_5.3 pat OpenSSH*debug1: Enabling compatibility mode for protocol 2.0debug1: Local version string SSH-2.0-OpenSSH_5.3debug1: SSH2_MSG_KEXINIT sentdebug1: SSH2_MSG_KEXINIT receiveddebug1: kex: server->client aes128-ctr hmac-md5 nonedebug1: kex: client->server aes128-ctr hmac-md5 nonedebug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sentdebug1: expecting SSH2_MSG_KEX_DH_GEX_GROUPdebug1: SSH2_MSG_KEX_DH_GEX_INIT sentdebug1: expecting SSH2_MSG_KEX_DH_GEX_REPLYdebug1: Host '192.168.1.102' is known and matches the RSA host key.debug1: Found key in /root/.ssh/known_hosts:1debug1: ssh_rsa_verify: signature correctdebug1: SSH2_MSG_NEWKEYS sentdebug1: expecting SSH2_MSG_NEWKEYSdebug1: SSH2_MSG_NEWKEYS receiveddebug1: SSH2_MSG_SERVICE_REQUEST sentdebug1: SSH2_MSG_SERVICE_ACCEPT receiveddebug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,passworddebug1: Next authentication method: gssapi-keyexdebug1: No valid Key exchange contextdebug1: Next authentication method: gssapi-with-mic --卡这里很久debug1: Unspecified GSS failure. Minor code may provide more informationCannot determine realm for numeric host addressdebug1: Unspecified GSS failure. Minor code may provide more informationCannot determine realm for numeric host addressdebug1: Unspecified GSS failure. Minor code may provide more informationdebug1: Unspecified GSS failure. Minor code may provide more informationCannot determine realm for numeric host addressdebug1: Next authentication method: publickeydebug1: Trying private key: /root/.ssh/identitydebug1: Trying private key: /root/.ssh/id_rsadebug1: Trying private key: /root/.ssh/id_dsadebug1: Next authentication method: passwordroot@192.168.1.102's password:
解决方法:修改客户端配置
原来是因为尝试了个没有意义而且会失败的 gssapi-with-mic 认证方式浪费了时间,打开客户端的/etc/ssh/ssh_config 把里面的 GSSAPIAuthentication yes 改成 no 关掉它,即可让 ssh 直接尝试美妙的 publickey 认证方式。
或者在使用ssh连接远程主机时加上 -o GSSAPIAuthentication=no 的选项,如下:
xxxxxxxxxxssh -o GSSAPIAuthentication=no 192.168.1.102
xxxxxxxxxx...debug1: Authentication succeeded (publickey).Authenticated to myserver.mydomain.com ([xx.xx.xx.xx]:22).debug1: channel 0: new [client-session]debug2: channel 0: send opendebug1: Requesting no-more-sessions@openssh.comdebug1: Entering interactive session.debug1: pledge: network --卡这里很久
解决方法
这可能是D-Busandsystemd导致的问题。如果dbus由于某种原因服务重新启动,也需要重新启动systemd-logind。
可以通过打开 ssh 守护程序日志(在 Ubuntu 上它应该是)来检查这是否是问题/var/log/auth.log并检查它是否有这些行:
xxxxxxxxxxsshd[2721]: pam_systemd(sshd:session): Failed to create session: Connection timed out
如果是,只需重新启动systemd-logind服务:
xxxxxxxxxxsystemctl restart systemd-logind
我在 CentOS 7 上遇到了同样的问题,因为它messagebus被重新启动了(这是D-Bus在 CentOS 上调用服务的方式)。
将本地端口的数据通过远程服务器转发到第三方服务器
xxxxxxxxxxssh -L [local_bind_addr:]local_port:remote:remote_port user@middle_hostssh -L 127.0.0.1:5601:es-cn-tl32s4kwn0018umvd.kibana.elasticsearch.aliyuncs.com:5601 fjcqy@43.137.11.251
实现远程服务器通过ssh登录本地PC的功能
什么是ssh反向连接? 反向连接是指主机A(受控端)主动连接主机B(控制端),在主机A和主机B之间建立一个远程连接,通过这个连接主机B可以主动的向主机A发送一些请求。
应用场景 1、平时我们登录远程服务器,一般使用ssh登录远程服务器 2、现在我们想通过远程服务器反向操作登录本地PC,但由于一般本地PC没有公网IP,导致远程服务器使用正常ssh命令无法登录PC,这个时候就需要使用ssh反向连接功能。
操作步骤 主机A执行命令与服务器建立连接
xxxxxxxxxxssh -R 33333:localhost:22 root@81.68.115.174,其中81.68.115.174是服务器的地址。
3、主机B(服务器)执行命令远程登录主机A:
xxxxxxxxxxssh test@localhost -p 33333, 其中test为主机A的用户(一般root用户不允许登录),即可ssh登录到主机A。
xxxxxxxxxx# 打开powershell终端cd OpenSSH-Win64# 安装powershell.exe -ExecutionPolicy Bypass -File install-sshd.ps1# 配置ssh服务netsh advfirewall firewall add rule name=sshd dir=in action=allow protocol=TCP localport=22# 启动net start sshd
添加环境系统变量
xxxxxxxxxxsetx "path" "C:\fjcqy\Program Files\OpenSSH-Win64;%path%"
xxxxxxxxxx.\logs\%Y-%M-%D_%h-%m-%s_%S.log
勾选 Start log upon connec
先登录ssh,查看sftp-server执行文件目录: cat /etc/ssh/sshd_config|grep sftp
此时返回sftp-server执行文件目录可能是: Subsystem sftp /usr/libexec/openssh/sftp-server
然后在winscp的高级设置中的SFTP选项中设置中填入sftp-server执行文件目录: sudo /usr/libexec/openssh/sftp-server
接着在Shell中填入切换root用户的命令: sudo -i
好了,保存,再次登录,就是root账号登录了。
Xshell 4 注册码: 690313-111999-999313
Xftp 4 注册码:101210-450789-147200
Xmanager注册码:101210-450789-147200
Xshell 4配置文件路径: %HOMEPATH%\AppData\Roaming\NetSarang\Xshell\Sessions
Xshell 5配置文件路径: %HOMEPATH%\Documents\NetSarang\Xshell\Sessions
PuTTY 的主机密钥指纹存储在 Windows 系统的注册表中。具体位置是:
xxxxxxxxxxHKEY_CURRENT_USER\Software\SimonTatham\PuTTY\SshHostKeys
windows端通过ssh工具远程连接linux服务器时出现“permissions are too open”的错误
ssh_key_权限.bat
xxxxxxxxxx:: 设置变量Set Key=%1:: 移除继承性Cmd /c Icacls %Key% /c /t /Inheritance:d:: 设置所有者的所有权关系Cmd /c Icacls %Key% /c /t /Grant %UserName%:F:: 除了所有者之外,移除其他所有用户Cmd /c Icacls %Key% /c /t /Remove Administrator "Authenticated Users" BUILTIN\Administrators BUILTIN Everyone System Users:: 验证一下Cmd /c Icacls %Key%
现在就可以使用下面的脚本登录服务器了。