11.Linux中的网络配置
- IP ADDRESS
—internet protocol ADDRESS
ipv4 ##网络进程地址
—internet protocol version 4
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
#网络设定工具
ping 检测网络是否通畅
ping -c1 ping1次
ping -w1 等待一次
ifconfig 查看,设定网络接口
ifconfig device ip/24 设定
ifconfig device down 关闭
up 开启
ip addr 检测或设定网络接口
ip addr show 检测
ip addr add ip/24 dev device 设定
ip addr del ip/24 dev device 删除
##图形方式设定ip
1.nm-connection-editor
更改
nmcli connection show 展示
nmcli connection down Ethernet connection 1/nmcli connection reload 重新启动
nmcli connection up Ethernet connection 1 连接
2.nmtui
1.init 3
2.nmtui
3.
4.
5.
- init 5
##命令方式设定网络
nmcli NetworkManager必须开启
nmcli device connect ens3 启用ens3网卡
nmcli device disconnect ens3 关闭ens3网卡
nmcli device show ens3 查看网卡信息
nmcli device status ens3 查看网卡服务接口信息
nmcli connection show 查看连接
nmcli connection down ens3 关闭
nmcli connection up ens3 开启
nmcli connection del ens3 删除
添加连接
nmcli connection add type ethernet con-name westos ifname ens3 ipv4.method manual ipv4.addresses 172.18.0.102/24
更改
nmcli connection modify westos ipv4.addresses 172.25.254.126/24
##管理网络配置文件
网络配置目录:
/etc/sysconfig/network-scripts
网络配置文件的命名规则
vim ifcfg-ens3(网卡名称)
DEVICE=ens3
ONBOOT=yes
IPADDR=172.18.0.102
PREFIX=24或NETMASK=255.255.255.0
BOOTPROTO=none
NAME=lcf
nmcli conncetion reload
若要多弄几个IP
只需在IPADDR与PREFIX GATEWAY 前加 0 1 2的序号
##lo回环接口
回环接口–127.0.0.1–localhost
好处:速度快 安全
##网关
#设定单网卡主机的网关
vim /etc/sysconfig/network
全局网关,针对所有有或没有设定的网卡生效
GATEWAY=172.25.254.250
vim /etc/sysconfig/network-scripts/ifcfg-ens3
GATEWAY=172.25.254.250 (多个ip时加0 1 2)
route -n 查看网关
设定dns:
vim /etc/hosts 本地解析
14.215.177.38 www.baidu.com
vim /etc/resolv.conf dns指向文件 不需要重新启动网络立即生效
nameserver 114.114.114.114 ==>当需要某个域名的IP地址去询问114.114.114.114
vim /etc/sysconfig/network-scripts/ifcfg-ens3
优先级:
/etc/hosts > /etc/resolv.conf
##dhcp 服务配置
rpm -ivh dhcp-server
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 **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.
2728行 删除
·# This is a very basic subnet declaration.
subnet **172.25.254.0** netmask **255.255.255.0** {
range **172.25.254.70 172.25.254.90**;
option routers **172.25.254.250**;
}
35行以后的所有全部删除
systemctl restart dhcpd
systemctl stop firewalld
测试:
在网络工作模式是dhcp的主机中重启网络
可以看到 ip GW dns 全部获取成功
**
init 3 关闭图形
init 5 打开图形