linux下的网络配置
1、什么是IP ADDRESS
internet protocol ADDRESS 网络进程地址
ipv4 interent protocol version 4
1.2x32
ip是由32个01组成
11111110.11111110.11111110.11111110=254.254.254.254
2、子网掩码
用来划分网络区域
子网掩码非零位对应的ip上的数字表示这个ip的网络位
子网掩码零位对应的数字是ip的主机位
网络位表示网络区域
主机位表示网络区域里的某台主机
3、ip通信判定
网络位一致,主机位不一致的两个IP可以直接通讯
172.25.254.1/24 24=255.255.255.0
4、网络设定工具
ping 检验网络是否通畅
ifconfig 查看或设定网络接口
ifconfig 查看
ifconfig device ip/24 设定
ip addr 检测或置顶网络接口
ip addr show 检测
ip addr add ip/24 dev device 设定
!!注意:device的名字是一个物理事实,看到什么名字就只能用什么名字。!!
5、图形方式设定ip
1.nm-connection-editor
systemctl stop NetworkManager
systemctl restart net
2.nmtui
6、命令方式设定网络
nmcli NetworkManager必须开启
nmcli device connect connect 启用eth0网卡
nmcli device connect disconnect eth0 关闭eth0网卡
nmcli device show eth0 查看网卡信息
nmcli device status eth0 查看网卡服务
7、管理网络配置文件
网络配置服务
/etc/sysconfig/network-scripts/
网络配置文件的命名规则
ifcfg-xxx(随意)
DEVICE=XXX 设备名称
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
ONBOOT=yes
IPADDR=172.25.254.100
NETMASK=255.255.255.0
BOOTPROTO=NONE
NAME=westos
ystemctl restart network
一块网卡上配置多个IP
vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
ONBOOT=yes
IPADDR0=172.25.254.xxx
NETMASK0=255.255.255.0
BOOTPROTO=none
NAME=westos
IPADDR1=172.25.254.XXX
PREFIX1=24
systemctl restart network
ip addr show eth0
8、lo回环接口
回环接口相当于人的神经
9、网关
1)把真实主机变成路由器
systemctl stop libvirtd
systemctl restart firewalld
systemctl start libvirtd
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:
2)设定虚拟机网关
vim /etc/sysconfig/network 全局网关,针对所有没有设定网关的网卡生效
GATEWAY=172.25.254.真机ID
vim /etc/sysconfig/network-scripts/ifcfg-eth0
GATEWAY0=172.25.254.真机ID 当网卡中设定的IP有多个时,指定对那个IP生效
GETEWAY=172.25.254.真机ID 当网卡中设定的IP只有一个时
route -n 查看网关
##可能出现的问题:
(1)网卡内核没有开启:
sysctl -a | grep ip_forward
net.ipv4.ip_forward = 0 ##证明没有开启##
vim /etc/sysctl.conf
net.ipv4.ip_forward=1 ##在最后一行添加##
sysctl -p ##开启配置##
sysctl -a | grep ip_forward
net.ipv4.ip_forward = 1 ##即为开启##
(2)网卡出现问题
virt-manager ##在真机上输入##
双击出问题的虚拟机
ifconfig eth0 up
点小灯泡后,删除NIC :00:00:0a
在Network下新建一个网卡,选中virtio
finish完成
10、设定dns
domain name server == 域名解析服务 解析就是把域名变成IP
vim /etc/hosts 本地解析文件
ip 域名
220.181.111.188 www.baidu.com
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
注意:
当网络工作模式为dhcp时
系统会自动获得ip 网关 dns
那么 /etc/resolv.conf会被获得到的信息修改
如果不需要获得dns信息
在网卡配置文件中加入 PREEDNS=no
11、设定解析的优先级
系统默认:
/etc/hosts > /etc/resolv.conf
vim /etc/nsswitch.conf
39 hosts: file dns ##/etc/hosts/优先
vim /etc/nsswitch.conf
39 hosts: dns file ##/etc/resolv.conf 优先
12、dhcp服务配置
vim /etc/dhcpd/dhcpd.conf
1 # dhcpd.conf
2 #
3 # Sample configuration file for ISC dhcpd
4 #
5
6 # option definitions common to all supported networks...
7 option domain-name "westos.com";
8 option domain-name-servers 114.114.11.114 ;
9
10 default-lease-time 600;
11 max-lease-time 7200;
12
13 # Use this to enble / disable dynamic dns updates globally.
14 #ddns-update-style none;
15
16 # If this DHCP server is the official DHCP server for the local
17 # network, the authoritative directive should be uncommented.
18 #authoritative;
19
20 # Use this to send dhcp log messages to a different log file (you also
21 # have to hack syslog.conf to complete the redirection).
22 log-facility local7;
23
24 # No service will be given on this subnet, but declaring it helps the
25 # DHCP server to understand the network topology.
26
27
28 # This is a very basic subnet declaration.
29
30 subnet 172.25.254.0 netmask 255.255.255.0 { 子网设定
31 range 172.25.254.160 172.25.254.200; IP地址池
32 option routers 172.25.254.71; 网关 (我自己电脑的ip就好似172.25.254.71)
33 }
删除35行以后
删除27、28行
注意:设定好以后一定要重新启动dhcp(自己一直在另一台i虚拟机上获取不到者太虚拟机分配的IP,是因为我没有重新启动 systemctl restart dhcp)
设定好以后在另一台虚拟机上打开: vim /etc/syscomfig/network-scripts/ifcfg-eth0
systemctl restart network
打开以后修改BOOTPROTO=dhcp 查看ifconfig (查看另一台虚拟机的ip 看看是不是在160---200之间的ip)