linux下簡單限制網卡速度


Linux下限制網卡的帶寬,可用來模擬服務器帶寬耗盡,從而測試服務器在此時的訪問效果。

  1、安裝iproute

yum -y install iproute


  2、限制eth0網卡的帶寬為50kbit:

/sbin/tc qdisc add dev eth0 root tbf rate 50kbit latency 50ms burst 1000


  3、限制帶寬為50kbit后,在百兆局域網中wget下載一個大文件:

[root@localhost ~]# wget http://192.168.1.7/test.zip
--19:40:27--  http://192.168.1.7/test.zip
Connecting to 192.168.1.7:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 23862312 (23M) [application/zip]
Saving to: `test.zip'

37% [=======>   ] 8,994,816    457K/s  eta 27s

  下載速度為457K/s,限制效果達到。

  4、解除eth0網卡的帶寬限制:

/sbin/tc qdisc del dev eth0 root tbf


  5、對比:未作帶寬限制情況下,在百兆局域網中wget下載一個大文件:

[root@localhost ~]# wget http://192.168.1.7/test.zip    
--19:44:33--  http://192.168.1.7/test.zip
Connecting to 192.168.1.7:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 23862312 (23M) [application/zip]
Saving to: `test.zip'

100%[==========>] 23,862,312  6.14M/s   in 3.7s   

19:44:36 (6.16 MB/s) - `test.zip' saved [23862312/23862312]

  下載速度為6.16MB/s。


linux下針對源地址可以做流量的限速:


# iptables -A INPUT -p tcp -s 192.168.80.12 -m limit --limit 30/sec --limit-burst 3 -j ACCEPT
# iptables -A INPUT -p tcp -s 192.168.80.12 -j DROP
# iptables -A OUTPUT -p tcp -d 192.168.80.12 -m limit --limit 30/sec --limit-burst 3 -j ACCEPT
# iptables -A OUTPUT -p tcp -d 192.168.80.12 -m limit --limit 30/sec --limit-burst 3 -j ACCEPT
#iptables -A OUTPUT -p tcp -d 192.168.80.12 -j DROP


iptables -A INPUT -p tcp -s 192.168.80.15 -m limit --limit 5/sec --limit-burst 3 -j ACCEPT
iptables -A INPUT -p tcp -s 192.168.80.15 -j DROP
iptables -A OUTPUT -p tcp -s 192.168.80.15 -m limit --limit 5/sec --limit-burst 3 -j ACCEPT
iptables -A OUTPUT -p tcp -s 192.168.80.15 -j DROP


Linux下限制網卡的帶寬

2010年9月6日

10:40

Q: Iptables限制包的流速

A: 由-m limit --limit <[!]limitnum> --limit-burst <burstnum>

--limit: 速率限制/sec /minute /hour

--limit-burst: 最大的連接數。這個是用來限制最大可用數的。因為:

1. 如果當前包速超過limit限定的值的時,超速部分將直接跳過當前規則,進

入下一條規則的匹配。

2. 如果當前沒有包來,則limit會將該單位時間內的剩余量累計入下個單位時

間,但最大值不超過--limit-burst指定的值。

實例:從10.226.52.1上下載一個大文件,比較限速前與限速后的下載速度。。

限制速度前 (10M/s):過程如下所示

-bash-3.1#wget http://10.226.52.1/5GB.zip

--16:38:38--  http://10.226.52.1/5GB.zip

Connecting to 10.226.52.1:80... connected.

HTTP request sent, awaiting response... 200 OK

Length: 5362862509 (5.0G) [application/octet-stream]

Saving to: `5GB.zip'

 2% [                                        ] 113,341,300 10.0M/s  eta 9m 43s

限制速度后:

-bash-3.1# iptables -A INPUT -p tcp -s 10.226.52.1 -m limit --limit 30/sec --limit-burst 3 -j ACCEPT

-bash-3.1# iptables -A INPUT -p tcp -s 10.226.52.1 -j DROP (加這條的原因是INPUT鏈上的默認規則是ACCEPT)

-bash-3.1# iptables -A OUTPUT -p tcp -d 10.226.52.1 -m limit --limit 30/sec --limit-burst 3 -j ACCEPT

-bash-3.1# iptables -A OUTPUT -p tcp -d 10.226.52.1 -j DROP

-bash-3.1# wget http://10.226.52.1/5GB.zip

--10:08:32--  http://10.226.52.1/5GB.zip

Connecting to 10.226.52.1:80... connected.

HTTP request sent, awaiting response... 200 OK

Length: 5362862509 (5.0G) [application/octet-stream]

Saving to: `5GB.zip'
 0% [                                                              ] 461,912     14.1K/s  eta 5d 19h
將上面的30/3改成5/5網速就限制在了7K/s左右


第三種:
#ethtool -s eth0 speed 10 將千兆網卡改成10兆的網卡


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM