LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-25-2014, 03:23 PM   #1
Nbiser
Member
 
Registered: Oct 2012
Location: Maryland
Distribution: Fedora, Slackware, Debian, Ubuntu, Knoppix, Helix,
Posts: 302
Blog Entries: 7

Rep: Reputation: 44
How to su root in a bash script:


Hi everyone!

I have been learning bash scripting and I am trying to write a simple script that will su root, with the password itself being in the bash script. I have been doing a lot of research online and reading in the man pages but haven't been able to figure out how to pull it off. Any ideas on how this can be done? Thanks in advance!

Nbiser
 
Old 09-25-2014, 03:27 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
You don't do that, because it would be an absolutely horrible security problem.
 
Old 09-25-2014, 03:31 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
Agreed.

Look into 'sudo' and the 'sudoers' file. From there you can allow a certain user permissions to run a certain program, with or without a password.
 
Old 09-25-2014, 03:36 PM   #4
Nbiser
Member
 
Registered: Oct 2012
Location: Maryland
Distribution: Fedora, Slackware, Debian, Ubuntu, Knoppix, Helix,
Posts: 302

Original Poster
Blog Entries: 7

Rep: Reputation: 44
Quote:
Originally Posted by suicidaleggroll View Post
You don't do that, because it would be an absolutely horrible security problem.
I just want to know if it can be done...I am planning on deleting it as soon as I see that it works.
 
Old 09-25-2014, 03:40 PM   #5
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
No.
 
Old 09-25-2014, 10:59 PM   #6
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
I tried piping the password into su:
Code:
$ echo 123 | su  
su: must be run from a terminal
I guess that settles that question.

BTW "123" isn't really my su password.
 
Old 09-26-2014, 01:16 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,848

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
actually it is a very bad idea, but theoretically you may try to make it in a test environment. I would suggest you to try the command expect, it is used to handle such kind of problems.
 
2 members found this post helpful.
Old 09-26-2014, 03:39 AM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Mayhaps it is su -c 'somepath/somescript' you want to do.
 
Old 09-26-2014, 04:58 PM   #9
Nbiser
Member
 
Registered: Oct 2012
Location: Maryland
Distribution: Fedora, Slackware, Debian, Ubuntu, Knoppix, Helix,
Posts: 302

Original Poster
Blog Entries: 7

Rep: Reputation: 44
Thanks for all of the posts guys! I will try some of the options that you gave me when I get a chance and let you know how it goes.

Nbiser
 
Old 09-29-2014, 03:53 PM   #10
Nbiser
Member
 
Registered: Oct 2012
Location: Maryland
Distribution: Fedora, Slackware, Debian, Ubuntu, Knoppix, Helix,
Posts: 302

Original Poster
Blog Entries: 7

Rep: Reputation: 44
I tried all of the options that you gave me on my machine but they didn't work. Expect seems to be the best option and I still have to fool around with it some...but I don't think that it will work on su and sudo. It seems that su and sudo are quite secure when it comes to bash scripts! Thanks for all the tips guys!

Nbiser
 
Old 09-30-2014, 12:13 AM   #11
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,848

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
http://www.linuxquestions.org/questi...h-sudo-638079/
http://www.datauniv.com/blogs/2013/0...expect-script/
http://www.linuxquestions.org/questi...or-4175459818/
 
  


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
what this bash script does and what the root cause for the failure Millar Linux - Software 1 03-11-2014 01:30 PM
[SOLVED] bash script syntax fail as root goncalopp Linux - General 6 04-14-2011 05:25 AM
detecting root user in a bash script radiodee1 Programming 2 11-30-2008 06:18 AM
Bash script to email setuid root files deoren LinuxQuestions.org Member Success Stories 1 01-30-2005 09:56 AM
Bash script to edit passwd without Root password? NetFlash Linux - Newbie 2 11-29-2003 04:22 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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