Main menu

Skip to content
  • About

Kernel Craft

Exploring Technology and Measuring it's Depth!!!

RSS

Monthly Archives: December 2013

Info on Sudo / Su / login shells

Posted by kernelcraft on December 13, 2013

I had few friends who asked me about the difference between Sudo, Su, login shells etc.

To explain this you need to know what the programs do:

su : The command su is to switch to the super user (root), but you can also switch to another user by invoking the command with the username su username. su asks you for the password of this user after typing the password you switched the user.

sudo : sudo is meant to run a single command with root privileges. But unlike su it prompts you for the password of the current user. This user must be in the sudoers file (or a group that is in the sudoers file). By default, Ubuntu “remembers” your password for 15 minutes, so that you don’t have to type you password every times.

bash : A text-interface to interact with the computer. It’s important to understand the difference between login, non-login, interactive and non-interactive shells:

Login shell: A login shell logs you into the system as a specified user, necessary for this is a username and password. When you hit ctrl+alt+F1 to login into a virtual terminal you get after successful login a login shell.

Non-login shell: A shell that is executed without logging in, necessary for this is a current logged in user. When you open a graphic terminal in gnome it is a non-login shell.

Interactive shell: A shell (login or non-login) where you can interactively type or interrupt commands. For example a gnome terminal.

Non-interactive shell: A (sub)shell that is probably run from an automated process you will see neither input nor output.

sudo su Calls sudo with the command su. Bash is called as interactive non-login shell. So bash only executes .bashrc. You can see that after switching to root you are still in the same directory:

user@host:~$ sudo su
root@host:/home/user#

sudo su – : This time it is a login shell, so /etc/profile, .profile and .bashrc are executed and you will find yourself in root’s home directory with root’s environment.

sudo -i : It is nearly the same as sudo su – The -i (simulate initial login) option runs the shell specified by the password database entry of the target user as a login shell. This means that login-specific resource files such as .profile, .bashrc or .login will be read and executed by the shell.

sudo /bin/bash : This means that you call sudo with the command /bin/bash. /bin/bash is started as non-login shell so all the dot-files are not executed, but bash itself reads .bashrc of the calling user. Your environment stays the same. Your home will not be root’s home. So you are root, but in the environment of the calling user.

sudo -s : reads the $SHELL variable and executes the content. If $SHELL contains /bin/bash it invokes sudo /bin/bash (see above).

To check if you are in a login shell or not (works only in bash because shopt is a builtin command):

shopt -q login_shell && echo ‘Login shell’ || echo ‘No login shell’

Advertisements
Leave a comment Posted in Linux Tagged Login shell

Categories

  • Linux (81)
  • Linux Scripts (8)
  • Other Tech-Stuffs (12)
  • Windows (30)
  • Windows Scripts (1)

Archives

  • June 2015 (1)
  • December 2013 (1)
  • September 2013 (2)
  • July 2013 (7)
  • June 2013 (3)
  • March 2013 (1)
  • July 2012 (3)
  • June 2012 (5)
  • May 2012 (3)
  • April 2012 (2)
  • March 2012 (1)
  • February 2012 (9)
  • January 2012 (11)
  • December 2011 (15)
  • November 2011 (9)
  • October 2011 (1)
  • February 2011 (3)
  • January 2011 (1)
  • January 2010 (2)
  • September 2009 (1)
  • August 2009 (1)
  • July 2009 (1)
  • June 2009 (4)
  • May 2009 (1)
  • April 2009 (2)
  • March 2009 (1)
  • February 2009 (2)
  • January 2009 (4)
  • December 2008 (2)
  • November 2008 (1)
  • October 2008 (1)
  • September 2008 (1)
  • July 2008 (3)
  • May 2008 (1)
  • April 2008 (1)
  • February 2007 (1)

Blogroll

  • Bash Shell
  • My Website
  • Security Related Stuffs
  • Theme Showcase

Meta

  • Register
  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.com
Advertisements

Blog at WordPress.com.