티스토리 뷰

카테고리 없음

[Linux] iftop 설치 및 사용법

드라이빙 인사이트 2019. 11. 21. 06:45
반응형

서버 머신의 네트워크 트래픽을 모니터링 할 일이 생겨서 여러가지 툴을 알아보다 'iftop' 이라는 툴을 알게 되었다. 이름에서 알 수 있듯이 'top' 명령어를 이용해 CPU와 메모리 사용 현황을 모니터링하는 것처럼 iftop 명령을 이용해서 네트워크 인터페이스의 트래픽을 모니터링 할 수 있다.

 

네트워크 인터페이스를 통해 오고가는 트래픽을 프로토콜 별, 소스별, 목적지 별로 모니터링 할 수 있으며 다양한 형태의 응용을 할 수 있도록 옵션들이 제공된다.

iftop 설치

iftop 사용을 위해서 libpcap과 libcurses가 설치되어 있어야 할 수 있으니 미리 설치해두자.

# Centos
yum install iftop
# ubuntu
apt-get install iftop

 

각 운영체제에 맞는 패키징 관리 명령을 통해 설치할 수 있다. 루트 권한이 필요할 수 있으니 머신의 루트 권한을 획득해야 할 필요가 있을 수 있다.

iftop 사용법

iftop의 사용법은 'iftop --help' 혹은 'iftop -h' 명령을 실행해서 확인할 수 있다.

$ sudo iftop -h
iftop: display bandwidth usage on an interface by host

Synopsis: iftop -h | [-npblNBP] [-i interface] [-f filter code]
[-F net/mask] [-G net6/mask6]

-h display this message
-n don't do hostname lookups
-N don't convert port numbers to services
-p run in promiscuous mode (show traffic between other
hosts on the same network segment)
-b don't display a bar graph of traffic
-B Display bandwidth in bytes
-i interface listen on named interface
-f filter code use filter code to select packets to count
(default: none, but only IP packets are counted)
-F net/mask show traffic flows in/out of IPv4 network
-G net6/mask6 show traffic flows in/out of IPv6 network
-l display and count link-local IPv6 traffic (default: off)
-P show ports as well as hosts
-m limit sets the upper limit for the bandwidth scale
-c config file specifies an alternative configuration file
-t use text interface without ncurses

Sorting orders:
-o 2s Sort by first column (2s traffic average)
-o 10s Sort by second column (10s traffic average) [default]
-o 40s Sort by third column (40s traffic average)
-o source Sort by source address
-o destination Sort by destination address

The following options are only available in combination with -t
-s num print one single text output afer num seconds, then quit
-L num number of lines to print

iftop, version 1.0pre4
copyright (c) 2002 Paul Warren <pdw@ex-parrot.com> and contributors

 

쉘에서 'iptop' 명령을 실행하면 현재 네트워크 사용량을 모니터링 할 수 있는 화면이 나온다.

$ sudo iftop

 

별도의 인터페이스를 옵션으로 명시하지 않으면 첫 번째 네트워크 인터페이스에 iftop 명령이 실행된다. 또 한 시스템의 리소스를 모니터링 하는 행위에 루트 권한이 필요할 수 있으니 모니터링에 root 계정을 사용하거나 sudo 권한이 필요할 수 있다.

 

iftop을 실행하면 현재 네트워크 인터페이스의 사용정보가 모니터링되기 시작한다. 화면에 나오는 여러 라인의 레코드들은 현재 접속중인 머신에서 통신을 하고 있는 호스트들과 그 호스트와의 통신에 대한 통계 정보를 보여준다.

 

각 라인의 첫 번째 항목은 현재 접속중인 머신의 호스트 네임 혹은 IP 주소를 의미한다. 위 사진에서 'c603ad4f5fd8'라는 정보로 표시되고 있는 것이 현재 접속중인 머신의 호스트 정보다. (테스트를 위해 도커 컨테이너를 띄워서 iftop 툴을 설치해 사용했기 때문에 컨테이너의 id가 호스트 정보에 표시되고 있다.)

 

바로 다음 항목은 접속중인 머신이 통신하고 있는 대상의 호스트 네임 혹은 IP 주소를 의미한다. 현재 접속해있는 머신이 서버라면 서버와 통신하고 있는 다른 서버 혹은 클라이언트의 정보가 여기에 표시된다. (위 사진에서는 'apt-get install' 명령과 'ping google.com' 명령을 실행했던 흔적을 볼 수 있다.)

 

그 다음 항목은 각각 2초/10초/40초 동안의 평균 전송량을 의미한다.

 

화면 아래쪽에 표시되는 정보는 NIC를 통해서 주고 받은 데이터의 양을 표시해준다.

  • TX : 전송량 (Transmitting)

  • RX : 수신량 (Receiving)

  • TOTAL : 총량

각 항목들은 다음과 같은 서브 항목들로 세분화 된다

  • cum : 누적 전송량

  • peak : 피크 상태일 때의 전송량

  • rates : 각각 2초/4초/10초간 평균 데이터 흐름

주요 iftop 옵션

iftop -i {interface}

-i 옵션으로 모니터링 할 네트워크 인터페이스 이름을 명시할 수 있다.

 

$ sudo iftop -i eth0

iftop -f {filter}

-f 옵션에 pcap-filter 형식의 필터링 문자열을 명시하여 모니터링하고 싶은 항목을 제한할 수 있다. (dst: Destination, src: Source) pcap-filter 형식은 대략적으로 다음과 같이 생겼다.

  • dst host XXXX

  • src host XXXX

  • dst net XXXX

  • src net XXXX

  • dst port XXXX

  • src port XXXX

  • dst portrange start-end

  • src portrange start-end

  • gateway xxxx

  • ip proto protocol

자세한 내용은 PCAP-FILTER 매뉴얼 페이지를 참조하도록 하자. (링크 : Manpage of PCAP-FILTER)

 

iftop -f 옵션은 다음과 같이 사용할 수 있다.

 

$ sudo iftop -i eth0 -f "dst host google.com"
$ sudo iftop -i eth1 -f "dst port 22"

 

이 필터 조건을 이용해서 원하는 호스트 혹은 원하는 포트를 사용하는 트래픽 통계만 뽑아 볼 수 있다.

인터랙티브 모드 단축키

iftop 명령을 실행시킨 상태에서 단축키들을 이용해 모니터링 표시 항목을 변경할 수 있다. 인터랙티브 모드에서의 단축키는 '?' 키를 입력하면 볼 수 있다.

 

Host display:
 n - toggle DNS host resolution
 s - toggle show source host
 d - toggle show destination host
 t - cycle line display mode

Port display:
 N - toggle service resolution
 S - toggle show source port
 D - toggle show destination port
 p - toggle port display

Sorting:
 1/2/3 - sort by 1st/2nd/3rd column
 < - sort by source name
 > - sort by dest name
 o - freeze current order

 General:
  P - pause display
  h - toggle this help display
  b - toggle bar graph display
  B - cycle bar graph average
  T - toggle cumulative line totals
  j/k - scroll display
  f - edit filter code
  l - set screen filter
  L - lin/log scales
  ! - shell command
  q - quit

 

설정파일

iftop 명령을 실행시킬 때 너무 많은 옵션을 주면 불편할 때가 있다. 이 때  설정 파일을 만들어서 파일 에디팅을 한 다음 iftop 실행시 명시하면 편하게 여러 옵션들을 다룰 수 있다.

 

기본적으로 ~/.iftoprc 파일에 설정 사항을 저장하면, iftop 명령 실행시 기본적으로 ~/.iftoprc 파일의 내용을 기준으로 동작하게 된다. (~/.bashrc 혹은 ~/.vimrc 파일의 동작원리를 생각해보면 이해하기 쉽다.)

 

혹은 iftop -c 옵션의 인자로 설정 파일을 넘겨줘도 된다.

$ sudo iftop -c <config file>

 

설정 파일에 명시 할 수 있는 옵션들은 다양한데 자세한 내용은 'iftop manpage'를 확인해보면 된다. (링크 : iftop manpage)

반응형
댓글
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/03   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
글 보관함