DEV Community

Brandon Rozek
Brandon Rozek

Posted on • Originally published at brandonrozek.com on

Permission Denied: Writing to Privileged Locations

Perhaps you’ve tried something like the following:

sudo echo "hi" > /etc/test

Enter fullscreen mode Exit fullscreen mode

Only to receive back an error

bash: /etc/test: Permission denied

Enter fullscreen mode Exit fullscreen mode

This is because the sudo part only applies to the echo command. Bash which is the process that takes the "hi" from the echo standard out and writes it to /etc/test is still under the unprivileged user.

Instead consider the following:

echo "hi" | sudo tee /etc/test

Enter fullscreen mode Exit fullscreen mode

The command tee takes whatever is in standard in, and writes it to the filename specified. Since we applied sudo to the tee command, it now has the necessary permissions to write to a privileged location.

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay