什么是IP ADDRESS

  • internet protocol ADDRESS      网络进程地址
  • ipv4 internet protocol version 4
  • ip是由3201组成
  • 11111110.11111110.11111110.11111110 = 254.254.254.254

子网掩码

  • 用来划分网络区域
  • 子网掩码非0的位对应的ip上的数字表示这个ip的网络位
  • 子网掩码0位对应的数字是ip的主机位
  • 网络位表示网络区域
  • 主机位表示网络区域里某台主机

ip通信判定

  • 网络位一致主机位不一致的2IP可以直接通讯
  • 172.25.254.1/24 24=255.255.255.0
  • 172.25.254.2/24
  • 172.25.0.1/16

网络设定工具

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 show ##检测
ip addr add ip/24 dev device ##设定
ip addr del dev ens160 ip/24 ##删除
"注意: device的名字一个物理事实,看到什么名字只能用什么名字"

图形方式设定ip

设定nmconnectioneditor(系统安装图形模式后用此命令)
nmcli connection reload
nmcli connection up free1
更改:
不建议用服务控制网络
systemctl restart NetworkManager
建议使用
nmcli connection show
nmcli connection down ens160
nmcli connection up ens160
nmtui(系统未安装图形模式时,使用类图形模式设置)

 

 命令方式设定网络


systemctl status 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

 

管理网络配置文件

/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=ens160 
ONBOOT=yes 
BOOTPROTO=dhcp 

systemctl restart network

静态网络设定文件

vim /etc/sysconfig/network-scripts/ifcfg-ens160 
DEVICE=ens160 
ONBOOT=yes 
IPADDR=192.168.2.100
NETMASK=255.255.255.0
BOOTPROTO=none 
NAME=free

systemctl restartnetwork 

一块网卡上配置多个IP 
vim /etc/sysconfig/network-scripts/ifcfg-ens160 
DEVICE=ens160 
ONBOOT=yes 
IPADDR0=192.168.2.100
NETMASK0=255.255.255.0 
BOOTPROTO=none 
NAME=westos 
IPADDR1= 192.168.2.200
PREFIX1=255.255.255.0

systemctl restart network 

ip addr show ens160

lo回环接口

回环接口——人的神经—-127.0.0.1—–localhost
本机程序之间的访问使用到的网络接口,一般情况下是不会更改的

网关

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 ens160
172.25.0.0 0.0.0.0 255.255.255.0 U 0 0 0 ens160
172.25.254.0 0.0.0.0 255.255.255.0 U 0 0 0 ens160

 

 

 

设定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
  • 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
  • 因为工作中我们不可能把要用的域名解析全部都写出来,这显然不合理。

 所以我们可以在dns指定文件中写入IP为dns主机的地址,写入dns后,如果要访问某个域名,当前主机中没有的解析。那么就会主动询问此文件中指定的地址,此地址会提供解析服务。

方法有两种,第一种:

第二种:

设定解析的优先级系统默认

  • /etc/hosts (指定本地解析)
  • /etc/resolv.conf(dns固定解析)
  • 实验得知本地指定解析高于dns固定解析

  • vim /etc/nsswitch.conf
  • files dns ##/etc/hosts优先
  • dns files  ##dns指向优先

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 "free.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. 
# This is a very basic subnet declaration. 
subnet 192.168.2.0 netmask 255.255.255.0 { 
    range 192.168.2.100 192.168.2.110; 
#  option routers rtr-239-0-1.example.org, rtr-239-0-2.example.org;

}

更改/etc/dhcp/dhcpd.conf 后的内容如下:

 测试主机命令


云野 » Linux下的网络配置

发表回复