How run sudo -s after ssh login

2

My Step By Step:

  1. On myserver.com i paste line "sudo -s" to file "~/.bashrc" in home directory for "mylogin"
  2. SSH Login to mylogin@myserver.com
  3. After login:

    root@myserver.com:~$

  4. But, press cntrl+D for exit

    root@myserver.com:~$ exit

    root@myserver.com:~$ exit

    ...

    root@myserver.com:~$

    5.Do not quit.

Why?

Please, tell me the correct method.

satels

Posted 2011-05-21T11:57:49.310

Reputation: 232

Answers

5

You can store a flag in an environment variable:

if [ -z $INITIAL_SUDO ]; then
    exec sudo -s INITIAL_SUDO=1
fi

In this case, initial shell invoked by ssh won't have the INITIAL_SUDO flag on and it will spawn sudo process. Shell invoked by sudo, on the other hand, will have INITIAL_SUDO variable set and won't try to call sudo again.

Note that I call exec sudo -s instead of sudo -s. Otherwise, you'll have to press ctrl+D twice: first for a root shell and second for an initial shell which invoked sudo in the first place.

berekuk

Posted 2011-05-21T11:57:49.310

Reputation: 51

Also note that if sudo -s breaks for any reason, you are locked out of the machine—at least in interactive mode. If you remember what is going on you can do a non-interactive ssh in to fix things. – Seth Robertson – 2011-05-22T03:25:58.617

4

The command sudo -s runs a new shell, which executes your .bashrc.

So what happened here, is that you created a loop where each new shell runs sudo -s, which in turn creates a new shell. At some point, the loop reached the RLIMIT_NPROC limit for the system, and the loop stopped, leaving a huge stack of nested shells. This limit is too high on most system (8192 or unlimited) for you to exit them using your keyboard.

Jerome

Posted 2011-05-21T11:57:49.310

Reputation: