1.前言
对于LINUX而言,如果我们想对其进行网络配置的话,那么主要涉及到如下方面的配置:
IP,子网掩码,网关,主机名,DNS服务器地址,路由信息。
那么下面,将对这些方面进行操作配置。如果大家对网络的有关知识,不太清楚的,可以参考我的博客:http://zhangfengzhe.blog.51cto.com/8855103/1438163 【这篇博客将快速让大家了解一些概念】
2.关于ifconfig
|
说明:
|
3.命令配置IP并指定子网掩码
|
说明:
注意 ifconfig eth1 192.168.1.130/24 up,这个24其实指定的就是子网掩码信息,一旦指定子网掩码 便会为我们自动计算广播地址。up表示启用这个网络接口。 利用ifconfig进行配置,会立即生效的,但是一旦重启网络服务或者主机,便会失效。 |
4.重启网络服务的方法
对于REDHAT 5而言,如下:
|
说明:
显然,这个脚本应该还有start,stop,status等。 |
5.命令配置路由
|
说明:
|
实例:
|
很显然,一旦重启网络服务或者主机的话,必然失效。
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,非常强大。下面附带一些实例供大家参考:
|
说明:
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 } |
本文转自zfz_linux_boy 51CTO博客,原文链接:http://blog.51cto.com/zhangfengzhe/1439749,如需转载请自行联系原作者