LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 02-13-2017, 05:10 AM   #1
kurwen
LQ Newbie
 
Registered: Mar 2016
Distribution: CentOS 7, RHEL6, Xubuntu 14.04
Posts: 8

Rep: Reputation: Disabled
Executing scripts over ssh (and sudo su)


Good morning,

I've been googling about this for a long while but I failed founding how to archive it (if possible....)

I need to run a a script on a Unix server from a linux machine:

myuser -> my user to connect to the server.
unixserver -> the server where the script is going to run

The problem is that I have to run this script with a generic system user (wich we are going to call: sysuser).

I've tried the following:

ssh -t myuser@unixserver "sudo su - sysuser && whoami"

But this, obviously, log you to the server, then on sysuser and when you exit the sysuser session it will launch the 'whoami' command...

I have to sudo su sysuser, and it is not possible for me to execute the script from myuser neither to change this infrastructure.

I don't have the sysuser password so I can't just su - sysuser.


I've tried too:

ssh -t myuser@unixserver "sudo su - sysuser --command=whoami"

but I've received the error:

Sorry, user myuser is not allowed to execute '/usr/bin/su - sysuser --command=whoami' as root on unixserver.

the same for:

ssh -t myuser@unixserver "sudo -u sysuser whoami"

I will appreciate you help and sorry for my awful English.

thanks!
 
Old 02-13-2017, 05:31 AM   #2
r3sistance
Senior Member
 
Registered: Mar 2004
Location: UK
Distribution: CentOS 6/7
Posts: 1,375

Rep: Reputation: 217Reputation: 217Reputation: 217
I'd say "sudo <user> <command>" is the better form to use, but as you can only use su... I'd suggest it is probably the case that the visudos file has only been set-up to only allow sudo su. Do you have control on the end server or not?
 
Old 02-13-2017, 05:33 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
in that case I would try to construct a shell script to do the job (as root) on unixserver and execute it from your host.
 
Old 02-13-2017, 05:38 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,331
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
The program su is probably the wrong way to go about things. What are you trying to solve? Do you need to run lots of different programs as sysuser, or just one or two?

About using sudo, the user myuser needs permission in sudoers to run things as the user sysuser. But rather than editing /etc/sudoers directly, you might consider adding a file under /etc/sudoers.d/ and making your additions there. See the manual page for sudoers for details.

Code:
man 5 sudoers
The book sudo Mastery, by M W Lucas, is quite good about giving a thorough walk through with examples. He also has a video around called "sudo: you're doing it wrong" with slides available.
 
Old 02-13-2017, 06:22 AM   #5
kurwen
LQ Newbie
 
Registered: Mar 2016
Distribution: CentOS 7, RHEL6, Xubuntu 14.04
Posts: 8

Original Poster
Rep: Reputation: Disabled
Thanks to everyone... that was quick!


Ok, as I've said I can't do modification to the system, I only have permission to execute /usr/bin/su to sysuser from myuser and I have no root access.

I meant, the only thing I can do when I connect to this server is:

sudo su - sysuser
./sysscript

What I want to do is to write a script that I will execute on my linux laptop so I can execute this 'syscript' on many servers without logging manually on each one.
 
Old 02-13-2017, 06:34 AM   #6
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,331
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
There might be several possibilities, but you'll need first confirm what you really are allowed to run on the remote machine.

Code:
ssh -t myuser@unixserver "sudo -l"
That will tell you if you are allowed to run su and if there are any limitations on options or runtime arguments.
 
Old 02-13-2017, 06:37 AM   #7
r3sistance
Senior Member
 
Registered: Mar 2004
Location: UK
Distribution: CentOS 6/7
Posts: 1,375

Rep: Reputation: 217Reputation: 217Reputation: 217
I think I know the issue, the visudo file has an explicit usage of su which is broken the moment you add the --command alias on it.

To be fair, at the moment you have granted a user "sudo su", then you might as well grant all in sudo because a user with sudo su can simply edit the sudoers file to give themselves that anyway.
 
Old 02-13-2017, 06:47 AM   #8
kurwen
LQ Newbie
 
Registered: Mar 2016
Distribution: CentOS 7, RHEL6, Xubuntu 14.04
Posts: 8

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Turbocapitalist View Post
There might be several possibilities, but you'll need first confirm what you really are allowed to run on the remote machine.

Code:
ssh -t myuser@unixserver "sudo -l"
That will tell you if you are allowed to run su and if there are any limitations on options or runtime arguments.
Thanks Turbocapitalist, I confirm you that the only thing I can do is the sudo su to this sysuser. This is an enterprise ldap and my user is very limited.
 
Old 02-13-2017, 06:49 AM   #9
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,331
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
Ok. Then maybe it has to be formulated like this:

Code:
ssh -t myuser@unixserver "sudo su -l sysuser --command=./sysscript"
If not, and you have restrictions on su, then you'll always have to it manually instead until you can convince the sysadmin to tweak the sudoers file for you.
 
Old 02-13-2017, 07:21 AM   #10
kurwen
LQ Newbie
 
Registered: Mar 2016
Distribution: CentOS 7, RHEL6, Xubuntu 14.04
Posts: 8

Original Poster
Rep: Reputation: Disabled
Thanks to everyone... I guess I'll have to do a lot of redundant job
 
Old 02-13-2017, 07:30 AM   #11
r3sistance
Senior Member
 
Registered: Mar 2004
Location: UK
Distribution: CentOS 6/7
Posts: 1,375

Rep: Reputation: 217Reputation: 217Reputation: 217
If you have a system with lots of servers in it that you have to do repeat processes like this, then maybe it'd be best to suggest moving it to some form of central control such as puppet, chef or salt. That is to whomever is in charge.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Error while executing sudo apt-get upgrade Sammehta Linux - Newbie 1 02-18-2016 06:15 AM
Executing sudo command from PHP script bilal_linux Linux - Newbie 10 02-15-2010 11:29 PM
problem executing scripts mierdatuti Linux - General 4 09-19-2008 04:21 AM
Executing scripts jonette20 Linux - General 3 10-15-2007 01:50 PM
Executing scripts upon login jungatheart Linux - Newbie 3 04-19-2004 06:06 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 07:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration