Run the last command as root

sudo !!
Useful when you forget to use sudo for a command. "!!" grabs the last run command.

1146
By: root
2009-01-26 10:26:48

5 Alternatives + Submit Alt

What Others Think

This one might be my most commonly used "one liner". I even wrote a post about it in my blog ;)
JackiesJungle · 483 weeks and 6 days ago
Doesn't this only work under ZSH ?
pyrho · 483 weeks and 5 days ago
doesnt seem to work under bash/centos4?
trix · 483 weeks and 4 days ago
work on Ubuntu 8.04
mrbyte · 483 weeks and 4 days ago
works on fedora with bash!
betsubetsu · 482 weeks and 6 days ago
I almost never use sudo. It promotes bad security: using the same password for root and and for regular users.
grep · 482 weeks and 6 days ago
This command owns all. chmod 755 foo.bar operation not permitted sudo !! sudo chmod 755 foo.bar [sudo] password for waldo: (insert text of elation here)
farglenargle · 482 weeks and 2 days ago
works in bash/Debian !!
prayer · 481 weeks and 5 days ago
I love you. Befor I'v been so pissed off after typing long stings of command, and then have forgotten sudo. Therefor having to go all the way to the beginning of the command and enter sudo. Love it
Meeko · 480 weeks and 1 day ago
@meeko "All the way to the beginning"? Just hit the HOME key. Life is short! :-) sudo is great for sandwich making also: http://www.xkcd.com/149/
mpb · 479 weeks and 5 days ago
lots of the machines we use don't do home key - they do ~ instead - go figure. this could be very usefull. Thanks. I use !$ a lot. repeats the last word in the previous command e.g. ping nslookup !$ will translate to nslookup obviously the first ! grabs the previous line, and the second character does somnething else - ! grabs the first word, $ grabs the last word. anyone know any other variants or where to look up the man page - er... is it regexp or part of sh or perl or what??? ('scuse my ignorance - the unix needs a reverse lookup database)
bulwynkl · 479 weeks and 1 day ago
Regarding using the "home" key to get to the beginning of a command.. I just wanted to note, that you can almost always use "Ctrl-A" to jump to the beginning of the line in Bash and many other programs. I think it's part of libreadline? Also, if you're using Bash in a Screen session, use Ctrl-A twice to jump to the beginning of a line.
cmang · 479 weeks ago
The HOME key is a single key press. Why chose a two key sequence instead of one?
mpb · 478 weeks and 5 days ago
I love this because it always feels like I'm yelling at my computer. do_something # i don't wanna... SUDO!! # ok, fine...
isaacs · 478 weeks and 2 days ago
I vote this down, because 'sudo' is not meant to be used in this way. Totally bad security practice! Even if you are the only user in your computer, you are advised to put only those commands that are safe and commonly used in the sudoers file. Otherwise, you are probably going to learn it in a hard way.
jxy · 478 weeks ago
And I vote it up, because this -is- how sudo is intended to be used in many modern systems such as Ubuntu and OS X.
qubyte · 476 weeks and 3 days ago
I use Ctrl+p, Ctrl+a, sudo before.
kylexlau · 471 weeks and 2 days ago
OK, this inspired me to patch xkcd-149 :) http://www.vanginderachter.be/2009/patched-xkcd-149-make-me-a-sandwich/
svg · 467 weeks and 4 days ago
This is great!
marcop · 452 weeks and 2 days ago
Should work under bash, but you may have it disabled (set -H or set -o histexpand to reenable)
unixmonkey6345 · 452 weeks and 2 days ago
nice
haven489 · 434 weeks and 6 days ago
I wrote an article about this and other popular one-liners. See it here: http://www.catonmat.net/blog/top-ten-one-liners-from-commandlinefu-explained/
pkrumins · 427 weeks and 2 days ago
pretty good
deargodzw · 427 weeks and 2 days ago
Explained the next 10 top one-liners: http://www.catonmat.net/blog/the-next-ten-one-liners-from-commandlinefu-explained/
pkrumins · 426 weeks and 3 days ago
Worky fine, OS X Snow Leopard 10.6.3.
hced · 422 weeks and 3 days ago
before I found `sudo !!` I used to do the following: 1) arrow up (to get the last command from history) 2) Ctrl+A (to put the cursor at the front of the command) 3) type "sudo " and hit enter
jemptymethod · 397 weeks and 5 days ago
WTF ? This is the best command for everyone ? Damn ubuntu lamers...
gormux · 387 weeks and 2 days ago
Don't listen to the "Sudo promotes bad security" types. You know what promotes worse security? Actually operating as root.
Karunamon · 338 weeks and 4 days ago
awesome
jldbasa · 328 weeks and 5 days ago
@jemptymethod me too
dfang · 303 weeks and 5 days ago
Works in OS X Mountain Lion 10.8.1
delete · 298 weeks and 2 days ago
this command trick and many like it are part of the history command. try man history for more variants if your shell does not have history it wont work
bulwynkl · 291 weeks ago
this command trick and many like it are part of the history command. try man history for more variants if your shell does not have history it wont work
bulwynkl · 291 weeks ago
safer to type: /usr/bin/sudo !! For *any* command that will prompt for a password, it is more secure to type the full path (or pull it from shell history) because a common hack technique, if they can get access to your account, is to mess with your $PATH and plant a spoofed sudo which captures your typed password, prints an error message, and then execs the real sudo. The user thinks "Oh, I miss-typed the password. I'll type it again".
mpb · 173 weeks and 2 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: