Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question

migrated from 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.

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 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

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/ 
echo `id -u`
cia@pinkpony:~$ ls -la /tmp/ 
-rwsr-sr-x 1 root root 23 2010-10-13 11:49 /tmp/

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/ 

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*/

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
cia@pinkpony:~$ /home/cia/wraproot /tmp/ 

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