LFCS · Question #837
You logged in as user tuxl, but now you want to switch users to tux2 with tux2's environment. How would you do this?
The correct answer is C. su - tux2. The su - tux2 command switches to the tux2 user by initiating a login shell, which ensures that tux2's full environment variables are loaded.
Question
Options
- Asu tux2
- Bsu-etux2
- Csu - tux2
- Dsu -m tux2
- Esu-ftux2
How the community answered
(25 responses)- C92% (23)
- D4% (1)
- E4% (1)
Why each option
The `su - tux2` command switches to the `tux2` user by initiating a login shell, which ensures that `tux2`'s full environment variables are loaded.
The `su tux2` command switches to the user `tux2` but retains the environment of the original user (`tux1`), which does not provide `tux2`'s environment.
`su -e` is not a standard or correct option for establishing a new user's environment; `-e` (or `--preserve-environment`) typically preserves the *current* user's environment.
The `su - tux2` command is the correct way to switch to user `tux2` and load their complete environment. The hyphen (`-`) after `su` simulates a full login, sourcing the target user's profile and setting up their home directory, PATH, and other environment variables appropriately.
The `su -m` (or `--preserve-environment`) option prevents `su` from setting up the target user's environment, keeping the original user's environment, which is not desired in this scenario.
`su -f` (or `--fast`) passes the `-f` flag to the shell, which typically prevents it from sourcing startup files, thus not loading the target user's full environment.
Concept tested: Linux user switching and environment
Source: https://man7.org/linux/man-pages/man1/su.1.html
Topics
Community Discussion
No community discussion yet for this question.