Tried this command:
echo "/export *(rw,async,no_root_squash,no_subtree_check)" > /etc/exports
But the file didn’t get updated.
Core Issue: Redirection (>) Happens Before sudo
When executing
sudo echo "line" > /etc/file
Only the echo runs with sudo. So this fails silently (or leaves the file empty).
Correct Approach: Use sudo tee
echo "/export *(rw,async,no_root_squash,no_subtree_check)" | sudo tee /etc/exports > /dev/null
This works because:
echoruns normally- Output is piped (
|) tosudo tee teeruns with root privileges and writes the file
IMPORTANT: Append Instead of Overwrite?
Use tee -a:
echo "another line" | sudo tee -a /etc/exports > /dev/null
What I Did (and Why It Went Wrong)
Ran:
echo "/export ..." > sudo tee /etc/exports > /dev/null
This:
- Created a file named
sudoin your current directory (with the NFS export line in it) - Did not invoke the
sudocommand
Summary
| Goal | Command |
|---|---|
| Overwrite root file | `echo “…” |
| Append to root file | `echo “…” |