通过 autofs 以用户身份运行 sshfs

bil*_*jmc 5 linux ssh sshfs autofs

我的情况:

  • 我的局域网上有几台我不管理的服务器
  • 我使用 SSH 访问它们以获取sshfs、shell 和远程 X11 应用程序
  • 我已经ControlMaster auto在我的~/.ssh/config文件中进行了设置,因此我不会遇到身份验证延迟
  • 我使用压缩和快速/弱密码,因为我要么在私人局域网上,要么在使用 VPN
  • 在可能的情况下,我已将我的(无密码)公共 RSA 密钥导出到服务器

我已经开始使用autofs简化我的工作,但是autofs想要以 root 身份运行它的所有挂载命令。当然,我可以以 root 身份生成一个新的 RSA 密钥对并将其导出,并将我自己的~/.ssh/config选项复制到超级用户的配置文件中,但我不想保留这些东西的两个副本,这并不能解决我的愿望每个主机只有一个打开的 SSH 连接。因此,我想让autofssshfs以非特权用户身份运行,就像在终端手动调用时一样。

我已经研究过autofs脚本,但这些脚本似乎不能解决我的问题。有什么建议?

小智 1

JFTR,我已经修改(并简化),ssh_user以便它首先尝试联系用户的ssh-agent

#!/bin/bash
# Open a ssh connection as a given user, thus using his/hers authentication
# agent and/or config files.
: ${ADDOPTS:="-2Ax"}
: ${LOCAL:="kreator"}
export SSH_AUTH_SOCK=$(find /tmp/ssh-* -type s -user ${LOCAL} -name agent* | tail -1)
declare -a options=( $* )

# Remove unwanted options
for (( i=0,fin=${#options[*]} ; i < fin ; i++ ))
do
    case ${options[$i]} in
            (-a|-oClearAllForwardings=*)    unset options[$i]
                                            ;;
    esac
done

exec /bin/su ${LOCAL} -c "$(which ssh) ${ADDOPTS} ${options[*]}"
Run Code Online (Sandbox Code Playgroud)