使用 SSH (Ubuntu) 时未加载 ~/.profile

Cha*_*s R 30 ssh .profile ruby ubuntu .bash-profile

编辑以反映我真正想解决的问题:

我需要设置我的 ruby​​ 环境,以便我可以通过 Capistrano 进行部署。

export PATH=$HOME/.rbenv/bin:$PATH
eval "$(rbenv init -)"
Run Code Online (Sandbox Code Playgroud)

我把这些放在 ~deploy/.profile 中,但是当我 ssh 进入时,它们没有运行。想法?

我正在运行 Ubuntu 12.04。


原来的问题是:

当我在本地主机上通过 ssh 连接到另一个帐户时,它不会加载我的 .profile。如何强制 ssh 加载它?我正在运行 Ubuntu 12.04。

小智 18

您可以明确指定要启动交互式登录 shell:

 ssh user@host bash --login -i 
Run Code Online (Sandbox Code Playgroud)

“角色”~/.profile(或〜/ .bash_profile中),并.bashrc为SSH有一些其他的文件,(见man ssh详细信息):

~/.ssh/环境

包含环境变量的附加定义;参见上文环境。

~/.ssh/rc

该文件中的命令在用户登录时由 ssh 执行,就在用户的 shell(或命令)启动之前。有关更多信息,请参阅 sshd(8) 手册页。


qme*_*ega 9

.profile仅为登录 shell 加载,ssh 会话不是(默认情况下)。如果您希望在启动时为所有交互式 shell 运行某些东西,请将其放入.bashrc(或.zshrc您的 shell 使用的任何东西)。

此外,如果您只想登录本地计算机上的另一个帐户,ssh 可能有点过分。您可能想要使用su或代替。

  • 似乎也没有加载 `.bashrc`。 (6认同)
  • 当给 ssh 一个要运行的命令时,它默认*不*分配 tty,然后 shell 也不是“交互式”的。 (3认同)

use*_*686 7

您可能有一个~/.bash_profile, 它会覆盖~/.profile.


小智 5

使用 bash 应该会导致读取~/.bashrc. 以下可能有助于 ksh 和 sh(sh 模式下的 bash),或者当您~/.bashrc在登录期间未执行时。

sshd 咨询~/.ssh/environment(检查 sshd_config(5) 的权限)和~/.ssh/sshrc~/.ssh/rc。这提供了设置ENV=~/.profileBASH_ENV=~/.profile和的可能性SSH_LOGIN=Y

~/.profile我有以下布局(使用 bash 时替换ENVBASH_ENV):


if [[ -n $SSH_LOGIN || -z $ENV ]]; then
     # Put here login initialization code
     unset SSH_LOGIN
     ENV=~/.profile
fi
 # Put here code thats get executed with each ksh/sh invocation
Run Code Online (Sandbox Code Playgroud)