0

I just confused to create file with sudo command. As i know, sudo command is commonly used for root privilege(by default) and when i created files using command below:

sudo cat > root.txt

And i saw the user, it still regular username not root. How that can be happen? and how to create files owned by root without changing user to root?

Zozzizzez
  • 497

2 Answers2

6

Stream redirection (> file) takes place in the parent process (running as $USER) before the command (sudo) even begins.

You can either use a different command:

sudo tee root.txt

or, afterwards, change the ownership of the file:

sudo chown root:root root.txt

waltinator
  • 37,856
4

Redirection is 'outside' of sudo in your example. Try

sudo bash -c "cat > root.txt"
sudodus
  • 47,684