diff --git a/Template.md b/Template.md index 6d7dbb9..c5690aa 100644 --- a/Template.md +++ b/Template.md @@ -1,17 +1,35 @@ -# (Message goes here) +# "To run commands as another user, use su -c or sudo. ### Problematic code: - (Simple example of problematic code) + ./configure + make + su root + make install ### Correct code: - (Simple example of above code, only fixed) + ./configure + make + sudo make install # or su -c 'make install' root ### Rationale: -(An explanation of why the code is problematic and how the correct code is an improvement) +`su` doesn't actually switch user. It starts a brand new shell, running as another user. You can't put `su foo` in a script to make the following command run as user `foo`. + +Use `sudo -u username cmd` or `su -c cmd username` instead. ### Contraindications -(Cases where the user may choose to ignore this warning, if any.) \ No newline at end of file +If you want to present the user with a root shell or when you're redirecting input, you can ignore this message: + + until mycommand + do + echo "Failed. You will now get a root shell. Fix problem and exit to retry." + su + done + +or + + exec < mycommands + su foo