Jun 142011

sudoI think that sudo is become a wide used command with Ubuntu, where you don’t even have a root password, before that probably it was used only in some data-centers to restrict access to some commands.

sudo allows a permitted user to execute a command as the superuser or another user, as specified in the sudoers file.

In this article I will show some uses a little less common for this command, for a general description of the command you could read the page about sudo on wikipedia


 sudo !!

you wanted to give a command as root but you forgot to use sudo ? Don’t worry with this command you’ll use the history combined with sudo, the argument !! has the same meaning that !-1 , so this command runs your last command as root.

Obviously this could be used also as

 sudo !-2

if you give another command after the one you want to run as root, but take care on which command are you running as root.


 sudo -i

you can use this command to have an interactive shell as root.
To have a shell with a different user use the format

 sudo -u username -i

The -i (simulate initial login) option runs the shell specified in the passwd entry of the target user 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. Otherwise, an interactive shell is executed.

3) Sudo output redirection, thanks to petur.eu:

We are faced with a problem when trying to redirect with sudo, as the second part of the command is not executed with root privileges.

sudo command > outputfile

The solution is to use;

‘sudo tee’ instead of the ‘>’ operator,
‘sudo tee -a’ instead of the ‘>>’ operator.

sudo command | sudo tee outputfile


 :w !sudo tee %

This command can be used in your vi/vim to save a file you have opened and edited as a user, but you can not save with your user rights. In this way you don’t have to save the file in /tmp, become root and move the file from /tmp to the real place.

Popular Posts:

Flattr this!

  12 Responses to “4 Tricks with sudo”

  1. “I think that sudo is become a wide used command with Ubuntu, where you don’t even have a root password, before that probably it was used only in some data-centers to restrict access to some commands.”


    Sudo has been around in BSD since 1980. That predates the first Linux kernel by 11 years so Ubuntu was not even a thought when sudo was becoming “wide used”.

    A Breif History of Sudo

    Since Ubuntu was not even publicly released until 2004 it predates Ubuntu by 24 years!

    A Breif History of Ubuntu

    Just because *YOU* are new to these commands and *YOU* are using them more often now does not mean that they are just now becoming more “wide used”.

    Please check your facts before you blindly type things like “sudo is become a wide used command with Ubuntu”. Ubuntu, is a good distribution *BECAUSE* of widely used GNU commands like sudo and others not the other way around.

    • Hello David,

      I used Sudo on AIX before Ubuntu even existed, so i know that it’s a old and solid command that is part of Unix/BSD standard.
      I meant that the use of sudo in the desktop version of GNU/Linux was not so widespread before ubuntu, is this better?

      Peace 🙂

      • Sorry if I came across a bit gruff in my comment, I just get a bit overly critical I guess when I see all the credit that keeps going towards Ubuntu. No offense, I appreciate Ubuntu for it’s marketing and drive to make Windows users more comfortable with Linux. It’s just that as a Long time Linux user (pre 2000) I know that many things that Ubuntu takes credit for were and are achieved by larger Linux Distributions like Redhat, Suse, and Debian (among many others).

        For instance sudo was very widly used in Redhat/Alliance days (1999) in data centers long before Ubuntu was even released. Ubuntu does a nice job of promoting the use of Linux as a whole but I am not sure I agree with the statement that sudo was not as widely used before Ubuntu and that Ubuntu is directly related to any growth in it’s usage.

        That said, agree or not, you have shown some nice usages of sudo here and I have another that can come in handy…

        If you want to ssh to a server and run a command on the other server as root you cannot simply use the command:

        ssh someserver sudo whoami

        It will fail with the error “sudo: sorry, you must have a tty to run sudo”

        However you can tell ssh to allocate a tty for it’s session using the “-t” flag as follows:

        ssh -t someserver sudo whoami
  2. Very informative and useful article on the use of sudo. I learned quite a few things from it that I plan to use in my Ubuntu installation. Keep up the good work!

  3. sudo bash -c ‘command > outputfile’

  4. Hi All,
    I have a problem here. I have logged in linux with my id, then sudo’d (sudo su – xyz) with my password into an account permissible only to view the log prints.
    Now, I need to edit some of the exploded files in the domain from my app, but it has permission other than root (abc).
    I did try :w !sudo tee % after I edited my read-only file (for my sudo’d account xyz) but it is prompting me to enter the sudo password for xyz. I entered properly but still it is not taking.

    Any workaround to get this working? Any help/leads are appreciated.


    • If you have the password of xyz, why you use (sudo su – xyz ?)

      If the account xyz can become root (i don’t know the setup of your machine), :w !sudo tee % should work.
      Or just do “sudo -i” become root and edit the files, or change the permissions/ownership of the files, if this is possible.

  5. sudo sudo su –

 Leave a Reply

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=""> <s> <strike> <strong>