sudo commands
permission issues
output redirection
Linux troubleshooting
system administration

How do I use sudo to redirect output to a location I don't have permission to write to?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

This problem surprises people because sudo only elevates the command it starts, not the shell syntax around it. Redirection operators such as > and >> are handled by your current shell first, so the write fails before the elevated command ever gets a chance to run.

Why sudo echo ... > file Fails

Consider this command:

bash
sudo echo "hello" > /etc/example.conf

What actually happens is:

  1. your shell tries to open /etc/example.conf for writing
  2. that file open happens as your normal user
  3. only after that would sudo echo run

If your user cannot open the file, the command fails immediately. The important rule is that the process performing the file redirection needs permission, not just the process generating the output text.

The Most Common Fix: sudo tee

The standard solution is to move the file write into a process that actually runs under sudo. tee is ideal for that:

bash
echo "hello" | sudo tee /etc/example.conf

tee reads from standard input and writes to the file with elevated permissions, so the protected path is opened by the privileged process.

To append instead of overwrite:

bash
echo "another line" | sudo tee -a /etc/example.conf

By default, tee also prints the same text back to your terminal. If you want a quiet write:

bash
echo "quiet write" | sudo tee /etc/example.conf > /dev/null

Using sudo sh -c or sudo bash -c

Another valid approach is to run a whole shell as root so the redirection happens inside that elevated shell:

bash
sudo sh -c 'echo "hello" > /etc/example.conf'

Or, with bash and printf:

bash
sudo bash -c 'printf "%s\n" "hello" > /etc/example.conf'

This works because the child shell, not your current shell, performs the redirection.

When tee Is Better Than sh -c

Both patterns are correct, but they are best used in different situations.

Use tee when:

  • you already have a pipeline
  • you want to write or append one generated stream
  • you want the command to stay readable

Use sudo sh -c when:

  • you need several redirections in one statement
  • the command contains shell operators that belong together
  • quoting the whole expression is manageable

For most single-file writes, tee is clearer and less error-prone.

A Practical Example with a Privileged Path

Writing to a sysctl setting is a common example:

bash
echo 1 | sudo tee /proc/sys/net/ipv4/ip_forward > /dev/null

The same pattern works for many root-owned files under /etc, /proc, and similar system paths.

If you need multiple lines, a here-document can work well with tee:

bash
1sudo tee /etc/example.conf > /dev/null <<'EOF'
2line one
3line two
4line three
5EOF

That is often easier to read than nested quotes inside sudo sh -c.

Prefer printf When Output Must Be Exact

echo is fine for simple text, but it behaves inconsistently across shells when escapes or special flags are involved. printf is usually more predictable:

bash
printf '%s\n' 'first line' 'second line' | sudo tee /etc/example.conf > /dev/null

If you are generating config files, exact output matters. printf is the safer habit.

Safer Editing Alternatives

Sometimes redirection is not the best tool at all. If you need to edit an existing privileged file manually, sudoedit is often safer because it lets you edit a temporary copy with your normal editor and then writes the result back with elevated permissions.

Redirection is best when:

  • you are creating known content programmatically
  • you are appending a specific line
  • you want the command to be reproducible in scripts or documentation

Common Pitfalls

  • Assuming sudo also elevates > or >>. It does not.
  • Using echo for complex content that includes escapes, backslashes, or quotes can produce the wrong file contents.
  • Forgetting that tee overwrites by default unless -a is used for append mode.
  • Quoting badly inside sudo sh -c can cause shell expansion or syntax errors.
  • Creating root-owned files in places you plan to edit later as a normal user can lead to confusing permission problems.

Summary

  • Shell redirection is performed by the shell, not by sudo.
  • 'sudo tee is the clearest solution for writing to protected files through a pipeline.'
  • 'sudo sh -c or sudo bash -c works when the whole redirection expression must run in an elevated shell.'
  • Use printf instead of echo when output must be exact.
  • For interactive edits to protected files, sudoedit is often safer than raw redirection.

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design