LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   can i give root credential in the script ? (https://www.linuxquestions.org/questions/linux-newbie-8/can-i-give-root-credential-in-the-script-4175572141/)

khan87 02-13-2016 12:30 AM

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 ?

Michael Uplawski 02-13-2016 01:06 AM

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...

khan87 02-13-2016 02:05 AM

Quote:

Originally Posted by Michael Uplawski (Post 5499457)
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

Michael Uplawski 02-13-2016 02:57 AM

Quote:

Originally Posted by khan87 (Post 5499471)
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.

Keith Hedger 02-13-2016 04:24 AM

You can do
Code:

echo "myadminpassword" | sudo -S /path/to/script
But don't!

Michael Uplawski 02-13-2016 04:40 AM

Quote:

Originally Posted by Keith Hedger (Post 5499505)
But don't!

I understand English, but I “do not feel” it. Are you trying to be cynical?

TobiSGD 02-13-2016 04:48 AM

Quote:

Originally Posted by khan87 (Post 5499450)
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.

Keith Hedger 02-13-2016 07:16 AM

Quote:

Originally Posted by Michael Uplawski (Post 5499508)
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.

Habitual 02-13-2016 02:57 PM

Safer to give script "user" permission via visudo to the adduser|useradd utility.

BW-userx 02-13-2016 04:48 PM

sudo -v

Blitzig 02-15-2016 09:38 AM

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.

suicidaleggroll 02-15-2016 10:02 AM

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?

Blitzig 02-15-2016 10:06 AM

You could add the user you are using to the wheel group which will give the user root privileges.

suicidaleggroll 02-15-2016 10:08 AM

Quote:

Originally Posted by Blitzig (Post 5500514)
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.

BW-userx 02-15-2016 10:16 AM

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


All times are GMT -5. The time now is 07:50 AM.