Shell scripting: Reading a root password with su-to-root shell script

by on May 8, 2006 · 2 comments· LAST UPDATED May 8, 2006

in

su-to-root is a simple script to give an interactive front-end to su. It can be used in menu entry commands to ask for the root password.

This is extermly useful when you want to use it from menu based shell script. Most menu entries simply start an editor or a game or whatever. But some menu entries would like to give the user the ability to change important settings in the system, that require root privileges. su-to-root script can be used to ask for the root password.

Examples:

To restart Apache web server you need to type command as follows:

$ su-to-root -c "/etc/init.d/apache-perl restart"

Output:

About to execute /etc/init.d/apache-perl restart.
This command needs root privileges to be executed.
enter root passwd:
Password:
Restarting apache-perl 1.3 web server....

If command or password failed it will prompt back user with "Try again? (y/n)" prompt.

$ su-to-root -c "/etc/init.d/apache-perl restart"

Output:

About to execute /etc/init.d/apache-perl restart.
This command needs root privileges to be executed.
enter root passwd:
Password:
su: Authentication failure
Sorry.
Incorrect password or command failed. Try again? (y/n)

This kind of flexibility always needed when you write menubased shell script. If your program/command is a X11 program that do not require a terminal then you need to pass -X option to su-to-root command:

$ su-to-root -c "network-admin"

Where,

  • -c COMMAND : The command to execute as a string. This option is mandatory.
  • -X : The command is a X11 program that do not require a terminal
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 2 comments… read them below or add one }

1 Puneet August 30, 2006 at 7:28 am

How to avoid the password prompt or provide some default value while running su command through shell script on Linux?

The problem I am facing is that the shell script contains the su oracle command which prompts for a password, whereas I don’t want any user interaction once the script is triggered.

To add on, the script is not triggered from root user, otherwise if run from root the password was not required.

Reply

2 nixCraft August 30, 2006 at 3:24 pm

Setup sudo to run command as root user so that you can avoid the password prompt:

http://www.cyberciti.biz/tips/allow-a-normal-user-to-run-commands-as-root.html

Reply

Leave a Comment

Previous post:

Next post: