####linux下的网络配置####
##1.什么是IP ADDRESS
internet protocol ADDRESS ##网络协议地址
ipv4 internet protocol version 4
1.2x32
ip是由32个0和1组成
11111110.11111110.11111110.11111110 = 254.254.254.254
##2.子网掩码
用来划分网络区域
子网掩码非0的位对应的ip上的数字表示这个ip的网络位
子网掩码0位对应的数字是ip的主机位
网络位表示网络区域
主机位表示网络区域里的某台主机
##3.ip通信判定
网络位一致,主机位不一致的两个IP可以直接通信
172.25.254.1/24 24=255.255.255.0
172.25.254.2/24
172.25.0.1/16
##4.网络设定工具
ping ##检测网络是否通畅
举例:
ping 172.25.78.11 (检查是否可以连通)
ifconfig ##查看或设定网络接口
ifconfig device ip/24 ##设定网络 (临时设定IP,再重启网络服务systemctl restart network,恢复到之前的ip)
举例:ifconfig eth0 172.25.78.100/24 (将ip改为172.25.78.100/24 的C类网段)
ifconfig (查看网络接口,将ip更改为172.25.78.100,子网掩码为255.255.255.0)
systemctl restart network (重启网络服务,ip恢复为之前的ip地址)
ifconfig device down ##关闭
ifconfig device up ##开启
##5.图形方式设定ip
1.nm-connection-editor图形方式设定ip
2.nmtui文本方式设置ip
##6.命令方式设定网络
nmcli
nmcli device connect eth0 ##启用eth0网卡
nmcli device disconnect eth0 ##关闭eth0网卡
nmcli device show eth0 ##查看网卡信息
nmcli device status eth0 ##查看网卡服务接口信息
nmcli connection show (显示所有网络链接)
nmcli connection down westos (关闭指定链接)
nmcli connection up westos (打开指定连接)
nmcli connection delete westos (删除westos链接)
nmcli connection add type ethernet con-name westos ifname eth0 ip4 172.25.254.100/24(添加静态网络)(ifname 添加网卡eth0,重启systemctl restart network生效)
nmcli connection modify “westos” ipv4.method auto (改变westos的工作方式为动态)
nmcli connection modify “westos” ipv4.method manual (改变westos的工作方式为静态)
nmcli connection modify “westos” ipv4.addresses 172.25.254.150/24 (改变wetos的ip)(重启systemctl restart network ,ip地址改变为所设定的172.25.254.150/24 )
注:nm-connection-editor (修改connection name , 从eth0变为westos,可以用nmcli connection show 查看connection name)
####7.管理网络配置文件
网络配置目录
/etc/sysconfig/network-scripts (cd /etc/sysconfig/network-scripts)
网络配置文件的命名规则
ifcfg-xxx
DEVICE=xxx ##设备名称
BOOTPROTO=dhcp|static|none(后两个是一个意思) ##设备的工作方式
ONBOOT=yes ##网络服务开启时自动激活网卡
IPADDR= ##IP地址
PREFIX=24 ##子网掩码
NETMASK=255.255.255.0 ##子网掩码
示例:
静态网络设定文件
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
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 eth0
####8.lo回环接口####
(内部网络传输信息的接口)
####9.网关####
1.把真实主机变成路由器
firewall-cmd --list-all
firewall-cmd --permanent --add-masquerade
firewall-cmd --reload
2.设定虚拟机网关
vim /etc/sysconfig/network
GATEWAY=172.25.254.250
(GATWAY要和真机大(第三个是255的)ip一样,虚拟机ip要和真机ip在一个网段)
####10.设定dns####
doamin name system
vim /etc/hosts ##本地解析文件
ip 域名
61.135.169.121 www.baidu.com
vim /etc/resolv.conf ##dns指向文件
nameserver 114.114.114.114(114.114.114.114相当于一个域名解析的数据库,当本地解析文件没有时,系统自动到114.114.114.114查看域名解析)(没有添加指向性文件中的114.114.114.144时,域名解析只有本地的域名解析,打开的网页只能是和百度的ip同网段的地址,其他的不同网段网址不能打开,所以,在添加完指向性文件中的114.114.114.114可以打开全部的不同ip的网址)
vim /etc/sysconfig/network-scripts/ifcfg-eth0(和上一个命令一个功能)
DNS1=114.114.114.114(这里DNS后根的数字必须从1开始)
##11.设定解析优先级
系统默认 (在查看优先级时,要把www.baidu.com本地解析的对应换成和真机一个大网段(第三位是255)的ip相同的网段)
/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优先
####12.dhcp服务配置
在服务端:
yum install -y dhcp
cp /usr/share/doc/dhcp*/dhcpd.conf.example /etc/dhcp/dhcpd.conf
vim /etc/dhcp/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 172.25.254.250;
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.150 172.25.254.200;
option routers 172.25.254.250;
}
systemctl start dhcpd
可以在服务端和客户端分别看到ip分配记录
服务端:/var/lib/dhcpd/dhcpd.leases
客户端:/var/log/messages
测试:
在网络工作模式为dhcp的主机中重启网络
可以看到
IP
网关
DNS
全部获取成功