linux系統配置ip地址,圖形化界面略過,這里只介紹文本行。做以下設置注意是否有此權限
查看當前路由及網關信息:
[root@localhost ~]# netstat -r
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
192.168.1.0 * 255.255.255.0 U 0 0 0 eth0
169.254.0.0 * 255.255.0.0 U 0 0 0 eth0
default 192.168.1.1 0.0.0.0 UG 0 0 0 eth0
查看物理網卡信息:
[root@iyunshusdk ~]# mii-tool [eth0]
eth0: negotiated 100baseTx-FD, link ok
ethtool eth0 查看網卡物理信息
ethtool -i eth0 查看網卡驅動信息
ethtool -S eth0 查看網卡工作信息
ifconfig命令:
查看網卡接口信息,默認列出所有接口
[root@localhost ~]# ifconfig
如果想知道主機所有網絡接口的情況,請用下面的命令:
[root@localhost ~]# ifconfig -a
ifconfig 網絡端口 IP地址 hw <HW> MAC地址 netmask 掩碼地址 broadcast 廣播地址 [up/down] [root@localhost ~]# ifconfig eth0 10.1.1.10 netmask 255.0.0.0
臨時配置網關 (重啟即消失)
[root@localhost ~]# route add default gw 192.168.0.254
網絡接口配置文件 (黃色背景最好都設置):
[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
# Intel Corporation 82545EM Gigabit Ethernet Controller (Copper)
TYPE=Ethernet #網卡類型
DEVICE=eth0 #網卡接口名稱
ONBOOT=yes #系統啟動時是否自動加載
BOOTPROTO=static #啟用地址協議 --static:靜態協議 --bootp協議 --dhcp協議
IPADDR=192.168.1.11 #網卡IP地址
NETMASK=255.255.255.0 #網卡網絡地址
GATEWAY=192.168.1.1 #網卡網關地址
DNS1=10.203.104.41 #網卡DNS地址
HWADDR=00:0C:29:13:5D:74 #網卡設備MAC地址
BROADCAST=192.168.1.255 #網卡廣播地址
添加子接口(虛擬接口):
需要禁用NetworkManager 服務
service NetworkManager stop #停止服務
chkconfig NetworkManager off #禁用服務,下次不自動啟動
臨時: ifconfig eth0:1 10.0.0.2 netmask 255.255.255.0
或者 ip addr add 10.0.0.2/24 dev eth0 label eth0:0
永久:在/etc/syscofig/network-scripts/ 下新建文件
ifcfg-eth0:1
增加內容
DEVICE=eth0:1 #網卡接口名稱
ONBOOT=yes #系統啟動時是否自動加載
BOOTPROTO=static #啟用地址協議 --static:靜態協議 --bootp協議 --dhcp協議
IPADDR=192.168.1.11 #網卡IP地址 NETMASK=255.255.255.0 (PREFIX=24) #網卡網絡地址
GATEWAY=192.168.1.1 #網卡網關地址 ONPARENT=YES 存在依附關系
注意:真正的接口標識,不是ifcfg-xxx的文件名,而是ifcfg-eth0中的DEVICE項目
network文件就是把/etc/sysconfig/network-script目錄下的所有文件都遍歷
凡是ONBOOT=yes的都enable ,並且enable 到DEVICE=eth0這個名字上
網絡服務啟動與關閉:
方法一:
[root@localhost ~]# service network stop #關閉網絡服務
[root@localhost ~]# service network start #啟動網絡服務
[root@localhost ~]# service network restart #重啟網絡服務
方法二:
[root@localhost ~]# /etc/init.d/network stop
[root@localhost ~]# /etc/init.d/network start
[root@localhost ~]# /etc/init.d/network restart
網卡狀態查詢
[root@localhost ~]# service network status
Configured devices:
lo eth0
Currently active devices:
lo eth0
[root@localhost ~]# /etc/init.d/network status
多網卡綁定:
linux有七種網卡綁定模式:
0. round robin 平衡輪詢
1.active-backup 主備模式
2.load balancing (xor), 廣播
3.fault-tolerance (broadcast), 4.lacp, 5.transmit load balancing, 6.adaptive load balancing。
mode=1(active-backup):
一個網卡處於活動狀態 ,一個處於備份狀態,所有流量都在主鏈路上處理。當活動網卡down掉時,啟用備份的網卡。
首先在/etc/sysconfig/network-scripts/下創建虛擬網卡bond0的配置文件ifcfg-bond0,內容如下
DEVICE=bond0
BOOTPROTO=none
IPADDR=192.168.168.1
NETMASK=255.255.255.0
ONBOOT=yes
TYPE=Ethernet
GATEWAY=192.168.168.250
USERCTL=no
然后分別修改eth0和eth1的配置文件
ifcfg-eth0內容:
DEVICE=eth0
BOOTPROTO=none
ONBOOT=yes
USERCTL=no
MASTER=bond0
SLAVE=yes
ifcfg-eth1內容
DEVICE=eth1
BOOTPROTO=none
ONBOOT=yes
USERCTL=no
MASTER=bond0
SLAVE=yes
因為linux的虛擬網卡是在內核模塊中實現的,所以需要安裝的時候已經裝好該module。
選項一:
在/etc/modules.conf文件中添加如下內容(如果沒有該文件,則新建一個):
alias bond0 bonding
options bond0 miimon=100 mode=1 primary=eth0
其中miimon=100表示每100ms檢查一次鏈路連接狀態,如果不通則會切換物理網卡
mode=1表示主備模式,也就是只有一塊網卡是active的,只提供失效保護。如果mode=0則是負載均衡模式的,所有的網卡都是active,還有其他一些模式很少用到
primary=eth0表示主備模式下eth0為默認的active網卡
miimon是毫秒數,每100毫秒觸發檢測線路穩定性的事件。
mode 是ifenslave的工作狀態。
選項二:
創建配置文件
cat /etc/modprobe.d/bonding.conf
寫入內容
alias bond0 bonding (告訴操作系統 bond0 使用的驅動是bonding)
重啟服務,完成
查看綁定在哪張網卡上
# cat /proc/net/bonding/bond0
Ethernet Channel Bonding Driver: v3.0.3 (March 23, 2006)
Bonding Mode: fault-tolerance (active-backup)
Primary Slave: None
Currently Active Slave: eth0
MII Status: up
MII Polling Interval (ms): 500
Up Delay (ms): 0
Down Delay (ms): 0
Slave Interface: eth0
MII Status: up
Link Failure Count: 0
Permanent HW addr: 00:0c:29:01:4f:77
Slave Interface: eth1
MII Status: up
Link Failure Count: 0
Permanent HW addr: 00:0c:29:01:4f:8b
