How to Run Commands as a Non-Login User on Linux Using sudo and su
This guide explains how Linux administrators can execute commands as a non‑login user by configuring sudo or using su with a custom shell, detailing step‑by‑step instructions, required sudoers entries, and important security considerations to prevent privilege misuse.
1. Understanding Non-Login Users
In Linux, non-login users (shell set to /sbin/nologin or /bin/false) are special accounts used to run services without interactive login, improving system security.
2. Using sudo
The sudo command allows authorized users to run commands as another user, including a non-login user.
Steps:
Edit sudoers : Run visudo to open the /etc/sudoers configuration file.
Add rule : Insert a line such as <your_username> ALL=(<non_login_user>) NOPASSWD: ALL to permit password‑less execution as the non‑login user.
Save and exit : Save the changes and quit the editor.
Execute command : Use sudo -u <non_login_user> <command> to run a command as the non‑login user.
3. Using su -s /bin/bash
If sudo is not desired, the su command can start a new shell as another user, even when that user has no login shell.
Steps:
Switch user : Run su -s /bin/bash <non_login_user> to start a Bash session as the non‑login user.
Run commands : Execute any needed commands inside the new shell.
4. Security Considerations
Misconfiguring the sudoers file can create vulnerabilities; always grant only the necessary permissions to trusted users.
5. Conclusion
Linux administrators can use sudo or su -s /bin/bash to run commands as a non‑login user, providing flexibility while requiring careful security management.
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.
