Linux 系统下的网络配置

1.什么是IP ADDRESS
          internet protocol ADDRESS      ##网络进程地址
          
          ipv4:   internet protocol version 4
          1).2x32
          ip是由32个0,1组成
          11111110.11111110.11111110.11111110 = 254.254.254.254
 
2.子网掩码
          用来划分网络区域
          子网掩码非0的位对应的ip上的数字表示这个ip的网络位
          子网掩码0位对应的数字是ip的主机位
          网络位表示网络区域
          主机位表示网络区域里的某台主机
3.ip通信判定
          网络位一致,主机位不一致的2个IP可以直接通讯
          172.25.254.1/24       24=255.255.255.0
          172.25.254.2/24
          172.25.0.1/16  可以和上面两个通信,是那个面两个不能与它通信,存在包含关系

           *网络区域越大,主机越多,网络稳定性越差
         
4.网络设定工具
         1) ping ip               ##检测网络是否畅通


         2)ifconfig                               ##查看或设定网络接口   


          ifconfig eth0 ip/24            ##设定eth0网卡ip
          ifconfig                              ##查看网络设备信息


          ifconfig eth0 down          ##关闭eth0网卡
          ifconfig eth0 up               ##开启eth0网卡

       
         3) ip addr                                            ##检测或设定网络接口
         

          ip addr show                                  ##检测


          ip addr add ip/24 dev device       ##设定网络接口ip(此设定方法是临时设定)

重启虚拟机之后上述方法设定的ip就没有了


 
*注意:device的名字是一个物理事实,看到什么名字只能用什么名字

5.图形方式设定ip(永久设定)
1)nm-connection-editor


   systemctl stop NetworlManager
   systemctl restart network
   systemctl start NetworkManager

2)nmtui

6.命令方式设定网络
nmcli                     ##前提:NetworkManager必须开启
nmcli  device connect eth0          ##启用eth0网卡
nmcli  device disconnect eth0     ##关闭eth0网卡
nmcli  device show  eth0             ##查看eth0网卡信息
nmcli  device status eth0             ##查看eth0网卡服务接口信息

nmcli connection show                          ##查看接口信息
nmcli connection down eth0                 ##关闭eth0接口
nmcli connection up eth0                      ##开启eth0接口
nmcli connection delete eth0               ##删除eth0接口


nmcli connection add type ethernet con-name westos ifname eth0 ip4 172.25.254.214/24                                                                          ##添加接口名称为westos,设备名称为eth0,ip为172.25.254.214/24的接口


nmcli connection modify westos ipv4.method auto                      ##更改westos接口的ipv4网络模式为动态


nmcli connection modify westos ipv4.method manual                    ##更改westos接口的ipv4模式为静态


nmcli connection modify westos ipv4.addresses 172.25.254.254/24      ##更改westos接口的ip为172.25.254.254/24


7.管理网络配置文件
网络配置目录
/etc/sysconfig/network-scripts/
网络配置文件的命名规则
ifcfg-xxx                                  ##设备名称
DEVICE=xxx                         ##网卡名称
BOOTPRROTO=dhcp|static|none        ##设备工作方式
ONBOOT=yes                         ##网络服务开启时自动激活网卡
IPADDR=                                 ##ip地址
PREFIX=24 | NETMASK=255.255.255.0  ##子网掩码
NAME=                                    ##接口名称

例:
静态网络的设定文件: 
vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
ONBOOT=yes
IPADDR=172.25.254.114
NETMASK=255.255.255.0
BOOTPROTO=none
NAME=shou

systemctl restart network                       ##重启服务

一块网卡上配置多个IP:
vim /etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE=eth0
ONBOOT=yes
IPADDR0=172.25.254.114
NETMASK0=255.255.255.0
BOOTPROTO=none
NAME=hahaha
IPADDR1=172.25.0.114
NERMASK1=255.255.255.0

systemctl restart network                       ##重启服务

8.lo回环接口
回环接口相当于人的神经


9.网关
1)把真实主机变成路由器
systemctl stop libvirtd
systemctl restart firewalld
systemctl start libvirtd

firewall-cmd --list-all


firewall-cmd --permanent --add-masquerade
firewall-cmd --reload
firewall-cmd --list-all

public (default, active)
  interfaces: br0 enp0s25 wlp3s0
  sources:
  services: dhcpv6-client ssh
  ports:
  masquerade: yes        <<地址伪装功能开启,真机变为路由器>>
  forward-ports:
  icmp-blocks:
  rich rules:

2)设定虚拟机网关
i:   vim /etc/sysconfig/network         ##全局网关,针对所有没有设定网关的网卡生效
GATEWAY=ip                                   ##此ip为真实主机ip

           

systemctl restart network                ##重启服务

route -n                                              ##查看网关

ii:   vim /etc/sysconfig/network-scrpts/ifcfg-etho
GATEWAY=172.25.254.250             ##当网卡中设定的ip只有一个时

GATEWAY0=172.25.254.250            ##当网卡中设定的ip有多个时,指定对于那个ip生效
route -n                           ##查看网关

排错:真机火墙是否开启,虚拟机网卡是否损坏,内核是否让真机ip连通


10.设定dns
domain name server == 域名解析服务    ##解析就是把域名变成ip

1)vim /etc/hosts                                              ##本地解析文件
220.181.111.188     www.baidu.com

2)vim /etc/resolv.conf                                    ##dns的指向文件
nameserver 114.114.114.114                 ##当需要某个域名的IP地址时去问114.114.114.114


3)vim /etc/sysconfig/network-scripts/ifcfg-xxx
DNS=114.114.114.114

注意:
当网络工作模式为dhcp时,
系统会自动获得ip网关dns,
那么/etc/resolv.conf会被获得到的信息修改,
如果不需要获得dns信息,
在网卡配置文件中加入
PEERDNS=no

当网卡配置文件中没有PEERDNS=no时:

当在网卡配置文件中加入PEERDNS=no后:


11.设定解析的优先级
系统默认优先级:
/etc/hosts   >   /etc/resolv.conf

vim  /etc/hosts

 vim  /etc/resolv.conf

vim /etc/nsswitch.conf
39 hosts:     files dns               ##/etc/hosts 优先

vim /etc/nsswitch.conf
39 hosts:     dns files               ##/etc/resolv.conf dns指向优先

12.dhcpd 服务配置

服务器环境设置:
先设定一个静态ip
vim /etc/yum.repos.d/rhel_dvd.repo

 5   baseurl = http://172.25.254.250/rhel7                       ##修改第五行。此地址为网络yum源地址

yum clean all                              ##清空系统中原有的yum信息
yum repolist

yum intall dhcp -y


cp /usr/share/doc/dhcp*/dhcpd.conf.example /etc/dhcp/dhcpd.conf
vim /etc/dhcp/dhcpd.conf
8 option domain-name-servers 114.114.114.114;                 ##dns设定
27,28行都删除
30 subnet 172.25.254.0 netmask 255.255.255.0 {                ##子网设定
31   range 172.25.254.60 172.25.254.100;                            ##IP地址池设定
32   option routers 172.25.254.14;                                           ##网关设定  
33 }
33行以后都删除

systemctl restart dhcpd              ##重启服务
systemctl stop firewalld             ##关闭火墙

测试:
在网络工作模式是dhcp的主机中重启网络
systemctl restart network  

 
cat /var/log/messages
可以看到ip,GW,dns全部获取成功

猜你喜欢

转载自blog.csdn.net/qq657886445/article/details/81257071
今日推荐