Linux下的网络配置

实验环境:
真机下切换到root用户
westos-network bridge
virt-manager
创建两台虚拟机westosa westosb

1.什么是IP ADDERSS

internet protocol ADDRESS #网络进程地址
ipv4 internet protocol version 4
1. 2X32
ip是由32个01组成

11111110.11111110.11111110.11111110=254.254.254.254
   主机位不同,网络位相同的两个ip可以进行通信
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 -c 1 ip	##ping 1 次
ping -w 1 ip	##等待 1 秒
ping -c1 -w1 ip	##ping 1次等待1秒

ifconfig	#查看或设定网络接口
ifconfig	#查看
ifconfig device ip/24	#设定
ifconfig device down	#关闭
ifconfig device up	#开启

“注意:device的名字是一个物理事实,看到什么名字只能使用什么名字”

##临时设定,未记录在原文件里,重启后还原

ip addr (ip a)      ##检测或设定网络接口
ip addr show         ##检测
ip addr add ip/24 dev device   #设定
ip addr del dev ens3 ip/24     #删除
#这里的设定指添加,旧的需要手动删除

例:

 ip addr del 172.25.254.189/24 dev ens3
 ip addr add 172.25.254.109/24 dev ens3
5.图形方式设定ip

设定:
1.nm-connection-editor
未删原有ip时,ifconfig查看并不会改变
此时需要 nmcli connection reload #刷新配置
nmcli connection up ens3 #读取配置文件

建议使用:

nmcli connection show
nmcli connection down westos(自定的连接名称)
nmcli connection up westos

2.nmtui #非图形界面设定ip
切换用init3(临时)
(永久)设定非图形 system set-default multi-user.target
最后一步:clear 退出编辑
切回图形界面用init5

6.命令方式设定网络

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 westos	##开启连接
nmcli connection up westos	##开启连接
nmcli connection delete westos ##删除连接

#添加连接:

nmcli connection add type ethernet ifname ens3 con-name westos ipv4.method manual ipv4.addresses 172.25.254.100/24

#更改

nmcli connection modify westos ipv4.addresses 172.25.254.200/24

#自动获取默认设置

nmcli connection modify westos ipv4.method auto
7.管理网络配置文件

网络配置目录 cd /etc/sysconfig/network-scripts/
网络配置文件的命名规则
vim ifcfg-ens3

DEVICE=ens3                  #设备名称
BOOTPROTO=dhcp|static|none   #设备工作方式
ONBOOT=yes		             #网络服务开启时自动激活网卡
IPADDR=172.25.254.100	#IP地址
NETMASK=255.255.255.0   #子网掩码
NAME=westos             #接口名称

dhcp网络设定
vim /etc/sysconfig/network-scripts/ifconfig-ens60

DIVICE=ens160
ONBOOT=yes
BOOTPROTO=dhcp

systemctl restart network

示例:一块网卡上配置多个IP
vim /etc/sysconfig/network-scripts/ifconfig-ens60

DIVICE=ens160
ONBOOT=yes
BOOTPROTO=none
IPADDR0=172.25.254.100	
NETMASK0=255.255.255.0
NAME=westos
IPADDR1=172.25.254.101
PREFIX1=24

systemctl restart network

8.网关

不同网段需要通讯需要路由器,路由器里内核含两个ip地址分别对应两个网段,做地址伪装网关,路由器里和来源ip处于同一网段的ip地址

vim /etc/sysconfig/network		#全局网关,针对所有没有设定网关的网卡生效
GATEWAY=172.25.254.250			##当网卡中设定的IP只有一个时

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

GATEWAY0=172.25.254.20			#当网卡中设定的ip有多个时,指定对于哪个ip生效
GATEWAY=172.25.254.20          #网卡中只设定一个ip

ip route add default via 172.25.254.250   #设置网关
ip route del default via 172.25.254.250	#删除网关

1.临时网关

route -n
ip route add default via 172.25.254.250
route -n
nmcli connection reload
nmcli connection up westos
route -n
ping 14.215.177.39

2.全局网关
##vim /etc/sysconfig/network

vim /etc/sysconfig/network
GATEWAY=172.25.254.250(如果要连接的IPADDR有数字,则加对应数字)
nmcli connection reload
nmcli connection up westos
route -n
ping 14.215.177.39


##vim /etc/sysconfig/network-scripts/ifcfg-ens3
要删掉之前的本地文件
vim /etc/sysconfig/network-scripts/ifcfg-ens3
GATEWAY=172.25.254.250
nmcli connection reload
nmcli connection up westos
route -n
ping 14.215.177.39
9.设定dns

地址解析–将域名解析成ip地址
系统网络通信需要通过ip地址这个数字
手动设置

vim /etc/hosts(本地解析文件)

vim /etc/resolv.conf(dns地址) ##dns的指向文件 不需要重新启动网络立即生效
nameserver 218.30.19.50

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

DNS1=114.114.114.114 ##需要重启网络,当网络重新启动,此参数会修改/etc/resolv.conf
10.dhcp服务配置

在主机中使用景象找到dhcp-server的安装包
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; ##dns
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.
27 删除
28 删除
# This is a very basic subnet declaration.
subnet 172.25.254.0 netmask 255.255.255.0 { ##子网设定
 range 172.25.254.160 172.25.254.200; ##IP地址池
 option routers 172.25.254.250; ##网关
 }
35以下全删除

systemctl restart dhcpd
systemctl stop firewalld
测试:
在网络工作模式是dhcp的主机中重启网络,可以看到,ip,GW,dns,全部获取成功。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值