How to create a file that only sudo can read?

Note that sudo is not synonymous with root/superuser. In fact, sudo command let you execute commands as virtually any user, as specified by the security policy:

$ sudo whoami
root
$ sudo -u bob whoami
bob

I assume you meant to create a file that only root user can read:

# Create the file
touch file

# Change permissions of the file
# '600' means only owner has read and write permissions
chmod 600 file

# Change owner of the file
sudo chown root:root file

When you need to edit the content of the file:

# Replace 'nano' with your prefered editor
sudo nano file

See how only root can read the file:

$ cat file
cat: file: Permission denied
$ sudo cat file
foo bar baz

Figured it out:

echo 'hello world' > test
sudo chown root test
sudo chmod 600 test
sudo cat test

In another terminal, if you do it without sudo:

> cat test
cat: test: Permission denied

Tags:

Sudo

Chmod