'sudo su -' vs 'sudo -i' vs 'sudo /bin/bash' - when does it matter which is used, or does it matter at all?












219















When I'm doing something that requires root be typed in dozens of times in a row, I prefer to switch my session to a root session. In the various tutorials and instructions I have used on the Internet, I see sudo su, sudo su -, sudo -i and sudo /bin/bash being used to open a root session, but I'm not clear on the difference between these and when or if that difference matters.



Can someone clear this up for me?










share|improve this question




















  • 11





    You forgot to ask about sudo -s and sudo su.

    – Radu Rădeanu
    Nov 13 '13 at 6:49








  • 2





    Related: Difference between “su”, “sudo -s”, “sudo -i”

    – Radu Rădeanu
    Nov 13 '13 at 10:45






  • 1





    Related: What's the difference between 'sudo command' and 'sudo sh command?

    – Radu Rădeanu
    Nov 13 '13 at 10:46













  • Related: What is the functional difference between sudo su and sudo -i?

    – Radu Rădeanu
    Nov 13 '13 at 10:49






  • 3





    @RaduRădeanu When I typed the question in I reviewed the suggested questions and none of those really addressed my complete question. The same is true for the questions you linked to. Though they do contain a lot of new information for me, and thank you for linking to them, I found them not as complete as the answer user chaos below provides, which is much more in line with what I was looking for.

    – Paul
    Nov 23 '13 at 23:57


















219















When I'm doing something that requires root be typed in dozens of times in a row, I prefer to switch my session to a root session. In the various tutorials and instructions I have used on the Internet, I see sudo su, sudo su -, sudo -i and sudo /bin/bash being used to open a root session, but I'm not clear on the difference between these and when or if that difference matters.



Can someone clear this up for me?










share|improve this question




















  • 11





    You forgot to ask about sudo -s and sudo su.

    – Radu Rădeanu
    Nov 13 '13 at 6:49








  • 2





    Related: Difference between “su”, “sudo -s”, “sudo -i”

    – Radu Rădeanu
    Nov 13 '13 at 10:45






  • 1





    Related: What's the difference between 'sudo command' and 'sudo sh command?

    – Radu Rădeanu
    Nov 13 '13 at 10:46













  • Related: What is the functional difference between sudo su and sudo -i?

    – Radu Rădeanu
    Nov 13 '13 at 10:49






  • 3





    @RaduRădeanu When I typed the question in I reviewed the suggested questions and none of those really addressed my complete question. The same is true for the questions you linked to. Though they do contain a lot of new information for me, and thank you for linking to them, I found them not as complete as the answer user chaos below provides, which is much more in line with what I was looking for.

    – Paul
    Nov 23 '13 at 23:57
















219












219








219


143






When I'm doing something that requires root be typed in dozens of times in a row, I prefer to switch my session to a root session. In the various tutorials and instructions I have used on the Internet, I see sudo su, sudo su -, sudo -i and sudo /bin/bash being used to open a root session, but I'm not clear on the difference between these and when or if that difference matters.



Can someone clear this up for me?










share|improve this question
















When I'm doing something that requires root be typed in dozens of times in a row, I prefer to switch my session to a root session. In the various tutorials and instructions I have used on the Internet, I see sudo su, sudo su -, sudo -i and sudo /bin/bash being used to open a root session, but I'm not clear on the difference between these and when or if that difference matters.



Can someone clear this up for me?







command-line bash sudo su






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 19 '14 at 14:54









chaos

19.3k85767




19.3k85767










asked Nov 12 '13 at 23:30









PaulPaul

1,48841330




1,48841330








  • 11





    You forgot to ask about sudo -s and sudo su.

    – Radu Rădeanu
    Nov 13 '13 at 6:49








  • 2





    Related: Difference between “su”, “sudo -s”, “sudo -i”

    – Radu Rădeanu
    Nov 13 '13 at 10:45






  • 1





    Related: What's the difference between 'sudo command' and 'sudo sh command?

    – Radu Rădeanu
    Nov 13 '13 at 10:46













  • Related: What is the functional difference between sudo su and sudo -i?

    – Radu Rădeanu
    Nov 13 '13 at 10:49






  • 3





    @RaduRădeanu When I typed the question in I reviewed the suggested questions and none of those really addressed my complete question. The same is true for the questions you linked to. Though they do contain a lot of new information for me, and thank you for linking to them, I found them not as complete as the answer user chaos below provides, which is much more in line with what I was looking for.

    – Paul
    Nov 23 '13 at 23:57
















  • 11





    You forgot to ask about sudo -s and sudo su.

    – Radu Rădeanu
    Nov 13 '13 at 6:49








  • 2





    Related: Difference between “su”, “sudo -s”, “sudo -i”

    – Radu Rădeanu
    Nov 13 '13 at 10:45






  • 1





    Related: What's the difference between 'sudo command' and 'sudo sh command?

    – Radu Rădeanu
    Nov 13 '13 at 10:46













  • Related: What is the functional difference between sudo su and sudo -i?

    – Radu Rădeanu
    Nov 13 '13 at 10:49






  • 3





    @RaduRădeanu When I typed the question in I reviewed the suggested questions and none of those really addressed my complete question. The same is true for the questions you linked to. Though they do contain a lot of new information for me, and thank you for linking to them, I found them not as complete as the answer user chaos below provides, which is much more in line with what I was looking for.

    – Paul
    Nov 23 '13 at 23:57










11




11





You forgot to ask about sudo -s and sudo su.

– Radu Rădeanu
Nov 13 '13 at 6:49







You forgot to ask about sudo -s and sudo su.

– Radu Rădeanu
Nov 13 '13 at 6:49






2




2





Related: Difference between “su”, “sudo -s”, “sudo -i”

– Radu Rădeanu
Nov 13 '13 at 10:45





Related: Difference between “su”, “sudo -s”, “sudo -i”

– Radu Rădeanu
Nov 13 '13 at 10:45




1




1





Related: What's the difference between 'sudo command' and 'sudo sh command?

– Radu Rădeanu
Nov 13 '13 at 10:46







Related: What's the difference between 'sudo command' and 'sudo sh command?

– Radu Rădeanu
Nov 13 '13 at 10:46















Related: What is the functional difference between sudo su and sudo -i?

– Radu Rădeanu
Nov 13 '13 at 10:49





Related: What is the functional difference between sudo su and sudo -i?

– Radu Rădeanu
Nov 13 '13 at 10:49




3




3





@RaduRădeanu When I typed the question in I reviewed the suggested questions and none of those really addressed my complete question. The same is true for the questions you linked to. Though they do contain a lot of new information for me, and thank you for linking to them, I found them not as complete as the answer user chaos below provides, which is much more in line with what I was looking for.

– Paul
Nov 23 '13 at 23:57







@RaduRădeanu When I typed the question in I reviewed the suggested questions and none of those really addressed my complete question. The same is true for the questions you linked to. Though they do contain a lot of new information for me, and thank you for linking to them, I found them not as complete as the answer user chaos below provides, which is much more in line with what I was looking for.

– Paul
Nov 23 '13 at 23:57












1 Answer
1






active

oldest

votes


















284














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





  • su - The command su is used to switch to another user (s witch u ser), but you can also switch to the root user by invoking the command with no parameter. su asks you for the password of the user to switch, after typing the password you switched to the user's environment.


  • 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 your password every time.


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


Types of 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 currently 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.


So the cases are:





  • 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).



Check:



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'





share|improve this answer





















  • 15





    Just a clarification: sudo allows a permitted user to execute a command as the superuser or another user. Anyway, +1 for your effort.

    – Radu Rădeanu
    Nov 13 '13 at 10:38






  • 2





    @chaos Thank you for this excellent answer! It mostly answers my questions, so I went ahead and marked the question answered, but I'm not understanding when it is desirable to run a particular shell. I really only use Ubuntu through command line and I find my most common use for opening a root user session (vs. using sudo) is when I'm doing something that requires heavy usage of root privileges, such as when installing something new or performing a major reconfiguration. I've been using sudo /bin/bash, but apparently that method has a bad rap for some reason I don't understand.

    – Paul
    Nov 23 '13 at 22:22








  • 2





    Also, it is more "correct" to say that su stands for switch user, rather than super user. I.e. run a php script: su www-data /usr/share/script.php or just su www-data for interactive shell. But su without any user name will asume the root (super user) account.

    – oblivian
    Feb 19 '14 at 22:29













  • Chaos - "shopt -q login_shell && echo 'Login shell' || echo 'No login shell'" These kinds of suggestions I see often, but why make it so long? The && operator means if exit code 0 then "do next command" and the double pipes (||) means else (if not 0) then do this command. So what it is basically saying if exit code 0 then echo "Login Shell", if exit code 1 (fail) then echo "No login". Why not just "shopt -q login_shell ; echo $?" $? means the result/exit code of the preceding command. In most if not all programs 0 means success, 1 or more means fail. So if the echo is 0 = success...

    – oblivian
    Feb 19 '14 at 22:45






  • 1





    @Paul: sudo -i is suggested. Read here: ubuntuforums.org/showthread.php?t=1817402 and here: unix.stackexchange.com/questions/98531/…

    – Marco Sulla
    Sep 10 '14 at 8:21











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f376199%2fsudo-su-vs-sudo-i-vs-sudo-bin-bash-when-does-it-matter-which-is-used%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









284














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





  • su - The command su is used to switch to another user (s witch u ser), but you can also switch to the root user by invoking the command with no parameter. su asks you for the password of the user to switch, after typing the password you switched to the user's environment.


  • 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 your password every time.


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


Types of 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 currently 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.


So the cases are:





  • 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).



Check:



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'





share|improve this answer





















  • 15





    Just a clarification: sudo allows a permitted user to execute a command as the superuser or another user. Anyway, +1 for your effort.

    – Radu Rădeanu
    Nov 13 '13 at 10:38






  • 2





    @chaos Thank you for this excellent answer! It mostly answers my questions, so I went ahead and marked the question answered, but I'm not understanding when it is desirable to run a particular shell. I really only use Ubuntu through command line and I find my most common use for opening a root user session (vs. using sudo) is when I'm doing something that requires heavy usage of root privileges, such as when installing something new or performing a major reconfiguration. I've been using sudo /bin/bash, but apparently that method has a bad rap for some reason I don't understand.

    – Paul
    Nov 23 '13 at 22:22








  • 2





    Also, it is more "correct" to say that su stands for switch user, rather than super user. I.e. run a php script: su www-data /usr/share/script.php or just su www-data for interactive shell. But su without any user name will asume the root (super user) account.

    – oblivian
    Feb 19 '14 at 22:29













  • Chaos - "shopt -q login_shell && echo 'Login shell' || echo 'No login shell'" These kinds of suggestions I see often, but why make it so long? The && operator means if exit code 0 then "do next command" and the double pipes (||) means else (if not 0) then do this command. So what it is basically saying if exit code 0 then echo "Login Shell", if exit code 1 (fail) then echo "No login". Why not just "shopt -q login_shell ; echo $?" $? means the result/exit code of the preceding command. In most if not all programs 0 means success, 1 or more means fail. So if the echo is 0 = success...

    – oblivian
    Feb 19 '14 at 22:45






  • 1





    @Paul: sudo -i is suggested. Read here: ubuntuforums.org/showthread.php?t=1817402 and here: unix.stackexchange.com/questions/98531/…

    – Marco Sulla
    Sep 10 '14 at 8:21
















284














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





  • su - The command su is used to switch to another user (s witch u ser), but you can also switch to the root user by invoking the command with no parameter. su asks you for the password of the user to switch, after typing the password you switched to the user's environment.


  • 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 your password every time.


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


Types of 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 currently 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.


So the cases are:





  • 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).



Check:



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'





share|improve this answer





















  • 15





    Just a clarification: sudo allows a permitted user to execute a command as the superuser or another user. Anyway, +1 for your effort.

    – Radu Rădeanu
    Nov 13 '13 at 10:38






  • 2





    @chaos Thank you for this excellent answer! It mostly answers my questions, so I went ahead and marked the question answered, but I'm not understanding when it is desirable to run a particular shell. I really only use Ubuntu through command line and I find my most common use for opening a root user session (vs. using sudo) is when I'm doing something that requires heavy usage of root privileges, such as when installing something new or performing a major reconfiguration. I've been using sudo /bin/bash, but apparently that method has a bad rap for some reason I don't understand.

    – Paul
    Nov 23 '13 at 22:22








  • 2





    Also, it is more "correct" to say that su stands for switch user, rather than super user. I.e. run a php script: su www-data /usr/share/script.php or just su www-data for interactive shell. But su without any user name will asume the root (super user) account.

    – oblivian
    Feb 19 '14 at 22:29













  • Chaos - "shopt -q login_shell && echo 'Login shell' || echo 'No login shell'" These kinds of suggestions I see often, but why make it so long? The && operator means if exit code 0 then "do next command" and the double pipes (||) means else (if not 0) then do this command. So what it is basically saying if exit code 0 then echo "Login Shell", if exit code 1 (fail) then echo "No login". Why not just "shopt -q login_shell ; echo $?" $? means the result/exit code of the preceding command. In most if not all programs 0 means success, 1 or more means fail. So if the echo is 0 = success...

    – oblivian
    Feb 19 '14 at 22:45






  • 1





    @Paul: sudo -i is suggested. Read here: ubuntuforums.org/showthread.php?t=1817402 and here: unix.stackexchange.com/questions/98531/…

    – Marco Sulla
    Sep 10 '14 at 8:21














284












284








284







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





  • su - The command su is used to switch to another user (s witch u ser), but you can also switch to the root user by invoking the command with no parameter. su asks you for the password of the user to switch, after typing the password you switched to the user's environment.


  • 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 your password every time.


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


Types of 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 currently 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.


So the cases are:





  • 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).



Check:



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'





share|improve this answer















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





  • su - The command su is used to switch to another user (s witch u ser), but you can also switch to the root user by invoking the command with no parameter. su asks you for the password of the user to switch, after typing the password you switched to the user's environment.


  • 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 your password every time.


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


Types of 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 currently 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.


So the cases are:





  • 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).



Check:



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'






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 17 at 7:04









Pablo Bianchi

2,6151532




2,6151532










answered Nov 13 '13 at 9:43









chaoschaos

19.3k85767




19.3k85767








  • 15





    Just a clarification: sudo allows a permitted user to execute a command as the superuser or another user. Anyway, +1 for your effort.

    – Radu Rădeanu
    Nov 13 '13 at 10:38






  • 2





    @chaos Thank you for this excellent answer! It mostly answers my questions, so I went ahead and marked the question answered, but I'm not understanding when it is desirable to run a particular shell. I really only use Ubuntu through command line and I find my most common use for opening a root user session (vs. using sudo) is when I'm doing something that requires heavy usage of root privileges, such as when installing something new or performing a major reconfiguration. I've been using sudo /bin/bash, but apparently that method has a bad rap for some reason I don't understand.

    – Paul
    Nov 23 '13 at 22:22








  • 2





    Also, it is more "correct" to say that su stands for switch user, rather than super user. I.e. run a php script: su www-data /usr/share/script.php or just su www-data for interactive shell. But su without any user name will asume the root (super user) account.

    – oblivian
    Feb 19 '14 at 22:29













  • Chaos - "shopt -q login_shell && echo 'Login shell' || echo 'No login shell'" These kinds of suggestions I see often, but why make it so long? The && operator means if exit code 0 then "do next command" and the double pipes (||) means else (if not 0) then do this command. So what it is basically saying if exit code 0 then echo "Login Shell", if exit code 1 (fail) then echo "No login". Why not just "shopt -q login_shell ; echo $?" $? means the result/exit code of the preceding command. In most if not all programs 0 means success, 1 or more means fail. So if the echo is 0 = success...

    – oblivian
    Feb 19 '14 at 22:45






  • 1





    @Paul: sudo -i is suggested. Read here: ubuntuforums.org/showthread.php?t=1817402 and here: unix.stackexchange.com/questions/98531/…

    – Marco Sulla
    Sep 10 '14 at 8:21














  • 15





    Just a clarification: sudo allows a permitted user to execute a command as the superuser or another user. Anyway, +1 for your effort.

    – Radu Rădeanu
    Nov 13 '13 at 10:38






  • 2





    @chaos Thank you for this excellent answer! It mostly answers my questions, so I went ahead and marked the question answered, but I'm not understanding when it is desirable to run a particular shell. I really only use Ubuntu through command line and I find my most common use for opening a root user session (vs. using sudo) is when I'm doing something that requires heavy usage of root privileges, such as when installing something new or performing a major reconfiguration. I've been using sudo /bin/bash, but apparently that method has a bad rap for some reason I don't understand.

    – Paul
    Nov 23 '13 at 22:22








  • 2





    Also, it is more "correct" to say that su stands for switch user, rather than super user. I.e. run a php script: su www-data /usr/share/script.php or just su www-data for interactive shell. But su without any user name will asume the root (super user) account.

    – oblivian
    Feb 19 '14 at 22:29













  • Chaos - "shopt -q login_shell && echo 'Login shell' || echo 'No login shell'" These kinds of suggestions I see often, but why make it so long? The && operator means if exit code 0 then "do next command" and the double pipes (||) means else (if not 0) then do this command. So what it is basically saying if exit code 0 then echo "Login Shell", if exit code 1 (fail) then echo "No login". Why not just "shopt -q login_shell ; echo $?" $? means the result/exit code of the preceding command. In most if not all programs 0 means success, 1 or more means fail. So if the echo is 0 = success...

    – oblivian
    Feb 19 '14 at 22:45






  • 1





    @Paul: sudo -i is suggested. Read here: ubuntuforums.org/showthread.php?t=1817402 and here: unix.stackexchange.com/questions/98531/…

    – Marco Sulla
    Sep 10 '14 at 8:21








15




15





Just a clarification: sudo allows a permitted user to execute a command as the superuser or another user. Anyway, +1 for your effort.

– Radu Rădeanu
Nov 13 '13 at 10:38





Just a clarification: sudo allows a permitted user to execute a command as the superuser or another user. Anyway, +1 for your effort.

– Radu Rădeanu
Nov 13 '13 at 10:38




2




2





@chaos Thank you for this excellent answer! It mostly answers my questions, so I went ahead and marked the question answered, but I'm not understanding when it is desirable to run a particular shell. I really only use Ubuntu through command line and I find my most common use for opening a root user session (vs. using sudo) is when I'm doing something that requires heavy usage of root privileges, such as when installing something new or performing a major reconfiguration. I've been using sudo /bin/bash, but apparently that method has a bad rap for some reason I don't understand.

– Paul
Nov 23 '13 at 22:22







@chaos Thank you for this excellent answer! It mostly answers my questions, so I went ahead and marked the question answered, but I'm not understanding when it is desirable to run a particular shell. I really only use Ubuntu through command line and I find my most common use for opening a root user session (vs. using sudo) is when I'm doing something that requires heavy usage of root privileges, such as when installing something new or performing a major reconfiguration. I've been using sudo /bin/bash, but apparently that method has a bad rap for some reason I don't understand.

– Paul
Nov 23 '13 at 22:22






2




2





Also, it is more "correct" to say that su stands for switch user, rather than super user. I.e. run a php script: su www-data /usr/share/script.php or just su www-data for interactive shell. But su without any user name will asume the root (super user) account.

– oblivian
Feb 19 '14 at 22:29







Also, it is more "correct" to say that su stands for switch user, rather than super user. I.e. run a php script: su www-data /usr/share/script.php or just su www-data for interactive shell. But su without any user name will asume the root (super user) account.

– oblivian
Feb 19 '14 at 22:29















Chaos - "shopt -q login_shell && echo 'Login shell' || echo 'No login shell'" These kinds of suggestions I see often, but why make it so long? The && operator means if exit code 0 then "do next command" and the double pipes (||) means else (if not 0) then do this command. So what it is basically saying if exit code 0 then echo "Login Shell", if exit code 1 (fail) then echo "No login". Why not just "shopt -q login_shell ; echo $?" $? means the result/exit code of the preceding command. In most if not all programs 0 means success, 1 or more means fail. So if the echo is 0 = success...

– oblivian
Feb 19 '14 at 22:45





Chaos - "shopt -q login_shell && echo 'Login shell' || echo 'No login shell'" These kinds of suggestions I see often, but why make it so long? The && operator means if exit code 0 then "do next command" and the double pipes (||) means else (if not 0) then do this command. So what it is basically saying if exit code 0 then echo "Login Shell", if exit code 1 (fail) then echo "No login". Why not just "shopt -q login_shell ; echo $?" $? means the result/exit code of the preceding command. In most if not all programs 0 means success, 1 or more means fail. So if the echo is 0 = success...

– oblivian
Feb 19 '14 at 22:45




1




1





@Paul: sudo -i is suggested. Read here: ubuntuforums.org/showthread.php?t=1817402 and here: unix.stackexchange.com/questions/98531/…

– Marco Sulla
Sep 10 '14 at 8:21





@Paul: sudo -i is suggested. Read here: ubuntuforums.org/showthread.php?t=1817402 and here: unix.stackexchange.com/questions/98531/…

– Marco Sulla
Sep 10 '14 at 8:21


















draft saved

draft discarded




















































Thanks for contributing an answer to Ask Ubuntu!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f376199%2fsudo-su-vs-sudo-i-vs-sudo-bin-bash-when-does-it-matter-which-is-used%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

flock() on closed filehandle LOCK_FILE at /usr/bin/apt-mirror

Mangá

Eduardo VII do Reino Unido