@Braiam Actually, I did mean duplicate; the two highest-voted answers to that one specifically explain what sudo !! does, and there are two answers that actually specifically explain what !! does.
–
Michael KjörlingNov 18 '13 at 18:50

sudo bang bang is a very useful command when working in Command Line Interface.

Some Linux distros have you login as a user instead of an administrator.

So, to do something admin-wise, you have to proceed the command with sudo (Super-User DO), which tells the system “you will do this, because I said so.” The !! / bang-bang (! = bang) is basically a shortcut you can use to repeat the previous command.

So, typical scenario is that you try a command, and it kicks back a message saying you have to be an admin to do that. So, you can either type sudo to run that command as super-user/admin, or you can type sudo !! where !! tells the system to use the previous command that was attempted.UfH

It's also a very dangerous command if you're not 100% sure what your previous command was. It's almost always safer to just hit the up arrow, then the home key, then type sudo and look at the command line.
–
ShadurNov 18 '13 at 16:06

2

@Shadur Usually your previous command was about 1 second ago. The normal use case is the one described in the answer: you forget to sudo something and get an error, so you immediately want to sudo it
–
Michael MrozekNov 18 '13 at 16:21

sudo doesn't mean "super-user do", it goes as do a command as if I had done su (su meaning switch user [and not super user]); i.e., you can sudo commands as any user, not only root (-u switch); same goes with su [user] [-c command]
–
ssiceNov 18 '13 at 19:11

The bang bang (!!) command is a shortcut to repeat and run the previous command you entered in your terminal. This command is very useful when you forget that you need admin rights to make a certain action, and lets you repeat it with super-user rights just by typing,

sudo !!

!! grabs the last run command.

For example:

apt-get update

The output will be,

E: Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied)
E: Unable to lock directory /var/lib/apt/lists/
E: Could not open lock file /var/lib/dpkg/lock - open (13: Permission denied)
E: Unable to lock the administration directory (/var/lib/dpkg/), are you root?

Which means !! part grabs the previous run command apt-get update and the preceeding sudo part makes the command to run with superuser rights.

And how the sudo !! runs the previous command with superuser privileges means,normally all the commands we entered on the terminal are stored in the command history.Run the history command on the terminal,it shows all the commands you entered.The !! part in the sudo !! grabs the last command stored in the command history and the whole sudo !! runs the last command with admin privileges.

!! is part of the functionality of the shell (in the case of Ubuntu this is probably bash, but other shells like zsh or csh support this, too) called "history expansion". It behaves in a similar way as other expansions in that the shell expands the 'placeholder' to a set of words. While foo* would be expanded to a list of all files starting with 'foo', !! gets expanded to the contents of the previous command line.

$ echo foobar
foobar
$ echo !!
echo foobar
$ !!
echo foobar

Like other expansions, this is done entirely by the shell, so if you type !! after some other command, this command is not aware that there was a !!, but will only see the previous command line. (Unlike other expansions, history expansion happens before a command is saved in the history, that is instead of !! the replaced command line will be saved to the history.)

The sudo command allows executing commands as another user, provided the permissions ar granted by the security policy (default is configured in /etc/sudoers).

By default the root password remains unset in Ubuntu. In order to perform system administration tasks the user created during installation is granted sudo rights. This user can now execute any command on the shell as root, just by prepending sudo. Some GUI programs use the sudo mechanism, too, for example package management.

The reason why sudo can execute other commands as root (or another user) is that the sudo binary (/usr/bin/sudo) has the setuid bit set in its permission and belongs to root. Any (binary) executable with set setuid bit is run with the permissions of its owner. This means that sudo runs effectively with root permissions no matter which user actually called it. Only he internal security policies of sudo manage which user is allowed what and prevent arbitrary users to do arbitrary things.