Linux: Run Last Command as Sudo

Objective: You wanted to run a command as root but you forgot to use sudo. There is a useful shell shortcut (using Event Designators) to re-run the previous command under sudo.

Let’s say you tried to cat the /etc/shadow file as a normal user.

$ cat /etc/shadow
cat: /etc/shadow: Permission denied

You will get permission denied, because the shadow file is only readable by root. To repeat the last command with sudo, simply use the following syntax.

$ sudo !!

The ‘!!‘ syntax is a reference to a command line entry in the history list. It refers to the previous command. An alternative way to run the previous command is to use ‘!-1’.

$ sudo !-1

The ‘!-1‘ syntax means refer to the previous command -n, where n is 1 in this case.

ibrahim = { interested_in(unix, linux, android, open_source, reverse_engineering); coding(c, shell, perl, php, python, java, javascript, nodejs, angular, react); plays_on(xbox, ps4); linux_desktop_user(true); }