V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
thunderbuddy
V2EX  ›  SSH

ssh 执行时$PATH 变量未替换

  •  
  •   thunderbuddy · 182 天前 · 938 次点击
    这是一个创建于 182 天前的主题,其中的信息可能已经有所发展或是发生改变。

    后台方式执行命令时,PATH 变量为"$PATH:/usr/local/bin",导致很多命令找不到

    ssh 登入后再执行,PATH 就正常了,请大家帮忙看看问题在哪呢

    aaron@Aaron-PC:~$ ssh 192.168.2.11 'export'
    declare -x HOME="/var/services/homes/aaron"
    declare -x LOGNAME="aaron"
    declare -x MAIL="/var/mail/aaron"
    declare -x OLDPWD
    declare -x PATH="\$PATH:/usr/local/bin"
    declare -x PWD="/volume2/homes/aaron"
    declare -x SHELL="/bin/ash"
    declare -x SHLVL="1"
    declare -x SSH_CLIENT="192.168.2.97 57086 22"
    declare -x SSH_CONNECTION="192.168.2.97 57086 192.168.2.11 22"
    declare -x USER="aaron"
    
    13 条回复    2023-11-03 17:47:58 +08:00
    adoal
        1
    adoal  
       182 天前   ❤️ 1
    $PATH 前面那个 \ 很可疑
    liangkang1436
        2
    liangkang1436  
       182 天前 via Android   ❤️ 1
    这两种方式加载的配置文件不一样,具体差别,看这篇博客 https://xiashuo.xyz/posts/devops/bash/bash_config_file_setup/
    julyclyde
        3
    julyclyde  
       182 天前
    login shell 和 non interactive shell 的区别?
    thunderbuddy
        4
    thunderbuddy  
    OP
       182 天前
    @julyclyde 是的,交互式的就没问题
    julyclyde
        6
    julyclyde  
       182 天前   ❤️ 1
    @thunderbuddy man bash 看 INVOCATION 那一段
    adoal
        7
    adoal  
       182 天前
    看看是不是某个 rc 文件里写 PATH="$PATH:/usr/local/bin" 的时候用了单引号
    thunderbuddy
        8
    thunderbuddy  
    OP
       181 天前
    我的是群晖的系统,环境变量检查过了,/etc/profile ,/etc.defaults/.bashrc_profile ,以及~/.zshrc 文件,PATH 变量是正常的,没有包含单引号

    执行环境是非登录非交互,没有设置$BASH_ENV 变量,找不出问题了 o(╥﹏╥)o
    aaron@Aaron-PC:~$ ssh 192.168.2.11 'echo $-;shopt login_shell'
    hBc
    login_shell off

    @adoal @julyclyde @Al0rid4l @liangkang1436
    julyclyde
        9
    julyclyde  
       181 天前
    @thunderbuddy ssh 的目标服务器是什么操作系统啊?我觉得你查看的这几个配置文件都好奇怪啊,不太像普通 Linux 的?
    julyclyde
        10
    julyclyde  
       181 天前
    @thunderbuddy 看看/etc/environments 这个?
    thunderbuddy
        11
    thunderbuddy  
    OP
       181 天前
    @julyclyde 目标服务器是群晖,木有这个文件
    julyclyde
        12
    julyclyde  
       181 天前
    那要不你开个 strace -f 吧,从 sshd listen 、accept 、fork 开始看看到底它从哪儿引入这个环境变量的

    (我就是觉得有点高射炮打蚊子)
    thunderbuddy
        13
    thunderbuddy  
    OP
       176 天前
    感谢各位大佬,找到问题了,原来是.ssh 目录下的 environment 文件中定义了这个变量(也不知道啥时候创建的这个文件)捂脸。。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5389 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 08:15 · PVG 16:15 · LAX 01:15 · JFK 04:15
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.