天天看點

LINUX網絡基礎

七層OSI 和四層TCP/IP參考模型:

  

應用層:最高層,提供用網絡與使用者應該軟體之間的接口服務,如HTTP,FTP,SMTP,POP3

表示層:負責資料格式變換,加密,壓縮,恢複等

會話層:用于組織兩個會話程序之間的通信

傳輸層:提供端到端服務;TCP/UDP

網絡層:通過路由選擇算法為分組通過子網選擇最佳路徑

資料鍊路層:

實體層:最底層,利用實體傳輸媒體,為資料鍊路層提供實體連接配接

應用層:

傳輸層:

互聯層:

主機-網絡層

IP位址:IPv4 主流

        IPv6 趨勢

A類: 1.0.0.0 - 126.255.255.255

B類: 128.0.0.0 - 191.255.255.255

C類:  192.0.0.0 - 223.255.255.255

特殊位址 10.0.0.0 -10.255.255.255

     127.0.0.1

     172.16.0.0 -172.31.255.255 

     192.168.0.0- 192.168.255.255

          

[root@Freedom1991 /]# ipcalc -bmn 192.168.1.3/24  計算子網路遮罩,廣播,和網絡号

NETMASK=255.255.255.0

BROADCAST=192.168.1.255

NETWORK=192.168.1.0

[root@Freedom1991 /]# route  -n  檢視路由資訊

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0

172.16.0.0      0.0.0.0         255.255.0.0     U     0      0        0 eth1

169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0

169.254.0.0     0.0.0.0         255.255.0.0     U     1003   0        0 eth1

0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth0

 增加一條路由,目的網段;211.220.1.0/24   所有資料通過網關 192.168.1.1 送出,且經過eth0網卡

[root@Freedom1991 /]# route add -net 211.220.1.0 netmask 255.255.255.0 gw 192.168.1.1 dev eth0     

[root@Freedom1991 /]# route  -n

211.220.1.0     192.168.1.1     255.255.255.0   UG    0      0        0 eth0

[root@Freedom1991 /]# route del -net 211.220.1.0/24 gw 192.168.1.1 dev eth0

删除

[root@Freedom1991 /]# ifconfig -   檢視網絡裝置接口資訊

eth0      Link encap:Ethernet  HWaddr 00:0C:29:A0:9B:D5  

          inet addr:192.168.1.3  Bcast:192.168.1.255  Mask:255.255.255.0

          inet6 addr: fe80::20c:29ff:fea0:9bd5/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:27697 errors:0 dropped:0 overruns:0 frame:0

          TX packets:8761 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000 

          RX bytes:2020334 (1.9 MiB)  TX bytes:2538083 (2.4 MiB)

          Interrupt:19 Base address:0x2000 

eth1      Link encap:Ethernet  HWaddr 00:0C:29:A0:9B:DF  

          inet addr:172.16.100.1  Bcast:172.16.255.255  Mask:255.255.0.0

          inet6 addr: fe80::20c:29ff:fea0:9bdf/64 Scope:Link

          RX packets:81 errors:0 dropped:0 overruns:0 frame:0

          TX packets:18 errors:0 dropped:0 overruns:0 carrier:0

          RX bytes:15030 (14.6 KiB)  TX bytes:1324 (1.2 KiB)

          Interrupt:16 Base address:0x2080 

lo        Link encap:Local Loopback  

          inet addr:127.0.0.1  Mask:255.0.0.0

          inet6 addr: ::1/128 Scope:Host

          UP LOOPBACK RUNNING  MTU:65536  Metric:1

          RX packets:1568 errors:0 dropped:0 overruns:0 frame:0

          TX packets:1568 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:0 

          RX bytes:89656 (87.5 KiB)  TX bytes:89656 (87.5 KiB)

設定一塊網卡的子接口,并激活

[root@Freedom1991 /]# ifconfig eth0:0 192.168.1.4 netmask 255.255.255.0 up

[root@Freedom1991 /]# ifconfig -a

          RX packets:27971 errors:0 dropped:0 overruns:0 frame:0

          TX packets:8892 errors:0 dropped:0 overruns:0 carrier:0

          RX bytes:2043236 (1.9 MiB)  TX bytes:2550985 (2.4 MiB)

eth0:0    Link encap:Ethernet  HWaddr 00:0C:29:A0:9B:D5  

          inet addr:192.168.1.4  Bcast:192.168.1.255  Mask:255.255.255.0

可以用setup指令設定網絡配置:

LINUX網絡基礎
LINUX網絡基礎

檢視配置檔案:

[root@Freedom1991 /]# cd /etc/sysconfig/network-scripts

[root@Freedom1991 network-scripts]# ls

ifcfg-eth0  ifdown       ifdown-ib    ifdown-isdn  ifdown-routes  ifup          ifup-eth   ifup-ipv6  ifup-plusb  ifup-routes  ifup-wireless     network-functions

ifcfg-eth1  ifdown-bnep  ifdown-ippp  ifdown-post  ifdown-sit     ifup-aliases  ifup-ib    ifup-isdn  ifup-post   ifup-sit     init.ipv6-global  network-functions-ipv6

ifcfg-lo    ifdown-eth   ifdown-ipv6  ifdown-ppp   ifdown-tunnel  ifup-bnep     ifup-ippp  ifup-plip  ifup-ppp    ifup-tunnel  net.hotplug

[root@Freedom1991 network-scripts]# cat ifcfg-eth0  網卡1

DEVICE=eth0

TYPE=Ethernet

UUID=d1680cf6-689c-4126-9c78-635e28957da6

ONBOOT=yes

NM_CONTROLLED=yes

BOOTPROTO=none

HWADDR=00:0c:29:a0:9b:d5

DEFROUTE=yes

PEERROUTES=yes

IPV4_FAILURE_FATAL=yes

IPV6INIT=yes

NAME="System eth0"

IPADDR=192.168.1.3

GATEWAY=192.168.1.1

USERCTL=no

[root@Freedom1991 network-scripts]# cat ifcfg-eth1  網卡2

CE=eth1

HWADDR=00:0c:29:a0:9b:df

NAME="System eth1"

IPADDR=172.16.100.1

NETMASK=255.255.0.0

DEVICE=eth1

[root@Freedom1991 network-scripts]# 

netstat:用法詳解

-a :顯示所有協定和socket連接配接資訊

-n :對應的源IP和目的IP位址

-l:  顯示正在監聽的資訊

-t :TCP協定

-u :UDP協定

-p :伺服器名稱

[root@Freedom1991 network-scripts]# netstat -antulp

Active Internet connections (servers and established)

Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   

tcp        0      0 0.0.0.0:21                  0.0.0.0:*                   LISTEN      1878/vsftpd         

tcp        0      0 0.0.0.0:53                  0.0.0.0:*                   LISTEN      1833/dnsmasq        

tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1855/sshd           

tcp        0      0 127.0.0.1:631               0.0.0.0:*                   LISTEN      1632/cupsd          

tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      2276/master         

tcp        0      0 127.0.0.1:6010              0.0.0.0:*                   LISTEN      3687/sshd           

tcp        0      0 0.0.0.0:50831               0.0.0.0:*                   LISTEN      1593/rpc.statd      

tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      1469/rpcbind        

tcp        0     52 192.168.1.3:22              192.168.1.2:4682            ESTABLISHED 3687/sshd           

tcp        0      0 :::53                       :::*                        LISTEN      1833/dnsmasq        

tcp        0      0 :::22                       :::*                        LISTEN      1855/sshd           

tcp        0      0 ::1:631                     :::*                        LISTEN      1632/cupsd          

tcp        0      0 :::3128                     :::*                        LISTEN      2351/(squid)        

nslookup :  domain name | ip address

/etc/sysconfig/network :用于設定主機名和系統啟動時是否加載網卡

/etc/service :為網絡服務配置檔案,用于将網絡服務名轉換為端口号/協定;

繼續閱讀