Linux-网络配置及网关

 

一、网络配置

1、IP地址的介绍

计算机要实现网络的通信,就必须要有一个用于快速定位的网络地址

IP地址就是计算机在网络中的唯一身份ID

IP地址的组成:IP地址=网络地址+主机地址

2、子网掩码

用来划分网络区域

子网掩码非0位置对应的ip上的数字表示这个ip的网络位

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

网络位表示网络区域

主机位表示网络区域内的某台主机

3、网络设定工具

ping         #监测网络是否畅通
ifconfig     #查看或者设定网络接口
ifconfig device(eth0网卡名称) ip/24         #设定
ifconfig device down     #关闭  
ifconfig device up       #开启

ip addr       #监测或者设定网络接口
ip addr show  #检测
ip addr add ip/24 dev device(eth0网卡名称)  #设定

#以上均为临时设定

注意:device名字是一个物理事实,你看到是什么名字就是什么名字

例:

4、图形设定方式设定IP

nm-connection-editor   #nm-networkManager  有可能不生效
systemctl stop NetworkManager    #记录网络服务状态
systemctl restart network

例:

5、命令的方式设定网络

nmcli device disconnect eth0      #关闭eth0网卡
nmcli device connect eth0         #开启eth0网卡
nmcli device show eth0            #查看网卡信息
nmcli device status eth0          #查看网卡服务接口信息


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 redhat(设定网卡名字) ifname eth0 ip4 172.25.254.100/24     # 命令设置网络

nmcli connection modify westos ipv4.method auto
nmcli cinnection modify westos ipv4.method manual
nmcli connection modify westos ipv4.addresses 172.25.254.200/24

例:

6、管理网络配置文件

/etc/sysconfig/network-scripts/      #网络配置目录

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

nmcli connection delete westos   #先删除,再添加(删除的是配置文件)
         

示例:

静态网络设定文件:

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

DEVICE=eth0
ONBOOT=yes
IPADDR=172.25.254.100
NETMASK=255.255.255.0
BOOTPROTO=none
NAME=westos

注:不要乱动物理机上的br0

systemctl restart network

一个网卡上配置多个IP

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

DEVICE=eth0
ONBOOT=yes
IPADDR0=172.25.254.100
NETMASK0=255.255.255.0
BOOTPROTO=none
NAME=westos
IPADDR1=172.25.0.100
PREFIX1=24

systemctl restart network    #重启网络服务

ip addr show rth0            #查看是否配置成功

二、网关

1、把真实主机变成路由器(路由器上配置的IP就是网关)

systemctl start firewalld       #先开启防火墙

firewall-cmd --list-all         #列出所有
  target: default
  icmp-block-inversion: no
  interfaces: 
  sources: 
  services: ssh dhcpv6-client
  ports: 
  protocols: 
  masquerade: yes               #地址伪装功能开启,真机主机变成路由器
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 

2、给虚拟机设定网卡

vim /etc/sysconfig/network
  GATEWAY=xxx.xxx.xxx.xxx(真机IP)   #写入到network文件中

vim etc/sysconfig/network-scripts/ifcfg-name
  GATEWAT0=xxx.xxx.xxx.xxx
  GATEWAT1=xxx.xxx.xxx.xxx      #网卡中有两个ip地址的时候
或
  GATEWAT=xxx.xxx.xxx.xxx       #网卡中只有一个IP地址的时候

systemctl restart network       #重启网络

route -n                        #查看网关

3、设定DNS

DNS: domin name server(域名解析服务  解析就是把域名变成IP)
vim /etc/resolv.conf         #DNS的指向文件
nameserver 114.114.114.114   #当需要某个域名的时候就去问114.114.114.114

vim /etc/sysconfig/network-scripts/ifcfg-name
DNS=114.114.114.114
DNS1=114.114.114.114

vim /etc/hosts        #本地解析文件

系统默认
/etc/hosts   > /etc/resolv.conf

vim /etc/nsseitch.conf
39 hosts:     dns file   #/etc/resolv.conf dns 执行优先
39 hosts:     file dns   #/etc/hosts 优先

4、dhcp服务的设置

vim /etc/hosts
172.25.254.250  content.example.com    

yum install dhcp -y      #下载dhcp服务
cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example  /etc/dhcp/dhcpd.conf
 vim /etc/dhcp/dhcpd.conf
  7 option domain-name "example.com";             #域名
  8 option domain-name-server 114.114.114.114;    #DNS
  
27 删除
28 删除

30 subnet  172.25.254.0  netmask 255.255.255.0 {    #子网设定
31 range 173.25.254.100  172.25.254.230;            #IP池(IP范围)
32 option routers 172.25.254.19;                   #网关
33  }
下面的内容全部删除

systemctl  start dhcpd               #开启dhcp服务

两台虚拟机:systemctl stop firewalld   #关闭两台虚拟机的防火墙

猜你喜欢

转载自blog.csdn.net/qq_40261882/article/details/86508117