How to access variable values of an user through ssh command using sudo?

Solution Verified - Updated -


  • There are two systems A and B
  • On System B, there are two users : test1 and userX. Both are sudo users
  • Its required to run a script from system A, which will access system B with credentials of userX and need to do sudo and run su with a single commandline option -c.
  • This will be scripted which have to be run automatically without human intervention.
  • The requirement is to access user test1's variables via this script. How to do that?
  • Those variables not global. These variables are declared in .bash_profile as follows.
[test1@localhost ~]$ cat .bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
    . ~/.bashrc

# User specific environment and startup programs

export PATH VAR
  • The command into the scripts are as follows.
# ssh userX@ sudo su - test1 -c 'echo $VAR'
  • Where System B is and variable VAR has to print the value in it.


  • Red Hat Enterprise Linux 6
  • sudo

Subscriber exclusive content

A Red Hat subscription provides unlimited access to our knowledgebase, tools, and much more.

Current Customers and Partners

Log in for full access

Log In