linux 的ip 设置lo_Linux下配置IP地址的几种方式--------CentOS 6.X

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}

[root@www ~]# service NetworkManager start

Setting network parameters...                           [  OK  ]

Starting NetworkManager daemon:

[root@www ~]# service NetworkManager stop

Stopping NetworkManager daemon:                         [  OK  ]

[root@www ~]# service NetworkManager 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}

[root@www ~]# service network stop

Shutting down loopback interface:                          [  OK  ]

[root@www ~]# service network start

Bringing up loopback interface:                            [  OK  ]

Bringing up interface eth0:                                [  OK  ]

[root@www ~]# service network restart

Shutting down interface eth0:                              [  OK  ]

Shutting down loopback interface:                          [  OK  ]

Bringing up loopback interface:                            [  OK  ]

Bringing up interface eth0:                                [  OK  ]

[root@www ~]# service network status

Configureddevices:

lo eth0

Currently activedevices:

lo eth0 eth1

[root@www ~]# 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*查看单个网卡

[root@www ~]# ifconfig eth0

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位数

[root@www ~]# ifconfig eth1 192.168.1.1/24

[root@www ~]# ifconfig eth1

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]启用或者禁用网卡

[root@www ~]# ifconfig lo down

[root@www ~]# ifconfig eth1 down

[root@www ~]# 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查看计算机上的路由条目

[root@www ~]# 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网关路由

[root@www ~]# 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本地网关地址

[root@CentOS ~]# route add -net 10.0.0.0/8 gw 192.168.43.1

[root@CentOS ~]# 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位数

[root@CentOS ~]# 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

[root@CentOS ~]# route del -net 10.0.0.0/8

[root@CentOS ~]# 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

[root@CentOS ~]# route add default gw 192.168.43.1

[root@CentOS ~]# 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

[root@CentOS ~]# route del -net 0.0.0.0

[root@CentOS ~]# 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指向

[root@CentOS ~]# 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]

[root@CentOS ~]# ip link show

1: lo: mtu16436 qdisc noqueue state UNKNOWN

link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen1000

link/ether 00:0c:29:57:5c:bb brd ff:ff:ff:ff:ff:ff

[root@CentOS ~]# ip link show eth0

2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000

link/ether 00:0c:29:57:5c:bb brd ff:ff:ff:ff:ff:ffip –s link show [dev] 查看接口收发了多少包

[root@CentOS ~]# ip -s link show eth0

2: eth0: 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       0ip link set [dev] [up|down]

[root@www ~]# ip link set eth1 down

[root@www ~]# ip link show

1: lo: mtu 16436 qdisc noqueue state UNKNOWN

link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen1000

link/ether 00:0c:29:4f:66:b2 brdff:ff:ff:ff:ff:ff

3:eth1: mtu 1500 qdiscpfifo_fast state DOWNqlen 1000

link/ether 00:0c:29:4f:66:bcbrd ff:ff:ff:ff:ff:ff

ip addrip aadr show [dev] 查看ip地址

ip addr add IP/mask位数 dev eth*  添加ip地址

[root@www ~]# ip addr add10.1.1.1/8 dev eth1

[root@www ~]# ip addr show eth1

3: eth1: 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 eth1ip addr del address dev eth*

[root@www ~]# ip addr del 10.1.1.1/8 deveth1ip addr flush [dev] to ip/mask位数

[root@www ~]# ip addr show eth1

3: eth1: 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

[root@www ~]# ip addr flush eth1 to 10/8

[root@www ~]# ip addr show eth1

3: eth1: 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 网关地址

[root@CentOS ~]# ip route add to 192.168.100.0/24 dev eth0 via 192.168.43.1

[root@CentOS ~]# ip route show

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命令。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值