0

I have a script (I call it dusort.sh) I use to gauge which files are taking up the most space on my drive in a given folder. It essentially boils down to this:

#!/bin/bash

du --all --human-readable --max-depth=1 $1 | sort -h -r

I need to run the command with sudo because otherwise du cannot read certain directories (/, docker volume directories, etc). Is there a way to make it so I can run this command without being prompted for my root user password every time I want to check file sizes?

I have tried changing the file ownership

sudo chown root:root dusort.sh

but that doesnt seem to affect things. The only way to run this command without getting Permission denied warnings on files is like so:

sudo ./dusort.sh
New contributor
andykais is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
0

Woo! Ok I've got a working solution prompted by lx07. To get the solution I want, which is running a script with root permissions without typing sudo or inputting a password each time, there are two steps.

First, add these two lines to your /etc/sudoers file (edit with sudo visudo)

myusername ALL = (ALL) ALL
myusername ALL = (root) NOPASSWD: /path/to/myscript.sh

Second, add an alias to script.sh prefixed with sudo. In my case, I added it to ~/.bash_aliases

alias myscript='sudo /path/to/myscript.sh'

now you can run the script as root without specifying sudo or giving a password!

$ myscript
# it runs as root!
New contributor
andykais is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
0

You can change the /etc/sudoers file and append the following lines:

Cmnd_Alias     MONITORING=/usr/bin/du  
your_username ALL=(ALL) NOPASSWD:MONITORING

if you are not the only one that will use the scripts, create a group containting whe users and replace the last line for: %group_name ALL=(ALL) NOPASSWD:MONITORING

This way you won't be required for a password every time you execute the script

If you already have sudo powers just append the sudo in you script:

sudo du --all --human-readable --max-depth=1 $1 | sort -h -r

Your Answer

andykais is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.