How to re run a previous command in your history with sudo privileges.

Posted: May 23, 2014. At: 9:58 PM. This was 3 years ago. Post ID: 7302

This is how to rerun a previous command with sudo if you forgot the sudo command to elevate privileges.

Here I have tried to cat a file and I have forgotten to elevate my privileges with the sudo command.

homer@calculate ~ $ cat /etc/shadow
cat: /etc/shadow: Permission denied

Instead of re-typing the command; I just use this command: sudo !!

homer@calculate ~ $ sudo !!
sudo cat /etc/shadow
 
We trust you have received the usual lecture from the local 
System Administrator. It usually boils down to these three things:
 
    #1) Respect the privacy of others.
    #2) Think before you type.
    #3) With great power comes great responsibility.
 
Password: 
root:$5$Mc3alPUE$Qiq/NVnEX2fc1jHKEZDt1JjqDLbkVKxqiBxuoi8JPQ6:16212:0:99999:7:::
halt:*:9797:0:::::
operator:*:9797:0:::::
shutdown:*:9797:0:::::
sync:*:9797:0:::::
bin:*:9797:0:::::
daemon:*:9797:0:::::
adm:*:9797:0:::::
lp:*:9797:0:::::
mail:*:9797:0:::::
postmaster:*:9797:0:::::
news:*:9797:0:::::
uucp:*:9797:0:::::
nobody:*:9797:0:::::
sshd:!:14924::::::
ldap:!:14935::::::
messagebus:!:14935::::::
polkituser:!:14935::::::
cron:!:14936::::::
ntp:!:14936::::::
rpc:!:14936::::::
gdm:!:14936::::::
haldaemon:!:14936::::::
openvpn:!:14940::::::
usbmux:!:14991::::::
vnstat:!:15049::::::
dnsmasq:!:15586::::::
polkitd:!:15621::::::
saned:!:15632::::::
guest:$5$e/joOLXL$5GfArWgJx7BEcGqil3hTZoPGqWLRUUjCbzvl3gTWN75:16212:0:99999:7:::
homer:$5$X2qWCBRa$t7DoaDoBVAYGY7NmC7s8AnKiIxcJw8ZjsxUeBTJaMbD:16212:0:99999:7:::

This enables me to re-run the previous command as the root user with sudo without re-typing the command. Very cool huh?

No comments have been made. Use this form to start the conversation :)

Leave a Reply