0

My issue is that I want run a script from root for which I always have to login with root manually by typing "su -" on command line.

My query is that the script which I am executing it automatically login with root by just prompting me for password. Help me!!!

::::::::::Script:::::::::::::

if [ "$(whoami)" != "root" ]; then

echo -e '\E[41m'"\033[1mYou must be root to run this script\033[0m"

**su - # at this line I want to login as root but it is not working**

exit 1

fi

sleep 1

if [ "$(pwd)" != "/root" ]; then

echo -e '\E[41m'"\033[1mCopy this script to /root & then try again\033[0m"

cd /root

exit 1

fi

sleep 1

echo -e '\E[36;45m'"\033[1mDownloading Flash Player from ftp.etilizepak.com\033[0m"

sleep 2

wget -vcxr ftp://soft:S0ft\!@ftp.abc.com/ubuntu/ubuntu\ 12.04/adobe-flashplugin=/install_flash_player_11_linux.i386.tar.gz

cd ftp.abc.com/ubuntu/ubuntu\ 12.04/adobe-flashplugin/

sleep 1

echo -e '\E[42m'"\033[1mUnzipping .tar File...\033[0m"

sleep 1

tar -xzf install_flash_player_11_linux.i386.tar.gz

echo "Unzipping Compeleted"

sleep 2

echo -e '\E[42m'"\033[1mCopying libflashplayer.so\033[0m"

cp libflashplayer.so /usr/lib/mozilla/plugins/

:::::::::::::::END:::::::::::::::::::::

P.P
  • 117,907
  • 20
  • 175
  • 238

2 Answers2

0

I'm not sure if I understand your question but I suppose you want to run something inside you script with root privileges - then you shuold use "sudo" command. You can also suppress the password prompt, this can be configured in sudoers" configuration file.

Some more info here: https://unix.stackexchange.com/questions/35338/su-vs-sudo-s-vs-sudo-bash Shell script calls sudo; how do I suppress the password prompt

There is tons of examples, google something like "linux sudo examples" and you will get lots of examples how to use su, sudo ans sudoers commands.

Community
  • 1
  • 1
  • You should cut all lines that must be run as root and put it inside another .sh file, then in the first script write: sudo /path/to/your/script.sh. Similar question here: http://stackoverflow.com/questions/1988249/how-do-i-use-su-to-execute-the-rest-of-the-bash-script-as-that-user – Kamil Tunkiewicz Sep 10 '13 at 08:28
  • but in my scenario I can not give sudo rights to the users all the time because this script is used usually at different users. So please give me the appropriate solution. – Sheikh-Asad Sep 10 '13 at 10:10
  • You can give sudo right for **this script only**, please read my second answer – Kamil Tunkiewicz Sep 10 '13 at 11:26
0

According to your comments to my previous answer, here is how i do it:

There are two files in the same directory:

-rwx------ 1 root root 19 Sep 10 13:04 test2.sh
-rwxrwxrwx 1 root root 29 Sep 10 13:06 test.sh

File test.sh:

#!/bin/bash
# put your message here
su -c ./test2.sh

File test2.sh:

#!/bin/bash
echo You run as:
whoami
# put your code here

Result:

> ./test.sh
Password:****
You run as:
root

If you want to suppress the password prompt for this script only, replace "su -c" with "sudo" and configure sudoers file according to insctructions from here: https://askubuntu.com/questions/155791/how-do-i-sudo-a-command-in-a-script-without-being-asked-for-a-password

Community
  • 1
  • 1