6

When I run '# sudo touch newfile' my expectation was that the file would be owned by me, not by root, as my understanding of sudo is that it is giving me, the user, root priviledges but does not actually switch the user.

Do I have a fundamental misunderstanding of what sudo is about?

4 Answers 4

9

When you use sudo, you run a program as the superuser (root), so files that are created are created as the superuser.

1
  • "sudo, sudoedit — execute a command as another user" Feb 26, 2015 at 10:20
6

What mipadi said, which you can demonstrate by comparing the output of these two 'whoami' ("who am I?") commands:

$ sudo whoami
$ whoami

4

sudo (name comes from su "do" or super user "do") allows you to run command as root. It does not give your user different privileges. Therefore, when you run #sudo touch newfile it is user root that executed command touch newfileand that is why newfile is owned by user root.

More information at Sudo page.

3

You can think of the 'su' in sudo as "switch user". If you don't say which user to run as, it will default to root, the superuser, also associated with su.

sudo -u yourself touch newfile if you want the file to be owned by yourself.

Your assumption that sudo doesn't change the userid is correct in the perspective of the rest of your session. Think of it as su + touch newfile + exit. So, in your shell you "remain" yourself, but sudo executes your command in a new process with another effective userid.

1
  • 1
    The 'su' in 'sudo' means "Super User" (sudo = Super User Do), not "set userid". The command 'su' means 'Switch User'.
    – mistiry
    Jul 9, 2010 at 15:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .