LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 02-13-2016, 12:30 AM   #1
khan87
LQ Newbie
 
Registered: Feb 2016
Posts: 2

Rep: Reputation: Disabled
can i give root credential in the script ?


Hi i'm new in linux.

i need to create user with bash script. Only root have right to create user.
if my script run from root, user created.
if my script run from normal user it give error = only root may have right to create.

is there anyway to give root credential in the script ?
For example it will ask root password in the runtime. can i do it ?
 
Old 02-13-2016, 01:06 AM   #2
Michael Uplawski
Member
 
Registered: Dec 2015
Location: Normandy, France
Distribution: Debian stretch/sid
Posts: 530
Blog Entries: 18

Rep: Reputation: 351Reputation: 351Reputation: 351Reputation: 351
You are trying something dangerous and I for my share, must refrain from giving you an advice on this. I am curious, though, as what others may have to say...
 
Old 02-13-2016, 02:05 AM   #3
khan87
LQ Newbie
 
Registered: Feb 2016
Posts: 2

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Michael Uplawski View Post
You are trying something dangerous and I for my share, must refrain from giving you an advice on this. I am curious, though, as what others may have to say...
Dear Michael,
i don't understand what do you mean by dangerous ?

i'm asking about prompt root password to create user using script. if u don't have root password then you can't create new users
 
Old 02-13-2016, 02:57 AM   #4
Michael Uplawski
Member
 
Registered: Dec 2015
Location: Normandy, France
Distribution: Debian stretch/sid
Posts: 530
Blog Entries: 18

Rep: Reputation: 351Reputation: 351Reputation: 351Reputation: 351
Quote:
Originally Posted by khan87 View Post
Dear Michael,
i don't understand what do you mean by dangerous ?

i'm asking about prompt root password to create user using script. if u don't have root password then you can't create new users
My answer would be a repetition of what I wrote before, as long as you give no indication about your comprehension of the reasons for the complication. The root password is there on purpose and being unable to create users, you should ask why? Just to provide some variation (but I am still unable to give a different response), I treasure that there is often just 1 way to do things the right way and an arbitrary number of ways to run havoc.

Actually.., for a question like “How can I run havoc in the most efficient way” you might be blessed with a rich harvest. Unable to tell you anything else, for now. But let's wait a little. Someone else may contribute her/his five cents, her/his opinions or experience. I'll open a bottle if I learn something new from this exchange.

Just an additional question: Do you know any of the commands “sudo”, “gksudo” or “su” ? All have man-pages, if you are interested.

Last edited by Michael Uplawski; 02-13-2016 at 03:11 AM. Reason: efficient -><- effective Kraut2English conversin
 
Old 02-13-2016, 04:24 AM   #5
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 2,441

Rep: Reputation: 611Reputation: 611Reputation: 611Reputation: 611Reputation: 611Reputation: 611
You can do
Code:
echo "myadminpassword" | sudo -S /path/to/script
But don't!
 
Old 02-13-2016, 04:40 AM   #6
Michael Uplawski
Member
 
Registered: Dec 2015
Location: Normandy, France
Distribution: Debian stretch/sid
Posts: 530
Blog Entries: 18

Rep: Reputation: 351Reputation: 351Reputation: 351Reputation: 351
Quote:
Originally Posted by Keith Hedger View Post
But don't!
I understand English, but I do not feel it. Are you trying to be cynical?
 
Old 02-13-2016, 04:48 AM   #7
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,144
Blog Entries: 2

Rep: Reputation: 4846Reputation: 4846Reputation: 4846Reputation: 4846Reputation: 4846Reputation: 4846Reputation: 4846Reputation: 4846Reputation: 4846Reputation: 4846Reputation: 4846
Quote:
Originally Posted by khan87 View Post
For example it will ask root password in the runtime. can i do it ?
Yes, that is what the su command is for. For example, if you put this into a script
Code:
 su -c "useradd -m tobi"
and then run the script it will ask for the root password and then, if the correct password is entered, run the command specified in the quotes.
More info with
Code:
man su
If you want users to have access to certain commands that must run as root should also look at the sudo command, it allows you to exactly specify what the user is allowed to do and you don't have to give away the root password.

Last edited by TobiSGD; 02-13-2016 at 04:50 AM.
 
1 members found this post helpful.
Old 02-13-2016, 07:16 AM   #8
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 2,441

Rep: Reputation: 611Reputation: 611Reputation: 611Reputation: 611Reputation: 611Reputation: 611
Quote:
Originally Posted by Michael Uplawski View Post
I understand English, but I do not feel it. Are you trying to be cynical?
No its just not a very good idea to have your admin password in clear text, but if you are the only person with access to your machine either phusically or remotly it's ok, depends on your set up and security really.
 
Old 02-13-2016, 02:57 PM   #9
Habitual
LQ 5k Club
 
Registered: Jan 2011
Location: Nowhere near you, thank God.
Distribution: OSX Sierra
Posts: 8,570
Blog Entries: 14

Rep: Reputation: Disabled
Safer to give script "user" permission via visudo to the adduser|useradd utility.
 
Old 02-13-2016, 04:48 PM   #10
BW-userx
Senior Member
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Void Linux / Slackware 14.2
Posts: 4,737

Rep: Reputation: 876Reputation: 876Reputation: 876Reputation: 876Reputation: 876Reputation: 876Reputation: 876
sudo -v
 
Old 02-15-2016, 09:38 AM   #11
Blitzig
LQ Newbie
 
Registered: Feb 2016
Location: South Africa
Distribution: Fedora, RedHat, Centos7
Posts: 18

Rep: Reputation: 2
Hi,

You can have something like this in your script if only you will be using it, but use it at your own risk.

#Runs the command
sudo cmd_name

#Enters root password and does not display output
echo "password" > /dev/null

This will run the command with sudo and then prompt for a password which will be entered with the second command echo.
 
Old 02-15-2016, 10:02 AM   #12
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,533

Rep: Reputation: 2108Reputation: 2108Reputation: 2108Reputation: 2108Reputation: 2108Reputation: 2108Reputation: 2108Reputation: 2108Reputation: 2108Reputation: 2108Reputation: 2108
Do not stick your root password in plain text inside a script. You said you're new to Linux, don't start out on the wrong foot.

This is exactly what sudo was built for. Give your user permission to add other users without requiring a password using sudo. Honestly though, how often are you adding new users on your system? Why can't you just su to root before running the script?
 
Old 02-15-2016, 10:06 AM   #13
Blitzig
LQ Newbie
 
Registered: Feb 2016
Location: South Africa
Distribution: Fedora, RedHat, Centos7
Posts: 18

Rep: Reputation: 2
You could add the user you are using to the wheel group which will give the user root privileges.
 
Old 02-15-2016, 10:08 AM   #14
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,533

Rep: Reputation: 2108Reputation: 2108Reputation: 2108Reputation: 2108Reputation: 2108Reputation: 2108Reputation: 2108Reputation: 2108Reputation: 2108Reputation: 2108Reputation: 2108
Quote:
Originally Posted by Blitzig View Post
You could add the user you are using to the wheel group which will give the user root privileges.
Also a bad idea. Regular users don't have unlimited root privileges for a reason.
 
Old 02-15-2016, 10:16 AM   #15
BW-userx
Senior Member
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Void Linux / Slackware 14.2
Posts: 4,737

Rep: Reputation: 876Reputation: 876Reputation: 876Reputation: 876Reputation: 876Reputation: 876Reputation: 876
the person using the script would need to be wheel or sudo group person depending on how that owner has his or her system set up. You might be able to limit a user or users to only having some sudo rights.


here is a read on how to set up permissions and limited permissions

just putting in a script
Code:
sudo -v
then calls for that persons password to be entered then it he or she will have sudo permissions for the time limit allowed as if they where doing sudo [command] off the cli

Last edited by BW-userx; 02-15-2016 at 10:24 AM.
 
  


Reply


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Script Credential Verification edwardcode Programming 1 06-23-2012 05:44 AM
[SOLVED] shell script to give root access to user for limited time? rakrr786 Linux - Newbie 9 05-31-2012 12:08 AM
A script called by another script would always give exit status other than Zero(0) vandana Linux - General 2 03-22-2010 04:26 AM
How to give myself root permissions royeo Linux - Newbie 11 08-12-2006 03:01 AM
How to give root's rights Qebex Linux - General 2 09-25-2002 11:44 AM

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

All times are GMT -5. The time now is 07:09 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration