FactorPad
Build a Better Process

Linux sudo Command Summary with Examples

Linux sudo allows you to run a command as superuser.
  1. Purpose - Learn what sudo is for and how to find help.
  2. Options - Review a few common options and arguments.
  3. Examples - Walk through code examples with sudo.
  4. A tip - Finish off with one more insight.
face pic by Paul Alan Davis, CFA
Updated: February 24, 2021
In this tutorial on Linux sudo, 79 of 100, below find a 3-4 minute introductory video, a text-based tutorial and all of the code examples from the videos.

Outline Back Tip Next

/ factorpad.com / tech / linux-essentials / sudo-command.html


An ad-free and cookie-free website.


Examples of the Linux sudo Command

Beginner

Learn to run a command as a superuser at the Linux command line.

Video Tutorial

Videos can also be accessed from the Linux Essentials Playlist on YouTube.

Linux sudo Command Summary with Examples (3:41)

Video Script

The Command and Why You Need It

Our seventy-ninth word, or command to memorize is sudo from our category Users.

sudo allows you to run a command as superuser.

Common Linux sudo Options
-options description
-h, --help Print help screen
-e, --edit Edit a file as superuser
-l, --list Shows user privileges as sudo, or path to program

Recall from the previous video (tutorial) on su we logged in as another user. Now we'll take a superuser role temporarily to run commands as an administrator, also known as a superuser.

Before we start, it helps to think of commands as mini programs and most follow this structure: command -option(s) argument(s).

The sudo command has 25 options, and the argument is the command to execute or a file to open.

Like most commands, help is available with double-dash --help. For files, the -e command goes right in to a default editor, and -l lists what you can do, assuming you are a superuser.

So why is sudo an important command? Well, we need to limit access to adminstrative duties like installing software, viewing logs and configuration files. And now you know how to do that.

Demonstration

Okay, the best way to embed this in your memory is by typing in your own terminal window.

Find this on your Mac using a program called Terminal. On Linux use Terminal or Konsole, and currently Microsoft is adding this functionality to Windows.

Here we go. Let's say I have a backup script called video79.script that Jennifer wants to run periodically, so I'll make her a copy.

$ ls -og video79.script -rw-r--r-- 1 0 Nov 9 17:14 video79.script

First, do a cp video79.script /home/jennifer.

$ cp video79.script /home/jennifer cp: cannot create regular file '/home/jennifer/video79.script': Permission denied

And notice, an error. I can't access others' directories unless I'm superuser, so add sudo on the front.

$ sudo cp video79.script /home/jennifer

And there we go.

Then, let's do an ls -og for permissions, or mode, from video (tutorial) #75.

$ ls -og /home/jennifer total 0 -rw-r--r-- 1 0 Nov 9 18:35 video79.script

And she can't execute this right? So let's do a sudo chmod 500, from the octals in video (tutorial) #75, with 5 for read and execute, applying to the owner only, and 0 0 for the others.

$ sudo chmod 500 /home/jennifer/video79.script

And then last, let's give her ownership with a sudo chown jennifer the owner, :jennifer the group, then the file name and there we go. Then let's do an ls -l to verify we have done everything correctly here.

$ sudo chown jennifer:jennifer /home/jennifer/video79.script $ ls -l /home/jennifer total 0 -r-x------ 1 jennifer jennifer Nov 9 18:35 video79.script

Very good.

A Final Tip

Okay now you know how to use sudo. And you know the syntax for commands, options and arguments.

One last tip about the sudo command. So please make some time to practice these commands it does take a little bit of work, and next, we'll learn about passwords.

Okay, thanks for visiting today. I hope this was a helpful introduction to the sudo command.


Learn More About The Series

For an overview of the 100 videos, the 8 quizzes, a cheat sheet, the categories and a Q&A section, visit:


What's Next?

If you'd like to be notified of new content, subscribe, join and follow. Simple as that.

Outline Back Tip Next

/ factorpad.com / tech / linux-essentials / sudo-command.html


linux sudo command
sudo examples
sudo command
linux sudo
sudo linux
man sudo
etc sudoers
sudoers file
sudo linux command
sudo command example
linux super user
root privilege
linux root
root user
superuser root
linux administration
linux system administrator
linux sudoers
sudo su

A newly-updated free resource. Connect and refer a friend today.