bash .bashrc 在 ssh 登录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/820517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 18:09:30  来源:igfitidea点击:

.bashrc at ssh login

ubuntusshbash

提问by Hobhouse

When I ssh into my ubuntu-box running Hardy 8.04, the environment variables in my .bashrcare not set.

当我通过 ssh 进入运行 Hardy 8.04 的 ubuntu-box 时,我的环境变量.bashrc没有设置。

If I do a source .bashrc, the variables are properly set, and all is well.

如果我做一个 source .bashrc,变量设置正确,一切都很好。

How come .bashrcisn't run at login?

为什么.bashrc在登录时不运行?

回答by Ayman Hourieh

.bashrcis not sourced when you log in using SSH. You need to source it in your .bash_profilelike this:

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

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

回答by ruseel

I had similar situation like Hobhouse. I wanted to use command

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

 ssh myhost.com 'some_command'

and 'some_command' exists in '/var/some_location' so I tried to append '/var/some_location' in PATH environment by editing '$HOME/.bashrc'

并且“some_command”存在于“/var/some_location”中,因此我尝试通过编辑“$HOME/.bashrc”在 PATH 环境中附加“/var/some_location”

but that wasn't working. because default .bashrc(Ubuntu 10.4 LTS) prevent from sourcing by code like below

但这不起作用。因为默认的 .bashrc(Ubuntu 10.4 LTS) 阻止通过如下代码进行采购

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

so If you want to change environment for ssh non-login shell. you should add code above that line.

所以如果你想改变 ssh 非登录 shell 的环境。您应该在该行上方添加代码。

回答by lhunath

For an excellent resource on how bash invocation works, what dotfiles do what, and how you should use/configure them, read this:

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

回答by Lo?c Wolff

If ayman's solution doesn't work, try naming your file .profileinstead of .bash_profile. That worked for me.

如果 ayman 的解决方案不起作用,请尝试将文件命名.profile.bash_profile. 那对我有用。