原文:http://jianlee.ylinux.org/Computer/SystemAdmin/network.html
ifconfig命令
手動設置網卡示例
假設想把網卡設置爲如下:
網卡:eth0 IP:192.168.2.110 掩碼:255.255.255.0 網管:192.168.2.1
可以這樣:
ifconfig eth0 192.168.2.110 netmask 255.255.255.0 up route add default gw 192.168.2.1 dev eth0
修改MAC
有時候我們需要修改網卡的mac地址,比如我現在的網卡信息如下:
eth0 Link encap:Ethernet HWaddr 00:15:22:99:36:c9 inet addr:192.168.2.110 Bcast:192.168.2.255 Mask:255.255.255.0 inet6 addr: fe80::215:22ff:fe99:36c9/64 Scope:Link ...
上面顯示的HWaddr就是mac地址:00:15:22:99:36:c9。現在我們想把 它修改成我們需要的:
ifconfig eth0 down # 一定要先停止網卡 ifconfig eth0 hw ether 00:15:22:99:36:c9 # 修改mac地址
然後可以重啓網絡:
/etc/init.d/networking restart
MAC和網卡名綁定
如果我們有多個網卡,Linux下可能叫eth0,eth1,eth2等等,有時候我 們自己都分不清到底哪個網卡對於哪個名字。這時候可以將mac可網卡 名綁定。
Redhat系列linux:
使用sysinit作爲scripts管理一些系統服務,可以在類似的 /etc/sysconfig/network-scripts/ifcft-ethX 中寫上 HWADDR=00:01:02:8C:50:09 ,這樣以後這個ethX網卡就是mac值爲 00:01:02:8C:50:09 的網卡了。這裏MAC換成自己機器網卡的 mac,ethX中的X代表0,1,2,等等數字。
有nameif命令的linux:
基本都有這個命令,可以man一下,這個可以設置mac和網卡名字綁定。
橋接網絡
創建網絡設備
創建一個指定用戶有權限的設備 tap0
# tunctl -t tap0 -u 用戶名 # chmod 0666 /dev/net/tun
網卡混雜模式
# ifconfig eth0 promisc // 設置eth0爲混雜模式 # ifconfig tap0 promisc // 設置tap0爲混雜模式
建立橋接接口
# brctl addbr br0 # brctl addif br0 eth0 # brctl addif br0 tap0
設置br0的IP
靜態IP
# ifconfig br0 IP地址 netmask 掩碼 # route add default gw 默認網關 dev br0
DHCP
# dhclient br0
常見應用
1. 查看網絡接口使用什麼驅動
[root@jianlee ~]# ethtool -i eth0 driver: 3c59x version: firmware-version: bus-info: 0000:01:02.0
2. 查看網絡接口link
[root@jianlee ~]# mii-tool eth0: negotiated 100baseTx-FD, link ok
3. 網卡名字eth0,eth1修改
modprobe 中修改模塊別名
使用 udev 的情況
fedora10 就是使用 udev 設置網卡的名字,比如我的系統有兩塊網卡,e1000e已經 壞了,3c59x還是好的。每次都認3c59x爲eth1,即使我在 /etc/sysconfig/network-scripts/ifcfg-eth0 中設置了mac都不行。後來發現在 /etc/udev/rules.d/70-persistent-net.rules 下有這些內容 :
# 3Com Corporation 3c905B 100BaseTX [Cyclone] (rule written by anaconda) SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:01:02:8c:50:09", ATTR{type}=="1", KERNEL=="eth*", NAME="eth1" # Intel Corporation 82567LM-3 Gigabit Network Connection (rule written by anaconda) SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="88:88:88:88:87:88", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"
我把 eth0,和 eth1 名字交換。這樣就可以了。
使用 ip link
ip link set eth0 name eth2 ip link set eth1 name eth0 ip link set eth2 name eth1
禁止某個 IP 訪問
如果不需要用複雜的 iptables,可以簡單地在 route 里加上一條:
route add -host IP-A gw 127.0.0.1
路由
參考: http://blog.ligj.eol.cn/628
route 命令
使用route 命令添加的路由,機器重啓或者網卡重啓後路由就失效了,方法:
添加到主機的路由
# route add –host 192.168.168.110 dev eth0 # route add –host 192.168.168.119 gw 192.168.168.1
添加到網絡的路由
# route add –net IP netmask MASK eth0 # route add –net IP netmask MASK gw IP # route add –net IP/24 eth1
添加默認網關
# route add default gw IP
刪除路由
# route del –host 192.168.168.110 dev eth0
設置永久路由
/etc/rc.local
route add -net 192.168.3.0/24 dev eth0 route add -net 192.168.2.0/24 gw 192.168.3.254
在/etc/sysconfig/network裏添加到末尾
方法:GATEWAY=gw-ip 或者 GATEWAY=gw-dev
/etc/sysconfig/static-router
any net x.x.x.x/24 gw y.y.y.y