Operations 4 min read

How to Redirect Output to Privileged Files Using sudo on Ubuntu

This guide explains why a simple sudo command may still produce a Permission denied error when redirecting output to a file you lack write access to, and presents four practical methods—including running a shell with sudo, using scripts, sudo -s, and sudo tee—to successfully write to such protected files on Ubuntu 18.04.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
How to Redirect Output to Privileged Files Using sudo on Ubuntu

Permission denied issue

If the current user lacks write permission for a file, redirecting command output to that file results in a "Permission denied" error. For example, user nick tries to list /root and write the result to /root/test.txt, which he cannot write.

sudo ls -al /root > /root/test.txt

Why the error occurs

The ls command runs with sudo, but the output redirection ( >) is performed by the current shell, which does not have permission to write to /root/test.txt. Hence the operation fails.

Understanding this allows us to apply four different solutions.

1. Run the whole command in a sudo shell

Execute the shell itself with sudo so that both the command and the redirection have elevated privileges.

sudo bash -c 'ls -al /root > /root/test.txt'

2. Write the command into a script and run the script with sudo

Create a script test.sh containing the command, make it executable, and run it with sudo.

#!/bin/bash
ls -al /root > /root/test.txt
chmod +x test.sh
sudo ./test.sh

Alternatively, use a here‑document:

sudo bash <<EOF
ls -al /root > /root/test.txt
EOF

Or pipe the command string to a sudo‑executed bash:

echo 'ls -al /root > /root/test.txt' | sudo bash

3. Use sudo -s to start a root shell

Switch to a root shell with sudo -s, run the command, then exit (Ctrl‑D).

4. Use sudo tee for redirection

The tee command writes its input to a file and optionally to standard output. By piping the output of a sudo command into sudo tee, the write operation gains root privileges.

sudo ls -al /root | sudo tee /root/test.txt > /dev/null

The > /dev/null part suppresses tee's output to the terminal.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxShellUbuntuSudooutput redirectionpermission denied
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.