Linux学习之路
Linux下的网络配置
1.IP地址
Internet Protocol Address (网络进程地址)
IPv4即, internet protocol version 4
ip地址是由32位二进制数组成。
比如:
11111110.11111110.11111110.11111110 = 254.254.254.254
2.子网掩码
子网掩码用来划分网络区域
子网掩码非0的位对应的ip上的数字表示这个ip的网络位
子网掩码0位对应的数字是ip的主机位
网络位表示网络区域
主机位表示网络区域里某台主机
- 通信判断
网络位一致,主机位不一致的2个IP可以直接通讯
例如:
172.25.254.1/24 与172.25.254.2/24
其中,24表示子网掩码为255.255.255.0
处于不同网段的两个IP,需要配置网关才能通信。
3.网络设定工具
- ping
ping 用于检测网络是否通畅
ping -c 1 设定ping 1 次
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 addr 检测或这顶网络接口
ip addr show 检测
ip addr add ip/24 dev device 设定
ip addr del dev ens160 ip/24 删除
4.图形设定工具
-
NetworkManager服务打开
- 使用命令nm-connection-editor设定网络
- 使用命令nm-connection-editor设定网络
5.命令方式设定网络
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 删除连接
6.通过配置文件管理网络
网络配置目录
/etc/sysconfig/network-scripts/
网络配置文件的名命规则
ifcfg-xxxx
DEVICE=xxx 设备名称
BOOTPROTO=dhcp|static|none 设备工作方式(DHCP动态获取IP|静态获取IP|手动指定IP)
ONBOOT=yes 网络服务开启时自动激活网卡
IPADDR= IP地址
PREFIX=24 子网掩码
NETMASK=255.255.255.0 子网掩码
NAME= 接口名称
7.网关
vim /etc/sysconfig/network ##全局网关,针对所有没有设定网关的网卡生效
GATEWAY=172.25.254.250
vim /etc/sysconfig/network-scripts/ifcfg-ens160
GATEWAY0=172.25.254.20 ##当网卡中设定的IP有多个时,指定对于那个IP生效
GATEWAY=172.25.254.20 ##当网卡中设定的IP只有一个时
route -n ##查看网关
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 "172.25.254.250" 0.0.0.0 UG 1024 0 0 ens3
172.25.0.0 0.0.0.0 255.255.255.0 U 0 0 0 ens3
8.设定DNS
- 地址解析
系统操作者对字符敏感,但是系统网络通信需要通过ip地址这个数字。
当操作者输入网址www.baidu.com,这个网址不是一个可以通信的IP地址,于是必须要在系统中把www.baidu.com解析为百度服务器的IP地址。这样的过程叫做地址解析。
domain name server 域名解析服务 解析就是把域名变成IP。
本地解析文件
vim /etc/hosts ##本地解析文件,此文件中直接提供地址解析ip 域名
220.181.111.188 www.baidu.com
DNS指向文件
vim /etc/resolv.conf ##dns的指向文件,不需要重新启动网络立即生效
nameserver 114.114.114.114
##当需要某个域名的IP地址时去访问114.114.114.114
在上网端口ens192设置DNS服务器,这会修改指向文件/etc/resolv.conf
vim /etc/sysconfig/network-scripts/ifcfg-ens192
DNS1=114.114.114.114 ##需要重启网络,当网络重新启动
注意:
当网络工作模式为dhcp时,系统会自动获得ip 网关 dns
那么/etc/resolv.conf会被获得到的信息修改。如果不需要获得dns信息在网卡配置文件中加入PEERDNS=no
设定解析的优先级
系统默认优先级:
/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 即dns指向优先
dhcp服务配置
配置
- 在nodea上安装dhcp:
- 复制配置文件
- 修改配置文件