Open In App

Running previous command with sudo

Last Updated : 06 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In the same way, sudo is used to execute any command that requires superuser privileges in Unix-like operating systems. At times, one executes some commands yet forgets to invoke them under sudo. Rather than rewriting all that again, you may use one simple trick: rerun the last command with 'sudo'.

Here, we will discuss the syntax, examples, common options, and FAQs about running commands with 'sudo'.

Syntax

To rerun the last command with sudo, the basic syntax is:

sudo !!
  • sudo elevates privileges; !! Recalls the last command.

Basic Example

To demonstrate, let’s assume you try updating your system without sudo:

yum update
Screenshot-2024-09-24-172852

Very often in linux, we execute a command only to see an error displaying 'Permission denied.'

Then we realize that we had forgotten sudo before the command and we write the command again, this time with a sudo:

sudo yum update
sudo yum update
sudo yum update

Now command successfully executed without permission decline

Examples:

In this example when we try to update the packages without sudo command they show the Permission denied error, so we use the !! with sudo here and the packages updated

apt-get update
sudo apt-get update
1

The situation becomes frustrating when the command is a long one which you have typed. You certainly wouldn't want to retype that long command. To solve this problem, the following cool linux hack is presented:

sudo !!
Screenshot-2024-09-24-173546

Here we see that when we execute 'sudo !!' its execute previously executed commands

Screenshot-2024-09-24-173829

This command repeats the previous command with sudo infront. It certainly saves a lot of time and effort and prevents frustration among geeks.

Key Options Commonly Used with the Command

Here are some options we can use with sudo:

Option

Description

-u <user>

Run the command as a specific user.

-k

Force sudo to ask for the password again.

-b

Run the command in the background.

!!

Repeat the last command with sudo.

All the Options Provided

-u <user>

Specify the user as whom the command should be executed.

-k

Forces sudo to ask for a password, even if you’ve already entered it recently.

!!

Repeats the last command in the terminal with sudo.

Conclusion

Turns out the usage of sudo !! is a time-saving method to rerun the last command with elevated privileges on Unix-like operating systems. This will definitely save the user a lot of time and probably some frustration when permission errors are encountered. Knowing syntax and common options of the sudo command, users can create automation by writing scripts and shortcutting long commands with the use of alias.

Be it on the server administration or the normal routine maintenance on one's local machine, this mastered technique adds to your efficiency and capability as a system administrator. The more you work with the command line, the more integrating sudo !! into your practices will pay dividends in keeping operations smooth and effective.


Next Article

Similar Reads