I want to set up a datacap system for a server I am running a proxy on.

I'm not interested in throttling bandwidth.

I just want to limit port to x GB per month/week/day etc. All my googling has returned data on throttling.

The server is an Ubuntu machine. Just a regular box.

Clients connect to it via an application I have no control over. The server is a proxy server.

The server accepts connections on port 2000, 3000, 4000 etc. Each port is bound to a user account.

I want to stop accepting connections on a per port basis once a data limit is reached.

PS: When I say port, I mean port number (0-65535)

Thank you.

  • 1
    The title seems misleading : you want to limit bw per port or per user?
    – schaiba
    Commented Mar 15, 2017 at 16:18
  • What kind of behavior are you expecting? User A can freely use a particular port Y up until Z GB, after which all traffic from A to Y is dropped? Meanwhile user B can use Y up until their cap of Z?
    – Centimane
    Commented Mar 15, 2017 at 16:42
  • 1
    doing this right is with netflow, however I am afraid it is out of scope here. Your post is also very scant on details Commented Mar 15, 2017 at 18:57
  • @RuiFRibeiro I made clarifications as regards to what I need.
    – Penguin
    Commented Mar 16, 2017 at 6:27
  • My comment still apllies. I assume it is a switch or router port; you mention no brand too. It is also not clear wether that machine behind that "port" is running or going through a proxy Commented Mar 16, 2017 at 6:32

1 Answer 1


There are several alternative ways to implement a traffic quota system.

I do think all involve at least some script/software development. It is far out of scope to help you with that here. However we can point you in the general direction.

In the general term, that involves accounting somehow the traffic used, and after x traffic, either modifying squid configuration on the fly or applying firewall rules in the source IPs of the customers. However, be aware that blocking than may create problems at the applicational level at least if you block them completely.

There are several ways of doing that accounting.

On the linux side, you can do it either:

  • parse squid logs for the traffic used per user;

  • create firewall rules on the ports the client arrive, and accounting for the traffic used (probably easier). see Traffic stats per network port . Just be aware that a server reboot resets the iptable stats, so it would be wiser to save them to an SQL database. (on a controlled reboot maybe iptables saves the traffic stats, however you can have a crash or power failure).

On the infra-structure side, you can also capture netflows on a router they go through if your equipment support it. There are open source solutions to use netflows, but then again it involves some script/software development. The solution will be more professional, but also more difficult to implement.

Again on the infra-structure side, one of the other ways to do it, is to capture SNMP data traffic from a switch port on your side where they go through.

Needless to say, that if you were doing it with netflows or SNMP, in a remote future, if using only networks, and not users as a source of traffic, you could probably do without the proxy, and just blocking the switch ports when the monthly quota is used up.


  • I used to run an ISP in Mozambique;
  • I used netflow data from my border routers that connected to the Internet;
  • when the customer exceed their monthly quota, their modem was blocked by my provisioning software;
  • nowadays I work somewhere else, still using netflows+Linux.

Some clues about neflows: How to gather full network usage statistics on a freebsd router?

  • Thank you Rui. I think I'll go ahead and script this, was hoping there was some magical Linux command. Thanks.
    – Penguin
    Commented Mar 16, 2017 at 9:06
  • Please read the updated answer. I saved it mid-writing due to my browser misbehaving. Commented Mar 16, 2017 at 9:07
  • 1
    I don't have control of the outgoing router. Will create the solution, and post as an answer.
    – Penguin
    Commented Mar 16, 2017 at 9:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .