LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-04-2014, 02:09 PM   #1
scottf456
LQ Newbie
 
Registered: Apr 2014
Posts: 4

Rep: Reputation: Disabled
Changing user inside of script


I have a script that I need to run as my user. Inside I will be reading a text file that is a list of users. I need to change to each of these users and execute some commands as that user. It is necessary to be that user because the commands set up files that must be owned by the owner. Typically I would use the command:

sudo su - ${USERNAME}

but this requires a manual entering of the password. Is there a way that I can execute the sudo command and have the password be passed in as part of the command?

I have tried the following:

echo ${PASSWORD} | sudo -S su - ${USERNAME}

which looked good but did not actually stay as the new user.

Please note that I do have permissions needed to execute the sudo command from the command line.
 
Old 08-04-2014, 02:21 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,414

Rep: Reputation: 8111Reputation: 8111Reputation: 8111Reputation: 8111Reputation: 8111Reputation: 8111Reputation: 8111Reputation: 8111Reputation: 8111Reputation: 8111Reputation: 8111
Quote:
Originally Posted by scottf456 View Post
I have a script that I need to run as my user. Inside I will be reading a text file that is a list of users. I need to change to each of these users and execute some commands as that user. It is necessary to be that user because the commands set up files that must be owned by the owner. Typically I would use the command:

sudo su - ${USERNAME}

but this requires a manual entering of the password. Is there a way that I can execute the sudo command and have the password be passed in as part of the command?

I have tried the following:

echo ${PASSWORD} | sudo -S su - ${USERNAME}

which looked good but did not actually stay as the new user.

Please note that I do have permissions needed to execute the sudo command from the command line.
Two things come to mind here: if you know the user's password, you could just omit the sudo, then your echo may work. Or, you could modify sudoers to not prompt those users for a password, either at all or just for that script.

EDIT:

After further thought, this jumped out at me:
"because the commands set up files that must be owned by the owner"

Since the script is ostensibly running as sudo/root, why not just set things up, and run a chmod/chown on the files that gets created?? Safer than having a script that can perform operations as another user....

Last edited by TB0ne; 08-04-2014 at 03:01 PM.
 
Old 08-04-2014, 02:52 PM   #3
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
Assuming that you are root and your list of users is users.txt, I would do this:

Code:
## This is untested,..

#!/bin/bash
while read username
do
 su - $username -c "your commands here"  
done < users.txt
 
1 members found this post helpful.
Old 08-05-2014, 07:13 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,418

Rep: Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785
I'd just start your script from by already being the root user; he/she never needs a passwd to switch to another user; basically as per szboardstretcher
 
Old 08-05-2014, 08:55 AM   #5
scottf456
LQ Newbie
 
Registered: Apr 2014
Posts: 4

Original Poster
Rep: Reputation: Disabled
Unfortunately I am not root. But the -c option gave me what I needed. I can now execute what I need as the user I need. Thank you szboardstretcher for your help.
 
  


Reply

Tags
script, sudo, user


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Is it possible to switch between the root/user inside automatic script? Michal Krzyz Linux - Newbie 10 12-12-2012 02:05 PM
Changing user and running commands inside of a script? cory94bailly Linux - Newbie 2 02-22-2011 01:57 PM
Changing user in bash script. r3gan Linux - Security 3 03-15-2006 12:27 PM
changing the user inside a shell script sanjith11 Programming 2 04-22-2004 08:44 AM
changing user in a shell script sanjith11 Programming 6 03-22-2004 02:18 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:13 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