리눅스 서버 차원의 트래픽 제한 명령어 tc > 서버관리

본문 바로가기
사이트 내 전체검색

서버관리

리눅스 서버 차원의 트래픽 제한 명령어 tc

페이지 정보

profile_image
작성자 관리자 (61.♡.12.126)
댓글 0건 조회 2,510회 작성일 16-05-10 13:09

본문

리눅스 서버에서 트래픽을 제한하고자 할 때,
웹서비스의 사이트별 트래픽 제어는 mod_cband ( apache 1.x 에서는 mod_throttle ) 등을 많이 사용한다.
그러나, 이는 웹서비스에 대한 트래픽만 제어가 가능한다.

서버 시스템 자체의 트래픽을 제한할 필요가 있다. 
ftp 서비스나 백업 트래픽, 시스템 크랙으로 인한 트래픽 과다 발생 등등의 이유에서다.

tc 라는 명령어로 랜카드( 보통 eth0 )의 트래픽 제어가 가능하다.
명령어가 생소하여 복잡하게 보일 수 있는데, 아래 스크립트를 이용하면 손쉽게 사용할 수 있다.

스크립트위치 : http://www.topwebhosts.org/tools/traffic-control.php
url 이 변경되었다. https://www.iplocation.net/traffic-control


[code]
#!/bin/bash
#
#  tc uses the following units when passed as a parameter.
#  kbps: Kilobytes per second
#  mbps: Megabytes per second
#  kbit: Kilobits per second
#  mbit: Megabits per second
#  bps: Bytes per second
#      Amounts of data can be specified in:
#      kb or k: Kilobytes
#      mb or m: Megabytes
#      mbit: Megabits
#      kbit: Kilobits
#  To get the byte figure from bits, divide the number by 8 bit
#

#
# Name of the traffic control command.
TC=/sbin/tc

# The network interface we're planning on limiting bandwidth.
IF=eth0            # Interface

# Download limit (in mega bits)
DNLD=1mbit          # DOWNLOAD Limit

# Upload limit (in mega bits)
UPLD=1mbit          # UPLOAD Limit

# IP address of the machine we are controlling
IP=xxx.xxx.xxx.xxx    # Host IP

# Filter options for limiting the intended interface.
U32="$TC filter add dev $IF protocol ip parent 1:0 prio 1 u32"

start() {

# We'll use Hierarchical Token Bucket (HTB) to shape bandwidth.
# For detailed configuration options, please consult Linux man
# page.

    $TC qdisc add dev $IF root handle 1: htb default 30
    $TC class add dev $IF parent 1: classid 1:1 htb rate $DNLD
    $TC class add dev $IF parent 1: classid 1:2 htb rate $UPLD
    $U32 match ip dst $IP/32 flowid 1:1
    $U32 match ip src $IP/32 flowid 1:2

# The first line creates the root qdisc, and the next two lines
# create two child qdisc that are to be used to shape download
# and upload bandwidth.
#
# The 4th and 5th line creates the filter to match the interface.
# The 'dst' IP address is used to limit download speed, and the
# 'src' IP address is used to limit upload speed.

}

stop() {

# Stop the bandwidth shaping.
    $TC qdisc del dev $IF root

}

restart() {

# Self-explanatory.
    stop
    sleep 1
    start

}

show() {

# Display status of traffic control status.
    $TC -s qdisc ls dev $IF

}

case "$1" in

  start)

    echo -n "Starting bandwidth shaping: "
    start
    echo "done"
    ;;

  stop)

    echo -n "Stopping bandwidth shaping: "
    stop
    echo "done"
    ;;

  restart)

    echo -n "Restarting bandwidth shaping: "
    restart
    echo "done"
    ;;

  show)

    echo "Bandwidth shaping status for $IF:"
    show
    echo ""
    ;;

  *)

    pwd=$(pwd)
    echo "Usage: tc.bash {start|stop|restart|show}"
    ;;

esac

exit 0
[/code]


주요 설정 부분은
IF : 랜카드 지정 ex) eth0
DNLD : 다운로드 제한 트래픽
UPLD : 업로드 제한 트래픽
IP : IF에 설정된 IP 주소

실행방법 - 위 스크립트를 tc.sh 로 저장하고 실행권한을 준 후에
# tc.sh start
# tc.sh stop
# tc.sh restart

위 스크립트의 자세한 설명과 사용법, 그리고 실제 제한되고 있는 mrtg 그래프 이미지는, 아래 사이트에서 잘 설명되어 있다.
http://www.xelloss.pe.kr/258


** 참고
http://www.topwebhosts.org/tools/traffic-control.php
http://www.xelloss.pe.kr/258
http://blog.hostmeca.com/4260


** 추가
잘 몰랐던 것이기도 하고, 한가지 재미있는 것은 ..
tc 로는 서버에서 나가는 트래픽만 제한이 가능하고 서버로 들어가는 트래픽은 제어하지 않는 다는 것이다. 헐~
https://kldp.org/node/59454

그럼 저 위의 스크립트에 있는 DNLD, UPLD 변수는 무슨 의미일까나? ㅡㅡ

테스트를 해 보니, UPLD 변수 값이 서버에서 나가는 트래픽 제한 값이다.
비트이기 때문에 바이트 값은 8 로 나누면 된다.


** 명령어 패키지 설치

redhat 계열에서 패키지로 tc 명령어를 설치하는 방법은 아래와 같다.
```
# dnf install iproute-tc
```
설치 패키지명은 배포본에 따라서 다르다.
https://command-not-found.com/tc

추천0

댓글목록

등록된 댓글이 없습니다.

Total 91건 2 페이지
  • RSS

검색


사이트 정보

Copyright © Baragi.Net. All rights reserved.