sudo-i(8)
Run a login shell as root or another user with a complete environment.
Synopsis
sudo -i [-u user] [command]Description
sudo -i invokes a login shell as root (or the specified user) with environment variables initialized as if you had logged in directly. This differs from sudo alone, which preserves your current user's environment.
The login shell reads startup files like .bash_profile or .profile, setting HOME, USER, MAIL, and PATH to match the target user. Useful for administrative tasks that depend on root's proper environment.
Common options
| Flag | What it does |
|---|---|
-u user | Run the shell as the specified user instead of root |
-s | Run the specified shell (can be combined with -i) |
-H | Set HOME environment variable to the target user's home directory |
-E | Preserve user's environment variables (use with caution) |
-l | Update timestamp file without running a command |
-v | Validate sudo credentials and extend timeout |
Examples
Open an interactive root login shell with root's environment and home directory
sudo -iRun whoami in a root login shell (prints 'root'), then exit
sudo -i whoamiSwitch to an interactive login shell as the postgres user
sudo -i -u postgresRun a command as www-data with proper HOME environment set
sudo -i -u www-data bash -c 'echo $HOME'Access root shell for multiple administrative commands in sequence
sudo -iPrint root's home directory (usually /root)
sudo -i pwd