########Linux下的网络配置####
##1.什么是IP ADDRESS##
internet protocol ADDRESS ##网络进程地址
ipv4 internet protocol version4
1. 2x32
ip是由32个01组成
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.网络设定工具##
ping ##检测网络是否通畅
ifconfig ##查看或设定网络接口
ifconfig ##查看
ifconfig device ip/24 ##设定
ifconfig device down ##关闭
ifconfig device up ##开启
ip addr ##检测或设定网络接口
ip addr show ##检测
ip addr add ip/24 dev device ##设定
注意: ”device的名字一个物理事实,看到什么名字只能用什么名字“
##5.图形方式设定ip##
1. nm-connection-editor
systemctl stop NetworkManager
systemctl restart network
systemctl start NetworkManager
2. nmtui设定ip
在命令中输入nmtui
进入后出现如下界面点击ok进入
然后删除原有的ip点击添加add,进入到以下这个界面选择etherner
然后在如下图中添加名字和ip,然后关闭保存即可。
##6.命令方式设定网络
nmcli ##NetworkManager必须开启
nmcli device connect eth0 ##启用eth0网卡
nmcli device disconnect eth0 ##关闭eth0网卡
nmcli device show eth0 ##查看网卡信息
如图 查看eth0 网卡信息:
nmcli device status eth0 ##查看网卡服务接口信息
nmcli connection show
nmcli connection down westos
nmcli connection up westos
nmcli connection delete westos
nmcli connection add type ethernet con-name westos ifname eth0 ip4 172.25.254.147/24
nmcli connection modify westos ipv4.method --help
nmcli connection modify westos ipv4.method auto
nmcli connection modify westos ipv4.method manual
nmcli connection modify westos ipv4.addresses 172.25.254.247/24
###7.管理网络配置文件
网络配置目录
/etc/sysconfig/network-scripts/
网络配置文件的命名规则
ifcfg-xxxx (网卡名称xxxx)
DEVICE=xxxx ##设备名称
BOOTPROTO=dhcp|static|none ##设备工作方式
ONBOOT=yes ##网络服务开启时自动激活网卡
TPADDR= ##IP地址
PREFIX=24 ##子网掩码
NETMASK=255.255.255.0##子网掩码
NAME= ##接口名称
示例:
静态网络设定文件
vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=none
IPADDR0=172.25.254.147
NAME=westos
NETMASK1=255.255.255.0
systemctl restart network
效果显示如下图:
一块网卡上配置多个IP
vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=none
IPADDR0=172.25.254.247
PREFIX0=24
NAME=westos
IPADDR1=172.25.0.247
NETMASK1=255.255.255.0
进入到/etc/sysconfig/network-scripts/ifcfg-eth0中更改上述内容的效果如图:
systemctl restart network
如果要查询就需要用命令 ip addr show eth0查看出 172.25.0.247
剩下的就要用ifconfigchakan
用ip addr show eth0查看如图:
###8.lo回环接口
回环接口-------人的神经---127.0.0.1---localhost
##9.网关
1.把真实主机变成路由器
systemctl stop libvirtd
systemctl restart firewalld
systemctl start libvirtd
firewall-cmd --list
firewall-cmd --permanent --add-masque
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:
操作如图所示:出现masquerade:yes 表明已经将真实主机变为了路由器。
2.设定虚拟机网关
vim /etc/sysconfig/network ##全局网关,针对所有没有设定网关的网卡生效
改动/etc/sysconfig/network 添加GATEWAY=172.25.254.47
如图所示:
查看设定虚拟机网关的效果如图:
vim /etc/sysconfig/network-scripts/ifcfg-eth0
GATEWAY0=172.25.254.47 ##当网卡中设定的ip有多个时,指定对于那个ip生效
GATEWAY=172.25.254.47 ##当网卡中设定的ip只有一个时
route -n ##查看网关
#10.设定dns
domain name server ==域名解析服务 ##解析就是把域名变成ip
1.vim /etc/hosts ##本地解析文件
ip 域名
如 183.232.231.172 www.baidu.com
进入到/etc/hosts后添加 百度的ip
效果如图:表明虚拟机可以上网查看百度
vim /etc/resolv.conf ##dns的指向文件
nameserver 114.114.114.114 ##当需要某个域名的ip地址时去问114.114.114.114
首先进入/etc/resolv.conf 进行修改,改动如图:
经过此改动后在虚拟机中访问网页时,可以和真机一样正常访问。
vim /etc/sysconfig/network-scripts/ifcfg-eth0
DNS1=114.114.114.114
注意;
当网络工作模式为dhcp时 系统会自动获得ip网关dns
那么/etc/resolv.conf会被获得到的信息修改
如果不需要获得dns信息
在王卡配置文件中加入PREEDNS=no
###11.设定解析的优先级
系统默认:
/etc/hosts > /etc/resolv.conf
vim /etc/nsswitch.conf
39 hosts: files dns ###/etc/hosts优先
如图所标示:当hosts: files dns 这行这样写的话则标示 /etc/hosts优先
39 host: dns files ###/etc/resolv.conf dns 指向优先
如图所标示:当hosts: files dns 这行这样写的话则标示etc/resolv.conf dns优先
###12.dhcp服务配置
在真实主机中执行yum install dhcp -y,安装完成后,
在进行切换cp /usr/share/doc/dhcp*/dhcpd.conf.example /etc/dhcp/dhcpd.conf
在进入到vim dhcpd.conf中,修改文件内容为以下:
# 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;
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.
# This is a very basic subnet declaration.
subnet 172.25.254.0 netmask 255.255.255.0 {
range 172.25.254.60 172.25.254.70;
option routers 172.25.254.47;
}
systemctl restart dhcpd
systemctl stop firewalld
测试:
在网络工作模式时dhcp的主机中重启网络
可以看到
ip
GW
dns
全部获取成功
在虚拟机上进行时。先进入/etc/sysconfig/network-scripts/ifcfg-eth0 改成BOOTPROTO=dhcp。然后操作如下图: