首页 文章

如何设置$ PATH使`ssh user @ host command`有效?

提问于
浏览
117

我似乎无法设置一个新的$ PATH,以便在通过 ssh user@host command 执行命令时使用它 . 我已经尝试在远程机器上添加 export PATH=$PATH:$HOME/new_path 到〜/ .bashrc和〜/ .profile,但是执行 ssh user@host "echo \$PATH" 表示没有获取更改(它显示/ usr / local / sbin:/ usr / local / bin:/ usr / sbin目录:在/ usr / bin中:/ sbin目录:/ bin中:在/ usr /游戏) . 远程计算机正在运行Ubuntu 8.04 .

我确信我可以将其破解到/ etc / profile中,但这不是一个干净的解决方案,它只有在具有root访问权限时才有效 .

6 回答

  • 19

    正如grawity所说,〜/ .bashrc就是你想要的,因为它是由非交互式非登录shell提供的 .

    我希望你遇到的问题与默认的Ubuntu~ / .bashrc文件有关 . 它通常以这样的东西开头:

    # If not running interactively, don't do anything
    [ -z "$PS1" ] && return
    

    您希望在此行之前为非交互式shell添加任何内容 .

  • 2

    你有 ~/.bash_login~/.bash_profile 吗?

    交互模式下的Bash检查这些文件,并按以下顺序使用 first existing one

    • ~/.bash_profile

    • ~/.bash_login

    • ~/.profile

    所以,如果你有一个 ~/.bash_profile ,那么你对 ~/.profile 做的任何改变都将是看不见的 .

    非交互模式下的Bash有时会读取文件 ~/.bashrc (通常也是来自交互式脚本的源文件 . )"sometimes"我的意思是它依赖于分发:很奇怪,有一个编译时选项可以启用它 . Debian启用了 ~/.bashrc 读数,例如拱门没有 .

    ssh 似乎是使用非交互模式,所以〜/ .bashrc就足够了 . 遇到这样的问题时,我通常会添加一些echo来查看正在运行的文件 .

  • 29

    ssh文档说:

    如果指定了command,则在远程主机上执行它而不是登录shell .

    这就是为什么添加到bashrc文件不起作用 . 但是你有以下选择:

    • 如果在sshd配置中设置了 PermitUserEnvironment 选项,则可以将PATH设置添加到 ~/.ssh/environment

    • ssh remotemachine 'bash -l -c "somecommand"'

  • 2

    你总是可以说:

    ssh remotemachine 'export PATH=wedontneedastinkingpath; echo $PATH'
    
  • 6

    除@signpolyma答案外,您还必须在这些行之前添加导出

    # If not running interactively, don't do anything
    case $- in
        *i*) ;;
          *) return;;
    esac
    
  • 165

    我自己也遇到了同样的问题,用以下方法解决了:

    ssh user@remotehost PATH=\$HOME/bin:\$PATH\; remote-command
    

相关问题