linux下的网络配置

1.IP ADDEESS
    internet protocal address    ##网络进程地址
    ipv4     internet protocal version 4


(1)ip是由32个二进制01组成
    11111110.11111110.11111110.11111110 = 254.254.254.254
    
2.子网掩码
    用来划分网络区域
    子网掩码非0位对应ip上的数字表示这个ip的网络位
    子网掩码0位对应ip上的数字是ip的主机位
    网络位表示网络区域
    主机位表示网络区域里某台主机


3.ip通信判定
    网络位一致,主机位不一致的2个IP可以直接通信
172.25.254.1/24

后面的24表示子网掩码的前24位为24个二进制1,后8位为8个二进制0。即11111111.11111111.111111111.00000000 ,划算成十进制为255.255.255.0

172.25.254.2/24

172.25.254.1/24 和 172.25.254.2/24 可以直接进行通信


4.网络设定工具
    ping                                ##检测网络是否通常


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


    ifconfig device ip/24    ##临时设定


    ifconfig device up         ##开启


    ifconfig device down    ##关闭


    ip addr                           ##检测或设定网络接口
    ip addr add ip/24 dev device    ##临时添加IP
    
注意:device的名字是一个物理事实,看到什么名字只能用什么名字

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

选择add进行添加网络,选择edit进行编辑网络

填写Connection name,MAC地址

选择Method为Manual,选择add进行添加网络,分别填写Address,Netmask,Gateway,然后点击save


systemctl stop NetworkManager
systemctl restart network
systemctl start NetworkManager

(2)nmtui

使用nmtui命令可以进入以下界面,选择Edit a connetcion

选择Add或Edit

在这个界面可以对Profile name,IPv4 Addresses Gateway,DNS,进行相关的配置

选择OK退出

6.命令方式设定网络
nmcli        ##NetworkManager必须开启
nmcli device connect eth0    ##启用eth0网卡


nmcli device disconnect eth0    ##关闭eth0网卡


nmcli device show eth0        ##查看网卡信息  

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

nmcli connection show        ##显示网卡服务接口信息


nmcli connection down westos    ##关闭westos接口


nmcli connection up westos    ##开启westos接口


nmcli connection delete westos    ##删除westos接口


nmcli connection add type ethernet con-name westos ifname eth0 ip4 172.25.254.105/24            ##设置westos接口


nmcli connection modify westos ipv4.method auto          ##通过DHCP自动获取IP
nmcli connection modify westos ipv4.method manual     ##手动设置IP
nmcli connection modify westos ipv4.addresses 172.25.254.205/24     ##添加westos的IP地址为172.25.254.205/24


7.管理网络配置文件
网络配置文件

网络配置文件的命名规则
ifcfg=xxxx
DEVICE=xxxx            ##设备名称
BOOTPROTO=dhcp|static|none    ##设备工作方式
ONBOOT=yes            ##网络服务开启时自动激活网卡
IPADDR=                ##IP地址
PREFIX=24            ##子网掩码
NETMASK=255.255.255.0        ##子网掩码
NAME=                ##网络接口名称

示例:

vim /etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE=eth0                     
BOOTPROTO=none            
ONBOOT=yes                      
IPADDR=172.25.254.105           
PREFIX=24                       
NAME=eth0

vim /etc/sysconfig/network-scripts/ifcfg-eth0

systemctl restart network
ip addr show eth0

一块网卡上配置多个IP

vim /etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE=eth0                     
BOOTPROTO=none                  
ONBOOT=yes                      
IPADDR0=172.25.254.105          
PREFIX0=24                       
IPADDR1=172.25.0.105
PREFIX1=24
NAME=eth0

systemctl restart network
ip addr show eth0

8.lo回环接口
回环接口-----人的神经-----127.0.0.1-----localhos

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

fierwall-cmd --list-all


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

c (active)
  target: default
  icmp-block-inversion: no
  interfaces: br0 enp2s0f1 wlp3s0
  sources:
  services: dhcpv6-client ssh
  ports:
  protocols:
  masquerade: yes        ##地址伪装功能开启,真实主机变成路由器
  forward-ports:
  sourceports:
  icmp-blocks:
  rich rules:

(2)设定虚拟机网关

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

vim /etc/sysconfig/network-scripts/ifcfg-eth0
GATEWAY0=172.25.254.5        ##当网卡中设定有多个IP时,指定对于那个IP生效
GATEWAY=172.25.254.5        ##当网卡中设定的IP只有一个时

route -n        ##查看网关

nation     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.3.1     0.0.0.0         UG    100    0        0 enp2s0f1
10.42.0.0       0.0.0.0         255.255.255.0   U     600    0        0 wlp3s0
172.25.61.0     0.0.0.0         255.255.255.0   U     425    0        0 br0
172.25.254.0    0.0.0.0         255.255.255.0   U     425    0        0 br0
192.168.3.0     0.0.0.0         255.255.255.0   U     100    0        0 enp2s0f1
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

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

vim /etc/hosts        ##本地解析文件
ip    域名
220.181.111.188    www.baidu.com

vim /etc/resolv.conf

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

vim /etc/sysconfig/network-scripts/ifcfg-eth0
DNS1=114.114.114.114


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

vim /etc/nsswitch.conf
hosts: files dns    ##/etc/hosts优先
systemctl restart network

12.dhcp服务配置

vim /etc/sysconfig/network-scripts/ifcfg-eth0
systemctl restart network

 

vim /etc/yum.repos.d/rhel_dvd.repoi

baseurl = http://172.25.254.250/rhel7

yum clean all
yum repolist

yum install dhcp -y

cp /usr/share/doc/dhcp*/dhcpd.conf.example /etc/dhcp/dhcpd.conf

systemctl restart dhcpd
systemctl stop firewalld

vim /etc/dhcp/dhcpd.conf

option domain-name "westos.com";
option domain-name-servers 114.114.114.114;

删除27,28行
subnet 172.25.254.0 netmask 255.255.255.0 {
  range 172.25.254.205 172.25.254.206;
  option routers 172.25.254.105;


删除35行以后


测试:
在网络工作模式是dhcp的主机中重启network,可以看到自动获取到IP地址172.25.254.205

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值