LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-13-2016, 12:38 PM   #1
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Rep: Reputation: Disabled
Logging via command as a certain user


I have received help from another user concerning Oracle Shell Scripting (thank you BW-userx) and I'm trying to log in as a different user other than what I'm currently logged in as.
Example: I'm logged in as user 'oracle' when the script first starts, but right in the beginning of the script I need to be logged in as 'eric'. from what I read, I tried using CONNECT (domain/username and username/domain) with no success. I'm assuming that is the syntax.

Basically I'm asking how can I log in as a different user using comman d line only, if it's even possible?
 
Old 12-13-2016, 12:45 PM   #2
lazydog
Senior Member
 
Registered: Dec 2003
Location: The Key Stone State
Distribution: CentOS Sabayon and now Gentoo
Posts: 1,249
Blog Entries: 3

Rep: Reputation: 194Reputation: 194
Code:
runuser -l  userNameHere -c 'command'
 
1 members found this post helpful.
Old 12-13-2016, 12:50 PM   #3
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Original Poster
Rep: Reputation: Disabled
@lazydog.......I get the response "could not open session". I tried "sudo -u username" with no success. If I have to, and if it is available I am willing to pass my username and password in the script.
 
Old 12-13-2016, 12:53 PM   #4
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
What does the script look like, and how are you running it?

What do you mean by logging in as a user? Do you mean logging in at the terminal as 'user' and running a command as another user? Or do you mean using ssh to login and run a script as another user?

Last edited by szboardstretcher; 12-13-2016 at 12:54 PM.
 
Old 12-13-2016, 12:58 PM   #5
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Original Poster
Rep: Reputation: Disabled
Here is my script:

#!/bin/bash
runuser -l eric -c 'command'
TODAY=$(date +%Y%m%d)
DIR0=//export/home/eric/reports
DIR2=//export/home/eric/reports/team/audits

mkdir -p "${DIR2}"/"${TODAY}"
find "${DIR0}" -mindepth 1 -maxdepth 1 -type f -name "dailyreport.xlsx" -mtime +1 -exec mv -t "${DIR2}"/"${TODAY}"/ "{}" \+


This script will only run successfully if I am immediately logged off 'oracle' and logged in as 'eric' before running the rest of the script.
 
Old 12-13-2016, 01:02 PM   #6
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
Code:
runuser -l eric -c 'command'
Will run only 'command' as the user 'eric', it does not affect the rest of the script.

Are you saying you want to run the rest of the script as eric when the script is run as well?

Last edited by szboardstretcher; 12-13-2016 at 01:03 PM.
 
1 members found this post helpful.
Old 12-13-2016, 01:03 PM   #7
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Original Poster
Rep: Reputation: Disabled
@szboardstretcher....yes
 
Old 12-13-2016, 01:04 PM   #8
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
If so, remove the runuser line out of the script, and actually run the whole script with runuser. Ie:

Code:
runuser -l eric -c '/your/path/your_script.sh'
Is that acceptable?
 
1 members found this post helpful.
Old 12-13-2016, 01:04 PM   #9
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Original Poster
Rep: Reputation: Disabled
Before the script is ran, Im logged in as a sudo user 'oracle'. I need to log off as 'oracle' right in the beginning of the script before the rest of the script is ran.
 
Old 12-13-2016, 01:06 PM   #10
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Original Poster
Rep: Reputation: Disabled
@szboardstretcher...it depends. can I put your suggestion in the 2nd line of a script after the shebang ?
 
Old 12-13-2016, 01:07 PM   #11
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
Show me what you are thinking of scripting and Ill let you know.

You are going to run a script AS oracle, that uses runuser to run commands? Or to run another script?

This is not that complicated, Im just trying to be sure of your exact intent.

Is this your steps?
  • Log in at the console as 'oracle'
  • Run a script called 'myscript.sh' as the user 'eric'

Last edited by szboardstretcher; 12-13-2016 at 01:11 PM.
 
Old 12-13-2016, 01:14 PM   #12
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Original Poster
Rep: Reputation: Disabled
@szboardstretcher.........yes those steps are correct. I just tried your previous suggestion. Running the script now keeping my fingers crossed
 
Old 12-13-2016, 01:15 PM   #13
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
In my mind then you would create a script as the user 'oracle' called 'myscript.sh' or whatever with this in it
Code:
#!/bin/bash
TODAY=$(date +%Y%m%d)
DIR0=//export/home/eric/reports
DIR2=//export/home/eric/reports/team/audits

mkdir -p "${DIR2}"/"${TODAY}"
find "${DIR0}" -mindepth 1 -maxdepth 1 -type f -name "dailyreport.xlsx" -mtime +1 -exec mv -t "${DIR2}"/"${TODAY}"/ "{}" \+
Then run it as 'oracle' with this commmand
Code:
runuser -l eric -c 'myscript.sh'
There might be permission related things we might have to walk through. But this should get you working, or at least closer to working.

Last edited by szboardstretcher; 12-13-2016 at 01:17 PM.
 
1 members found this post helpful.
Old 12-13-2016, 01:17 PM   #14
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Original Poster
Rep: Reputation: Disabled
Here is the script I tried:

#!/bin/bash
runuser -l exg85 -c '/export/home/exg85/reports/RUNUSEREXG85.sh

Ran from command prompt, got this error:

./RUNUSEREXG85.sh: line 2: unexpected EOF while looking for matching `''
./RUNUSEREXG85.sh: line 3: syntax error: unexpected end of file

UGH!!!!!!
 
Old 12-13-2016, 01:20 PM   #15
trickydba
Member
 
Registered: Nov 2016
Location: Atlanta,Georgia
Posts: 310

Original Poster
Rep: Reputation: Disabled
@szboardstrectcher......no. the script initially would be ran under 'oracle', but immediately in the script I need to switch to the user 'exg85'.

I just reread your last entry, yes you are right
 
  


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
Command logging ideas needed! ssh user@host "echo logme" ZiGz Linux - Security 6 04-15-2009 02:08 AM
Logging in as user with SU priviledge bigshots Red Hat 4 03-05-2009 12:48 PM
IP/USER Logging scalforama Linux - Software 3 08-21-2007 12:22 AM
command logging esdeedee Linux - Security 2 05-24-2006 12:29 PM
Prevent a user from logging into X? sorrodos Linux - Security 6 06-26-2004 03:30 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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