#######linux下的网络配置############
###1.什么是IP ADDRESS
internet protocol ADDRESS ##网络进程地址
ipv4 internet protocol version 4
1. 2x32
ip是由32个01组成
11111110.11111110.11111110.11111110 = 254.254.254.254
###2.子网掩码
用来划分网络区域
子网掩码非0的位对应的ip上的数字表示这个ip的网络位
子网掩码0位对应的数字是ip的主机位
网络位表示网络区域
主机位表示网络区域里某台主机
###3.ip通信判定
网络位一致,主机位不一致的2个IP可以直接通讯
####4.网络设定工具
ping ping -c1 -w1 ip | ##检测网络是否通畅 ##ping1次等待1秒 |
ifconfig ifconfig device ip/24 | ##查看或设定网络接口 ##查看 ##设定 |
ip addr ip addr add ip/24 dev device | ##检测或设定网络接口 ##检测 ##设定 |
####5.图形方式设定ip
设定:
1.nm-connection-editor
systemctl status NetworkManager 查看服务状态
systemctl stop NetworkManager 服务关闭,网络工具将无法管理网络
systemctl start NetworkManager 打开服务
当用图像改变已经存在的链接ip时,更改保存的只是配置文件,无法立即生效
如果需要更改生效,需要刷新链接,并重新启动指定链接才可以
2.nmtui
####6.命令方式设定网络
nmcli | ##NetworkManager必须开启 |
nmcli device connect ens3 | ##启用ens160网卡 |
nmcli device disconnect ens3 | ##关闭ens160网卡 |
nmcli device show ens3 | ##查看网卡信息 |
nmcli device status ens3 | ##查看网卡服务接口信息 |
nmcli connection show | ##查看连接 |
nmcli connection down ens3 | ##关闭连接 |
nmcli connection up ens3 | ##开启连接 |
nmcli connection delete ens3 | ##删除连接 |
#添加连接
更改链接
####7.管理网络配置文件
网络配置目录
/etc/sysconfig/network-scripts/
网络配置文件的名命规则
ifcfg-xxxx
DEVICE=xxx BOOTPROTO=dhcp|static|none ONBOOT=yes | ##设备名称 ##设备工作方式 |
增加网卡,设置双网卡
双网卡设置成功,尝试与其他两台不同频段主机通信,因此先增加频段1.1.1.xxx的频段主机。
####8.lo回环接口
回环接口------人的神经----127.0.0.1-----localhost
####9.网关#####
设置网关,需要在双网卡主机中设定,开启火墙中的地址伪装功能,双网卡主机变为路由器。
临时添加网关
设定全局网关,如果当前系统有多个网关,这个设定对于所有网卡生效
##10 设定dns
dns实验环境设定
环境配置好后,便可用ip地址访问百度,但此时不能后使用网址。
#地址解析
#系统操作者对字符敏感,系统网络通信需要通过ip地址这个数字,当操作着输入网址www.baidu.com,这个网址不是一个可以通信的IP地址,于是必须要在系统中把www.baidu.com变成百度服务器的IP地址,这样的过程叫做地址解析。
vim /etc/hosts | ##本地解析文件,此文件中直接提供地址解析 |
ip | 域名 |
220.181.111.188 | www.baidu.com |
vim /etc/resolv.conf | ##dns的指向文件 不需要重新启动网络立即生效 | |
nameserver 114.114.114.114 | ##当需要某个域名的IP地址时去问114.114.114.114 |
vim /etc/sysconfig/network-scripts/ifcfg-xxxx | |
DNS1=114.114.114.114 | ##需要重启网络,当网络重新启动 ##此参数会修改/etc/resolv.conf |
注意:
档网络工作模式为dhcp时,系统会自动获得ip 网关 dns,那么/etc/resolv.conf会被获得到的信息修改。如果不需要获得dns信息,在网卡配置文件中加入PEERDNS=no
####11.dhcp服务配置
在主机中使用景象找到dhcp-server的安装包
rpm -qc dhcp-server
cp /usr/share/doc/dhcp-srever/dhcpd.conf.example /etc/dhcp/dhcpd.conf ##用模板生成配置文件
vim /etc/dhcp/dhcpd.conf
修改/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行有面全删除
##12.设定解析的优先级
系统默认:
/etc/hosts > /etc/resolv.conf
vim /etc/nsswitch.conf
39 hosts: files files ##/etc/hosts指向优先
vim /etc/nsswitch.conf
39 hosts: dns files ##/etc/resolv.conf dns指向优先