summaryrefslogtreecommitdiff
path: root/Executing bash commands as another user.md
blob: c934f16ba02d25e82b795400f2982c24cac024ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
---
tags:
  - bash
---
[[Bash]] [[Temporary set permissions with which new files are created]]

Using `sudo` we can execute commands as other users, most commonly as the root user. When passing `-u` switch we can specify different users.

When the command involves output redirection it is common to pass the entire command to a shell executed as that user: `sudo -u user bash -c 'command > redirect'`.

## Why do I get "command not found" as I execute with another user?
This can be because `sudo` normally discards the environment. Passing the `-i` switch makes sudo "login" which causes `.profile` et al to be sourced, thus configuring the user environment.