Linux网络配置详解
1.前言
对于LINUX而言,如果我们想对其进行网络配置的话,那么主要涉及到如下方面的配置:
IP,子网掩码,网关,主机名,DNS服务器地址,路由信息。
那么下面,将对这些方面进行操作配置。如果大家对网络的有关知识,不太清楚的,可以参考我的博客:http://zhangfengzhe.blog.51cto.com/8855103/1438163 【这篇博客将快速让大家了解一些概念】
2.关于ifconfig
[root@localhost ~]# ifconfig eth0 Link encap:Ethernet HWaddr 00:0C:29:6A:2D:34 inet addr:192.168.204.88 Bcast:192.168.204.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe6a:2d34/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:69 errors:0 dropped:0 overruns:0 frame:0 TX packets:109 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:7622 (7.4 KiB) TX bytes:14522 (14.1 KiB) Interrupt:67 Base address:0x2000 eth1 Link encap:Ethernet HWaddr 00:0C:29:6A:2D:3E inet addr:192.168.1.116 Bcast:192.168.1.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe6a:2d3e/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:155 errors:0 dropped:0 overruns:0 frame:0 TX packets:57 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:13616 (13.2 KiB) TX bytes:9968 (9.7 KiB) Interrupt:67 Base address:0x2080 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:12 errors:0 dropped:0 overruns:0 frame:0 TX packets:12 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:908 (908.0 b) TX bytes:908 (908.0 b) |
说明:
|
3.命令配置IP并指定子网掩码
[root@localhost ~]# ifconfig eth1 eth1 Link encap:Ethernet HWaddr 00:0C:29:6A:2D:3E inet addr:192.168.1.116 Bcast:192.168.1.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe6a:2d3e/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:350 errors:0 dropped:0 overruns:0 frame:0 TX packets:57 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:30883 (30.1 KiB) TX bytes:9968 (9.7 KiB) Interrupt:67 Base address:0x2080 [root@localhost ~]# ifconfig eth1 192.168.1.130/24 up [root@localhost ~]# ifconfig eth1 eth1 Link encap:Ethernet HWaddr 00:0C:29:6A:2D:3E inet addr:192.168.1.130 Bcast:192.168.1.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe6a:2d3e/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:383 errors:0 dropped:0 overruns:0 frame:0 TX packets:68 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:33823 (33.0 KiB) TX bytes:12966 (12.6 KiB) Interrupt:67 Base address:0x2080 [root@localhost ~]# |
说明:
注意 ifconfig eth1 192.168.1.130/24 up,这个24其实指定的就是子网掩码信息,一旦指定子网掩码 便会为我们自动计算广播地址。up表示启用这个网络接口。 利用ifconfig进行配置,会立即生效的,但是一旦重启网络服务或者主机,便会失效。 |
4.重启网络服务的方法
对于REDHAT 5而言,如下:
[root@localhost ~]# /etc/init.d/network restart Shutting down interface eth0: [ OK ] Shutting down interface eth1: [ OK ] Shutting down loopback interface: [ OK ] Bringing up loopback interface: [ OK ] Bringing up interface eth0: [ OK ] Bringing up interface eth1: Determining IP information for eth1... done. [ OK ] |
说明:
显然,这个脚本应该还有start,stop,status等。 |
5.命令配置路由
[root@localhost ~]# route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.1.0 * 255.255.255.0 U 0 0 0 eth1 192.168.204.0 * 255.255.255.0 U 0 0 0 eth0 169.254.0.0 * 255.255.0.0 U 0 0 0 eth1 default localhost 0.0.0.0 UG 0 0 0 eth1 [root@localhost ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1 192.168.204.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 0 0 0 eth1 0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 eth1 [root@localhost ~]# |
说明:
|
实例:
[root@localhost init.d]# route add -net 10.0.0.0/8 gw 192.168.1.1 [root@localhost init.d]# route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.1.0 * 255.255.255.0 U 0 0 0 eth1 192.168.204.0 * 255.255.255.0 U 0 0 0 eth0 169.254.0.0 * 255.255.0.0 U 0 0 0 eth1 10.0.0.0 localhost 255.0.0.0 UG 0 0 0 eth1 default localhost 0.0.0.0 UG 0 0 0 eth1 [root@localhost ~]# route del -net 10.0.0.0/8 gw 192.168.1.1 [root@localhost ~]# route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.1.0 * 255.255.255.0 U 0 0 0 eth1 192.168.204.0 * 255.255.255.0 U 0 0 0 eth0 169.254.0.0 * 255.255.0.0 U 0 0 0 eth1 default localhost 0.0.0.0 UG 0 0 0 eth1 |
很显然,一旦重启网络服务或者主机的话,必然失效。
6.在文件中进行网络配置
如果我们想IP,NETMASK,GATEWAY这些信息在重启后有效的话,可以选择在/etc/sysconfig/network-scripts/ifcfg-ethXXX进行配置 对于ifcfg-ethXXX而言: 其实就是一个KEY=VALUE的形式,比如: DEVICE=eth0 【关联的设备,一定要和文件名称的后半部分一致。 】 HWADDR=00:0c:29:1c:95:d2 【硬件地址,不可修改】 ONBOOT=yes 【是否开机时自动启动此设备】 NETMASK=255.255.255.0 【子网掩码】 IPADDR=192.168.152.2 【IP地址】 TYPE=Ethernet 【网络接口类型】 BOOTPROTO=static 【静态地址类型】 需要注意的是, BOOTPROTO还可以取值DHCP,即由DHCP服务器分配地址。 USERCTL={yes|no} :是否允许普通用户控制此接口 PEERDNS={yes|no} :在BOOTPROTO为DHCP时,是否接受DHCP服务器指定的DNS地址。 如果我们想路由信息在重启后有效的话,可以选择在/etc/sysconfig/network-scripts/route-ethXXX进行配置 目标主机 via 网关 目标网络 via 网关 【需要加入子网掩码,并且网关与你的ethXXX应处于同一网络,也就说,是从ethXXX发出去的】 还可以采用另一种格式: ADDRESS0= NETMASK0= GATEWAY0= ADDRESS1= NETMASK1= GATEWAY1= |
7.DNS配置
关于DNS的配置方式,只有一种,那就是编辑配置文件。 [root@localhost ~]# cat /etc/resolv.conf nameserver 202.106.46.151 nameserver 192.168.1.1 注意最多只能有3个,也就是说: nameserver DNS_IP_1 nameserver DNS_IP_2 nameserver DNS_IP_3 指定本地解析,类似于WINDOWS的HOSTS文件。 /etc/hosts 主机IP 主机名称 别名 |
8.主机名配置
配置主机名: hostname HOSTNAME 同上,可以生效,不能永久有效。 可以编辑 /etc/sysconfig/network文件,里面有hostname这一行。 [root@localhost ~]# cat /etc/sysconfig/network NETWORKING=yes ===》是否启用本机的网络功能,是网络功能的总开关。 NETWORKING_IPV6=no HOSTNAME=localhost.localdomain |
9.ip命令介绍
可能很多同学没有听说过ip命令。iproute2是一个非常强大的软件包,它提供了一个命令,就叫ip,非常强大。下面附带一些实例供大家参考:
[root@localhost ~]# ip link show ------------》相当于ifconfig -a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue 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 qlen 1000 link/ether 00:0c:29:6a:2d:34 brd ff:ff:ff:ff:ff:ff 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000 link/ether 00:0c:29:6a:2d:3e brd ff:ff:ff:ff:ff:ff 4: sit0: <NOARP> mtu 1480 qdisc noop link/sit 0.0.0.0 brd 0.0.0.0 [root@localhost ~]# ip -s link show ------------>s选项会显示一些额外的统计信息。 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 RX: bytes packets errors dropped overrun mcast 5965 36 0 0 0 0 TX: bytes packets errors dropped carrier collsns 5965 36 0 0 0 0 [root@localhost ~]# ifconfig eth0:0 192.168.152.3/24 [root@localhost ~]# ifconfig eth0 Link encap:Ethernet HWaddr 00:0C:29:1C:95:D2 inet addr:192.168.152.2 Bcast:192.168.152.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe1c:95d2/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:196 errors:0 dropped:0 overruns:0 frame:0 TX packets:394 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:21710 (21.2 KiB) TX bytes:48480 (47.3 KiB) Interrupt:67 Base address:0x2024 eth0:0 Link encap:Ethernet HWaddr 00:0C:29:1C:95:D2 inet addr:192.168.152.3 Bcast:192.168.152.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 Interrupt:67 Base address:0x2024 [root@localhost network-scripts]# ip addr del 192.168.152.3/24 dev eth0 [root@localhost network-scripts]# ip addr show to 192.168.152.2/24 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000 inet 192.168.152.2/24 brd 192.168.152.255 scope global eth0 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000 inet 192.168.152.129/24 brd 192.168.152.255 scope global eth1 inet 192.168.152.4/24 scope global secondary eth1 [root@localhost network-scripts]# ip addr flush to 192.168.152.129/24 dev eth1 |
说明:
ip link set DEVICE { up | down | arp { on | off } | promisc { on | off } | allmulti { on | off } | dynamic { on | off } | multicast { on | off } | txqueuelen PACKETS | name NEWNAME | address LLADDR | broadcast LLADDR | mtu MTU } |
本文出自 “我想超越自我” 博客,请务必保留此出处http://zhangfengzhe.blog.51cto.com/8855103/1439749
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。