Sudo Keystoke Optimisation

If like me, and a couple of others I’ve spoken to recently, you were not previously aware of “sudo -i”[1] then you might be interested to know that you can save yourself two keystokes by switching from:

sudo su -

To:

sudo -i

From the man page:

‑i‑-login

Run the shell specified by the target user’s password database entry as a login shell. This means that login-specific resource files such as .profile or .login will be read by the shell. If a command is specified, it is passed to the shell for execution via the shell’s ‑c option. If no command is specified, an interactive shell is executed. sudo attempts to change to that user’s home directory before running the shell. The command is run with an environment similar to the one a user would receive at log in. The Command Environment section in the sudoers(5) manual documents how the ‑i option affects the environment in which a command is run when the sudoers policy is in use.

Simple, but useful if you’re someone that enjoys the quest to reduce keystokes.

Footnotes
[1] – This post was originally written about “-s”, but as pointed out by Paul in the comments this is not equivalent to “sudo su -“.

4 thoughts on “Sudo Keystoke Optimisation

  1. Paul Cantle

    Naaasssh,

    This isn’t quite correct. The commands are not equivalent.

    “sudo -s” is not the same as “sudo su -” (and doesn’t behave as such). It’s the equivalent of “sudo su” (without the minus). The minus invokes the user you’re becoming (in this case, root)’s profile (.bash_profile, or whatever). As well as not invoking the profile, without the minus, you will stay in the current working directory of the user who initiated the sudo command.

    So really, you’re not saving any keystrokes.

    Paul

    Reply
    1. Martin Post author

      Hi Paul,

      Thanks for the comment. I should have done a little more investigation. Having poked about in the man page I found the -i option that appears to be a much better fit when looking to achieve the same result as “sudo su -“. I’ll update the post accordingly.

      Martin

      Reply
  2. Paul Cantle

    Hi Mart,

    Actually after a bit more digging and building some new kit, it would appear that -i is still not equivalent to su –

    It would appear that with -i, the $PATH variable is kept as the source user (rather than the destination (in this case, root) user) with the exception that it tags on /root/bin to the end

    See below.

    cantlep@bastion1 /home $ echo $PATH
    /usr/bin:/bin:/usr/local/bin:/usr/local/sbin:/usr/sbin:/home/cantlep/.local/bin:/home/cantlep/bin

    cantlep@bastion1 /home $ sudo -i
    [root@bastion1 ~ ]# echo $PATH
    /usr/bin:/bin:/usr/local/bin:/usr/local/sbin:/usr/sbin:/home/cantlep/.local/bin:/home/cantlep/bin:/root/bin

    cantlep@bastion1 /home $ sudo su –
    Last login: Tue Aug 26 12:37:24 BST 2014 on pts/1
    [root@bastion1 ~ ]# echo $PATH
    /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

    This would probably cause some issues (for example) with calling any commands (as root with -i) that reside in /sbin (unless it’s in the non-priv user’s $PATH variable in the first place (or sym linked from /usr/sbin)).

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>