Skip to content
geeksforgeeks
  • 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
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • 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:
Tail command in Linux with examples
Next article icon

sudo Command in Linux with Examples

Last Updated : 24 Apr, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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 another user, such as the superuser. This is the equivalent of the “run as administrator” option in Windows. The option of sudo lets us have multiple administrators. 

These users who can use the sudo command need to have an entry in the sudoers file located at “/etc/sudoers”. Remember that to edit or view the sudoers file you have to use the sudo command. To edit the sudoers file it is recommended to use the “visudo” command. 

By default, sudo requires that users authenticate themselves with a password that is the user’s password, not the root password itself. 

Syntax for sudo command: 

sudo -V | -h | -l | -v | -k | -K | -s | [ -H ] [-P ] [-S ] [ -b ] |  [ -p prompt ] [ -c class|- ] [ -a auth_type ] [-r role ] [-t type ]  [ -u username|#uid ] command

Options Available in the sudo command

Options                                                                                                              Description Syntax
-V The -V (version) option causes sudo to print the version number and exit. If the invoking user is already root, the -V option will print out a list of the defaults sudo was compiled with. 
sudo -V
-l The -l (list) option will print out the commands allowed (and forbidden) the user on the current host. 
sudo -l
-h or –help The -h (help) option causes sudo to print a usage message and exit.
sudo -h
-v If, given the -v (validate) option, sudo will update the user’s timestamp, prompting for the user’s password if necessary. This extends the sudo timeout for another 5 minutes (or as given in sudoers) but does not run a command. This does not give any output. 
sudo -v
-k The -k (kill) option to sudo invalidates the user’s timestamp. So, the next time sudo is run a password will be required. This option does not require a password and was added to allow a user to revoke sudo permissions from a logout file. 
sudo -k
-K Similar to the -k option, the -K (sure kill) option is used to remove the user’s timestamp entirely. Likewise, this option does not require a password. 
sudo -K
 -b The -b (background) option tells sudo to run the given command in the background. Note that if you use the -b option you cannot use shell job control to manipulate the process. 
sudo -b [command]

(replace “command” with the command you want run in the background)

-p the sudo -p prompt command allows you to customize the password prompt that sudo displays when it requests the user’s password. By default, sudo will display a generic password prompt that looks like
sudo -p "Enter your password" [command]

(replace “command” with the command you want run in the background)

 -n The -n option allows sudo to execute a command without prompting for a password. This option is useful when running sudo commands as background jobs or in a shell script. The -n option stands for non-interactive.
sudo -n [command]

(replace “command” with the command you want run in the background)

-u The -u option causes sudo to run the specified command as a user other than root. To specify a UID instead of a username, use #uid.
sudo -u [user] [command]

(replace “command” with the command you want run in the background)

-s The -s option runs the shell specified by the SHELL environment variable if it is set or the shell as specified in the file passwd.
sudo -s [command]

(replace “command” with the command you want run in the background)

-H The -H option sets the HOME environment variable to the home directory of the target user (root by default) as specified in passwd. By default, sudo does not modify HOME.
sudo -H [command]

(replace “command” with the command you want run in the background)

-S The -S option causes sudo to read the password from standard input instead of the terminal device.
sudo -S [command]

(replace “command” with the command you want run in the background)

-a The -a option causes sudo to use the specified authentication type when validating the user, as allowed by /etc/login.conf. The system administrator may specify a list of sudo-specific authentication methods by adding an “auth-sudo” entry in /etc/login.conf.
sudo -a [auth-type] [command]

(replace “command” with the command you want run in the background)

— The — flag indicates that sudo should stop processing command line arguments. It is most useful in conjunction with the -s flag.
sudo -- [command]

(replace “command” with the command you want run in the background)

The output of few commands

1. -V: The -V (version) option causes sudo to print the version number and exit. If the invoking user is already root, the -V option will print out a list of the defaults sudo was compiled with. 
 

sudo -V

sudo -V

2. -l: The -l (list) option will print out the commands allowed (and forbidden) the user on the current host. 

sudo -l

sudo -l

3. -h or –help: The -h (help) option causes sudo to print a usage message and exit. 

sudo -h

sudo -h

Environment Variables

These environment variables are used by sudo 

Tag Description
EDITOR 
 
Default editor to use in -e (sudoedit) mode if VISUAL is not set 
HOME 
 
In -s or -H mode (or if sudo was configured with the 
–enable-shell-sets-home option), set to homedir of the target user 
PATH 
 
Set to a sane value if the secure_path sudoers option is set. 
SHELL 
 
Used to determine shell to run with -s option 
SUDO_PROMPT 
 
Used as the default password prompt 
SUDO_COMMAND 
 
Set to the command run by sudo 
SUDO_USER 
 
Set to the login of the user who invoked sudo
SUDO_UID 
 
Set to the uid of the user who invoked sudo 
SUDO_GID 
 
Set to the gid of the user who invoked sudo 
SUDO_PS1 
 
If set, PS1 will be set to its value
USER  Set to the target user (root unless the -u option is specified) 
VISUAL 
 
Default editor to use in -e (sudoedit) mode 

Conclusion

In Linux, the sudo command acts like a magic key that unlocks special powers, letting you do important tasks that usually only the superuser can do. It’s a way of asking for permission to carry out commands with elevated privileges, like installing software or changing system settings. You use sudo before a command to show you’re allowed to run it as the superuser. To use sudo, you usually need to type your own password to prove it’s really you doing it, not someone else. Remember, sudo is powerful, so use it carefully! It can change important stuff on your computer. By understanding sudo and its options, you can use Linux more effectively and safely.



Next Article
Tail command in Linux with examples

M

Magisk
Improve
Article Tags :
  • Linux-Unix

Similar Reads

  • shred Command in Linux with Examples
    When you delete a file from Linux or from any Operating System, then the file is not deleted permanently from the hard disk. When a file is deleted, it first gets moved to the trash and as soon as you clear off the trash the files get deleted for the file system. But the file is still there on your
    6 min read
  • shutdown command in Linux with Examples
    The `shutdown` operation in Linux is a crucial command for managing system power states, allowing administrators to halt safely, power off, or reboot the system. This command provides flexibility in scheduling downtimes, ensuring minimal disruption to users and processes. In this article, we will ex
    5 min read
  • sleep Command in Linux with Examples
    sleep command is used to create a dummy job. A dummy job helps in delaying the execution. It takes time in seconds by default but a small suffix(s, m, h, d) can be added at the end to convert it into any other format. This command pauses the execution for an amount of time which is defined by NUMBER
    5 min read
  • source Command in Linux with Examples
    If you're new to the world of Linux, you might have heard about commands that do various tasks, but some like the 'source' command might seem a bit confusing at first. Don't worry; let's break it down step by step. What is the Source Command?The source command in Linux is like a magic wand that lets
    7 min read
  • How to sort lines in text files in Linux | sort Command
    SORT command is used to sort a file, arranging the records in a particular order. By default, the sort command sorts file assuming the contents are ASCII. Using options in the sort command can also be used to sort numerically.  SORT command sorts the contents of a text file, line by line.sort is a s
    7 min read
  • Split Command in Linux with Examples
    Split command in Linux is used to split large files into smaller files . It splits the files into 1000 lines per file(by default) and even allows users to change the number of lines as per requirement. The names of the files are PREFIXaa, PREFIXab, PREFIXac, and so on. By default the PREFIX of files
    6 min read
  • How to use SSH to connect to a remote server in Linux | ssh Command
    Secure Shell, commonly known as SSH, is like a super-secure way to talk to faraway computers, called servers. It's like a secret tunnel on the internet that keeps your conversations safe and private. Imagine you're sending a letter, and instead of sending it openly, you put it in a magic envelope th
    8 min read
  • Strace command in Linux with Examples
    'strace' is a powerful tool for monitoring and diagnosing processes in Linux. It is primarily used for debugging programs, troubleshooting issues, intercepting and recording system calls, and tracing running processes. It provides valuable insights into how a program interacts with the system, espec
    4 min read
  • stty command in Linux with Examples
    stty command in Linux is used to change and print terminal line settings. This command shows or changes terminal characteristics. Syntaxstty [-F DEVICE | --file=DEVICE] [SETTING]...stty [-F DEVICE | --file=DEVICE] [-a|--all]stty [-F DEVICE | --file=DEVICE] [-g|--save]where, -F DEVICE or --file=DEVIC
    2 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
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