{"id":1761,"date":"2026-11-16T22:39:21","date_gmt":"2026-11-16T22:39:21","guid":{"rendered":"https:\/\/virtualserversvps.com\/blog\/?p=1761"},"modified":"2026-11-16T22:39:21","modified_gmt":"2026-11-16T22:39:21","slug":"iproute2-tc-htb-fq-codel-shaping-vps","status":"publish","type":"post","link":"https:\/\/virtualserversvps.com\/blog\/iproute2-tc-htb-fq-codel-shaping-vps\/","title":{"rendered":"Egress Shaping on a VPS with tc HTB and fq_codel: Config, Counters, Verification"},"content":{"rendered":"<div style=\"padding:20px\">\n<p>Egress shaping on a VPS is usually done badly or not at all. Without a queue discipline, a burst of uploads fills the provider&#8217;s buffer, latency to every interactive user climbs, and everyone blames the network. A small HTB tree with fq_codel leaves gives you a hard ceiling per class, low queuing delay, and counters you can read at any time.<\/p>\n<h2>Confirming your shaping options<\/h2>\n<p>You need CAP_NET_ADMIN inside the guest and the <code>sch_htb<\/code> and <code>sch_fq_codel<\/code> modules available. Check with <code>tc qdisc show dev eth0<\/code> and <code>modprobe sch_htb<\/code>. Most KVM providers permit this; container plans often do not, and <code>tc<\/code> will fail with an operation-not-permitted error.<\/p>\n<p>Know your real uplink. Shaping at exactly the advertised port speed produces drops upstream where the provider polices, which is worse than shaping slightly below it. Set the HTB root rate to 90% to 95% of measured upload. Measure it first with a single <code>iperf3 -c<\/code> upload run, not with the plan&#8217;s marketing figure. On a 1 Gbit plan that typically means a root rate of 900 Mbit, not 1000.<\/p>\n<h2>The HTB tree layout<\/h2>\n<p>A practical design has one root class at the measured rate, then child classes for traffic types: interactive SSH and DNS at low latency, HTTP responses at a guaranteed rate, and bulk backup at a capped rate. HTB allocates unused bandwidth to classes that need it, so the sum of class rates can exceed the root rate as long as ceilings are set correctly.<\/p>\n<p>Structure: root qdisc <code>handle 1:<\/code> htb default 30, then a class <code>1:1<\/code> at the uplink rate, with children <code>1:10<\/code> for interactive, <code>1:20<\/code> for web, and <code>1:30<\/code> for bulk. Give interactive a rate of 5% with a 2 Mbit ceiling, web 60%, and bulk the remainder. Each leaf gets fq_codel, never htb&#8217;s own queue. HTB&#8217;s built-in fifo has no flow hashing and produces head-of-line blocking when one connection saturates.<\/p>\n<h2>Writing the commands<\/h2>\n<p>Apply from a shell script so the change is reproducible after reboot. Start with <code>tc qdisc del dev eth0 root<\/code>, ignoring the error if nothing is attached, then <code>tc qdisc add dev eth0 root handle 1: htb default 30<\/code>. Add the parent class with <code>tc class add dev eth0 parent 1: classid 1:1 htb rate 95mbit ceil 95mbit<\/code>.<\/p>\n<p>Then add each leaf and its fq_codel qdisc: <code>tc qdisc add dev eth0 parent 1:30 handle 30: fq_codel limit 10240 flows 1024 quantum 1514 target 5ms interval 100ms<\/code>. The <code>limit<\/code> of 10240 packets and <code>interval<\/code> of 100 ms are the defaults worth starting from; lower the target to 5 ms for all leaves. Classify traffic with <code>tc filter add dev eth0 parent 1: protocol ip prio 1 u32 match ip dport 22 0xffff flowid 1:10<\/code>.<\/p>\n<h2>Classifying without breaking connections<\/h2>\n<p>Destination-port filters are cheap and effective. Match SSH, DNS, and NTP to the interactive class, match your application ports to the web class, and leave everything else in the default bulk class. Avoid deep packet inspection; on a VPS, u32 or flower matches cost almost nothing while netfilter-based classifiers add a kernel dependency and conntrack load.<\/p>\n<p>For application-level classification without port guessing, have the service mark packets itself with <code>setsockopt(SO_MARK)<\/code> or <code>iptables -j MARK<\/code>, then match marks in tc with <code>match meta mark 0x10<\/code>. This keeps the classifier aligned with intent even when ports change.<\/p>\n<h2>Reading the counters<\/h2>\n<p>Run <code>tc -s qdisc show dev eth0<\/code> and watch for <code>dropped<\/code> and <code>overlimits<\/code>. Drops inside an fq_codel leaf are expected under saturation and mean the queue was full; drops at the root HTB class mean the sum of child demand exceeds the uplink and shaping is working. A steady climb in HTB drops with interactive traffic intact is a healthy sign.<\/p>\n<p>Check per-class statistics with <code>tc -s class show dev eth0<\/code>. If the bulk class shows far more than its rate in bytes over a minute, the ceiling is misconfigured or traffic is landing in the wrong class. Verify with <code>tc -s filter show dev eth0<\/code> that no rule after the match you expect is being hit instead.<\/p>\n<h2>Measuring the effect on latency<\/h2>\n<p>Start a saturating upload and simultaneously ping an external host. Without shaping, RTT often triples because the provider buffers the excess. With HTB plus fq_codel and a correctly set root rate, the added delay should stay under 20 ms. Record both numbers; the ping under load is the real proof the shaping works.<\/p>\n<p>Use <code>ping -i 0.2 -c 300 1.1.1.1 &gt; ping.log<\/code> during the upload, then read min, avg, and max from the summary. A max of 60 ms under load against 20 ms idle indicates either an incorrect root rate or a provider-side shaper that is policing below your configured limit. Repeat the test with ten parallel uploads instead of one, since a single stream can hide queue growth behind TCP&#8217;s own pacing.<\/p>\n<h2>Persisting and testing the configuration<\/h2>\n<p>Write the script to <code>\/usr\/local\/sbin\/tc-shape.sh<\/code>, make it executable, and call it from a systemd unit with <code>After=network-online.target<\/code>. Do not rely on rc.local, which many images no longer run. Add an idempotent guard so the script removes an existing root qdisc before adding a new one.<\/p>\n<p>After a reboot, confirm with <code>tc -s qdisc show dev eth0<\/code> that the tree is present and counters are moving. Then run the upload-and-ping test again. If behaviour differs from the manual run, the unit started before the interface had a link, a common cause of silent failure. Compare shaping against unmanaged <a href=\"https:\/\/virtualserversvps.com\/\">VPS hosting<\/a> defaults and, if you need more headroom for bulk transfers, review the dedicated bandwidth options in this <a href=\"https:\/\/virtualserversvps.com\/vps-vs-dedicated-server.html\">server comparison<\/a>.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Egress shaping on a VPS is usually done badly or not at all. Without a queue discipline, a burst of uploads fills the provider&#8217;s buffer, latency to every interactive user&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":2,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1761","post","type-post","status-publish","format-standard","hentry","category-performance-optimization"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.1 (Yoast SEO v26.1) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Egress Shaping on a VPS with tc HTB and fq_codel: Config, Counters, Verification - Virtual Servers VPS Blog<\/title>\n<meta name=\"description\" content=\"Egress shaping on a VPS is usually done badly or not at all. Without a queue discipline, a burst of uploads fills the provider&#039;s buffer, latency to every\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/virtualserversvps.com\/blog\/iproute2-tc-htb-fq-codel-shaping-vps\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Egress Shaping on a VPS with tc HTB and fq_codel: Config, Counters, Verification\" \/>\n<meta property=\"og:description\" content=\"Egress Shaping on a VPS with tc HTB and fq_codel: Config, Counters, Verification\" \/>\n<meta property=\"og:url\" content=\"https:\/\/virtualserversvps.com\/blog\/iproute2-tc-htb-fq-codel-shaping-vps\/\" \/>\n<meta property=\"og:site_name\" content=\"Virtual Servers VPS Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-11-16T22:39:21+00:00\" \/>\n<meta name=\"author\" content=\"Virtual-Servers-Vps-Editor\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Virtual-Servers-Vps-Editor\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/virtualserversvps.com\/blog\/iproute2-tc-htb-fq-codel-shaping-vps\/\",\"url\":\"https:\/\/virtualserversvps.com\/blog\/iproute2-tc-htb-fq-codel-shaping-vps\/\",\"name\":\"Egress Shaping on a VPS with tc HTB and fq_codel: Config, Counters, Verification - Virtual Servers VPS Blog\",\"isPartOf\":{\"@id\":\"https:\/\/virtualserversvps.com\/blog\/#website\"},\"datePublished\":\"2026-11-16T22:39:21+00:00\",\"author\":{\"@id\":\"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/82a299a8284a66ff49f97c74684724a0\"},\"description\":\"Egress shaping on a VPS is usually done badly or not at all. Without a queue discipline, a burst of uploads fills the provider's buffer, latency to every\",\"breadcrumb\":{\"@id\":\"https:\/\/virtualserversvps.com\/blog\/iproute2-tc-htb-fq-codel-shaping-vps\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/virtualserversvps.com\/blog\/iproute2-tc-htb-fq-codel-shaping-vps\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/virtualserversvps.com\/blog\/iproute2-tc-htb-fq-codel-shaping-vps\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/virtualserversvps.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Egress Shaping on a VPS with tc HTB and fq_codel: Config, Counters, Verification\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/virtualserversvps.com\/blog\/#website\",\"url\":\"https:\/\/virtualserversvps.com\/blog\/\",\"name\":\"Virtual Servers VPS Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/virtualserversvps.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/82a299a8284a66ff49f97c74684724a0\",\"name\":\"Virtual-Servers-Vps-Editor\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d820b15f1cd028e97610d9adf536df7be5cb6423869967037d468d5355fa003f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d820b15f1cd028e97610d9adf536df7be5cb6423869967037d468d5355fa003f?s=96&d=mm&r=g\",\"caption\":\"Virtual-Servers-Vps-Editor\"},\"sameAs\":[\"https:\/\/virtualserversvps.com\/blog\"],\"url\":\"https:\/\/virtualserversvps.com\/blog\/author\/virtualserversvps\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Egress Shaping on a VPS with tc HTB and fq_codel: Config, Counters, Verification - Virtual Servers VPS Blog","description":"Egress shaping on a VPS is usually done badly or not at all. Without a queue discipline, a burst of uploads fills the provider's buffer, latency to every","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/virtualserversvps.com\/blog\/iproute2-tc-htb-fq-codel-shaping-vps\/","og_locale":"en_US","og_type":"article","og_title":"Egress Shaping on a VPS with tc HTB and fq_codel: Config, Counters, Verification","og_description":"Egress Shaping on a VPS with tc HTB and fq_codel: Config, Counters, Verification","og_url":"https:\/\/virtualserversvps.com\/blog\/iproute2-tc-htb-fq-codel-shaping-vps\/","og_site_name":"Virtual Servers VPS Blog","article_published_time":"2026-11-16T22:39:21+00:00","author":"Virtual-Servers-Vps-Editor","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Virtual-Servers-Vps-Editor","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/virtualserversvps.com\/blog\/iproute2-tc-htb-fq-codel-shaping-vps\/","url":"https:\/\/virtualserversvps.com\/blog\/iproute2-tc-htb-fq-codel-shaping-vps\/","name":"Egress Shaping on a VPS with tc HTB and fq_codel: Config, Counters, Verification - Virtual Servers VPS Blog","isPartOf":{"@id":"https:\/\/virtualserversvps.com\/blog\/#website"},"datePublished":"2026-11-16T22:39:21+00:00","author":{"@id":"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/82a299a8284a66ff49f97c74684724a0"},"description":"Egress shaping on a VPS is usually done badly or not at all. Without a queue discipline, a burst of uploads fills the provider's buffer, latency to every","breadcrumb":{"@id":"https:\/\/virtualserversvps.com\/blog\/iproute2-tc-htb-fq-codel-shaping-vps\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/virtualserversvps.com\/blog\/iproute2-tc-htb-fq-codel-shaping-vps\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/virtualserversvps.com\/blog\/iproute2-tc-htb-fq-codel-shaping-vps\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/virtualserversvps.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Egress Shaping on a VPS with tc HTB and fq_codel: Config, Counters, Verification"}]},{"@type":"WebSite","@id":"https:\/\/virtualserversvps.com\/blog\/#website","url":"https:\/\/virtualserversvps.com\/blog\/","name":"Virtual Servers VPS Blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/virtualserversvps.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/82a299a8284a66ff49f97c74684724a0","name":"Virtual-Servers-Vps-Editor","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/virtualserversvps.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d820b15f1cd028e97610d9adf536df7be5cb6423869967037d468d5355fa003f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d820b15f1cd028e97610d9adf536df7be5cb6423869967037d468d5355fa003f?s=96&d=mm&r=g","caption":"Virtual-Servers-Vps-Editor"},"sameAs":["https:\/\/virtualserversvps.com\/blog"],"url":"https:\/\/virtualserversvps.com\/blog\/author\/virtualserversvps\/"}]}},"_links":{"self":[{"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/posts\/1761","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/comments?post=1761"}],"version-history":[{"count":1,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/posts\/1761\/revisions"}],"predecessor-version":[{"id":1762,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/posts\/1761\/revisions\/1762"}],"wp:attachment":[{"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/media?parent=1761"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/categories?post=1761"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/virtualserversvps.com\/blog\/wp-json\/wp\/v2\/tags?post=1761"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}