Ubuntu: Run same command again but as root



Question:

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?


Solution:1

The simplest way is to run:

sudo !!  

This will run the last command but as super user.

Source.


Solution:2

​​​​​​​​​​​​​​​​​​​​​​​​​​​​​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.


Solution:3

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.


Solution:4

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.


Solution:5

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


Solution:6

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.


Solution:7

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.


Solution:8

su -c "!!"  

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


Solution:9

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.


Solution:10

If you want to do it in an alias, you have to use alias redo='sudo $(history -p !!)' for some reason.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »