Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Shell Scripting
  • Kali Linux
  • Ubuntu
  • Red Hat
  • CentOS
  • Docker in Linux
  • Kubernetes in Linux
  • Linux interview question
  • Python
  • R
  • Java
  • C
  • C++
  • JavaScript
  • DSA
Open In App
Next Article:
Running previous command with sudo
Next article icon

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
Running previous command with sudo

R

rohan23chhabra
Improve
Article Tags :
  • Technical Scripter
  • Linux-Unix
  • Technical Scripter 2018
  • linux-command

Similar Reads

    Run commands as root with sudo
    Introduction - Linux follows the very tough permission model. A root user can do anything but normal user has no permissions. To run any command, they need to ask for permissions from the superuser. The easy and common way to grant administrative privileges to non-root users is, a user can use su co
    3 min read
    sudo Command in Linux with Examples
    sudo (Super User DO) command in Linux is generally used as a prefix for some commands that only superusers are allowed to run. If you prefix any command with "sudo", it will run that command with elevated privileges or in other words allow a user with proper permissions to execute a command as anoth
    8 min read
    setsid command in Linux with Examples
    setsid command in Linux system is used to run a program in a new session. The command will call the fork(2) if already a process group leader. Else, it will execute a program in the current process. The main advantage of using 'setsid' is that it allows programs to run independently of the terminal
    3 min read
    usermod command in Linux with Examples
    usermod command or modify user is a command in Linux that is used to change the properties of a user in Linux through the command line. After creating a user we have to sometimes change their attributes like password or login directory etc. so in order to do that we use the Usermod command. The info
    4 min read
    exec command in Linux with examples
    The 'exec' command in Linux is a powerful shell built-in used to replace the current shell process with a new command process. Unlike typical commands that create a new process, 'exec' transforms the existing process, directly impacting the efficiency and behavior of scripts.What is the exec Command
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences