Unable to set JAVA_HOME in ssh session (on root user) - bash

I want a ssh session with root account and env variable JAVA_HOME. I have export JAVA_HOME in /etc/bash.bashrc. When running command,
ssh -t root#localhost "source /etc/bash.bashrc; echo javahome=\$JAVA_HOME"
the printout in the shell is javahome=. But when doing following, I can see JAVA_HOME is correctly set.
ssh root#localhost
source /etc/bash.bashrc
echo javahome=$JAVA_HOME
what could be the possible cause? and in generally, how to have a ssh session with JAVA_HOME set. The scripts are run with bash4 on ubuntu12.04-64
PS: I finally put the configuration script into /root/.profile, then everything works... which is kinda workaround but not the solution to the problem...

Add the variable definition to /etc/environment.

Maybe this would help: .bashrc at ssh login
although I am not sure if it is correct that you are using bash.bashrc file when I would expect .bashrc

Related

there is environment variable set in remote host. $home=/apps/tesco/app/. i want to ssh to that host and want to go cd $home

I am able to ssh into the host but I am not able to go to the location of the environment variable that I set at a remote host.
Remote host env variable:
$HOME=/apps/tesco/app/
After doing ssh to the remote host
when I give
cd $HOME in my script.
It's not going to the location /apps/tesco/app/
Summary:
I want to go to the location of the environment variable that is set at a remote host. how to call in a shell script.
ssh ${user}#${host[$nn]} -qtt -oStrictHostKeyChecking=no -oUserKnownHostsFile=/dev/null "sudo su - ${sid[$nn]}adm <<EOF
cd $HOME ------it should go /apps/tesco/app/...But its not going
ls -ltr
it should go to the location that is set in environment variable and list down the file
under location /apps/tesco/app/.
You've probably created your $HOME variable in .bashrc file which is not executed/sourced when you ssh into a machine. Add the following in your .bash_profile file instead. It gets sourced when you ssh.
export $HOME=/apps/tesco/app/

AWS EC2 User Data: Commands not recognized when using sudo

I'm trying to create an EC2 User-data script to run other scripts on boot up. However, the scripts that I run fail to recognize some commands and variables that I'd already declared. I'm running the commands as the "ubuntu" user but it still isn't working.
My user-data script looks something like this:
export user="ubuntu"
sudo su $user -c ". ./run_script"
Within the script, I have these lines:
THIS_PATH="/some/path"
echo "export SOME_PATH=$THIS_PATH" >> ~/.bashrc
source ~/.bashrc
However, the script can't run SOME_PATH/application, and echo $SOME_PATH this returns a blank line. I'm confused because $SOME_PATH/application works when I log into the EC2 using SSH and my debug logs using whoami returns "ubuntu."
Am I missing something here?
Your data script is executed as root and su command leaves $HOME and other ENV variables intact (note that sudo is redundant). "su -" does not help either
So, do not use ~ or $HOME but full path /home/ubuntu/.bashrc
I found out the problem. It seems that source ~/.bashrc isn't enough to restart the shell -- the environment variables worked after I referenced them in another bash script.

Alternative to export variable in non interactive shell

I have a requirement wherein a script needs to be executed which requires a few environment variables to be set. Typically the following is required:
1) I login to target node, say T1 using 'user1' and set my environment variable, say TEMPDIR=~/tmp; Do a source ~/.bashrc and logoff.
2) Now I'm required to do a non interactive shell script execution. This is
Login to a admin server and run the command:
ssh user1#T1 "sudo -u user2 /path/filescript"
The file script that I execute requires the TEMPDIR to be set.
The above setup doesn't work, as the bash shell is not sourced during non interactive shell (if my understanding is correct). To overcome this, I have tried the below suggestions available in this site and other, but without any luck:
ssh user1#T1 "source ~/.bashrc; sudo -u user2 /path/filescript"
ssh user1#T1 "sudo -u user2 env -i /path/filescript"
Please help. Thanks.
Note: I'm trying to get this done without any intervention from root (I mean using root to set / unset any variables.
I'm not sure step 1 is necessary in any way:
ssh user1#T1 sudo -u user2 env -i TEMPDIR=/home/user2/tmp /path/filescript"

sbt (Scala) via SSH results in command not found, but works if I do it myself

So I'm trying to do something that involves running sbt over an SSH command, and this is what I'm trying:
ssh my_username#<server ip> "cd <project folder>; sbt 'run-main Foo' "
When I do that however, I get an error message: bash: sbt: command not found
Then I go SSH into the server myself, cd to the project folder, and run sbt 'run-main Foo' and everything works nicely. I have checked to make sure sbt is on the $PATH variable on the remote server via ssh my_username#<server ip> "echo $PATH" and it shows the correct value.
I feel like this is a simple fix, but cannot figure it out... help?
Thanks!
-kstruct
When you log in, bash is run as an interactive shell. When you run commands directly through ssh, bash is run as a non-interactive shell, and therefore different initialization files are sourced (see the bash manual pages for which exactly). There are a number of ways to fix this, e.g.:
Use the full path to sbt when calling it directly through ssh
Edit .bashrc and add the missing directories to the PATH environment variable
Note that your test ssh my_username#<server ip> "echo $PATH" actually prints PATH on your client, not your server, because of the double quotes. Use ssh my_username#<server ip> 'echo $PATH' or ssh my_username#<server ip> env to print PATH from the server's environment. When checking using env, you will see that PS1 is only set in interactive shells.

how to set environment variable for root user

I'm Mac user.
I want to set PYTHONPATH env for root. so
$ sudo su -
# vi ~/.profile
and add to file 'export PYTHONPATH=/mypythonlib'
then
# env
I can see this line
PYTHONPATH=/Users/simpnet2/projects/meiji/src/hershey
but..
when I use sudo command, cannot find that
$ sudo env
.. there's no PYTHONPATH
My program has to run with sudo command and needs PYTHONPATH.
If you use sh try /etc/profile, bash try /etc/bashrc and if you use zsh try /etc/zshenv.
You can make PYTHONPATH visible to sudo be editing your sudoers file. Notice you should ONLY do this through visudo as explained here.
You should try sudo -i which will simulate logging in as root and source the ~root/.profile.
As of 10.8.5, putting my environment statements in the .profile path in the home of the root user (/var/root) worked. after quitting bash and coming back to the root user prompt with 'su -', I could see my new path, etc. with the 'env' command and my MacPorts installationw orking correctly.
MacBook-Pro:~ root# cat /var/root/.profile
export MANPATH=/opt/local/share/man:$MANPATH
export PATH=/opt/local/bin:/opt/local/sbin:$PATH
MacBook-Pro:~ root# which port
/opt/local/bin/port
Well, in other Linux system, it is also right that 'sudo' does not use local environment variable. But you can declare the temporary environment variable along with 'sudo' command.
For example, in your case, you can add 'PYTHONPATH=/mypythonlib' in your command 'sudo env', and the final command is:
sudo PYTHONPATH=/mypythonlib env
You can also read this article: Using sudo. You can see how 'sudo' keep or ignore user-defined environment variables.
In the case of logging in as a normal user and invoking "su - root" I found that Mac OS 10.8.5's bash was ignoring .profile and .bash_profile; I was unable to change root's $PATH by editing those files. What did work was editing /etc/paths. After exiting the root shell and entering again with "su - root" the new path was present.

Resources