什么是IP ADDRESS
网络位(前三段) 主机位(最后一段)
网络地址进程:intrenet protocol ADDRESS
ipv4:internet protocol version4
ip由32个0/1组成
11111110.11111110.11111110.111111110=255.255.255.255
子网掩码
用来划分网络区域
子网掩码非0的位对应ip上的数字表示这个ip的网络位
子网掩码0的位对应ip上的数字表示这个ip的主机位
网络位表示网络区域
主机位表示区域内某台主机
其中主机位按理来说应该有0-255,但是0没意义,255是广播位;故能用的只有1-254
ip通信判断
网络位一致的两个ip可以互相通信
172.25.254.1/24
172.25.254.2/24
172.25.0.1/16
24=255.255.255.0
网络设定工具
名称 | 方法 | 备注 |
---|---|---|
检测网络是否通畅 | ping | |
查看正在使用的网络接口 | ifconfig | |
设置ip | ifconfig device ip/24 | 设置的ip为临时ip,并且改变原ip |
关闭该网卡 | ifconfig device down | |
开启该网卡 | ifconfig device up | |
查看网络接口 | ip addr | |
查看某个网络接口 | ip addr show | |
设置ip | ip addr add ip/24 dev device | 增加一个新ip,原ip不改变 |
给某个网卡删除ip地址 | ip adde del dev eth0/24 |
更改ip
ip addr flush etho
ip addr add ip/24 dev device
注意:device的名字是一个事实,看到什么用什么
设置ip
## 图形方式设置ip——nm-connection-edtior
在终端输入nm-connection-edtior
第一步 删除原ip
第二步:添加以太网
第三步:手动选择
第四步:设置ip
在使用该方法时:使用ifconfig查看ip有可能未生效
可以依次运行以下命令进行ip的更新
systemctl stop NetworkManager
systemctl restart network
systemctl start NetworkManager
##无图形方式设置ip——nmtui
在无图形界面输入nmtui即可进入ip设置
其中,Tab表示切换
##命令方式设置ip——nmcli
名称 | 方法 |
---|---|
启用eth0网卡 | nmcli device connect eth0(网卡名) |
关闭eth0网卡 | nmcli device disconnect eth0 (网卡名) |
查看网卡信息 | nmcli device show eth0 |
查看网卡服务接口信息 | nmcli device status eth0 |
查看westos网络详细信息 | nmcli connection westos(网络名称) |
关闭westos网络 | nmcli connection down westos(网络名称) |
开启westos网络 | nmcli connection up westos(网络名称) |
删除westos网络 | nmcli connection delete westos(网络名称) |
##有ip的话则为设定好的ip,没ip则自动获得ip(dhcp)
nmcli connection add type ethernet con-name chen ifname eth0 ip4 172.25.254.107/24
##由静态转dhcp
nmcli connection modify chen ipv4.method auto
##由dhcp转静态
nmcli connection modify chen ipv4.method manual
##修改ip
nmcli connection modify chen ipv4.addresses 172.25.254.207/24
注意:当dhcp没有初始ip时转为静态ip会报错
使用命令方式设置ip时必须要开启NetworkManager
##更改配置文件设置ip——/etc/sysconfig/network-scripts/
修改/etc/sysconfig/network-scripts/中的文件可以起到修改ip的效果
其中文件的命名必须是ifcfg-xxxx
名称 | 方法 |
---|---|
设备名称 | DEVICE=XXX |
设备工作方式 | BOOTPROTO=dhcp/static/none |
网络服务开启时自动激活网卡 | ONBOOT=yes |
ip地址 | IPADDR=xxx.xxx.xxx.xxx |
子网掩码 | PREFIX=24 |
子网掩码 | NETMASK=255.255.255.0 |
名字 | NAME=xxx |
dhcp网络设定:
vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=dhcp
systemctl restart network
静态网络设置:
vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=none
IPADDR=172.25.254.107
PREFIX=24
NAME=chen
systemctl restart network
一块网卡配置多个ip:
vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=none
IPADDR0=172.25.254.107
PREFIX0=24
IPADDR1=172.25.254.108
PREFIX1=24
NAME=chen
systemctl restart network
ip addr show eth0 ##查看一下
注意:当有数字的时候都要有对应的数字
必须要注意的时该目录下的文件都是有用文件,不可出现没用的文件,如果出现,重启网络服务的时候就会报错
回环接口——lo
数据在主机内部传输,系统内部传输,数据安全,高效,不需要对外开放——心里想的说不出来
例如:日志服务和ssh服务,不需要外部刺激。
下图中的lo就是回环接口
网关
大家都知道,从一个房间走到另一个房间,必然要经过一扇门。同样,从一个网络向另一个网络发送信息,也必须经过一道“关口”,这道关口就是网关。顾名思义,网关(Gateway)就是一个网络连接到另一个网络的“关口”。
##1.搭建双网卡虚拟机
在Linux系统中,我们可以开启两个虚拟机,设置两个不同网段的ip,然后进行两个虚拟机之间的互联。
首先,我们需要为其中一个虚拟机增加一个网卡,使之拥有两个网卡,可以作路由器。
在真机中以root用户身份输入virt-manage打开虚拟平台。本机让server虚拟机为路由器,故点击server虚拟机。
点击左上角的灯泡的样子,进入页面后选择左下角的add添加按钮,选择network,其中Device modle选择为virtro。返回后或发现出现俩个网卡。
##2.开启路由功能和火墙伪装功能
打开westos_server主机,想要将其设置为路由器需要两个步骤
1.打开内核的路由功能;
##sysctl -a 是内核参数net.ipv4.ip_forward = 1时代表路由功能开启
[root@westos_server Desktop]# sysctl -a | grep ip_forward
net.ipv4.ip_forward = 0
[root@westos_server Desktop]# vim /etc/sysctl.conf
##应用内核功能
[root@westos_server Desktop]# sysctl -p
net.ipv4.ip_forward = 1
2.开启火墙伪装功能
##查看火墙功能 masquerade是火墙伪装功能
[root@westos_server Desktop]# firewall-cmd --list-all
public (default, active)
interfaces: eth0
sources:
services: dhcpv6-client ssh
ports:
masquerade: no
forward-ports:
icmp-blocks:
rich rules:
##打开火墙伪装功能
[root@westos_server Desktop]# firewall-cmd --add-masquerade
success
[root@westos_server Desktop]# firewall-cmd --list-all
public (default, active)
interfaces: eth0
sources:
services: dhcpv6-client ssh
ports:
masquerade: yes
forward-ports:
icmp-blocks:
rich rules:
在/etc/sysctl.conf 里面添加net.ipv4.ip_forward = 1即可打开内核路由功能
3.设置路由器的两块网卡ip
设置两块网卡ip分别为172.25.254.70和9.9.9.9
然后使用ifconfig查看
##3.设定网关
设置westos_client主机,这个主机的ip为9.9.9.99,现在需要ping通路由器172.25.254.70
##先查看本机地址,然后设置网关
[root@westos_client Desktop]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 9.9.9.99 netmask 255.255.255.0 broadcast 9.9.9.255
inet6 fe80::5054:ff:fe00:460a prefixlen 64 scopeid 0x20
ether 52:54:00:00:46:0a txqueuelen 1000 (Ethernet)
RX packets 19932 bytes 918007 (896.4 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 721 bytes 117077 (114.3 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10
loop txqueuelen 0 (Local Loopback)
RX packets 1828 bytes 154768 (151.1 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1828 bytes 154768 (151.1 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
##查看网关
[root@westos_client Desktop]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
9.9.9.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
##在/etc/sysconfig/network中添加网关:GATEWAY 9.9.9.9
[root@westos_client Desktop]# vim /etc/sysconfig/network
##重启network服务
[root@westos_client Desktop]# systemctl restart network
##查看网关
[root@westos_client Desktop]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 9.9.9.9 0.0.0.0 UG 1024 0 0 eth0
9.9.9.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
[root@westos_client Desktop]# ping 172.25.254.70
PING 172.25.254.70 (172.25.254.70) 56(84) bytes of data.
64 bytes from 172.25.254.70: icmp_seq=1 ttl=64 time=0.558 ms
64 bytes from 172.25.254.70: icmp_seq=2 ttl=64 time=0.233 ms
64 bytes from 172.25.254.70: icmp_seq=3 ttl=64 time=0.405 ms
64 bytes from 172.25.254.70: icmp_seq=4 ttl=64 time=0.375 ms
64 bytes from 172.25.254.70: icmp_seq=5 ttl=64 time=0.335 ms
64 bytes from 172.25.254.70: icmp_seq=6 ttl=64 time=0.246 ms
^C
— 172.25.254.70 ping statistics —
6 packets transmitted, 6 received, 0% packet loss, time 5000ms
rtt min/avg/max/mdev = 0.233/0.358/0.558/0.111 ms
网关的设定有三种方式
1. 全局网关——针对没有设定网关的网卡生效
在/etc/sysconfig/network中添加网关:GATEWAY x.x.x.x
2. 针对网卡的网关——在一个网卡含有多个ip地址时只针对单个ip生效
在/etc/sysconfig/network-scripts/ifcfg-eth0中添加网关:GATEWARN(N表示数字几),只针对单个ip
3. 针对网卡的网关——在一个网卡含有一个ip地址时
在/etc/sysconfig/network-scripts/ifcfg-eth0中添加网关:GATEWARN,只有一个ip
地址解析(DNS)
domain name server == 域名解析服务
DNS主机:所有网络和网站的对应关系
##解析就是把域名变成IP
##电信DNS服务器114.114.114.114
地址解析的设定有三种方式*
1. 本地解析——解析文件/etc/hosts
自己告诉自己域名与IP的对应方式 (ip 域名)
2. dns服务器的ip ——解析文件 /etc/resolv.conf name
有问题找谁,指向文件问其他人(DNS主机) nameserver xx(访问DNS的ip)
3. DNS写入主配置文件—— /etc/sysconfig/network-scrp/ifgf-**
在文件中写了DNS1/2/3=dns服务器的ip
##调整地址解析的优先级——/etc/nsswitch.conf
在寻找DNS时我们的主机默认以解析本地文件/etc/hosts为主,然后解析 dns服务器的ip /etc/resolv.conf name
可以在vim /etc/nsswitch.conf下的39行左右,修改hosts: dns files
搭建DHCP服务器
第一步:查询对应的Linux系统版本,下载对应版本的dhcp安装包
本机的版本为7.3版本,可在终端输入hostnamectl查询
第二步:切换到超级用户下进行安装
使用rpm命令进行安装(rpm命令在后面会详细列出)
##切换到超级用户
[kiosk@foundation70 ~]$ su - root
密码:
上一次登录:三 7月 24 06:58:06 CST 2019pts/0
##安装dhcp文件
[root@foundation70 Desktop]# rpm -ivh /sofeware/dhcp-4.2.5-47.el7.x86_64.rpm
warning: /sofeware/dhcp-4.2.5-47.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY
Preparing… ################################# [100%]
Updating / installing…
1:dhcp-12:4.2.5-47.el7 ################################# [100%]
[root@foundation70 Desktop]# cd /etc/dhcp/
[root@foundation70 dhcp]# ls
dhclient.d dhcpd6.conf dhcpd.conf
[root@foundation70 dhcp]# vim dhclient.d
##查看dhcpd文件内容,提示需要去/usr/share/doc/dhcp*/dhcpd.conf.example看模版
[root@foundation70 dhcp]# vim dhcpd.conf
##将模版复制到当前并覆盖原文件
[root@foundation70 dhcp]# cp /usr/share/doc/dhcp*/dhcpd6.conf.example /etc/dhcp/dhcpd.conf
cp: overwrite ‘/etc/dhcp/dhcpd.conf’? y
[root@foundation70 dhcp]# ls
dhclient.d dhcpd6.conf dhcpd.conf
[root@foundation70 dhcp]# vim dhcpd.conf
##重启dhcpd服务,出错;先清空日志,在查看日志内容,寻找错误
[root@foundation70 dhcp]# systemctl restart dhcpd
Job for dhcpd.service failed because the control process exited with error code. See “systemctl status dhcpd.service” and “journalctl -xe” for details.
[root@foundation70 dhcp]# > /var/log/messages
##日志说存在多于文件,该文件有数字,故说明该目录下只能存在有效文件
[root@foundation70 dhcp]# ls
dhclient.d dhcpd6.conf dhcpd.conf
[root@foundation70 dhcp]# rm -rf dhcpd6.conf
[root@foundation70 dhcp]# ls
dhclient.d dhcpd.conf
##重启服务成功
[root@foundation70 dhcp]# systemctl restart dhcpd
##打开虚拟机进行测试
[root@foundation70 dhcp]# rht-vmctl start desktop
Starting desktop.
[root@foundation70 dhcp]# rht-vmctl view desktop
dhcp配置文件内容
将复制过来的文件27、28 ;35行以后删除,然后对文件进行配置
# dhcpd.conf
#
# Sample configuration file for ISC dhcpd
#
# option definitions common to all supported networks...
##dhcp的域名
option domain-name "westos";
##DNS主机的访问地址
option domain-name-servers 114.114.114.114;
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.
##snbnet是网段,netmask是子网掩码
subnet 172.25.254.0 netmask 255.255.255.0 {
##地址池范围(dhcp自动分配的地址范围)
range 172.25.254.11 172.25.254.77;
##网关,虚拟机能联网的条件之一
option routers 172.25.250.170;
}
~
~
第三步 测试
修改虚拟机的ip配置文件,将BOOTBROTO=none改为dhcp并删除ip地址,重启网络服务后进行ifconfig查询网络,看是否自动获取到11-77之内的ip地址