Gateway + Shaper for home network on Ubuntu
Sometimes even a page in the FF opens for 2-3 minutes, since on a neighboring computer it downloads torrent at full speed.
The board of system administrators (that is, me) decided to create a gateway with a shaper that would dynamically share the channel into everyone.
Well, let's start.
There are two network cards, eth0 - looks to the Internet (the modem is in router mode, and eth1 - looks to the local network)
I will not describe the configs of the network interfaces themselves, but I will say that eth0 receives IP from the router, while on eth1 static IP is set, I selected 10.2.2.1
First, let's raise the DHCP server so that the remaining computers can get IP addresses automatically.
Install the DHCP server.
sudo apt-get install dhcp3-serverAfter that, we edit the /etc/dhcp3/dhcp.conf config, I brought it to this form, then we edit the / etc / default / dhcp3-server file and enter a line in it so that the server “listens” to this interface After what can we start the server to “distribute” the Internet to the internal network, we use IP masquerading (IPMASQUARADE) In an abbreviated form (without comments and non-functional message output ) the script looks like this: Save this to a file in /etc/profile.d and name it, to for example masq.sh. We make it executable and execute
subnet 10.2.2.0 netmask 255.255.255.0
{
option routers 10.2.2.1;
option subnet-mask 255.255.255.0;
option domain-name-servers 195.54.2.1;
option domain-name-servers 195.54.3.2;
range 10.2.2.10 10.2.2.254;
default-lease-time 21600;
max-lease-time 28800;
}INTERFACES=eth1sudo /etc/init.d/dhcp3-server start#!/bin/sh
# полная версия находится здесь: lafox.net/docs/masq
IPTABLES=/sbin/iptables
DEPMOD=/sbin/depmod
MODPROBE=/sbin/modprobe
EXTIF="eth0"
INTIF="eth1"
$DEPMOD -a
$MODPROBE ip_tables
$MODPROBE ip_conntrack
$MODPROBE ip_conntrack_ftp
$MODPROBE ip_conntrack_irc
$MODPROBE iptable_nat
$MODPROBE ip_nat_ftp
$MODPROBE ip_nat_irc
echo "1" > /proc/sys/net/ipv4/ip_forward
echo "1" > /proc/sys/net/ipv4/ip_dynaddr
$IPTABLES -P INPUT ACCEPT
$IPTABLES -F INPUT
$IPTABLES -P OUTPUT ACCEPT
$IPTABLES -F OUTPUT
$IPTABLES -P FORWARD DROP
$IPTABLES -F FORWARD
$IPTABLES -t nat -F
$IPTABLES -A FORWARD -i $EXTIF -o $INTIF -m state --state ESTABLISHED,RELATED -j ACCEPT
$IPTABLES -A FORWARD -i $INTIF -o $EXTIF -j ACCEPT
$IPTABLES -A FORWARD -j LOG
$IPTABLES -t nat -A POSTROUTING -o $EXTIF -j MASQUERADE
echo -e "done.\n"
sudo chmod +x /etc/profile.d/masq.sh
sudo sh /etc/profile.d/masq.sh
After these steps, you need to “lower” and then “raise” the eth1 network interface again. After that, clients will be able to receive IP addresses and use the Internet))) And now we will configure the shaper, in principle, this was all we thought about in order to dynamically share the speed Ineta. I chose the htb.init script for the shaper, which can be downloaded here sourceforge.net/projects/htbinit in the folder where HTB_PATH points to (I personally corrected this variable and I got / etc / htb, naturally this folder is not needed in the system create) create the following files: eth1: R2Q - coefficient determining the ratio of accuracy / speed of the shaper DEFAULT
sudo ifonfig eth1 down
sudo ifonfig eth1 up
sudo cp htb.init /etc/init.d/htb
sudo chmod +x /etc/init.d/htb
sudo update-rc.d htb defaults
R2Q=20
DEFAULT=0
- identifier of the class into which the packets fall if they do not fall under other rules. A class with identifier 0 always exists and passes packets without any shaping, that is, at full speed.
With this file, we initialized the shaper on the eth0 interface.
eth1-2.root:
RATE=24MbitWith this file, we created the root traffic class on the eth0 interface and limited the maximum upload speed through this class to 24 megabits.
eth1-2: 2001: With this file we created a class for the first client. RATE - guaranteed speed for the client. Since in our case there is no need to guarantee any speed, but HTB requires it, we proceed from the inequality: 24000Kbps / 3> RATE. CEIL - maximum speed for the client with a free channel. Leaf
RATE=512Kbit
CEIL=24Mbit
LEAF=sfq
RULE=10.2.2.10/24
- indicates that the class is one of the leaves of the tree, that is, traffic that meets a certain rule (RULE) gets into it. The sfq parameter means that we want the speed to be evenly distributed between sessions inside this class.
RULE is a rule that determines what traffic will fall into this class (see Note 1). In this case, all traffic with destination IPs from 10.2.2.10 to 10.2.2.255 gets into the class.
The purpose and values of the parameters that are indicated in the files, and file names can be found in the htb.init script - there is good help at the top.
We start our shaper
sudo /etc/init.d/htb startEverything, the shaper is on. Next, if something changes in the configuration, you need to do /etc/init.d/htb restart.
You can check the operation of the htb.init script, in addition to speed tests, by viewing the configuration with the following commands: Well, you can still screw a lot of things to our server, and Clam AV and firewall, but I will leave it to you))) Good luck! PS The article was published at the request of a friend who does not have access to Habr, but who wants to become one of the Habroyuzers (his mail is [email protected]). Upd1. Thanks for the karma transferred to Ubuntarium Upd2. The author of the article is now a Habr user - KTATTOO .
tc class show dev eth1
tc qdisc show dev eth1