Manage sudo NOPASSWD mode with ease — enable, disable, and set automatic expiry timers.
- About
- Features
- How It Works
- Installation
- Usage
- Command Reference
- Configuration
- Examples
- Files & Structure
- Troubleshooting
- License
sudotoggle is a lightweight Bash utility that simplifies managing passwordless sudo access on Linux systems. It creates and manages temporary or permanent NOPASSWD rules in /etc/sudoers.d/, complete with optional auto-expiry and real-time debug output.
Perfect for development sessions, automation scripts, or any workflow where frequent sudo access is needed without compromising long-term security.
| Feature | Description |
|---|---|
| 🚀 Quick Toggle | Enable or disable passwordless sudo with a single command |
| ⏱️ Timed Sessions | Set automatic expiry (in seconds or at a specific time) |
| 🔍 Debug Mode | Real-time status output before every sudo command |
| 🛡️ Safe Rollback | Validates sudoers syntax before applying changes |
| 📊 Status Overview | View current state, expiry time, and remaining duration |
| 🐚 Shell Integration | Automatic hook installation for Bash and Zsh |
┌─────────────────────────────────────────────────────────────────┐
│ sudotoggle.sh │
├─────────────────────────────────────────────────────────────────┤
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────────────┐ │
│ │ Commands │ │ Config │ │ Shell Hook │ │
│ │ -on / -off │◄─┤ ~/.config/ │◄─┤ preexec / DEBUG │ │
│ │ -debug │ │ sudotoggle/ │ │ trap (Bash/Zsh) │ │
│ │ -status │ │ config │ │ │ │
│ └──────────────┘ └──────────────┘ └──────────────────────┘ │
│ │ │ │ │
│ ▼ ▼ ▼ │
│ ┌──────────────────────────────────────────────────────────┐ │
│ │ /etc/sudoers.d/nopasswd_<user> │ │
│ │ (NOPASSWD rule file) │ │
│ └──────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────┘
User runs: sudotoggle -on
│
▼
┌───────────────────┐
│ Authenticate │ ──► sudo -v (password prompt)
└───────────────────┘
│
▼
┌───────────────────┐
│ Create Rule │ ──► /etc/sudoers.d/nopasswd_<user>
└───────────────────┘
│
▼
┌───────────────────┐
│ Validate Syntax │ ──► visudo -cf
└───────────────────┘
│
▼
┌───────────────────┐
│ Save Expiry │ ──► ~/.config/sudotoggle/config
└───────────────────┘
│
▼
✅ Enabled!
# Clone the repository
git clone https://github.com/ashxj/sudotoggle.git
cd sudotoggle
# Make the script executable
chmod +x sudotoggle.sh
# (Optional) Move to a directory in your PATH
sudo mv sudotoggle.sh /usr/local/bin/sudotogglesudotoggle -statussudotoggle <command> [options]| Command | Arguments | Description |
|---|---|---|
-on |
(none) | Enable NOPASSWD indefinitely |
-on |
-time <seconds> |
Enable for specified duration |
-on |
-timef <HH:MM> |
Enable until specific time |
-off |
(none) | Disable NOPASSWD immediately |
-debug |
on / off |
Toggle debug output mode |
-status |
(none) | Display current configuration state |
-help |
(none) | Show help message |
| Argument | Type | Required | Default | Description |
|---|---|---|---|---|
-time |
Integer (seconds) | Yes (with -time) |
— | Duration in seconds (e.g., 3600 for 1 hour) |
-timef |
String (HH:MM) | Yes (with -timef) |
— | Target time in 24-hour format (e.g., 18:30) |
Configuration is stored in ~/.config/sudotoggle/config:
DEBUG=on
EXPIRY=1742334600| Key | Values | Description |
|---|---|---|
DEBUG |
on / off |
Enables debug output before sudo commands |
EXPIRY |
Unix timestamp / unlimited |
Automatic disable time |
# Enable indefinitely
sudotoggle -on
# Enable for 1 hour (3600 seconds)
sudotoggle -on -time 3600
# Enable until 6:30 PM today
sudotoggle -on -timef 18:30sudotoggle -off# Enable debug output
sudotoggle -debug on
# Sample output before sudo commands:
# [sudotoggle] 🔓 NOPASSWD ACTIVE — until 2025-03-17 18:30:00 (1234s remaining)sudotoggle -statusSample Output:
─────────────────────────────────────
NOPASSWD: ✅ ENABLED
Expiry: 2025-03-17 18:30:00 (1234s remaining)
Debug: on
User: quintarionity
File: /etc/sudoers.d/nopasswd_quintarionity
─────────────────────────────────────
| Path | Purpose |
|---|---|
/etc/sudoers.d/nopasswd_<user> |
Sudoers rule granting NOPASSWD access |
~/.config/sudotoggle/config |
User configuration (debug, expiry) |
~/.config/sudotoggle/hook.sh |
Shell hook for debug output and auto-expiry |
Solution: Run sudotoggle -status to check current state. Use -off first if you want to reset.
Solution: Source your shell config:
source ~/.bashrc # for Bash
source ~/.zshrc # for ZshSolution: Run sudo -k in those terminals to clear the credential cache.
Solution: The auto-disable triggers on the next sudo command. Run any sudo command to trigger the cleanup.
This project is provided as-is for educational and productivity purposes.
Made with ❤️ for the Linux community