天天看點

linux網絡常用指令

netstat    (檢視端口)

使用方法:

netstat -lnp    (檢視監聽端口,以及此端口用于那個服務)

netstat -an    (檢視端口在與哪個IP,哪個端口進行通訊,以及現在的狀态)

netstat -an |grep 192.168.111.129:80 |grep -ic ESTAB    (檢視一秒内與80端口通信的連接配接的和)

tcpdump    (抓包)

安裝tcpdump

tcpdump -nn    (-nn選項可将域名轉換為IP,将端口也轉換為數值)

tcpdump -nn -c 100    (抓100個包)

tcpdump -nn -i eth1    (抓指定網卡eth1的包)

tcpdump -nn port 22    (抓指定端口22的包)

tcpdump -nn tcp/udp and port 80 and host 211.157.132.91    (可以抓tcp或者udp的包,也可以用and連接配接,抓指定的端口和指定的主機位址)

tcpdump -nn -s0 tcp and port 22 and host 192.169.111.128 -w 1.cap    (-w将包内容寫入檔案,-s0抓包的全部内容;将抓到的包以二進制格式寫入到1.cap檔案中)

抓到的包可用tcpdump -r 1.cap指令檢視資料包的流向

用專業的二進制讀取軟體可檢視到1.cap檔案中抓到的包的資料,如包是圖檔則能看到該圖檔

tshark

tshark -n -t a -R http.request -T fields -e "frame.time" -e "ip.src" -e "http.host" -e "http.request.method" -e "http.request.uri"

selinux    (防火牆)

getenforce    (檢視selinux目前狀态)

setenforce    (在selinux處于enforcing狀态下時,開啟和關閉selinux)

setenforce 0    (關閉)

setenforce 1    (開啟)

iptables    (防火牆)

包含三張表:mangle、nat、filter

iptables -t filter -nvL    (檢視filter表下有哪些鍊,filter表主要是做包過濾)

iptables -t filter -I INPUT -p tcp --drop 80 -s 192.168.111.123 -j DROP   (針對filter表的插入規則INPUT包,協定為tcp的,對80端口,host

iptables -P INPUT ACCEPT    (将filter表中的INPUT鍊預設規則改為ACCEPT)為192.168.111.123的包進行過濾,過濾出來後直接扔掉,如果想要拒絕或接受,則為-j REJECT/ACCEPT)

iptables -t filter -A INPUT -p tcp --drop 80 -s 192.168.111.123 -j DROP   (-A與-I選項的差別為-A添加到最下面,-I添加到最上面;最上面的規則最先生效)

iptables -nvL    (檢視filter表下的規則)

iptables -t filter -D INPUT -p tcp --drop 80 -s 192.168.111.123 -j REJECT    (-D删除filter表中的這個規則)

iptables -t nat -nvL    (檢視nat表下有哪些鍊)

iptables -t mangle -nvL    (檢視mangle表下有哪些鍊)

iptables -Z    (重置filter表中的計數器)

iptables -t nat -Z    (重置nat表中的計數器)

iptables -F    (清空filter表中的所有規則)

service iptables save    (儲存iptables規則)

iptables-save > 1.ipt    (将iptables規則備份到1.ipt檔案中)

iptables-restore < 1.ipt    (将備份的iptables規則恢複)

#!bin/bash

##iptables練習題

iptables -F

iptables -P INUPT DROP

iptables -P OUTPUT ACCEPT

iptables -P FORWARD ACCEPT

iptables -A INPUT -s 192.168.0.0/24 -p tcp --dport 22 -j ACCEPT

iptables -A INPUT --dport 80 -p tcp -j ACCEPT

iptables -A INPUT --dport 21 -p tcp -j ACCEPT

filter;nat;mangle表的作用

filter

内建三個鍊INPUT、OUTPUT以及FORWARD。INPUT作用于進入本機的包;OUTPUT作用于本機送出的包;FORWARD作用于那些跟本機無關的包。

nat

使用nat表實作路由器的功能

mangle

給包做标記,再對包進行操作

本文轉自 粗糧面包 51CTO部落格,原文連結:http://blog.51cto.com/culiangmianbao/1770457,如需轉載請自行聯系原作者

繼續閱讀