# sudo and Who Owns a File

Every file has an owner and a group, and only root, the computer's administrator account, may change anything; sudo runs one command as root after asking for your password, and chown gives a file a new owner, so both are used rarely and on purpose.

Journeyman · Unix · Linux · Updated November 10, 2026

## After this lesson you can

- Find a file's owner and group in ls -l, and tell when a job needs root
- Explain what sudo and chown do, and when sudo is the right choice

## Key takeaways

- Every file has an owner and a group, the two names in ls -l. whoami prints your own user name.
- root is the administrator account. It may read, change and delete anything, whatever the permissions say.
- sudo runs one command as root, after asking for your own password. It's for changing the system, such as installing software.
- chown gives a file a new owner and group, as in sudo chown sam:sam notes.txt. Only root may give a file away, so it needs sudo.
- Read a command before you put sudo in front of it, and never use sudo for files in your own home folder.

You try to install a program with `apt`, and it answers that you need to be root. A tutorial tells you to put `sudo` in front of half its commands. Or you edited a settings file with `sudo`, and now your own editor can't save your own file. All three are about who owns what on a computer, and about the one account that may do anything. People who look after Linux servers deal with this every day, and anyone with a Raspberry Pi or a Linux laptop meets it in the first week.

This lesson looks and explains; it doesn't change anything on your computer. The commands you'll run only read. The two that change things, `sudo` and `chown`, are shown so you know them when a tutorial uses them.

## Users, owners and groups

A computer can have several **users**, each with an account and a home folder. One of them is you:

```bash
whoami
```

```text
sam
```

Every file belongs to one user, its **owner**, and one **group**, a named set of users who share access. They're the two names in `ls -l`. In the workshop from *Variables in the Shell*:

```bash
ls -l ~/terminal-practice/workshop/diary.txt
```

```text
-rw------- 1 sam sam 44 Nov  9 05:27 /home/sam/terminal-practice/workshop/diary.txt
```

Owner `sam`, group `sam`, and private (`rw-------`) since the last lesson's `chmod 600`. On Ubuntu, each user gets a group of their own with the same name. On a Mac, the group is usually `staff`. The three sets of letters from *Permissions: Read, Write and Run* say what the owner, the group and everyone else may do.

## root: the account that may do anything

Files that keep the computer running belong to **root**, the administrator account. bash itself is one:

```bash
ls -l /usr/bin/bash
```

```text
-rwxr-xr-x 1 root root 1540520 Feb 13  2026 /usr/bin/bash
```

Everyone may read and run it (`r-x` for others), but only root may change it. That's what keeps a typo, or a program you downloaded, from breaking the computer. Some files are closed even to reading. `/etc/shadow` holds every user's password, scrambled:

```bash
ls -l /etc/shadow
cat /etc/shadow
```

```text
-rw-r----- 1 root shadow 778 May 13 19:41 /etc/shadow
cat: /etc/shadow: Permission denied
```

Others get `---`: nothing. That `Permission denied` is the system working as it should. (A Mac keeps its passwords elsewhere and has no `/etc/shadow`, so this one is for Linux and WSL.)

root is different from every other user in one way: permissions don't stop it. It may read, change or delete any file on the computer. That's useful for looking after the computer, and it's why nobody works as root all day.

## sudo: one command as root

When a job really does need root, such as installing software for everyone or changing a system setting, you put `sudo` in front of that one command. It's read "sue-doo", short for *superuser do*. These lines aren't for running now. They show what installing a program looks like, as in *Installing Tools: Homebrew and apt*:

```bash
sudo apt update
sudo apt install tree
```

`sudo` first asks for **your own** password, the one you sign in with (in WSL, the one you chose when you set up Ubuntu). Nothing shows on the screen while you type it, not even dots. After that, it runs the command as root, and remembers your password for a few minutes, so the next `sudo` doesn't ask again. Only users an administrator has allowed may use `sudo`; on Ubuntu and on a Mac, the first account you made is one of them.

Use it rarely and on purpose:

- **Read the command first.** `sudo` turns off the safety net, so a typo in a `sudo rm` deletes whatever it names, system files included.
- **Not in your home folder.** Your own files are yours already. If a tutorial says `sudo` for a file in your home folder, something else is wrong, and `sudo` would hide it.
- **Know where it came from.** Never `sudo` a script you downloaded and haven't read.

## chown: give a file a new owner

`chown` (*change owner*) gives a file a new owner and group, written `owner:group`. Only root may give a file away, so it nearly always comes with `sudo`. The common reason is a mistake: you made or edited `notes.txt` in your home folder with `sudo nano notes.txt`, so it now belongs to root and your own editor can't save it. This line, not for running now, gives it back to you:

```bash
sudo chown sam:sam notes.txt
```

It prints nothing, and `ls -l notes.txt` then shows `sam sam` again. Write your own user name in place of `sam`. This is the one time `sudo` near your home folder is the right tool: it undoes an earlier `sudo`.

## Check yourself

2 questions to try on the lesson page.

## So what

The two names in `ls -l` say who owns a file, and root may do anything. `sudo` borrows root's power for one command, so use it when the job is the computer's, never out of habit.

## Key terms

- [sudo](https://www.ask-codey.com/glossary/sudo)
- [chown](https://www.ask-codey.com/glossary/chown)
- [Root User](https://www.ask-codey.com/glossary/root-user)
- [User and Group](https://www.ask-codey.com/glossary/user-and-group)

## Cheat sheets

- [sudo and Who Owns a File](https://www.ask-codey.com/cheat-sheets/bash-sudo-and-ownership-cheat-sheet)

## Related

- [Permissions: Read, Write and Run](https://www.ask-codey.com/lessons/bash-permissions)
- [Installing Tools: Homebrew and apt](https://www.ask-codey.com/lessons/bash-installing-tools)

Source: https://www.ask-codey.com/lessons/bash-sudo-and-ownership
