Reputation: 2262
There is a shell script (/bin/sh, not bash) that requires root permissions for execution.
If it is ran by a normal user it should ask user a password to get root access and re-run itself.
Now it uses the following code:
if [ $(id -u) -ne 0 ]; then su root -- $0 $@ ; ... fi
That works fine, but there are some OS like Ubuntu that has no root password at all. On the other hand, a lot of systems use sudo for root permissions.
The question is: how can the script detect whether to use su
or sudo
without asking the user to enter too much passwords (e.g. enter sudo
password, if it fails - run su
).
Upvotes: 6
Views: 10460
Reputation: 651
Create one more .sh file from this file call your original .sh
file like -
su - oracle /u01/enlightics/Enlightiks/UploadFTP/ExportScript2.sh
Upvotes: 1
Reputation: 38758
It shouldn't. If script requires root privileges, it should be run as root. It's the user's business how he's going to accomplish that -- using su, sudo or some other mechanism.
If you are concerned with security issues and don't want to do everything from root, you can drop root privileges for those parts.
Upvotes: 6
Reputation: 1
While this doesn't fully answer your question, it's worth noting that you can check if the sudo package is installed using the following:
Debian based systems:
dpkg -s sudo
RPM based systems:
rpm -q sudo
Upvotes: 0
Reputation: 34654
Check if sudo
ist installed
SU='su'
which sudo > /dev/null && SU='sudo'
Upvotes: 0
Reputation: 58715
There isn't a bullet-proof way of doing this, because any distribution can lay files in any way it wants. Debian and Ubuntu often place system files in directories other than Red Hat, for example. It's much easier to customize the script for the OS it's installed on.
Upvotes: 3
Reputation: 23085
You can setup the account not to need a password for sudo in /etc/sudoers:
yourusername ALL=(ALL) NOPASSWD: ALL
If you don't want to do that, you can force them to run the script as root. Add something like this to the top of your shell script:
if [ "$UID" -ne 0 ]; then
echo "You must be root to run this script"
exit 1
fi
This way, the user can get to be root however they choose (su or sudo).
Upvotes: 2