当前位置: 移动技术网 > 网络运营>服务器>Linux > 详解Linux 主机网络接入配置

详解Linux 主机网络接入配置

2019年04月21日  | 移动技术网网络运营  | 我要评论

详解linux 主机网络接入配置

前言:

网络配置是我们在安装好操作系统之后,需要解决的第一步。现时代没有接入网络的主机已然等同于一堆废铁。在网络配置的过程中,通常我们需要配置本机ip地址,缺省网关,dns,主机名等等。本文主要描述在linux环境下,主要以传统命令行方式讲解如何将主机接入网络。对于网路配置的新命令如ip,nmcli等等在以后的文章中描述。

一、网络配置概述

主机接入互联网前提:遵循tcp/ip协议栈

配置主机接入tcp/ip网络需要配置的内容:

  ip/netmask
  路由:
    主机路由:一台主机
    网络路由:是一个网络
    默认路由(缺省网关):
  dns服务器:
    主dns服务器
    备用dns服务器
  主机名:computer_name.domain.com

网络设备表示方法(centos7版本)

  内核识别设备:驱动

  网卡命名机制

    centos 7中,systemd对网络设备的命名方式
    (a) 如果firmware或bios为主板上集成的设备提供的索引信息可用,且可预测则根据此索引进行命名,例如eno1
    (b) 如果firmware或bios为pci-e扩展槽所提供的索引信息可用,且可预测,则根据此索引进行命名,例如ens1
    (c) 如果硬件接口的物理位置信息可用,则根据此信息进行命名,例如enp2s0
    (d) 如果用户显式启动,也可根据mac地址进行命名,enx2387a1dc56;
    (e) 上述均不可用时,则使用传统命名机制,基于bios支持biosdevname中
    (f) 本地回环 lo
    内置网卡:em1,em2
     pci卡:pypx y:slot ,x:port

  名称组成格式

    en: ethernet 有线局域网
    wl: wlan 无线局域网
    ww: wwan无线广域网

  名称类型:

    o: 集成设备的设备索引号
    s: 扩展槽的索引号
    x: 基于mac地址的命名
    ps: enp2s1

二、ip地址配置

动态配置(ip/netmask):

  dhcp服务器(dynamic host configuration protocol)

静态指定:

  用户空间工具(ifconfig,ip命令),立即生效,但不会长久有效(地址配置在内核,显示在用户空间)
  网络设备服务配置文件,完成长久有效配置:
    /etc/sysconfig/network-script/ifcfg-eth0
  gui/tui配置工具

网路配置命令

ifconfig(传统配置命令)
  ifconfig:显示所有活动状态的相关信息
  ifconfig interface:仅显示指定接口的相关信息
  ifconfig interface address
    ip/mask
      长格式:
      ifconfig interface ip netmask mask
    cidr:无类别的预见路由
    ifconfig interface ip/mask

ifconfig示例

# more /etc/redhat-release ###当前环境
centos linux release 7.2.1511 (core) 
# ifconfig |grep eno -a1 ###当前服务器有2块网卡,分别为eno16777728,eno33554960
eno16777728: flags=4163<up,broadcast,running,multicast> mtu 1500
inet 172.24.8.131 netmask 255.255.255.0 broadcast 172.24.8.255
--
eno33554960: flags=4163<up,broadcast,running,multicast> mtu 1500
inet 192.168.81.144 netmask 255.255.255.0 broadcast 192.168.81.255

# ifconfig eno33554960 192.168.81.145/24
# ifconfig eno33554960|head -2
eno33554960: flags=4163<up,broadcast,running,multicast> mtu 1500
inet 192.168.81.145 netmask 255.255.255.0 broadcast 192.168.81.255  

# ifconfig eno33554960 down ###禁用当前网卡
# ifconfig eno33554960 up  ###激活当前网卡
# ifconfig eno33554960|head -2 ###再次查看网卡,由于使用了dhcp,ip地址被分配为原地址
eno33554960: flags=4163<up,broadcast,running,multicast> mtu 1500
inet 192.168.81.144 netmask 255.255.255.0 broadcast 192.168.81.255

网卡手工配置示例
# vi /etc/sysconfig/network-scripts/ifcfg-eno16777728 
type="ethernet"   //指定网络类型为以太网模式
bootproto="dhcp" //指定启动地址协议的获取方式(dhcp或bootp为自动获取,none
defroute="yes"  //是否把这个eno16777728设置为默认路由 
peerdns="yes"   //是否允许dhcp服务分配地址时直接更新/etc/resolv.conf中的dns服务器地址
peerroutes="yes"
ipv4_failure_fatal="no" //如果ipv4配置失败,设备是否被禁用
ipv6init="yes"   //允许在该网卡上启动ipv6的功能 
ipv6_autoconf="yes" //是否使用ipv6地址的自动配置
ipv6_defroute="yes"  ### author : leshami 
ipv6_peerdns="yes"   ### blog : http://blog.csdn.net/leshami 
ipv6_peerroutes="yes" ### qq/weixin : 645746311
ipv6_failure_fatal="no"
name="eno16777728"  //网络连接标识名 
uuid="52ff246a-d965-4056-b34f-16e8f4df2c0a" //网卡全球通用唯一识别码
device="eno16777728"      //网络连接标识名
onboot="yes"        //网卡开机后自动启动
userctl=no         //是否允许普通用户操作网卡
peerdns={yes|no}  //是否允许dhcp服务分配地址时直接更新/etc/resolv.conf中的dns服务器地址
# systemctl restart network.service  //重启网络服务,使配置生效

三、路由配置

route(传统配置命令)
route:
 route:显示路由信息(网关指的就是下一跳的地址,下一跳地址必须与本地主机位于同一网络)
  -n:使用数字格式显示,不反解地址到主机名
 route add //添加路由
  -host host_ip gw next [dev device]
  -net net_addr
  -net 0.0.0.0 目标地址为任意地址,即默认路由

  route del //删除路由
   -host host_ip gw 下一跳
   -net net_addr gw 下一跳

示例:
# route 
# route
kernel ip routing table
destination gateway  genmask  flags metric ref use iface
default  192.168.81.2 0.0.0.0  ug 100 0  0 eno33554960
172.24.8.0  0.0.0.0  255.255.255.0 u 100 0  0 eno16777728
192.168.81.0 0.0.0.0  255.255.255.0 u 100 0  0 eno33554960

示例中对于目表为192.168.81.0以及172.24.8.0网络段的访问,gateway为4个0表示没有网络,也就是说属于直连网络,无需路由
对于目标为default的(当使用route -n时,default为4个0)网络段时,通过缺省网关192.168.81.2连接目标指向单个
主机的为主机路由,目标网络地址的为网络路由,目标指向所有地址(0.0.0.0或者default)的为默认路由。  

route输出具体描述
route命令输出的路由表字段含义如下:
 destination 目标
   the destination network or destination host. 目标网络或目标主机。
 gateway 网关
   the gateway address or '*' if none set. 网关地址,如果没有就显示星号。
 genmask 网络掩码
   the netmask for the destination net; '255.255.255.255' for a
   host destination and '0.0.0.0' for the default route.
 flags:总共有多个旗标,代表的意义如下:      
  u (route is up):该路由是启动的;      
  h (target is a host):目标是一部主机 (ip) 而非网域;      
  g (use gateway):需要透过外部的主机 (gateway) 来转递封包;      
  r (reinstate route for dynamic routing):使用动态路由时,恢复路由资讯的旗标;      
  d (dynamically installed by daemon or redirect):已经由服务或转 port 功能设定为动态路由      
  m (modified from routing daemon or redirect):路由已经被修改了;      
  ! (reject route):这个路由将不会被接受(用来抵挡不安全的网域!)
  a (installed by addrconf)
  c (cache entry)
 metric 距离、跳数。暂无用。
   the 'distance' to the target (usually counted in hops). it is
   not used by recent kernels, but may be needed by routing dae-
   mons.
 ref 不用管,恒为0。
   number of references to this route. (not used in the linux ker-nel.)
 use 该路由被使用的次数,可以粗略估计通向指定网络地址的网络流量。
   count of lookups for the route. depending on the use of -f and
 iface 从哪一个网络接口路由出去,如本例中的eno33554960
   interface to which packets for this route will be sent.

添加网络路由
 当前本机ip地址为172.24.8.131
 假定要到达目标地址为:192.168.10.0/24,下一跳为 172.24.8.2 则添加路由命令如下
 # route add -net 192.168.10.0/24 gw 172.24.8.2 ###此时为网络网关(路由)

 # route -n
 kernel ip routing table
 destination gateway  genmask  flags metric ref use iface
 0.0.0.0  192.168.81.2 0.0.0.0  ug 100 0  0 eno33554960
 172.24.8.0  0.0.0.0  255.255.255.0 u 100 0  0 eno16777728
 192.168.10.0 172.24.8.2  255.255.255.0 ug 0  0  0 eno16777728 
 192.168.81.0 0.0.0.0  255.255.255.0 u 100 0  0 eno33554960

 可以看到上述多出了一条路由记录,目标网络为192.168.10.0
 新添加的路由接口设备自动使用了eno16777728网卡,因为172.24.8.2与eno16777728 ip为同一网段内

添加主机路由
 假定要到达目标地址为:192.168.20.1,下一跳为: 172.16.100.177 
 # route add -host 192.168.20.1 gw 172.16.100.177
 siocaddrt: network is unreachable #提示网络不可达,因为172.16.100.177不在当前两块网卡的任一网段内

 下面修改下一跳地址为172.24.8.254再次尝试添加
  # route add -host 192.168.20.1 gw 172.24.8.254 ###此处无需指定隐码
  # route -n ###在下面的列表中flags有一个h,表示此条为主机路由
  kernel ip routing table
  destination gateway  genmask  flags metric ref use iface
  0.0.0.0  192.168.81.2 0.0.0.0  ug 100 0  0 eno33554960
  172.24.8.0  0.0.0.0  255.255.255.0 u 100 0  0 eno16777728
  192.168.10.0 172.24.8.2  255.255.255.0 ug 0  0  0 eno16777728
  192.168.20.1 172.24.8.254 255.255.255.255 ugh 0  0  0 eno16777728
  192.168.81.0 0.0.0.0  255.255.255.0 u 100 0  0 eno33554960

 路由表描述
  对于请求到192.168.10.0的网路,路由地址为172.24.8.2,经由网卡eno16777728
  对于请求到192.168.20.1的主机,路由地址为172.24.8.254,经由网卡eno16777728
  对于请求172.24.8.0以及192.168.81.0的网络,为直连网络,无需路由
  对于请求到0.0.0.0的网络经由192.168.81.2从网卡eno33554960进行路由,如访问百度则由此路由

 # ping www.baidu.com -c 2
 ping www.baidu.com (14.215.177.38) 56(84) bytes of data.
 64 bytes from 14.215.177.38: icmp_seq=1 ttl=128 time=5.76 ms
 64 bytes from 14.215.177.38: icmp_seq=2 ttl=128 time=6.13 ms

删除路由
 假定需要删除默认路由
  # route del -net 0.0.0.0 netmask 0.0.0.0 ###等价于 route del default
  # route |grep -i default

  # ping www.baidu.com ###缺省路由被删除后,ping百度不可达
  connect: network is unreachable    

 删除之前添加的网络路由和主机路由    
  # route del -net 192.168.10.0/24
  # route del -host 192.168.20.1
  # route -n
  kernel ip routing table
  destination gateway  genmask  flags metric ref use iface
  172.24.8.0  0.0.0.0  255.255.255.0 u 100 0  0 eno16777728
  192.168.81.0 0.0.0.0  255.255.255.0 u 100 0  0 eno33554960

添加缺省网关
 # route add default gw 192.168.81.2
 # route add -net 0.0.0.0 gw 192.168.81.2 ###与上一条命令等价

路由静态配置文件
 /etc/sysconfig/network
 /etc/sysconfig/network-scripts/route-ethx | route-pppx

 配置文件的格式1:每行一个要配置的路径条目
  目标 via 下一跳 
  例子:
  192.168.0.0/24 via 172.16.100.177

 配置文件的格式2:每一组一个路由条目
  address#=目标
  netmask#=掩码
  gateway#=下一跳

  address0=192.168.0.0
  netmask0=255.255.255.0
  gateway0=172.16.100.1

四、dns配置

dns服务器指向:
 配置文件:/etc/resolv.conf
  最多三个:
    nameserver dns_server_ip
    nameserver 8.8.8.8

 测试dns解析
  dig -t a 域名 //正解测试
  dig -x ip  //反解测试

查看当前主机dns解析配置
 # more /etc/resolv.conf 
 # generated by networkmanager
 search localdomain example.com
 nameserver 192.168.81.2
 nameserver 172.24.8.1

 # dig -t a www.oracle.com

 ; <<>> dig 9.9.4-redhat-9.9.4-29.el7 <<>> -t a www.oracle.com
 ;; global options: +cmd
 ;; got answer:
 ;; ->>header<<- opcode: query, status: noerror, id: 11680
 ;; flags: qr rd ra; query: 1, answer: 3, authority: 0, additional: 0

 ;; question section:
 ;www.oracle.com.      in  a

 ;; answer section:
 www.oracle.com.  5  in  cname ds-www.oracle.com.edgekey.net.
 ds-www.oracle.com.edgekey.net. 5 in cname e11582.dscx.akamaiedge.net.
 e11582.dscx.akamaiedge.net. 5 in  a  184.28.37.127

 ;; query time: 12 msec
 ;; server: 192.168.81.2#53(192.168.81.2) //通过本地ip 192.168.81.2递交外部dns服务器解析
 ;; when: mon sep 04 21:14:31 cst 2017
 ;; msg size rcvd: 128

 # dig -x 184.28.37.127

 ; <<>> dig 9.9.4-redhat-9.9.4-29.el7 <<>> -x 184.28.37.127
 ;; global options: +cmd
 ;; got answer:
 ;; ->>header<<- opcode: query, status: noerror, id: 20357
 ;; flags: qr rd ra; query: 1, answer: 1, authority: 8, additional: 9

 ;; opt pseudosection:
 ; edns: version: 0, flags:; mbz: 0005 , udp: 512
 ;; question section:
 ;127.37.28.184.in-addr.arpa. in  ptr

 ;; answer section:
 127.37.28.184.in-addr.arpa. 5 in  ptr a184-28-37-127.deploy.static.akamaitechnologies.com.

 ;; authority section:
 28.184.in-addr.arpa. 5  in  ns  ns5.reverse.deploy.akamaitechnologies.com.
 28.184.in-addr.arpa. 5  in  ns  ns1.reverse.deploy.akamaitechnologies.com.
 28.184.in-addr.arpa. 5  in  ns  ns7.reverse.deploy.akamaitechnologies.com.
 28.184.in-addr.arpa. 5  in  ns  ns3.reverse.deploy.akamaitechnologies.com.
 28.184.in-addr.arpa. 5  in  ns  ns4.reverse.deploy.akamaitechnologies.com.
 28.184.in-addr.arpa. 5  in  ns  ns8.reverse.deploy.akamaitechnologies.com.
 28.184.in-addr.arpa. 5  in  ns  ns2.reverse.deploy.akamaitechnologies.com.
 28.184.in-addr.arpa. 5  in  ns  ns6.reverse.deploy.akamaitechnologies.com.

 ;; additional section:
 ns1.reverse.deploy.akamaitechnologies.com. 5 in a 96.7.50.32
 ns2.reverse.deploy.akamaitechnologies.com. 5 in a 2.16.40.32
 ns3.reverse.deploy.akamaitechnologies.com. 5 in a 23.74.25.32
 ns4.reverse.deploy.akamaitechnologies.com. 5 in a 95.100.168.32
 ns5.reverse.deploy.akamaitechnologies.com. 5 in a 95.100.173.32
 ns6.reverse.deploy.akamaitechnologies.com. 5 in a 23.61.199.32
 ns7.reverse.deploy.akamaitechnologies.com. 5 in a 184.26.161.32
 ns8.reverse.deploy.akamaitechnologies.com. 5 in a 2.22.230.32

 ;; query time: 205 msec
 ;; server: 192.168.81.2#53(192.168.81.2)
 ;; when: mon sep 04 21:17:05 cst 2017
 ;; msg size rcvd: 407

网络服务启动脚本

 centos 5/6:
  服务脚本:/etc/init.d/network

 centos 6
  服务脚本:/etc/init.d/networkmanager

  服务脚本:sysv style lsb
   控制运行在后台守护进程,接受参数{start|stop|restart|status}
   脚本位置:/etc/rc.d/init.d或/etc/init.d

  配置某服务开机自动启动:chkconfig src_script_name on
  禁止开机自动启动:chkconfig src_script_name off
  查看 chkconfig --list

  调用机制:
   1./etc/init.d/script_name {start|stop|restart|status}
   1.service script_name {start|stop|restart|status}

  centos 7
   服务脚本:/etc/init.d/network
   # systemctl start network.service

如何实现在单网卡上配置多个地址(通常用于vip)
 通过网络接口别名来实现
 eth0x
 eth0:x eth0:0 eth0:1....

 配置1:
 ifconfig ethx:y ip/mask
 配置2:
 ifcfg-ethx:y
  device=ethx:y
  bootproto={none|static}
  ipaddr=
  netmask=
  gateway=
 非主地址(别名地址)不支持dhcp协议获取

五、主机名配置

短主机名,如mycomputer1
长主机名(fqdn),如mycomputer.example.com

centos 5/6配置主机名
 查看主机名: 
  # hostname 
  # more /etc/sysconfig/network
  # more /proc/sys/kernel/hostname

 临时修改主机名:
  # hostname new_host_name 
  # hostname new_host_name.domain.com

 永久修改主机名(需要重启):
  # vi /etc/sysconfig/network 

 修改以及立即生效
  # echo new_host_name >/proc/sys/kernel/hostname 
  # sysctl kernel.hostname=new_host_name     

  示例
   # more /etc/redhat-release 
   centos release 6.7 (final)
   # hostname
   node133
   # hostname host133
   # hostname
   host133

   [root@host133 ~]# echo host133.example.com >/proc/sys/kernel/hostname
   [root@host133 ~]# hostname
   host133.example.com
   [root@host133 ~]# sysctl kernel.hostname=host133.example.com
   kernel.hostname = host133.example.com
   [root@host133 ~]# hostname
   host133.example.com

centos 7 配置主机名  
 在rhel7中,引入了静态(static)、瞬态(transient)和灵活(pretty)三种主机名。
 “静态”主机名——也称为内核主机名,是系统在启动时从/etc/hostname自动初始化的主机名。

 “瞬态”主机名——是在系统运行时临时分配的主机名,例如,通过dhcp或dns服务器分配。
  静态主机名和瞬态主机名都遵从作为互联网域名同样的字符限制规则。

 “灵活”主机名——是允许使用自由形式(可包括特殊/空白字符)的主机名,以展示给终端用户(如tom‘s computer)

 查看主机名的命令一般格式如下:

  hostnamectl [status] [--static|--transient|--pretty] 

  选项说明如下:
  status——可同时查看静态、瞬态和灵活三种主机名及其相关的设置信息。
  --static——仅查看静态(永久)主机名。
  --transient——仅查看瞬态(临时)主机名。
  --pretty ——仅查看灵活主机名。

  [root@centos7 ~]# hostnamectl status
   static hostname: centos7.example.com
     icon name: computer-vm
     chassis: vm
    machine id: 8ef737feff1942798ec060bf5a162df6
     boot id: e2c8eded07f04e879d2b160d9a6bc6cf
   virtualization: vmware
   operating system: centos linux 7 (core)
    cpe os name: cpe:/o:centos:centos:7
     kernel: linux 3.10.0-327.el7.x86_64
    architecture: x86-64

  修改主机名的命令一般格式如下
  # hostnamectl [--static|--transient|--pretty] set-hostname <new_hostname>

  [root@centos7 ~]# hostnamectl --transient
  centos7.example.com

  # hostnamectl --transient set-hostname centos7-a //修改瞬态主机名
  # hostnamectl       //查看修改后的瞬态主机名
   static hostname: centos7.example.com
  transient hostname: centos7-a

  # hostnamectl --static  //查看修改前的静态主机名
  centos7.example.com   
  # hostnamectl --static set-hostname centos7-a.example.com //修改静态主机名
  # hostnamectl --static        //查看修改后的瞬态主机名
  centos7-a.example.com

  hostnamectl设置主机名后,会立即刷新内核主机名,以及更新/etc/hostname文件,
    但当前bash提示符需重新登陆shell

  [root@centos7 ~]# bash     //重新开启shell
  #

  /etc/hosts文件中主机名不会自动更新,应手动更新主机名与ip地址的映射关系

   vim /etc/hosts
  127.0.0.1  localhost localhost.localdomain localhost4 localhost4.localdomain4
  ::1  localhost localhost.localdomain localhost6 localhost6.localdomain6
  192.168.81.142 centos7-a.example.com centos7-a //添加此行,其中192.168.81.142是本机ip地址
  :wq    //保存退出 

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网