十一.Linux中的网络配置

Table of Contents

一.(一).什么是IP ADDRESS

(二).子网掩码

二.ip通信判定

三.网络设定工具(临时性的)

四.图形方式设定ip

五.命令方式设定网络

六.管理网络配置文件

八.网关

九.设定dns

十.设定解析的优先级

十一.dhcp服务配置


一.IP

(一)什么是IP ADDRESS

    internet protocol ADDRESS    网络进程地址   
    ipv4    internet protocol version 4
    1.2x32
    ip是由32个01组成
    11111110.11111110.11111110.11111110 = 254.254.254.254

(二)子网掩码

     用来划分网络区域     172.25.254.10/24    24=255.255.255.0
    子网掩码非0的位对应的ip上的数字表示这个ip的网络位

     255.255.255                172.25.254
    子网掩码0位对应的数字是ip的主机位

      0                                   10
    网络位表示网络区域
    主机位表示网络区域里某台主机

二.ip通信判定

   网络位一致,主机位不一致的2个IP可以直接通讯
    172.25.254.1/24    24=255.255.255.0
    172.25.254.2/24
    172.25.0.1/16         16=255.255.0.0

三.网络设定工具(临时性的)

(一).ping         检测网络是否通畅
    ping -c 1    ping 1 次
    ping -w 1    等待1秒
    ping -c1 -w1 ip    ping1次等待1秒

 (二)ifconfig    查看或设定网络接口(只能临时性的添加一个ip)
    ifconfig    查看


    ifconfig device ip/24    设定 


    ifconfig device down    关闭


    ifconfig device up    开启  

    (三)ip addr        检测或这顶网络接口(临时性的添加多个IP)
    ip addr show    检测


    ip addr add ip/24 dev device    设定    如: ip addr add  dev ens160 172.25.254.2/24

   
    ip addr del dev ens160 ip/24    删除    

    ip addr flush ens160        更改ip步骤(刷新后临时IP都没有了)

"注意:device的名字一个物理事实,看到什么名字只能用什么名字,设备名如lo, ens160,virbr0,virbr0-nic"

四.图形方式设定ip

(一).nm-connection-editor

更改:

(不建议用服务控制网络systemctl restart NetworkManager)

设定完后,建议使用,使其生效:
nmcli connection show
nmcli connection down westos
nmcli connection up westos

(二).nmtui


    

五.命令方式设定网络

nmcli            NetworkManager必须开启

(一)设备方面

nmcli device connect ens160    启用ens160网卡
nmcli device disconnect ens160    关闭ens160网卡


nmcli device show ens160    查看网卡信息


nmcli device status ens160    查看网卡服务接口信息

(二)连接方面

1.nmcli connection show        查看连接 


nmcli connection down westos    关闭连接
nmcli connection up westos    开启连接


nmcli connection delete westos    删除连接

2.添加连接
nmcli connection add type ethernet con-name westos ifname ens160 ip4 172.25.254.100/24

直接修改文件,在同一个设备上(ens160)重新建立网络设定窗口

3.更改
nmcli connection modify westos  ipv4.addresses 172.25.254.200/24

六.管理网络配置文件

网络配置目录
/etc/sysconfig/network-scripts/
网络配置文件的名命规则    ifcfg-xxxx 文件名
BOOTPROTO=dhcp|static|none       设备工作方式  (动态\静态\无)
ONBOOT=yes                                   网络服务开启时自动激活网卡
DEVICE=xxx                                      设备名称
NAME=                                              接口名称(自定义)

IPADDR=                                           IP地址
PREFIX=24                                        子网掩码
NETMASK=255.255.255.0                子网掩码

(一)dhcp网络设定

vim /etc/sysconfig/network-scripts/ifcfg-lee
DEVICE=ens160
ONBOOT=yes
BOOTPROTO=dhcp

nmcli connection reload ens160(设备名)  +nmcli connection up ens160(连接名)
(二)静态网络设定文件
vim /etc/sysconfig/network-scripts/ifcfg-ens160
DEVICE=ens160
ONBOOT=yes
IPADDR=172.25.254.100
NETMASK=255.255.255.0
BOOTPROTO=none
NAME=westos

nmcli connection reload ens160(设备名)  +nmcli connection up ens160(连接名)

(三)一块网卡上配置多个IP
vim /etc/sysconfig/network-scripts/ifcfg-ens160
DEVICE=ens160
ONBOOT=yes
IPADDR0=172.25.254.100
NETMASK0=255.255.255.0
BOOTPROTO=none
NAME=westos
IPADDR1=172.25.0.100
PREFIX1=24

nmcli connection reload ens160(设备名)  +nmcli connection up ens160(连接名)

七.lo回环接口
回环接口------人的神经----127.0.0.1-----localhost

八.网关

(一).把node2/test2双网卡虚拟机变成路由器

1.添加双网卡

ping 172.25.254.2真机,检查是否通信

2.双网卡变成路由器,地址伪装功能开启

systemctl start firewalld 
firewall-cmd --list 
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:


(二).设定单网卡虚拟机node1/test1的网关

单网卡IP 1.1.1.102,设定完后,ping 1.1.1.202双网卡node2,检查网络通信

1.临时性添加网关 ip route add default via 1.1.1.202

2.vim /etc/sysconfig/network    全局网关,针对所有没有设定网关的网卡生效
GATEWAY=1.1.1.202

        

3.vim /etc/sysconfig/network-scripts/ifcfg-enp1s0  (局部加网关,会覆盖掉全局的网关)网卡网关优先级大于全局网关
GATEWAY0=1.1.1.202        当网卡中设定的IP有多个时,指定对于哪个IP生效
GATEWAY=1.1.1.202        当网卡中设定的IP只有一个时

route -n         查看网关

实现node1虚拟机1.1.1.102通过node2虚拟机1.1.1.202/172.25.254.202地址伪装,访问真机172.25.254.2

九.设定dns

地址解析:系统操作者对字符敏感,系统网络通信需要通过ip地址这个数字,当操作者输入网址www.baidu.com,这个网址不是一个可以通信的IP地址,于是必须要在系统中把www.baidu.com变成百度服务器的IP地址,这样的过程叫做地址解析.
domain name server   域名解析服务    解析就是把域名变成IP

(一)vim /etc/hosts            

本地解析文件,此文件中直接提供地址解析
ip    域名

(二)vim /etc/resolv.conf        

dns的指向文件    不需要重新启动网络立即生效

nameserver 114.114.114.114    当需要某个域名的IP地址时去问114.114.114.114

 

(三)vim /etc/sysconfig/network-scripts/ifcfg-xxxx
DNS1=114.114.114.114 (最多有三个dns)       需要重启网络,当网络重新启动,此参数会修改/etc/resolv.conf

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

当网络配置文件内容单词忘记时,可以看下 cat /usr/share/doc/initscripts/sysconfig.txt

十.设定解析的优先级

系统默认:
/etc/hosts   >   /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指向优先

十一.dhcp服务配置

(一)在node2中建立动态IP获取

(二)在node1中开启dhcp服务

cp /usr/share/doc/dhcp.service/dhcpd.conf.example /etc/dhcp/dhcpd.conf    用模板生成配置文件
vim /etc/dhcp/dhcpd.conf

systemctl stop firewalld 关闭火墙

# dhcpd.conf
# #
# Sample configuration file for ISC dhcpd
#

# option definitions common to all supported networks...
option domain-name "westos.com";        ##域名
option domain-name-servers 114.114.114.114;    ##dns

default-lease-time 600;                ##默认租约
max-lease-time 7200;                ##最长租约

# Use this to enble / disable dynamic dns updates globally.
#ddns-update-style none;

# If this DHCP server is the official DHCP server for the local
# network, the authoritative directive should be uncommented.
#authoritative;

#Use this to send dhcp log messages to a different log file (you also
# have to hack syslog.conf to complete the redirection).
log-facility local7;

# No service will be given on this subnet, but declaring it helps the 
# DHCP server to understand the network topology.
27    删除
28    删除

# This is a very basic subnet declaration.

subnet 172.25.254.0 netmask 255.255.255.0 {        ##子网设定
  range 172.25.254.210 172.25.254.230;            ##IP地址池
    option routers 172.25.254.102;            ##网关
    }

35行后面全删除

也可以在dhcp主机node1中cat /var/log/messages 中可以看到ip,gw,dns获取

172.25.254.210则由node1分配给node2的地址

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值