LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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, 01: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, 01:21 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
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 02:01 PM.
 
Old 08-04-2014, 01: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, 06:13 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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, 07: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



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 01:05 PM
Changing user and running commands inside of a script? cory94bailly Linux - Newbie 2 02-22-2011 12:57 PM
Changing user in bash script. r3gan Linux - Security 3 03-15-2006 11:27 AM
changing the user inside a shell script sanjith11 Programming 2 04-22-2004 07:44 AM
changing user in a shell script sanjith11 Programming 6 03-22-2004 01:18 PM

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

All times are GMT -5. The time now is 12:45 PM.

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