【问题标题】:Environment variables not defined in SSH AuthorizedKeysCommand (Docker)SSH AuthorizedKeysCommand (Docker) 中未定义的环境变量
【发布时间】:2022-04-22 16:15:13
【问题描述】:

我正在尝试与 LDAP 建立私钥 SSH 连接。

/etc/ssh/sshd_config

AuthorizedKeysCommand /etc/ldap_ssh_authorized_keys.sh
AuthorizedKeysCommandUser nobody

从 LDAP 服务器获取公钥的脚本

/etc/ldap_ssh_authorized_keys.sh

#!/bin/bash
USERSLIST=$( ldapsearch -x -D "${LDAP_USER}" -w "${LDAP_PASSWORD}" -H $LDAP_URI -b "${LDAP_BASEDN}" -s sub '(objectClass=posixAccount)' -u 'uid' \
    grep '^uid:' | sed -n '/^ /{H;d};/uid:/x;$g;s/\n *//g;s/uid: //gp'  \
)
while IFS= read -r line; do
        exists=$(ldapsearch -x -D "${LDAP_USER}" -w "${LDAP_PASSWORD}" -H $LDAP_URI -b "${LDAP_BASEDN}" \
        -s sub "(&(objectClass=posixGroup)(cn=sysadmin)(memberUid=${line}))" | grep "^# numEntries:")
        if [[ ! -z $exists ]]
        then
            ldapsearch -x -D "${LDAP_USER}" -w "${LDAP_PASSWORD}" -H $LDAP_URI -b "${LDAP_BASEDN}" \
                  -s sub "(&(objectClass=posixAccount)(uid=${line}))" \
                  -u 'sshPublicKey' \
            | sed -n '/^ /{H;d};/sshPublicKey:/x;$g;s/\n *//g;s/sshPublicKey: //gp'
           echo -e "";

        fi;
done <<< "$USERSLIST"

当我使用 /bin/bash 运行脚本时,它运行良好并返回我的公钥。

正常定义的所有环境变量。

  • LDAP_URI
  • LDAP_BASEDN
  • LDAP_USER
  • LDAP_PASSWORD

脚本在尝试建立 SSH 连接时也正常运行。但环境变量不可用。

我也在尝试使用 AuthorizedKeysCommandUser 作为 root。但什么都没有改变。

【问题讨论】:

    标签: bash docker ssh ldap sshd


    【解决方案1】:

    我通过从 /proc/1/environ 获取环境变量解决了这个问题。

    Reference

    【讨论】:

      猜你喜欢
      • 2021-08-18
      • 2020-03-27
      • 1970-01-01
      • 2022-06-18
      • 2021-09-13
      • 2020-08-09
      • 2021-08-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多