Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

how to login to root in linux through sh file with example?

share|improve this question

migrated from stackoverflow.com Oct 13 '10 at 8:00

This question came from our site for professional and enthusiast programmers.

closed as not a real question by wzzrd, jscott, Warner, Dennis Williamson, John Gardeniers Oct 14 '10 at 0:30

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

1  
I think the good people at Serverfault may help you better than the programmers here - this looks more like a system settings/permissions issue. (if you have the permissions set up already, sudo whateverscript_you_have.sh would be the way) –  Piskvor Oct 13 '10 at 7:53
    
Can you add a lot more detail and clarity to your question please. –  Chopper3 Oct 13 '10 at 8:08

2 Answers 2

Typically, you'd have a file with the following contents:

#! /bin/sh
# This requires you to know the root password
su -

This will display a password prompt (unless you're already root) and once you have entered the root password you will be at a root shell, as if you had logged in as root initially.

share|improve this answer

I suppose you are asking for setuid shell scripts. That is, from a normal user shell, execute a shell script with root privileges. Setuid scripts don't work as expected because what is really exec'ed by the os is the shell program called with your script as the argument. Shell doesn't honor the setuid bit you have on your script. So this will not work as you wish:

cia@pinkpony:~$ cat /tmp/iamweasel.sh 
#!/bin/sh
echo `id -u`
cia@pinkpony:~$ ls -la /tmp/iamweasel.sh 
-rwsr-sr-x 1 root root 23 2010-10-13 11:49 /tmp/iamweasel.sh

that script is owned by root, has setuid bit set and it is supposed to print my effective user id as 0. it doesn't

cia@pinkpony:~$ /tmp/iamweasel.sh 
1000

what you need to do is to use a binary setuid wrapper for exec'ing the script

the wrapper:

#include <unistd.h>
#include <stdio.h>
const char *shell="/bin/sh";

int main(int argc, char **argv)
{
    int rc = 0;
    /*dirty argv hack, close your eyes */
    argv[0] = shell;
    /*thank you*/
    seteuid(0);
    setegid(0);
    execv(shell,argv);
}

compile it

gcc -o wraproot wraproot.c

chown and setuid:

sudo chown root:root /home/cia/wraproot
sudo chmod oug+s /home/cia/wraproot

execute the shell script:

cia@pinkpony:~$ id -u
1000
cia@pinkpony:~$ /home/cia/wraproot /tmp/iamweasel.sh 
0

but you may want to use sudo instead because the security implications of the solution described above are... kinda big. for automating tasks which are supposed to run as root you can use a NOPASSWD config in sudoers file granted to a secured admin user.

share|improve this answer