Linux配置IP地址
Linux下一切皆文件,要想配置的参数永久有效基本上要修改配置文件。以下主要讲怎么使用ifconfig,route,ip等命令配置IP地址等相关参数并配置相关配置文件使其永久有效。主要使用CentOS 6系列
重启网络服务
从CentOS 6系列开始系统默认使用 /etc/init.d/NetworkManager脚本管理网络服务。但也可使用/etc/init.d/network来管理。NetworkManager不支持网络桥接等高级功能。以下使用实例
NetworkManager脚本
/etc/init.d/NetworkManager {start|stop|status|restart}
或者
service NetworkManager{start|stop|status|restar}
[[email protected] ~]# serviceNetworkManager start
Setting network parameters... [ OK ]
Starting NetworkManager daemon:
[[email protected] ~]# serviceNetworkManager stop
Stopping NetworkManager daemon: [ OK ]
[[email protected] ~]# serviceNetworkManager restart
Stopping NetworkManager daemon: [ OK ]
Setting network parameters... [ OK ]
Starting NetworkManager daemon: [ OK ]
network脚本
/etc/init.d/network {start|stop|status|restart|reload}
或者
Service network {start|stop|status|restart|reload}
[[email protected] ~]# servicenetwork stop
Shutting down loopback interface: [ OK ]
[[email protected] ~]# servicenetwork start
Bringing up loopback interface: [ OK ]
Bringing up interface eth0: [ OK ]
[[email protected] ~]# servicenetwork restart
Shutting down interface eth0: [ OK ]
Shutting down loopback interface: [ OK ]
Bringing up loopback interface: [ OK ]
Bringing up interface eth0: [ OK ]
[[email protected] ~]# service network status
Configureddevices:
lo eth0
Currently activedevices:
lo eth0 eth1
[[email protected] ~]# service network reload
Shutting downinterface eth0: [ OK ]
Shutting downloopback interface: [ OK ]
Bringing uploopback interface: [ OK ]
Bringing upinterface eth0: [ OK ]
ifconfig
ifconfig是最常用的查看及配置IP的命令,使用十分简单。但只是当时有效重启网络服务或者重启计算机即失效
ifconfig 查看启用了的网卡
eth0 Link encap:Ethernet HWaddr 00:0C:29:4F:66:B2
inet addr:192.168.43.2 Bcast:192.168.43.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe4f:66b2/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:144 errors:0 dropped:0 overruns:0 frame:0
TX packets:84 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:14976 (14.6 KiB) TXbytes:8234 (8.0 KiB)
lo Link encap:Local Loopback
inetaddr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:24 errors:0 dropped:0 overruns:0 frame:0
TX packets:24 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:1560 (1.5 KiB) TXbytes:1560 (1.5 KiB)
ifconfig - a 查看系统所有网卡
eth0 Link encap:Ethernet HWaddr 00:0C:29:4F:66:B2
inet addr:192.168.43.2 Bcast:192.168.43.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe4f:66b2/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:866 errors:0 dropped:0 overruns:0 frame:0
TX packets:777 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:75777 (74.0 KiB) TXbytes:66732 (65.1 KiB)
eth1 Link encap:Ethernet HWaddr 00:0C:29:4F:66:BC
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.0b)
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:24 errors:0 dropped:0 overruns:0 frame:0
TX packets:24 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:1560 (1.5 KiB) TXbytes:1560 (1.5 KiB)
ifconfig eth*查看单个网卡
[[email protected] ~]# ifconfigeth0
eth0 Link encap:Ethernet HWaddr 00:0C:29:4F:66:B2
inet addr:192.168.43.2 Bcast:192.168.43.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe4f:66b2/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1255 errors:0 dropped:0 overruns:0 frame:0
TX packets:1154 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:108690 (106.1 KiB) TXbytes:99586 (97.2 KiB)
ifconfig配置临时使用的IP地址
ifconfig是红帽系列系统上的老牌管理命令道7系列这个命令将被废弃改用ip命令配置IP地址这个命令配置管理IP地址简单方便。
ifconfig eth* IP/MASK位数
[[email protected] ~]# ifconfigeth1 192.168.1.1/24
[[email protected] ~]# ifconfigeth1
eth1 Link encap:Ethernet HWaddr 00:0C:29:4F:66:BC
inet addr:192.168.1.1 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe4f:66bc/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 b) TX bytes:468(468.0 b)
ifconfig eth* [up|down] 启用或者禁用网卡
[[email protected] ~]# ifconfig lodown
[[email protected] ~]# ifconfigeth1 down
[[email protected] ~]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:4F:66:B2
inet addr:192.168.43.2 Bcast:192.168.43.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe4f:66b2/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1811 errors:0 dropped:0 overruns:0 frame:0
TX packets:1485 errors:0dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:157057 (153.3 KiB) TXbytes:137398 (134.1 KiB)
route命令配置路由和gateway。
route命令可以查看并配置路由和gateway,
route 查看计算机上的路由条目
[[email protected] ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.43.0 * 255.255.255.0 U 0 0 0 eth0
link-local * 255.255.0.0 U 1002 0 0 eth0
default zhaopeng 0.0.0.0 UG 0 0 0 eth0
U代表UP
G代表gateway网关路由
[[email protected] ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.43.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
0.0.0.0 192.168.43.254 0.0.0.0 UG 0 0 0 eth0
route 添加路由条目
route add –net 目标网络/MASK位数 gw 本地网关地址
[[email protected] ~]# routeadd -net 10.0.0.0/8 gw 192.168.43.1
[[email protected] ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.43.0 * 255.255.255.0 U 0 0 0 eth0
10.0.0.0 192.168.43.1 255.0.0.0 UG 0 0 0 eth0
route 删除路由
route del –net Destination网络/MASK位数
[[email protected] ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.43.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
10.0.0.0 192.168.43.1 255.0.0.0 UG 0 0 0 eth0
[[email protected] ~]# routedel -net 10.0.0.0/8
[[email protected] ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.43.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
添加及删除默认路由
添加默认路由和删除默认路由都有两种方法。
route add default gw 网关地址 或者 routeadd –net 0.0.0.0 gw 网关地址
route deldefault route del –net 0.0.0.0
[[email protected] ~]# routeadd default gw 192.168.43.1
[[email protected] ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.43.0 * 255.255.255.0 U 0 0 0eth0
default 192.168.43.1 0.0.0.0 UG 0 0 0 eth0
[[email protected] ~]# routedel -net 0.0.0.0
[[email protected] ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.43.0 * 255.255.255.0 U 0 0 0 eth0
配置永久的IP和路由
ifconfig route配置的都是临时的只有写在配置文件的参数才会永久有效不会立即生效重启主机或服务有效
IP
ip地址的配置文件vim /etc/sysconfig/network-scripts/ifcfg-eth*
DEVICE=eth0 设备名称一定要与eth*的名字匹配
HWADDR=00:0C:29:57:5C:BB 网卡的MAC地址
TYPE=Ethernet 类型
ONBOOT=yes 开机是否激活此接口
NM_CONTROLLED=yes
BOOTPROTO=static 引导协议 3个选项{static|none|dhcp}static和none表示使用静态地址
IPADDR=192.168.1.112 IP地址
NETAMSK=255.255.255.0 子网掩码
GETWAY=192.168.1.1 网关地址
DNS1=61.128.128.68 DNS服务器地址
route路由
路由的配置文件vim /etc/sysconfig/network-scripts/route-eth*
Destination/mask位数 via nexthop
192.168.1.0/24 via 192.168.1.1
添加默认路由
default via 192.168.1.1
0.0.0.0/0 via 192.168.1.1
DNS指向
[[email protected] ~]# cat /etc/resolv.conf DNS配置文件但是建议直接写在/etc/sysconfig/network-scripts/ifcfg-eth*文件中
# Generated by NetworkManager
# No nameservers found; try putting DNSservers into your
# ifcfg files in/etc/sysconfig/network-scripts like so:
# DNS1=xxx.xxx.xxx.xxx
# DNS2=xxx.xxx.xxx.xxx
# DOMAIN=lab.foo.com bar.foo.com
nameserver 61.128.128.68 :命名规则
setup配置ip dns等信息
直接输入setup命令
IP命令的使用
重启计算机就将失效,重启网络服务不会失效。
ip link
ip link show 查看网络接口信息
- ip link show [dev]
[[email protected] ~]# ip linkshow
1: lo: <LOOPBACK,UP,LOWER_UP> mtu16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen1000
link/ether 00:0c:29:57:5c:bb brd ff:ff:ff:ff:ff:ff
[[email protected] ~]# ip linkshow eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP>mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:57:5c:bb brd ff:ff:ff:ff:ff:ff
- ip –s link show [dev] 查看接口收发了多少包
[[email protected] ~]# ip -s link show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500qdisc pfifo_fast state UP qlen 1000
link/ether00:0c:29:57:5c:bb brd ff:ff:ff:ff:ff:ff
RX: bytes packets errors dropped overrun mcast
185812 2152 0 0 0 0
TX: bytes packets errors dropped carrier collsns
128240 1187 0 0 0 0
- ip link set [dev] [up|down]
[[email protected] ~]# ip link set eth1 down
[[email protected] ~]# ip link show
1: lo:<LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0:<BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen1000
link/ether 00:0c:29:4f:66:b2 brdff:ff:ff:ff:ff:ff
3: eth1: <BROADCAST,MULTICAST> mtu 1500 qdiscpfifo_fast state DOWN qlen 1000
link/ether 00:0c:29:4f:66:bcbrd ff:ff:ff:ff:ff:ff
ip addr
- ip aadr show [dev] 查看ip地址
- ip addr add IP/mask位数 dev eth* 添加ip地址
[[email protected] ~]# ip addr add10.1.1.1/8 dev eth1
[[email protected] ~]# ip addrshow eth1
3: eth1: <BROADCAST,MULTICAST> mtu1500 qdisc pfifo_fast state DOWN qlen 1000
link/ether 00:0c:29:4f:66:bc brd ff:ff:ff:ff:ff:ff
inet 10.1.1.1/8 scope global eth1
- ip addr del address dev eth*
[[email protected] ~]# ip addr del 10.1.1.1/8 deveth1
- ip addr flush [dev] to ip/mask位数
[[email protected] ~]# ip addr show eth1
3: eth1: <BROADCAST,MULTICAST> mtu1500 qdisc pfifo_fast state DOWN qlen 1000
link/ether 00:0c:29:4f:66:bc brd ff:ff:ff:ff:ff:ff
inet 192.168.100.1/24 scope global eth1
inet 10.2.2.2/8 scope global eth1
[[email protected] ~]# ip addrflush eth1 to 10/8
[[email protected] ~]# ip addrshow eth1
3: eth1: <BROADCAST,MULTICAST> mtu1500 qdisc pfifo_fast state DOWN qlen 1000
link/ether 00:0c:29:4f:66:bc brd ff:ff:ff:ff:ff:ff
inet 192.168.100.1/24scope global eth1
ip route
ip route add to destination网络/mask位数 dev eth*via 网关地址
[roo[email protected] ~]# ip routeadd to 192.168.100.0/24 dev eth0 via 192.168.43.1
[[email protected] ~]# ip routeshow
192.168.100.0/24 via 192.168.43.1 dev eth0
192.168.43.0/24 dev eth0 proto kernel scope link src 192.168.43.3
总结:
ifconfig和route配置十分简单配置很容易功能比较单一,ip命令是属于iproute2软件包中的一个强大的网络配置工具功能十分强悍建议学习使用ip命令。
下载此文档的下载地址:http://down.51cto.com/data/1875820