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

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
🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source & DevOps topics via:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
2 comments… add one
  • 🐧 nixCraft Aug 30, 2006 @ 15:24

    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

  • Puneet Aug 30, 2006 @ 7:28

    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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.