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.


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):


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


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.

| improve this answer | |
  • “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 Sep 25 '19 at 12:39

.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.

| improve this answer | |
  • 4
    It seems .bashrc is not loaded either. – kenorb Oct 2 '14 at 20:25
  • 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 Jul 23 '15 at 11:23
  • ~/.zshenv works for me. unix.stackexchange.com/questions/4921/sh-startup-files-over-ssh – user91155 May 3 '16 at 3:22
  • 1
    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 Sep 25 '19 at 12:39

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
 # Put here code thats get executed with each ksh/sh invocation

| improve this answer | |

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

| improve this answer | |

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).
| improve this answer | |

If you have root access then you can put environment variables in /etc/environment. That way, they are set no matter what shell or login type you are using.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.