第十四單元 Linux網絡原理及基礎設置
三種網卡模式圖
使用ifconfig命令來維護網絡
ifconfig命令的功能
ifconfig命令的用法舉例
使用ifup和ifdown命令啟動和停止網卡
ifup命令的功能
ifdown命令的功能
ifup命令的用法舉例
ifdown命令的用法舉例
網絡配置文件
網卡對應的網絡配置文件
什么是網絡配置
ip命令
ping命令
setup命令
課后作業
【本節內容】
1. 使用ifconfig命令來維護網絡(詳見linux系統管理P422)
1) 掌握ifconfig命令的功能:顯示所有正在啟動的網卡的詳細信息或設定系統中網卡的IP地址。
2) 靈活應用ifconfig命令設定網卡的IP地址:
例如:修改eth0網卡的IP地址為172.18.48.56和子網掩碼255.255.255.128的命令:
ifconfig eth0 172.18.48.56 netmask 255.255.255.128
2. 使用ifup和ifdown命令啟動和停止網卡(詳見linux系統管理P424)
1) 掌握ifup命令的功能:啟動系統上指定的網卡。
2) 掌握ifdown命令的功能:停止系統上指定的網卡。
3) 掌握使用ifup命令啟用指定網卡的舉例:
例如:啟動eth0網卡的命令:ifup eth0
4) 掌握使用ifdown命令停用指定網卡的舉例:
只能作用於配置文件
例如:停用eth0網卡的命令:ifdown eth0
3. 網絡配置文件 (詳見linux系統管理P425)
1) 了解網卡的配置文件存放在/etc/sysconfig/network-scripts目錄中,且每個網卡對應的配置文件的文件名以ifcfg-開始,如eth0這個網卡對應的網絡配置文件就是ifcfg-eth0。
補:虛擬機新添加網卡沒有配置文件需手動添加配置或圖形添加網卡
λ 掌握網絡配置的定義:所謂的網絡配置(設定)就是要修改網卡所對應的網絡配置文件,可以通過使用文字編輯器(vi)直接編輯網絡配置文件來重新配置網絡。
臨時配置網絡ip地址
1 [root@template ~]# ifconfig eth0 192.168.1.103/24 2 3 [root@template ~]# ifconfig eth0 4 eth0 Link encap:Ethernet HWaddr 00:0C:29:45:CA:65 5 inet addr:192.168.1.102 Bcast:192.168.1.255 Mask:255.255.255.0 6 inet6 addr: fe80::20c:29ff:fe45:ca65/64 Scope:Link 7 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 8 RX packets:1902597 errors:0 dropped:0 overruns:0 frame:0 9 TX packets:4990 errors:0 dropped:0 overruns:0 carrier:0 10 collisions:0 txqueuelen:1000 11 RX bytes:2416682435 (2.2 GiB) TX bytes:482159 (470.8 KiB)
重啟網卡的幾種常見方式
1 法一: 2 推薦這種方式 3 [root@template ~]# ifdown && ifup eth0 4 5 6 法二: 7 盡量不要用這條命令,這條命令會影響所有網卡 8 9 [root@template ~]# service network restart 10 正在關閉接口 eth1: 11 12 法三: 13 盡量不要用這條命令,這條命令會影響所有網卡 14 [root@template ~]#/etc/init.d/network restart
關閉守護進程方法:
1 關閉守護進程 2 [root@template ~]# service NetworkManager stop 3 停止 NetworkManager 守護進程:[確定] 4 5 關閉開機自啟動守護進程服務 6 [root@template ~]# chkconfig --level 35 NetworkManager off 7 [root@template ~]# chkconfig --list|grep 'Network' 8 NetworkManager 0:關閉 1:關閉 2:啟用 3:關閉 4:啟用 5:關閉 6:關閉 #注意安裝操作系統init 設置的啟動模式 (1---6)
關閉某塊網卡的方法
1 關閉eth1 2 3 [root@template ~]# ifconfig eth1 down 4 5 6 開啟eth1 7 [root@template ~]# ifconfig eth1 up 8 9 10 查看ipadd 11 [root@template ~]# ifconfig 12 eth1 Link encap:Ethernet HWaddr 00:0C:29:45:CA:65 13 inet addr:192.168.11.31 Bcast:192.168.11.255 Mask:255.255.255.0 14 inet6 addr: fe80::20c:29ff:fe45:ca65/64 Scope:Link 15 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 16 RX packets:1899415 errors:0 dropped:0 overruns:0 frame:0 17 TX packets:3251 errors:0 dropped:0 overruns:0 carrier:0 18 collisions:0 txqueuelen:1000 19 RX bytes:2414954677 (2.2 GiB) TX bytes:331129 (323.3 KiB)
配置DNS
1 [root@template ~]# cat /etc/resolv.conf 2 # Generated by NetworkManager 3 search com 4 nameserver 192.168.11.254 5 nameserver 202.106.0.20 6 nameserver 8.8.8.8
檢測本地arp網卡沖突地址命令
1 [root@template ~]# arping -I eth1 192.168.11.11
查看網關命令
1 [root@template ~]# route -n 2 Kernel IP routing table 3 Destination Gateway Genmask Flags Metric Ref Use Iface 4 192.168.11.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
#臨時添加網關,臨時生效
1 route add default gw 192.168.11.254
4. 課后作業(用時35分鍾)
1) 查看當前所有正在啟動的網卡的詳細信息
ifconfig eth0
2) 激活系統的第一塊網卡
ifup eth0
3) 為系統臨時設置ip為192.168.1.10/24
ifconfig eth0 192.168.1.10/24
or
ifconfig eth0 192.168.1.10/24 netmask 255.255.255.0
4) 查看第一塊網卡的詳細信息
ipconfig
5) 修改網卡配置文件將系統的第一塊網卡的ip地址設定為192.168.3.2,子網掩碼設置為255.255.255.0,是否啟動設置為yes
[root@template ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
BOOTPROTO=static
TYPE=Ethernet
HWADDR=00:0c:29:45:ca:65
IPADDR=192.168.3.2
NETMASK=255.255.255.0
GATEWAY=192.168.3.1
DNS1=8.8.8.8
IPV6INIT=no
ONBOOT=no
USERCTL=no
6) 使用setup命令為系統第一塊網卡設置ip為10.0.0.1/8