~/.profile is not loaded when using SSH (Ubuntu)

22

9

Edited to reflect the problem I really wanted to solve:

I need to set up my ruby environment so I can deploy via Capistrano.

export PATH=$HOME/.rbenv/bin:$PATH
eval "$(rbenv init -)"

I put these in ~deploy/.profile, but when I ssh in, they aren't being run. Ideas?

I'm running Ubuntu 12.04.


The original question was:

When I ssh into another account at localhost, it doesn't load my .profile. How can I force ssh to load it? I'm running Ubuntu 12.04.

Charles R

Posted 2013-03-12T21:24:34.407

Reputation: 351

Answers

15

You may explicitly specify that you want to start an interactive login shell:

 ssh user@host bash --login -i 

The "role" of ~/.profile (or ~./bash_profile) and .bashrc for ssh have some other files, (see man ssh for details):

~/.ssh/environment

Contains additional definitions for environment variables; see ENVIRONMENT, above.

~/.ssh/rc

Commands in this file are executed by ssh when the user logs in, just before the user's shell (or command) is started. See the sshd(8) manual page for more information.

user86064

Posted 2013-03-12T21:24:34.407

Reputation:

“You may” is often not true, because ssh is being run by some other tool (continuous delivery) and the command can't be easily changed. – Jan Hudec – 2019-09-25T12:39:04.960

6

.profile is only loaded for login shells, which an ssh session is not (by default). If you want something to run on startup for all interactive shells, put it in .bashrc instead (or .zshrc or whatever your shell uses).

Also, if you just want to log into another account on the local machine, ssh is probably overkill. You might want to use su or something instead.

qmega

Posted 2013-03-12T21:24:34.407

Reputation: 2 530

3It seems .bashrc is not loaded either. – kenorb – 2014-10-02T20:25:28.790

This is incorrect. It is a login shell. From the man page: "If command is specified, it is executed on the remote host instead of a login shell." – mkj – 2015-07-23T11:23:52.793

~/.zshenv works for me. http://unix.stackexchange.com/questions/4921/sh-startup-files-over-ssh/

– user91155 – 2016-05-03T03:22:03.197

When ssh is given a command to run, it defaults to not allocate a tty and then the shell is not “interactive” either. – Jan Hudec – 2019-09-25T12:39:56.533

4

Using bash should result in reading ~/.bashrc. The following might help with ksh and sh (bash in sh mode), or when your ~/.bashrc is not executed during login.

The sshd consults ~/.ssh/environment (check sshd_config(5) for permissions) and ~/.ssh/sshrc or ~/.ssh/rc. This gives the possibility to setup ENV=~/.profile or BASH_ENV=~/.profile and SSH_LOGIN=Y

In ~/.profile I've the following layout (Replace ENV with BASH_ENV when using bash):


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

eremmel

Posted 2013-03-12T21:24:34.407

Reputation: 41

0

Bash reads ~/.profile only when it is a login shell and ~/.bash.bashrc only if it has a terminal, neither of which is true by default when invoking a command with ssh. However, there are several other options to set environment, on the server, all unfortunately depending on the system setup:

  • Zsh reads ~/.zshenv even in this case; there is no corresponding config file for bash though.
  • If the PermitUserEnvironment option is on in /etc/sshd_config, ssh will read ~/.ssh/environment. Unfortunately this option defaults to off.
  • If the pam_env.so is called with user_readenv=1 in /etc/pam.d/sshd, it will read ~/.pam_environment. While it is not default of the module, it is called that way at least in Ubuntu.
  • If all else fails, you can put a command= directive in the authorized keys file calling a wrapper script that sets the environment and executes the $SSH_ORIGINAL_COMMAND at the end (I the command is one for shell, so eval is appropriate here, but I am not sure).

Jan Hudec

Posted 2013-03-12T21:24:34.407

Reputation: 885

0

You probably have a ~/.bash_profile, which overrides ~/.profile.

user541686

Posted 2013-03-12T21:24:34.407

Reputation: 21 330