I have some shell users on a server with 300GB of monthly data transfer. How can I monitor bandwidth usage per user?
-
is this even possible? It seems that, theoretically, it'd be more hassle than its worth, because you'd technically have to have each process using the internet identify who's running it, then figure out the combined bandwidth usage. Any specific reasons you're looking to monitor on a per-user basis apart from the shell users you have? Because with 300GB monthly in/out data, it seems unlikely you'll top that amount :/– Thomas Ward ♦Mar 14, 2011 at 4:58
-
It must be possible, else how do ISPs cap bandwidth per user?– djeikybMar 14, 2011 at 9:21
-
@EvilPhoenix I want to monitor usage per user.300GB is not that much for a server with at least 20 users who use this server as a proxy server.– PedramMar 14, 2011 at 20:50
-
1Looks like you have a great practical answer from Kees Cook, but if you're still looking for methods, I wonder if phrasing the question differently could help. I found this guide googling "how to limit user bandwidth on linux": faqs.org/docs/Linux-HOWTO/Bandwidth-Limiting-HOWTO.html– djeikybMar 16, 2011 at 3:01
4 Answers
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.
-
Thanks.It's great.I did this.But why can't I use the same method for INPUT?– PedramMar 15, 2011 at 9:21
-
1The owner of the packet isn't known for incoming packets because from the kernel's perspective, it came from outside the machine. CONNMARK could be used to tie packets in TCP streams to their originator, etc, but I don't have a working example of this. Mar 15, 2011 at 19:11
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.
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.
-
It can also be started in the mode to sum up traffic:
sudo nethogs -v 3 eth0
(or pressm
repeatedly after starting to cycle through modes). Combine withtmux
for running persistently (even if yourssh
session crashes).– taniusMar 31, 2018 at 13:57
You can use Cacti
Cacti is a complete frontend to RRDTool, it stores all of the necessary information to create graphs and populate them with data in a MySQL database. The frontend is completely PHP driven. Along with being able to maintain Graphs, Data Sources, and Round Robin Archives in a database, cacti handles the data gathering. There is also SNMP support for those used to creating traffic graphs with MRTG.
Or vnStat
vnStat is a console-based network traffic monitor for Linux and BSD that keeps a log of network traffic for the selected interface(s). It uses the network interface statistics provided by the kernel as information source. This means that vnStat won't actually be sniffing any traffic and also ensures light use of system resources.
Both of them are great.
-
3Thanks but I saw both of them already and it seems non of them provide per user monitoring.I want to monitor usage per user.– PedramMar 4, 2011 at 7:23
I looked a bit, and I haven't found a comprehensive gui package that does what you want. Hopefully one exists and someone will post about it here eventually.
I'm not really a networking guy, but from what I've read, among many other things netstat
and iptables
are supposed to do for ip/host -based user accounting what the acct
tools did for system process accounting. This cyberciti.biz link might set you on your way to developing a system with these tools:
http://www.cyberciti.biz/faq/linux-configuring-ip-traffic-accounting/
-
Thanks, but as you said they are used for ip-based user accounting.I have some shell user which may use the server in different locations, specially in a university department with the same ip address (behind a NAT).So I'm looking for a user-based accounting solution, If it's possible.– PedramMar 14, 2011 at 20:12
-
netstat -e
shows the shell users, letting you link ip/host to username.– djeikybMar 15, 2011 at 2:05