How to Run Commands as Non‑Login Users with su – No Shell Required
Learn how to use the Linux su command with the -s option to execute a single command as a non‑login user whose shell is set to /sbin/nologin or /bin/false, while preserving security and avoiding a full interactive shell session.
The su command normally switches to another user and starts that user’s login shell, but it can also run a single command as a user whose account is disabled for login (e.g., shell set to /sbin/nologin or /bin/false).
Understanding non‑login users
Non‑login users are created for security reasons; they cannot log in interactively because their login shell is set to /sbin/nologin or /bin/false.
Running a single command with su
Use the -s option to specify an alternative shell (commonly /bin/bash) and the -c option to pass the command to execute. The general syntax is:
su -s /bin/bash <username> -c '<command>'Example
Suppose you need to run script.sh as the apache user, whose shell is /sbin/nologin: su -s /bin/bash apache -c '/path/to/script.sh' This runs the script with the privileges of the apache user.
Security tips
When using su to act as a non‑login user, ensure the command does not expose sensitive data or unintentionally modify system settings; always follow the principle of least privilege.
Administrator usage
Only users with appropriate privileges (typically root) can invoke su to switch to a non‑login account, preventing abuse of elevated rights.
Conclusion
Using su -s /bin/bash lets administrators execute tasks as any user, even those configured as non‑login, without altering the user’s original shell. This provides flexibility while requiring careful security considerations.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Ops Development & AI Practice
DevSecOps engineer sharing experiences and insights on AI, Web3, and Claude code development. Aims to help solve technical challenges, improve development efficiency, and grow through community interaction. Feel free to comment and discuss.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
