-2

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

2
  • 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) Oct 13, 2010 at 7:53
  • Can you add a lot more detail and clarity to your question please.
    – Chopper3
    Oct 13, 2010 at 8:08

2 Answers 2

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.

1

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.