linux网络配置

1. linux系统配置网络-----让其可以与外界连通

     作为一个linux的疯狂热爱者和工作者,我们首先需要掌握的就是在linux系统下怎么配置IP、路由和主机名基本的linux网络配置,如果这都不会的话,那真实没什么颜面。当然如果要我们接入互联网基本的IP地址配置还是不够的,访问网络的话我们需要配置我们的域名解析服务器DNS,(否则我们只能访问ip地址,不能访问带域名的网址)下面作为菜鸟的我来和大家介绍一下,主要是从实战的角度讲解怎么配置一个可以和外界联通的网络,本节暂不讲域名配置。

     众所周知,网络地址有两种类型:

暂时性网络地址:利用ifconfig、ip等命令配置的网络信息,会立即生效,但重启网络服务或系统会失效

永久性的网络地址:通过修改系统内的网络配置文件(/etc/sysconfig/network-scripts/ifcfg-*)进行的修改,不会立即生效,需要重启网络服务或者系统会生效,并且会永久性的生效。 

1.1 通过命令配置一个暂时性可以联通外界的地址

1.给eth0配置一个网络地址

ifconfig eth0 172.18.186.74 netmask 255.255.255.0

来ping 其他ip地址,看看网络的连通情况

可以看得可以直接ping同同一网段内的ip主机。但是ping不通不再同一个网络段的ip主机。如果想ping通外网,就要设置一个默认网卡(其实就是默认路由,该台电脑和外界通信必须要经由该默认路由出去)

2 给系统添加一个默认网关(默认路由)

 route add default gw 172.16.186.254

如下图,可以看得到可以ping同外界网络了。

可以看得到最后一条就是默认路由,当找不到目的路由,最后都会走默认路由。

但是重启之后就会无效了。

1.2. 同过修改网卡配置文件,让其永久起效

    linux网络配置_第1张图片

可以看得到eth0网卡的配置文件如上:

DEVICE="eth0"                        设备名

NM_CONTROLLED="yes"        设备是否被NetworkManager管理

ONBOOT="yes"                        开机或重启网络是否启动

HWADDR="00:0C:29:59:E2:D3" 硬件地址(MAC地址)

TYPE=Ethernet                           类型

BOOTPROTO=none                  启动协议{none|static|dhcp}                       

                               static(静态IP)
                               none(不指定,设置固定ip的情况,这个也行,但是如果要设定多网口绑定bond的时候,必须设成none)
                               dhcp(动态获得IP相关信息)

IPADDR=192.168.0.1                 IP地址

NETMASK=255.255.255.0         子网掩码       

GATEWAY=192.168.0.254         默认网关

写好上面的配置文件后,执行service network restart(或者/etc/init.d/network restart)或重启系统就可以永久生效。

注意:如果没有重启网络或重启系统,配置文件里面的ip地址不会起效

2.一些基本的命令和配置文件详解

2.1 ifconfig 命令    

# ifconfig  eth0  up          # 开启eth0网卡
# ifconfig  eth0  down        # 关闭eth0网卡
# ifconfig  eth0  -arp        # 关闭eth0网卡arp协议
# ifconfig  eth0  promisc     # 开启eth0网卡的混合模式
# ifconfig  eth0  mtu 1400    # 设置eth0网卡的最大传输单元为1400
# ifconfig  eth0  192.168.0.2/24    # 设置eth0网卡IP地址
# ifconfig  eth0  192.168.0.2  netmask 255.255.255.0    # 功能同上
注:单网卡多ip 
    ifconfig  eth0:1  192.168.0.3 netmask 255.255.255.0    # 功能同上

2.2 与ifconfig相配置ip命令

ip link show                  # 显示网络接口信息
# ip link set eth0 up        # 开启网卡
# ip link set eth0 down         # 关闭网卡
# ip link set eth0 promisc on   # 开启网卡的混合模式
# ip link set eth0 promisc offi # 关闭网卡的混个模式
# ip link set eth0 txqueuelen 1200    # 设置网卡队列长度
# ip link set eth0 mtu 1400     # 设置网卡最大传输单元
# ip addr show                  # 显示网卡IP信息
# ip addr add 192.168.0.1/24 dev eth0 # 设置eth0网卡IP地址192.168.0.1
# ip addr del 192.168.0.1/24 dev eth0 # 删除eth0网卡IP地址
注:单网卡多ip 
        # 功能同上
         ip addr add 192.168.0.1/24 dev eth0 label eth0:1

2.3 route命令设置网关

route  add [-net|-host] target [netmask] gw
route  del [-net|-host] target [netmask] gw
# route add  -net 192.168.3.0/24  gw  192.168.0.254    # 设置到192.168.3.0网段的网关为192.168.0.254
# route add  -net 192.168.3.0 netmask 255.255.255.0  gw  192.168.0.254    # 功能同上
# route add  -host 192.168.4.4  gw  192.168.0.254    # 设置到192.168.4.4主机的网关为192.168.0.254
# 
# route del  -net 192.168.3.0/24                        # 删除192.168.3.0网段的网关信息
# route del  -host 192.168.4.4                        # 删除192.168.4.4主机的网关信息
# route add default gw  192.168.0.254                # 设置默认网关为192.168.0.254
# route del default gw  192.168.0.254                # 删除默认网关为192.168.0.254

2.4 与route命令相同的ip命令

ip route list                 # 查看路由信息
# ip route add 192.168.4.0/24  via  192.168.0.254 dev eth0 # 设置192.168.4.0网段的网关为192.168.0.254,数据走eth0接口
# ip route add default via  192.168.0.254  dev eth0    # 设置默认网关为192.168.0.254
# ip route del 192.168.4.0/24    # 删除192.168.4.0网段的网关
# ip route del default    # 删除默认路由

3. 几个常用的网络配置相关文件

3.1DNS配置文件

DNS配置文件位置:/etc/resolv.conf 

       DNS配置格式: 
 
         nameserver DNS_IP1 
 
         nameserver DNS_IP2 
 
指定本地解析: 
 
     /etc/hosts下添加 
 
    目标主机IP        主机名       
 
    fg:172.16.36.1  www.chris.com  
 
DNS解析过程-->/etc/hosts-->DNS 服务器

3.2主机名配置文件

     文件位置: /etc/sysconfig/network, 典型的配置如下

NETWORKING=yes
HOSTNAME=localhost.localdomain
GATEWAY=172.16.186.254

参数简要解释:

NETWORK          设置网络是否有效,yes有效,no无效

NETWORKING_IPV6  设置ipv6网络是否有效,yes有效,no无效

HOSTNAME         设置服务器的主机名,最好和/etc/hosts里设置一样,否则在使用一些程序的时候会有问题。

GATEWAY          指定默认网关IP

3.3私有ip对应的主机名

    /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

可见,默认的情况是本机ip和本机一些主机名的对应关系,第一行是ipv4信息,第二行是ipv6信息,如果用不上ipv6本机解析,一般把该行注释掉。

第一行表示  localhost localhost.localdomain localhost4 localhost4.localdomain4

都会被解析成127.0.0.1,我们可以用ping试试。

测试如下:

[root@localhost ~]# ping -c 3 localhost

PING localhost (127.0.0.1) 56(84) bytes of data.

64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.029 ms

64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.038 ms

64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.026 ms


--- localhost ping statistics ---

3 packets transmitted, 3 received, 0% packet loss, time 1999ms

rtt min/avg/max/mdev = 0.026/0.031/0.038/0.005 ms

[root@localhost ~]# ping -c 3 localhost.localdomain

PING localhost (127.0.0.1) 56(84) bytes of data.

64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.022 ms

64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.037 ms

64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.064 ms


--- localhost ping statistics ---

3 packets transmitted, 3 received, 0% packet loss, time 1999ms

rtt min/avg/max/mdev = 0.022/0.041/0.064/0.017 ms

[root@localhost ~]# ping -c 3 localhost4

PING localhost (127.0.0.1) 56(84) bytes of data.

64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.023 ms

64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.027 ms

64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.021 ms


--- localhost ping statistics ---

3 packets transmitted, 3 received, 0% packet loss, time 1999ms

rtt min/avg/max/mdev = 0.021/0.023/0.027/0.006 ms

[root@localhost ~]# ping -c 3 localhost4.localdomain4

PING localhost (127.0.0.1) 56(84) bytes of data.

64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.023 ms

64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.028 ms

64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.029 ms


--- localhost ping statistics ---

3 packets transmitted, 3 received, 0% packet loss, time 1999ms

rtt min/avg/max/mdev = 0.023/0.026/0.029/0.006 ms

至此,总结完毕!




你可能感兴趣的:(linux网络配置)