class_9LINUX下的网络配置#

#1。什么是IP ADDRESS
internet pro ADDRESS #网络进程地址
ipv4 internet protocol version 4
1.2*32
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 检测网络是否通畅
ping -c 1 #ping一次
ping -w 1 #等待1秒
ping -c1 -w1 ip #ping1次等待1秒
在这里插入图片描述

ifconfig #查看或设定网络接口
ifconfig #查看
在这里插入图片描述

ifconfig device ip/24 #设定
ifconfig device down #关闭
ifconfig device up #开启
ip addr #检测或指定网络接口
ip adde show #检测
ip addr add ip/24 dev device #设定
ip addr del dev ens160 ip/24 #删除
ip addr flush ens160 #更改ip步骤
“注意:device的名字是一个物理事实,看到什么名字只能用什么名字”
#5.图形方式设定ip
设定:
1。nm-connection-editor
在这里插入图片描述
首先删除之前的网络配置
更改:
不建议用服务控制网络
systemctl restart NetworkManager
建议使用:
nmcli connection show
nmcli connection down westos
nmcli connection up westos
2.nmtui
#6.命令方式设定网络
nmcli #NetworkManager必须开启

nmcli device connect ens160 #开启ens160网卡
nmcli device disconnect ens160 #关闭ens160网卡
nmcli device show ens160 (查看网卡信息,但是好像没法使用)
nmcli device status ens160 #查看网卡服务接口信息

nmcli connection show #查看连接
在这里插入图片描述

nmcli connection down westos #关闭连接
nmcli connection up westos #开启连接
nmcli connection delete westos #删除连接
在这里插入图片描述

添加连接
nmcli connection add type ethernet con-name westos ifname ens160 ip4 172.25.254.100/24

更改
nmcli connection modify westos ipv4.addresses 172.25.254.200/24

#7。管理网络配置文件
网络配置目录
/etc/sysconfig/network-scripts/
网络配置文件的命名规则
ifcfg-xxxx
DEVICE=xxx #设备名称
BOOTPROTO=dhcp|static|none #设备工作方式(前两个为静态)
ONBOOT=yes #网络服务开启时自动激活网卡
IPADDR= #IP地址
PREFIX=24 #子网掩码
NETMASK=255。255。255。0 #子网掩码
NAME= #接口名称
在这里插入图片描述

dhcp网络设定
vim /etc/sysconfig/network-scripts/ifcfg-ens160
DEVICE=ens3
ONBOOT=yes
TYPE=Ethernet
BOOTRPOTO=none
IPADDR=172.25.254.129
PREFIX=24

systemctl restart network

ip addr show ens3

#8.网关
1。把westos_node1双网卡主机变成路由器
systemctl start firewalld
firewall-cmd --list
firewall-cmd --permanent --add-masquerade
firewall-cmd --reload
firewall-cmd --list-all
public (default,active)
interface:br0 enp0s25 wlp3s0
source:
在这里插入图片描述

9.设定dns
#地址解析
系统操作者对字符敏感

#10.dhcp服务配置
在主机中使用镜像找到dhcp-server的安装包
rpm -ivh dhcp-server

cp /usr/share/doc/dhcp*/dhcpd.conf.example /etc/dhcp/dhcpd.conf #用木板生成配置文件

#dhcpd.conf
#Sample configuration file for ISC dcpd

#option definition 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 720 #最长租约

#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 message to a different log file (you also
#have to hack syslog.conf to complete the redirection.)

#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

############设置一个虚拟主机为路由##########
1。再添加一个物理网卡,设置成另一个IP
cd /etc/sysconfig/network-scripts/
cp ifcfg-ens3 ifcfg-ens8
vim ifcfg-ens8
nmcli connection reload
nmcli connection show
2。设置火墙掩码
sysctl -a |grep ip_forward
systemctl enable --now firewalld
firewall-cmd --permanent --add-masquerade
firewall-cmd --reload
firewall-cmd --list-all
3。另一台IP:1.1.1.xx主机 通过路由主机 连接IP:172.25.254.29主机
route -n #查看网关
ip route add default via 1.1.1.100

tips:
1。cd /etc/sysconfig/network-scripts/下要注意设置
经验来说,对于路由主机,最好将主机位设置成相同数字
name设置的名字在执行nmcli connection down/up + 名字 时会用到
每次设置物理网卡IP时,需要经历reload,down/up。
更改主机IP时,需要重新设置网关:
ip route add default via 1.1.1.100
#最后数字即位路由主机IP
在这里插入图片描述
此外,要注意将两个虚拟机的网络设置为虚拟桥接模式,否则ping时会出现hostname unreachable
##############################################

2。设定单网卡主机westos_linux的网关
vim /etc/sysconfig/network #全局网关,针对所有没有设定网关的网卡生效
GATEWAY=172.25.254.20

vim /etc/sysconfig/network-scripts/ifcfg-ens160
GATEWAY0=172.25.254.20 #当网卡设定的IP有多个时,指定对于哪个IP生效
GATEWAY=172。25。254。20 #当网卡中设定的IP只有一个时

route -n #查看网关

注意:只有当能上网的主机开启了路由功能时,其他主机才能设置开启路由功能的主机
的网关IP进行上网

###网址解析#############################################
1。修改网关至可以上网的IP地址
ip route add default via 172。25。254。250 /临时(关机就没了)
或者
vim /etc/sysconfig/network
添加GATEWAY=172。25。254。250
2。添加网址解析
1。手动单个网址添加
/etc/hosts
14.215.177.39 www.baidu.com >>
2.设置DNS
vim /etc/resolv.conf
nameserver 114.114.114.114>>


11。设定解析的优先级
系统默认:
/etc/hosts > /etc/resolv.conf
vim /etc/nsswitch.conf
39 hosts: files dns #/etc/osts优先
vim /etc/nsswitch.conf
39 hosts: dns file #/etc/resolv.conf dns指向优先

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值