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

Solution Verified - Updated -

Issue

  • 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
fi

# User specific environment and startup programs

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

Environment

  • 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

New to Red Hat?

Learn more about Red Hat subscriptions

Using a Red Hat product through a public cloud?

How to access this content