Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Sometimes when I run a command I don't notice that I needed to run it as super user to have enough permission.

Is there a way to run the same command again but as a super user?

share|improve this question

9 Answers 9

up vote 135 down vote accepted

The simplest way is to run:

sudo !!

This will run the last command but as super user.

Source.

share|improve this answer
5  
I found this quite useful and I hadn't come across it before (though it may be common knowledge). I couldn't find it on the site so I thought I would answer it myself. –  Trufa May 27 '13 at 20:32
25  
As an aside, executing !! on its own runs your previous command as per saved in history. In addition, doing !-1, !-2, and so on executes your last command, second last command, and so on. Handy! –  oaskamay May 27 '13 at 20:41
6  
As an another aside, !<command> will execute last event from ~/.bash_history that start with <command> –  Radu Rădeanu May 27 '13 at 20:54
1  
ctrl-r is also very useful. it will search backwards in your history for commands matching what you type. This will allow you edit the command before running it again –  KayEss May 28 '13 at 3:37
4  
This stupid ! bash thing copied from tcsh has screwed me up more times than it's ever been useful. Double quotes don't protect from the interpretation of !. $ foo "!bar" -> !bar: event not found. –  Kaz May 28 '13 at 7:26

​​​​​​​​​​​​​​​​​​​​​​​​​​​​​enter image description here

​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​Answer: you enter sudo !! straight afterwards to repeat the previous command with escalated privileges.

edit: Image found via reddit, and it's a parody of the original xkcd comic by Randall Munroe.

share|improve this answer
3  
please do not have a pro-or-against conversation here in comments. If you wish to discuss the merits or otherwise, please use Meta. Thanks. –  fossfreedom May 28 '13 at 17:42

You could try the up-arrow key to scroll through your old commands and rewrite/change them and add the sudo in front of them. (the home button above the arrow keys will set the position to the beginning.)

I dislike the sudo !! idea, because sometimes you add another command in between and don't remember anymore.

share|improve this answer
1  
Fortunately, when you do sudo !!, the shell will expand !! so that you can see what command it's trying to run. So, it may end up being irritating to hit Ctrl-C and find the right command, but it at least won't take you unawares. –  Reid May 27 '13 at 21:58
4  
@Reid It won't expand until after you hit enter, and if you're within that window where you don't have to enter your password again, it will run without confirmation. +1 to using the history instead of blindly using !! –  Izkata May 28 '13 at 1:33
5  
@Izkata: I suppose that's true. I use zsh, so I just type sudo !!<TAB> and it autocompletes !! for me. Bash can expand history with M-^ (for me, alt+shift+6), but that's less fluent (you could rebind this, though). There is also shopt -s histverify, which makes it so that when you use history expansion and hit enter, it instead gives you another prompt line with the expansion performed to let you see/edit it. –  Reid May 28 '13 at 1:46
1  
I was thinking more of the situation where I typed a command, the command returned that permission was denied, and I know I want to run this exact same command again but as sudo, the process of pressing up, home, typing sudo and feels much slower to me. It's the way I have been doing it forever and it just felt inefficient. –  Trufa May 28 '13 at 9:03
    
@Reid: shopt -s histverify won't help a single bit. Most people will get used to hitting Enter twice. At that point they're still too late to check what happened. –  spaceknarf May 30 '13 at 9:29

I'll just add an alternative: as short as typing sudo !! and more flexible:

Up-arrow (once, or until you find the command you want to sudo/edit, if it's older)
ctrl+A   (place cursor at beginning of line)
"sudo "
Enter

It's exactly the same number of keystrokes (I count "ctrl+A" as one...), but adding "up arrow" you can do it on whichever command is in your history.

share|improve this answer
1  
ctrl+A is from the emacs bindings, you can also use the vi bindings (set -o vi). Then you can Escape (go to command mode), and then 'I' (capital 'i', insert at beginning of line). –  Gerhard Burger May 30 '13 at 10:31
1  
@GerhardBurger: I know, but, weirdly, I love vi (especially vim) for multi-line documents editing, but I do much prefer the emacs binding in the shell (having to switch from command to edit mode in the shell doesn't really makes sense, imo, and the shortcuts of emacs are quite enough for everyday use, even if less powerfull) –  Olivier Dulac May 30 '13 at 10:54
    
True, I added it just for completeness, for me it's the same, however I find searching with '/' very convenient ;) –  Gerhard Burger May 30 '13 at 11:01
    
@GerhardBurger: about vi mode's "/", regular (emacs binding) bash has also a very handy way to find previous commands by typing a few letters then 'ctrl+r' until you find it –  Olivier Dulac Jun 13 '13 at 8:27

There are a few ways to do this

  • Simply enter the command again adding sudo before the command

  • Press Up arrow to get the last command and put sudo in front of it.

  • Enter sudo !!

The !! expands to the last entered command. You can do similar things with other things with the command line history see here

share|improve this answer

I use zsh instead of bash and have this in my ~/.zshrc, so pressing Alt+S inserts sudo at the beginning of the command line:

insert_sudo () { zle beginning-of-line; zle -U "sudo " }
zle -N insert-sudo insert_sudo
bindkey "^[s" insert-sudo

Unfortunately, I couldn't track down a way to do the same thing in bash.

share|improve this answer
2  
You can easily do the same in Bash using GNU Readline: add to your ~/.inputrc "\es":"\C-asudo " and you will move to the start of a long line and sudo will be inserted at the start of the line upon pressing alt-s. Restart terminal or re-read ~/.inputrc for it to work. –  user76204 May 28 '13 at 17:25

This question could have been generalized into "Run same command but with some modification", since one of the brilliant design decisions in Unix, is that what's implemented once in the shell, can be leveraged by any command being invoked from the shell.

In other words, this trick doesn't apply just to sudo:

  • Repeat the previous command as superuser (sudo !!)
  • Repeat the previous command and time it (time !!)
  • Repeat the previous command with a different environment (env A=b B=z !!)
  • Repeat the previous command (!!)

History expansion (also known as history substitution) is just a shell feature to make repeating previous commands easier (with less keystrokes). Check your shell man page for history expansion. It describes many more tricks and short-cuts you can do with history for example:

You may refer to previous commands by number:

!55                 # repeat command number 55
!-1                 # repeat the previous command, alias for !!
!-2                 # repeat the command before the previous command

You may refer to individual words in previous commands:

some-command !$     # run some-command on the last arg of previous command

You can also search before repeating:

!?awk               # repeat the last command including the string 'awk'

You can search the history and replace something by something else:

!?awk?:s/str1/str2/  # repeat previous command matching 'awk' replacing str1 by str2

And much more, since you can combine command selection, arg-selection, search and replace independently.

The man page of your shell (in this case man bash) is your friend.

share|improve this answer
1  
briliant! that one should be the accepted answer. –  Eliran Malka Jun 14 '13 at 11:44
su -c "!!"

If you do not have permission to be root with sudo or sudo program did not install on system.

share|improve this answer

The way I prefer it:

  • Arrow key up
  • Home
  • "sudo "
  • Enter

But it might be easier to just do "sudo !!", it's up to you.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.