Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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

share|improve this question
    
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 W. Mar 14 '11 at 4:58
    
It must be possible, else how do ISPs cap bandwidth per user? –  djeikyb Mar 14 '11 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. –  Pedram Mar 14 '11 at 20:50
1  
Looks 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 –  djeikyb Mar 16 '11 at 3:01
    
Looks great.Thanks.I will give it a try. –  Pedram Mar 16 '11 at 7:47
add comment

4 Answers

up vote 11 down vote accepted
+50

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.

share|improve this answer
    
Thanks.It's great.I did this.But why can't I use the same method for INPUT? –  Pedram Mar 15 '11 at 9:21
1  
The 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. –  Kees Cook Mar 15 '11 at 19:11
    
Yeah.You're right.Thanks for the best answer. –  Pedram Mar 15 '11 at 22:35
add comment

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.

share|improve this answer
add comment

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/

share|improve this answer
    
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. –  Pedram Mar 14 '11 at 20:12
    
netstat -e shows the shell users, letting you link ip/host to username. –  djeikyb Mar 15 '11 at 2:05
add comment

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.

share|improve this answer
2  
Thanks but I saw both of them already and it seems non of them provide per user monitoring.I want to monitor usage per user. –  Pedram Mar 4 '11 at 7:23
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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