• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 343
  • Last Modified:

Linux, ssh and environment initialization (with and without scripts)

If I use the following command

[root@xxclnt1 sbin]# sshpass -p 'xxxx' ssh root@xxclnt1 /sbin/test.sh

Open in new window


This command will have a different environment than a command such as the following where I ssh into the clinet, and then manually perform the script

[root@xxclnt1 sbin]# sshpass -p 'xxxx' ssh root@xxclnt1 
/sbin/test.sh

Open in new window



This appears difficult to fix.  Is there an easy way to get the remote ssh scripts to have the same command environment as the ssh sessions run without a command file

Thanks
0
Los Angeles1
Asked:
Los Angeles1
  • 3
  • 2
1 Solution
 
woolmilkporcCommented:
There are two ways to achieve this.

1)

ssh reads ~/.ssh/environment on the target server, and adds lines of the format "VARNAME=value" to the environment if the file exists and users are allowed to change their environment.

sshd_config on the target machine must have set "PermitUserEnvironment yes" to make this work.

2)

The client can send environment variables. To make this work the server's sshd_config must have defined which variables to accept by means of  "AcceptEnv" and the client's ssh_config must have specified which variables to send to the target machine from the local environment by means of "SendEnv".

See the sshd_config and ssh_config manpages for more detail.
0
 
Los Angeles1Author Commented:
wmP

In regards to method 1>

I do not have a .ssh/environment

Is there any yum install or other method in which I can make this happen

Thanks
0
 
woolmilkporcCommented:
~/.ssh/environment

is just a text file in the .ssh subdirectory of the home directory of the target user on the target machine - to be created by you.

It must contain entries in the format

MYVAR=myvalue

one per line.

ssh will add these variables to the target user's environment before executing the batch commands.
0
 
Los Angeles1Author Commented:
1> So if I get you right, if I ssh in as root, I should create the following file

/home/root/.ssh/environment

2> Next I assume I should ssh into the target, and perform the command

set

Open in new window


and cut and paste that into the file created above

Is this all correct ?

Thanks
0
 
woolmilkporcCommented:
You can do it that way, correct. It seems a bit overdone, because some variables already exist even in the ssh batch environment, but it's not wrong nonetheless.

Don't copy and paste - run

set > ~/.ssh/environment

Please remember that the sshd server config on the target machine must contain "PermitUserEnvironment yes" to make this work. Don't forget to restart the sshd server after making changes!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now