Why my ~/.bashrc is sourced while I'm connected and not when I only execute a command?

StackOverflow https://stackoverflow.com/questions/17489934

  •  02-06-2022
  •  | 
  •  

Question

I have a target AIX 6.1 server where I need to execute a script.

The ~/.bashrc on my target server is configured to add a variable MACHINE=1

When I ssh the server and connect I effectively view the ~/.bashrc variables in my env:

[user@source ~]$ ssh user@10.10.10.10
[user@target]
$env |grep "MACHINE"
MACHINE=1
[user@target]
$

But when I execute directly the env command in the ssh the variable is not set:

[user@source ~]$ ssh user@10.10.10.10 "env" |grep MACHINE
[user@source ~]$

Is there something to configure more on the server?

Was it helpful?

Solution

One option you have is to tell sshd to set the environment variables for you. In /etc/sshd_config:

PermitUserEnvironment yes

In ~/.ssh/environment:

MACHINE=1

OTHER TIPS

ssh remote command execution happens in a non-interactive shell. As such, your ${HOME}/.bashrc, /etc/bashrc aren't read.

If you want ssh commands to read environment variables, add those to /etc/profile.

Alternatively, try hacks:

ssh user@10.10.10.10 'source ${HOME}/.bashrc; echo ${MACHINE}'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top