website/content/blog/permission-denied-writing-privileged-locations.md

31 lines
851 B
Markdown
Raw Normal View History

2022-04-07 20:17:10 -04:00
---
2023-02-18 13:12:02 -05:00
date: 2022-04-07 20:09:40-04:00
2022-04-07 20:17:10 -04:00
draft: false
math: false
2023-01-05 14:04:45 -05:00
medium_enabled: true
2023-02-18 13:12:02 -05:00
medium_post_id: 7d133254be3f
tags: []
title: 'Permission Denied: Writing to Privileged Locations'
2022-04-07 20:17:10 -04:00
---
Perhaps you've tried something like the following:
```bash
sudo echo "hi" > /etc/test
```
Only to receive back an error
```
bash: /etc/test: Permission denied
```
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:
```bash
echo "hi" | sudo tee /etc/test
```
2023-02-18 13:12:02 -05:00
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.