How to monitor network bandwidth per user on Ubuntu server?

I have some shell users on a server with 300GB of monthly data transfer. How can I monitor bandwidth usage per user?


Solution 1:

As root, you could at least measure the outgoing traffic on a per-user basis using the "owner" module of iptables. If all the users you want to monitor are in /root/list-of-users.txt, you can do:

for login in $(cat /root/list-of-users.txt);
do
    iptables -N out_user_$login
    iptables -A OUTPUT -m owner --uid-owner $(id -u $login) -j out_user_$login
done

And then the packet and byte counts for each user's outbound traffic are visible:

iptables -L OUTPUT -n -v | grep out_

This could be extended further with CONNMARK to track the inbound side too.

Solution 2:

I just came across NetHogs:

NetHogs is a small 'net top' tool. Instead of breaking the traffic down per protocol or per subnet, like most tools do, it groups bandwidth by process.

enter image description here

This ought to let you track bandwidth by username. Might still need another couple tools to log the info and add it all up, but it's a good start without directly using iptables.