我正在尝试通过 root 帐户从我的 FreeBSD 11.3 机器访问 git 服务器。 git 服务器受到保护,只允许通过 SSH 公钥身份验证进行访问。
当我登录我的 FreeBSD 盒子时,我以用户身份登录。然后sudo到root。当尝试连接到 git 服务器时,它会提示输入密码。当我不使用 sudo 但保持用户身份并尝试连接到 git 服务器时,SSH 公钥身份验证就像魅力一样。这是 ssh -v gitserver 的输出
OpenSSH_7.5p1, OpenSSL 1.0.2s-freebsd 28 May 2019
debug1: Reading configuration data /root/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Connecting to gitserver.xxx.tld [192.168.x.y] port 22.
debug1: Connection established.
debug1: permanently_set_uid: 0/0
debug1: Fssh_key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_rsa type -1
debug1: Fssh_key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_rsa-cert type -1
debug1: Fssh_key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_dsa type -1
debug1: Fssh_key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_dsa-cert type -1
debug1: Fssh_key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ecdsa type -1
debug1: Fssh_key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ecdsa-cert type -1
debug1: Fssh_key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ed25519 type -1
debug1: Fssh_key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.5 FreeBSD-20170903
debug1: Remote protocol version 2.0, remote software version OpenSSH_7.2p2 Ubuntu-4ubuntu2.8
debug1: match: OpenSSH_7.2p2 Ubuntu-4ubuntu2.8 pat OpenSSH* compat 0x04000000
debug1: Authenticating to gitserver.xxx.tld:22 as '<gituser>'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: [email protected]
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
debug1: kex: server->client cipher: [email protected] MAC: <implicit> compression: none
debug1: kex: client->server cipher: [email protected] MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:<here comes a long key expression>
DNS lookup error: data does not exist
debug1: Host 'gitserver.xxx.tld' is known and matches the ECDSA host key.
debug1: Found key in /root/.ssh/known_hosts:1
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_EXT_INFO received
debug1: Fssh_kex_input_ext_info: server-sig-algs=<rsa-sha2-256,rsa-sha2-512>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,password,keyboard-interactive
debug1: Next authentication method: publickey
debug1: Trying private key: /root/.ssh/id_rsa
debug1: Trying private key: /root/.ssh/id_dsa
debug1: Trying private key: /root/.ssh/id_ecdsa
debug1: Trying private key: /root/.ssh/id_ed25519
debug1: Next authentication method: keyboard-interactive
Password:
似乎 ssh-agent 没有向远程主机提供我的密钥......
有什么想法如何解决这个问题吗?我是否可能使用不允许 root 用户使用的密钥密码(只是一个想法)?
问候奥拉夫
答案1
sudo
默认情况下清除(或重置为默认的已知安全值)几乎所有环境变量,以便它们不能用于利用 sudo 获得的权限(例如通过设置 PATH 或 LD_LIBRARY_PATH)。
可以通过关闭(env_reset
中的选项)来禁用此功能/etc/sudoers
非常强烈地不建议)或者sudo
可以配置为保留其他环境变量。例如,在/etc/sudoers
:
env_keep += "SSH_AGENT_PID SSH_AUTH_SOCK"
请参阅man sudoers
和man sudo
了解详细信息。
或者,如果这个 root 帐户不与其他人共享(例如,它是您自己的个人计算机),您可以为 root 生成一个密钥对,然后将 root 的公钥添加到您的 git 服务器用户帐户中。
PS:我从来不需要使用我的 ssh 代理,但是当我想运行像via这样的工具时env_keep
我使用过......我什至在-ing 到远程计算机上运行时使用过它。env_keep += "DISPLAY"
gparted
sudo
ssh -X
gparted
答案2
如果您root
在本地机器上,默认情况下,ssh
会尝试使用root
用户和root
公钥登录远程机器。
您需要使用user@hostname
语法来指定用于登录远程计算机的用户,以及-i
指定要使用的键的选项。
例如:
ssh -i ~user/.ssh/id_rsa [email protected]
看SSH(1)了解详情。