SSH登录时使用.bashrc

当我ssh进入运行Hardy 8.04的ubuntu盒子时,我的.bashrc中的环境变量没有设置。

如果我做一个源.bashrc,变量被正确设置,一切正常。

为什么登陆时不运行.bashrc ?

178841 次浏览

当您使用SSH登录时,.bashrc不是源的。你需要像这样在你的.bash_profile中来源它:

if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

如果ayman的解决方案不起作用,尝试将你的文件命名为.profile而不是.bash_profile。这对我很管用。

关于bash调用如何工作,dotfiles做什么,以及应该如何使用/配置它们的优秀资源,请阅读以下内容:

我和霍布豪斯有类似的情况。我想使用这个命令

ssh myhost.com 'some_command'

其中some_command存在于/var/some_location中。

我试图通过编辑$HOME/.bashrc/var/some_location附加到PATH环境变量,但这不起作用。因为在默认情况下,.bashrc(在Ubuntu 10.4 LTS上)会提前退出,原因是这段代码:

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

这意味着如果您想更改ssh非登录shell的环境,则应该在该行之上添加代码。

类似于@Loïc Wolff,在我的$HOME/.bash_profile Ubuntu 16中添加

if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
echo "Executed .bash_profile , calling .bashrc"
. "$HOME/.bashrc"
fi
fi