Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute 'iftop' without sudo

I have a script that runs iftop in text mode, cuts down the output to what I'm concerned in, and saves it to a text file along with the output of the date command (I am monitoring network usage on various interfaces over time). Only problem I'm having is I'm trying to run my script every 15 minutes via the crontab, and in order to run the iftop command I need sudo permissions. Does anyone know some way to change the permissions of iftop to make it so I don't need sudo permissions?

Alternatively if I can give the script the ability to run the command with sudo that would be fine by me as well. I tried adding the script to the sudoers file via sudo visudo and adding the line:

user ALL=(ALL) NOPASSWD: /home/user/network_usage.sh

but that didn't work...perhaps a result of executing from the crontab?

Thanks,

-Eric

like image 254
Eric Avatar asked Feb 07 '23 11:02

Eric


1 Answers

A more granular approach would be to use:

# setcap cap_net_raw=eip $(which iftop)

This lets iftop capture packets but does not give the process full root privileges. In case of a security problem or bug with "iftop" its side effects would be much more limited.

Related: https://unix.stackexchange.com/questions/189750/how-can-i-run-script-without-root-to-sniff-network-libpcap

like image 159
Steve Bonds Avatar answered Feb 10 '23 23:02

Steve Bonds