[root@deyu ~]# ifconfig {interface} {up|down} <== 觀察與啟動介面 [root@deyu ~]# ifconfig interface {options} <== 設定與修改介面 參數: interface:網路卡介面代號,包括 eth0, eth1, ppp0 等等 options :可以接的參數,包括如下: up, down :啟動 (up) 或關閉 (down) 該網路介面(不涉及任何參數) mtu :可以設定不同的 MTU 數值,例如 mtu 1500 (單位為 byte) netmask :子遮罩網路; broadcast:廣播位址。 範例: 範例一:觀察所有的網路介面(直接輸入 ifconfig) [root@deyu ~]# ifconfig eth0 Link encap:Ethernet HWaddr 00:0D:60:AA:A3:81 UP BROADCAST MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 (0.0 b) TX bytes:0 (0.0 b) eth1 Link encap:Ethernet HWaddr 00:50:FC:80:2B:32 inet addr:192.168.122.19 Bcast:192.168.122.255 Mask:255.255.255.0 inet6 addr: 2002:a311:c13:5:250:fcff:fe80:2b32/64 Scope:Global inet6 addr: 2002:a311:cbc:5:250:fcff:fe80:2b32/64 Scope:Global inet6 addr: fec0::5:250:fcff:fe80:2b32/64 Scope:Site inet6 addr: fe80::250:fcff:fe80:2b32/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:69589379 errors:1 dropped:11 overruns:1 frame:0 TX packets:10011453 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:3012738247 (2.8 GiB) TX bytes:2820412995 (2.6 GiB) Interrupt:22 Base address:0xc000 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:16436 Metric:1 RX packets:10178 errors:0 dropped:0 overruns:0 frame:0 TX packets:10178 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:12722824 (12.1 MiB) TX bytes:12722824 (12.1 MiB) # 列出目前已經被啟動的卡,不論這個卡是否有給予 IP,都會被顯示出來。 # 輸入 ifconfig eth0,則會秀出這張介面的相關資料,不管該介面是否有啟動。 * eth0:網路卡的代號; * HWaddr:網路卡的硬體位址,俗稱 MAC; * inet addr:IPv4 的 IP 位址,後續的 Bcast, Mask 分別代表的是 Broadcast 與 netmask * inet6 addr:IPv6 版本的 IP,沒有使用,所以略過; * MTU: * RX:網路由啟動到目前為止的封包接收情況, packets 代表封包數、errors 代表封包發生錯誤的數量、dropped 代表封包有問題而遭丟棄的數量 * TX:與 RX 相反,為網路由啟動到目前為止的傳送情況; * collisions:代表封包碰撞的情況,如果發生太多次,表示網路狀況不太好; * txqueuelen:代表用來傳輸資料的緩衝區的儲存長度; * RX bytes, TX bytes:總傳送、接收的位元組總量 * Interrupt, Memory:網路卡硬體的資料, IRQ 岔斷與記憶體位址; 範例二:暫時修改網路介面 [root@deyu ~]# ifconfig eth0 192.168.1.11 [root@deyu ~]# ifconfig eth0 eth0 Link encap:Ethernet HWaddr 00:0D:60:AA:A3:81 inet addr:192.168.1.11 Bcast:192.168.1.255 Mask:255.255.255.0 UP BROADCAST MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 (0.0 b) TX bytes:0 (0.0 b) [root@linux ~]# ifconfig eth0:0 192.168.50.50 # eth0:0 是在該網路介面上,再模擬一個網路介面,亦即在一張網路卡上面設定多個 IP! [root@deyu ~]# ifconfig eth0:0 192.168.1.13 [root@deyu ~]# ifconfig eth0 Link encap:Ethernet HWaddr 00:0D:60:AA:A3:81 inet addr:192.168.1.11 Bcast:192.168.1.255 Mask:255.255.255.0 UP BROADCAST MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 (0.0 b) TX bytes:0 (0.0 b) eth0:0 Link encap:Ethernet HWaddr 00:0D:60:AA:A3:81 inet addr:192.168.1.13 Bcast:192.168.1.255 Mask:255.255.255.0 UP BROADCAST MULTICAST MTU:1500 Metric:1 ....以下省略.... # 重新啟動網路 ifconfig 設定的資料全部失效,會以 ifcfg-ethx 的設定為主! [root@deyu ~]# /etc/init.d/network restart Shutting down interface eth0: [ OK ] Shutting down interface eth1: [ OK ] Shutting down loopback interface: [ OK ] Bringing up loopback interface: [ OK ] Bringing up interface eth0: [ OK ] Bringing up interface eth1: [ OK ] [root@deyu ~]# ifconfig eth0 Link encap:Ethernet HWaddr 00:0D:60:AA:A3:81 UP BROADCAST MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 (0.0 b) TX bytes:0 (0.0 b) eth1 Link encap:Ethernet HWaddr 00:50:FC:80:2B:32 inet addr:192.168.122.19 Bcast:192.168.122.255 Mask:255.255.255.0 inet6 addr: 2002:a311:c13:5:250:fcff:fe80:2b32/64 Scope:Global inet6 addr: fec0::5:250:fcff:fe80:2b32/64 Scope:Site inet6 addr: 2002:a311:cbb:5:250:fcff:fe80:2b32/64 Scope:Global inet6 addr: fe80::250:fcff:fe80:2b32/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:69593888 errors:1 dropped:11 overruns:1 frame:0 TX packets:10011643 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:3013177959 (2.8 GiB) TX bytes:2820447763 (2.6 GiB) Interrupt:22 Base address:0xc000 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:16436 Metric:1 RX packets:10178 errors:0 dropped:0 overruns:0 frame:0 TX packets:10178 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:12722824 (12.1 MiB) TX bytes:12722824 (12.1 MiB)
#1. 暫時啟動,重新開機後復原 [root@linux ~]# echo "1" > /proc/sys/net/ipv4/ip_forward #2. 開機啟動封包轉遞: [root@linux ~]# vi /etc/sysctl.conf # 將底下這個設定值修改正確即可! net.ipv4.ip_forward = 1 [root@linux ~]# sysctl -p <==立刻讓該設定生效 # sysctl 是在核心工作時用來直接修改核心參數的一個指令
[root@deyu ~]# route [-nee] [root@deyu ~]# route add [-net|-host] [網域或主機] netmask [mask] [gw|dev] [root@deyu ~]# route del [-net|-host] [網域或主機] netmask [mask] [gw|dev] 觀察的參數: -n :不要使用通訊協定或主機名稱,直接使用 IP 或 port number; -ee :使用更詳細的資訊來顯示 增加 (add) 與刪除 (del) 路由的相關參數: -net :表示後面接的路由為一個網域; -host :表示後面接的為連接到單部主機的路由; netmask :與網域有關,可以設定 netmask 決定網域的大小; gw :gateway 的簡寫,後續接的是 IP 的數值喔,與 dev 不同; dev :如果只是要指定由那一塊網路卡連線出去,則使用這個設定,後面接 eth0 等 範例:觀察路由狀態 [root@deyu ~]# route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.122.0 * 255.255.255.0 U 0 0 0 eth1 169.254.0.0 * 255.255.0.0 U 0 0 0 eth1 default 192.168.122.1 0.0.0.0 UG 0 0 0 eth1 [root@deyu ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1 169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth1 0.0.0.0 192.168.122.1 0.0.0.0 UG 0 0 0 eth1 # Destination, Genmask:分別是 network 與 netmask,組合成一個完整的網域。 # Gateway:該網域是通過那個 gateway 連接出去? # Gateway 0.0.0.0 表示該路由是直接由本機傳送,亦即可以透過區域網路的 MAC 直接傳訊; # Gateway 有顯示 IP,表示該路由需要經過路由器 (通訊閘) 才能夠傳送出去。 # Flags:總共有多個旗標,代表的意義如下: o U (route is up):該路由是啟動的; o H (target is a host):目標是一部主機 (IP) 而非網域; o G (use gateway):需要透過外部的主機 (gateway) 來轉遞封包; o R (reinstate route for dynamic routing):使用動態路由時,恢復路由資訊的旗標; o D (dynamically installed by daemon or redirect):已經由服務或轉 port 功能設定為動態路由 o M (modified from routing daemon or redirect):路由已經被修改了; o ! (reject route):這個路由將不會被接受(用來抵擋不安全的網域!) # Iface:這個路由傳遞封包的介面。 # 查看最後 default gateway 192.168.122.1 與設定是否一致?
[root@deyu ~]# ping [-bcstnM] IP 參數: -b :後面接的是 broadcast 的 IP,用在你『需要對整個網域的主機進行 ping 』時; -c :後面接的是執行 ping 的次數,例如 -c 5 ; -n :不進行 IP 與主機名稱的反查,直接使用 IP ; -s :發送出去的 ICMP 封包大小,預設為 56(bytes),再加 8 bytes 的 ICMP 表頭資料 -t :TTL 的數值,預設是 255,每經過一個節點就會少一; -M [do|dont] :主要在偵測網路的 MTU 數值大小,兩個常見的項目是: do :代表傳送一個 DF (Don't Fragment) 旗標,讓封包不能重新拆包與打包; dont:代表不要傳送 DF 旗標,表示封包可以在其他主機上拆包與打包 範例一:偵測 192.168.122.1 DNS 主機是否存在? [root@deyu ~]# ping -c 3 192.168.122.1 PING 192.168.122.1 (192.168.122.1) 56(84) bytes of data. 64 bytes from 192.168.122.1: icmp_seq=1 ttl=63 time=0.315 ms 64 bytes from 192.168.122.1: icmp_seq=2 ttl=63 time=0.292 ms 64 bytes from 192.168.122.1: icmp_seq=3 ttl=63 time=0.317 ms --- 192.168.122.1 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2005ms rtt min/avg/max/mdev = 0.292/0.308/0.317/0.011 ms # 64 bytes:表示這次傳送的 ICMP 封包大小為 64 bytes; # icmp_seq=1:ICMP 所偵測進行的次數; # ttl=63:每經過一個帶有 MAC 的節點 (node),例如 router, bridge 時, TTL 就會減 1; # time=0.315 ms:回應時間,單位有 ms(0.001秒)及 us(0.000001秒)。 範例二:偵測 192.168.122.1 GATEWAY 主機是否存在? [root@deyu ~]# ping -c 3 192.168.122.1 PING 192.168.122.1 (192.168.122.1) 56(84) bytes of data. 64 bytes from 192.168.122.1: icmp_seq=1 ttl=64 time=0.166 ms 64 bytes from 192.168.122.1: icmp_seq=2 ttl=64 time=0.155 ms 64 bytes from 192.168.122.1: icmp_seq=3 ttl=64 time=0.139 ms --- 192.168.122.1 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 1998ms rtt min/avg/max/mdev = 0.139/0.153/0.166/0.015 ms
[root@deyu ~]# cat /etc/resolv.conf nameserver 192.168.122.1 nameserver 168.95.1.1 nameserver 139.175.10.20
[root@deyu ~]# host [-a] hostname [server] 參數: -a :列出該主機詳細的各項主機名稱設定資料 [server] :可以使用非為 /etc/resolv.conf 的 DNS 主機來查詢。 範例一:列出 csie.cyut.edu.tw 的 IP [root@deyu ~]# host csie.cyut.edu.tw csie.cyut.edu.tw has address 163.17.10.1 範例二:列出 www.google.tw 的 IP [root@deyu ~]# host www.google.tw www.google.tw is an alias for www.google.com. www.google.com is an alias for www.l.google.com. www.l.google.com has address 64.233.183.147 www.l.google.com has address 64.233.183.99 www.l.google.com has address 64.233.183.103 www.l.google.com has address 64.233.183.104 www.l.google.com has address 64.233.183.105 www.l.google.com has address 64.233.183.106 www.google.tw is an alias for www.google.com. www.google.com is an alias for www.l.google.com. www.google.tw is an alias for www.google.com. www.google.com is an alias for www.l.google.com.
[root@deyu ~]# dig [@server] [FQDN] [type] 參數說明: @server :如果不想以 /etc/resolv.conf 來作為 DNS 主機,則可在此填入其他的 IP type :預設是查詢 A 標誌,你可以在這裡入其他的標誌,如 mx, ns 等。 此功能亦可使用 [-t type] 來處理。 範例一:查詢 csie.cyut.edu.tw [root@deyu ~]# cat /etc/resolv.conf nameserver 168.95.192.1 nameserver 168.95.1.1 [root@deyu ~]# dig csie.cyut.edu.tw ; <<>> DiG 9.3.2 <<>> csie.cyut.edu.tw ;; global options: printcmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 19533 ;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 0 ;; QUESTION SECTION: ;csie.cyut.edu.tw. IN A ;; ANSWER SECTION: csie.cyut.edu.tw. 23100 IN A 163.17.10.1 ;; AUTHORITY SECTION: csie.cyut.edu.tw. 60365 IN NS csie.cyut.edu.tw. ;; Query time: 991 msec ;; SERVER: 168.95.192.1#53(168.95.192.1) ;; WHEN: Wed Jun 30 09:03:21 2010 ;; MSG SIZE rcvd: 64 # HEADER(標題):顯示查詢的內容有哪些,包括一個 query, 一個 answer 及一個驗證部分。 # QUESTION(問題):顯示所要查詢的內容。 # ANSWER(回應):依據剛剛的 QUESTION 去查詢所得到的結果。 # AUTHORITY(驗證):查閱 cyut.edu.tw 領域是由 csie.cyut.edu.tw 設定。 # SERVER 查詢的 DNS 為 168.95.192.1,與 /etc/resolv.conf 設定 nameserver 相同。 範例二:使用 163.17.10.1 DNS 主機查詢 csie.cyut.edu.tw [root@deyu ~]# dig @163.17.10.1 csie.cyut.edu.tw ; <<>> DiG 9.3.2 <<>> @163.17.10.1 csie.cyut.edu.tw ; (1 server found) ;; global options: printcmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 19765 ;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 0 ;; QUESTION SECTION: ;csie.cyut.edu.tw. IN A ;; ANSWER SECTION: csie.cyut.edu.tw. 86400 IN A 163.17.10.1 ;; AUTHORITY SECTION: csie.cyut.edu.tw. 86400 IN NS csie.cyut.edu.tw. ;; Query time: 1030 msec ;; SERVER: 163.17.10.1#53(163.17.10.1) ;; WHEN: Wed Jun 30 09:07:59 2010 ;; MSG SIZE rcvd: 64 # SERVER 查詢的 DNS 為 168.95.192.1。
2015-04-13