1748169 Members
4265 Online
108758 Solutions
New Discussion юеВ

Automate switch user

 
SOLVED
Go to solution
ivanrc
Occasional Contributor

Automate switch user

Hi, it's possible to automate the use of "su" command??
I refer to the issue of the introduction of the password.
There is something like this:
su - username/password???

Thanks.
3 REPLIES 3
Ivan Ferreira
Honored Contributor

Re: Automate switch user

You can use "sudo" instad of su. Or maybe a combination of both, for example:


sudo su - user -c "command"

You need to configure the sudoers file with the visudo command.

Check for information about sudo in google.

Cheers.
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Matti_Kurkela
Honored Contributor
Solution

Re: Automate switch user

Combining su and sudo means you're first transitioning to root, and then to target user. The logs will reflect that. Sometimes, seeing a message about JoeUser becoming root can cause undue agitation in security auditors.

If you have a relatively modern version of sudo, the effect of:

sudo su - -c

can be exactly replicated with:

sudo -u -i

It will also consolidate all the essential information into one log line: JoeUser becomes for the purpose of running the .

Of course, to use "sudo -i" effectively, your sudoers file must be written to say what you really mean: instead of

JoeUser ALL=(root) su -

you should write:

JoeUser ALL=() ALL

If you need to allow JoeUser to execute a particular command automatically, without prompting a password, you might write:

JoeUser ALL=() NOPASSWD:

(OK, I admit: this is my pet peeve. But I see "sudo su - ..." as a nothing more than a bad habit, encouraged by sloppy sudoers rule writing practices.)

MK
MK
ivanrc
Occasional Contributor

Re: Automate switch user

THX.