Granting sudo permissions for specific tasks on accounts with nologin or no password in Linux involves configuring the /etc/sudoers file. Here’s a guide on how to achieve this:
1. Edit sudoers File:
Use the visudo command to safely edit the sudoers file:
sudo visudo2. Add sudo Permissions:
Add an entry for the user specifying the allowed command(s) without a password prompt. Replace <username> and <command> with your actual username and command(s).
<username> ALL=(ALL:ALL) NOPASSWD: <command>For example, allowing a user to restart the Apache web server:
john ALL=(ALL:ALL) NOPASSWD: /usr/sbin/service apache2 restart3. Save and Exit:
Save the file and exit the editor.
- In
nano, pressCtrl + X, then pressY, and finally pressEnter. - In
vim, type:wqand pressEnter.
4. Test the Configuration:
Before relying on the new configuration, test it to ensure there are no syntax errors:
sudo -l -U <username>Replace <username> with the actual username. This command should list the allowed commands for the specified user.
5. Using nologin Shell:
If the user account has the nologin shell, you can still grant sudo permissions. The nologin shell prevents interactive logins but doesn’t affect sudo execution.
Example:
- User with
nologinshell:sudo useradd -m -s /usr/sbin/nologin john - Grant
sudopermissions:sudo visudoAdd the following line:
john ALL=(ALL:ALL) NOPASSWD: /path/to/allowed/commandSave and exit.
- Test the configuration:
sudo -l -U johnThis should list the allowed commands without prompting for a password.
Important Notes:
- Be cautious when editing the sudoers file. Syntax errors can lock you out of sudo access.
- Always use
visudoto edit sudoers to prevent syntax errors. - Specify the full path to the allowed command to avoid security risks.
- Test thoroughly to ensure the sudo configuration works as intended.
By following these steps, you can grant specific sudo permissions to users with nologin or no password on Linux.





