多种方式设定网络IP

1.网络设定

(1)以太网和局域网

以太网(Ethernet)指的是由Xerox公司创建并由Xerox、Intel和DEC公司联合开发的基带局域网规范,是当今现有局域网采用的最通用的通信协议标准。
以太网络使用CSMA/CD(载波监听多路访问及冲突检测)技术,并以10M/S的速率运行在多种类型的电缆上。以太网与IEEE802.3系列标准相类似。
它不是一种具体的网络,是一种技术规范

局域网(Local Area Network,LAN)是指在某一区域内由多台计算机互联成的计算机组。一般是方圆几千米以内。局域网可以实现文件管理、
应用软件共享、打印机共享、工作组内的日程安排、电子邮件和传真通信服务等功能。局域网是封闭型的,可以由办公室内的两台计算机组成,
也可以由一个公司内的上千台计算机组成。

以太网是一种局域网,而局域网却不一定是以太网,只是由于目前大多数的局域网是以太网,所以一般说局域网,大家都默认为以太网。

(2)常用的网络协议

IPv4,是互联网协议(Internet Protocol,IP)的第四版,也是第一个被广泛使用,构成现今互联网技术的基础的协议。
1981年 Jon Postel 在RFC791中定义了IP,Ipv4可以运行在各种各样的底层网络上,比如端对端的串行数据链路(PPP协议和SLIP协议) ,卫星链路等等。局域网中最常用的是以太网。

IPv6是Internet Protocol Version 6的缩写,其中Internet Protocol译为“互联网协议”。IPv6是IETF(互联网工程任务组,Internet Engineering Task Force)
设计的用于替代现行版本IP协议(IPv4)的下一代IP协议。目前IP协议的版本号是4(简称为IPv4),它的下一个版本就是IPv6。

(3)网络位和主机位

32位 二进制 = 10进制
11111110.11111110.11111110.11111110=254.254.254.254

172.25.0.10/255.255.255.0    #ip地址/子网掩码(netmask也可以直接写24)

netmask用来标识ip的网络位和主机位

网络位:(表示网络区域)
netmask非0位所对应的ip地址数值为这个ip的网络位

主机位:(表示网络区域中的某台主机)
netmask0位所对应的ip地址数值为这个ip的主机位

注意:网络位一致,主机位不一致的两个ip才可以直接通信,这样的两台主机叫做直接网络

(4)查看网络配置的两种方式

ifconfig        ##查看所有网络设备(正在使用)的网络配置
ifconfig eth0   ##查看eth0网络设备的网络配置

ip addr         ##查看所有网络设备的网络配置
ip addr eth0    ##查看eth0网络设备的网络配置

注意:网络设备是真实存在的,这是一个事实!不能随意写

2.设定ip的方法:

(1)临时设定

##更改ip
ifconfig eth0 172.25.254.134 netmask 255.255.255.0
ifconfig eth0 172.25.254.134/24      ##24表示子网掩码(netmask)

##添加ip
ip addr add dev eth0 172.25.254.134/24
##删除ip
ip addr del dev eth0 172.25.254.134/24
注意:这样的设定是临时的在网络重启时后会失效
实验:

##注意:此实验只能在图形界面完成,因为ssh连接网络不稳定
[root@node1 Desktop]# ifconfig eth0

在这里插入图片描述

##图形方式删除ip;必须先删除原来的ip,再重新添加
[root@node1 Desktop]# nm-connection-editor 

在这里插入图片描述

[root@node1 Desktop]# ifconfig eth0

在这里插入图片描述

##用ifconfig方式给eth0设备添加ip
[root@node1 Desktop]# ifconfig eth0 172.25.254.130/24
[root@node1 Desktop]# ifconfig eth0

在这里插入图片描述

##检测新添加的ip是否可以使用
[root@node1 Desktop]# ping 172.25.254.66

在这里插入图片描述

[root@node1 Desktop]# ip addr

在这里插入图片描述

[root@node1 Desktop]# ip addr show eth0

在这里插入图片描述

[root@node1 Desktop]# ip addr 
add      change   del      flush    help     replace  show     
##用ip addr方式删除eth0设备上的ip
[root@node1 Desktop]# ip addr del dev eth0 172.25.254.134/24
[root@node1 Desktop]# ip addr show eth0

在这里插入图片描述

  ##用ip addr方式给eth0设备添加新的ip
    [root@node1 Desktop]# ip addr add dev eth0 172.25.254.134/24
    [root@node1 Desktop]# ip addr show eth0

在这里插入图片描述

##-c表示次数(count)
[root@node1 Desktop]# ping -c 3 172.25.254.66

在这里插入图片描述

##-w表示等待时间(wait)
[root@node1 Desktop]# ping -w 2 172.25.254.66

在这里插入图片描述

##ping-次且只ping-秒
[root@node1 Desktop]# ping -c1 -w1 172.25.254.66

在这里插入图片描述

(2)永久性设定

注意:所有设定动态ip的方式,均需要先做好dhcp服务的配置,否则将无法获取到ip

1)图形的方式

网络设备设定ip的两种方式:

automatic(DHCP)    ##动态获取,ip不固定(dhcp服务给什么ip就用什么ip)
static             ##静态ip是操作着自行设置,ip固定

在此图形工具中选择DHCP为动态网络
在此图形工具中选择manual为静态网络,ip须手动输入
方式1:
nm-connection-eidtor  #图形设定ip
 @1 设定静态ip:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

@2设定动态ip    
 前提:在另一台主机中先做好dhcp服务的配置

在这里插入图片描述

[root@node1 Desktop]# ifconfig eth0

在这里插入图片描述

 ##先删除ip
 [root@node1 Desktop]# ip addr del dev eth0 172.25.254.134/24
 [root@node1 Desktop]# ip addr show eth0

在这里插入图片描述

##重新添加动态ip(从DHCP服务获取ip)
[root@node1 Desktop]# nm-connection-editor 

在这里插入图片描述

 [root@node1 Desktop]# ifconfig eth0

在这里插入图片描述

[root@node1 Desktop]# > /var/log/messages 
[root@node1 Desktop]# cat /var/log/messages 
[root@node1 Desktop]# systemctl restart network
##可查看到获取动态ip的来源
[root@node1 Desktop]# cat /var/log/messages

在这里插入图片描述

##如果关闭来源主机的dhcp服务,此时再重启网络便会失败,因为没有ip获取来源
[root@node2 Desktop]# systemctl stop dhcpd
[root@node1 Desktop]# systemctl restart network 

在这里插入图片描述

[root@node1 Desktop]# ifconfig eth0

在这里插入图片描述

方式2:
nmtui         #当系统没有图形时可以用此命令开启网络设定的ui界面

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2)nmcli 命令

device            ##设备
connnection       ##链接

设备名与链接名的区别:

设备名必须真实存在  而链接名可以随意取

在这里插入图片描述
在这里插入图片描述

nmcli device

nmcli device ...(用tab键可自动补齐)
nmcli device status eth0(设备)   #查看eth0的状态
nmcli device show eth0            #显示eth0的详细信息
nmcli device disconnect eth0      #断开eth0
nmcli device connect eth0         #连接eth0
实验:

##先用图形方式设定静态ip
[root@node1 Desktop]# nm-connection-editor
[root@node1 Desktop]# ifconfig eth0

在这里插入图片描述

##用tab键可自动补齐参数
[root@node1 Desktop]# nmcli device 
connect     disconnect  help        show        status      wifi        wimax
##查看eth0的状态
[root@node1 Desktop]# nmcli device status eth0
Unknown parameter: eth0
DEVICE  TYPE      STATE      CONNECTION 
eth0    ethernet  connected  eth0       
lo      loopback  unmanaged  --  
##显示eth0的详细信息       
[root@node1 Desktop]# nmcli device show eth0
GENERAL.DEVICE:                         eth0
GENERAL.TYPE:                           ethernet
GENERAL.HWADDR:                         52:54:00:00:42:0A
GENERAL.MTU:                            1500
GENERAL.STATE:                          100 (connected)
GENERAL.CONNECTION:                     eth0
GENERAL.CON-PATH:                       /org/freedesktop/NetworkManager/ActiveConnection/4
WIRED-PROPERTIES.CARRIER:               on
IP4.ADDRESS[1]:                         ip = 172.25.254.130/24, gw = 0.0.0.0
IP6.ADDRESS[1]:                         ip = fe80::5054:ff:fe00:420a/64, gw = ::

在这里插入图片描述

##断开eth0
[root@node1 Desktop]# nmcli device disconnect eth0
[root@node1 Desktop]# nmcli device status eth0
Unknown parameter: eth0
DEVICE  TYPE      STATE         CONNECTION 
eth0    ethernet  disconnected  --         
lo      loopback  unmanaged     -- 
##此时查看不到ip        
[root@node1 Desktop]# ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 52:54:00:00:42:0a  txqueuelen 1000  (Ethernet)
        RX packets 23  bytes 1278 (1.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 129  bytes 19164 (18.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

在这里插入图片描述

##连接eth0
[root@node1 Desktop]# nmcli device connect eth0
Device 'eth0' successfully activated with 'a39bc318-3f33-4a01-a617-454b1c0cf6bc'.
[root@node1 Desktop]# nmcli device status eth0
Unknown parameter: eth0
DEVICE  TYPE      STATE      CONNECTION 
eth0    ethernet  connected  eth0       
lo      loopback  unmanaged  --         
[root@node1 Desktop]# ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.25.254.130  netmask 255.255.255.0  broadcast 172.25.254.255
        inet6 fe80::5054:ff:fe00:420a  prefixlen 64  scopeid 0x20<link>
        ether 52:54:00:00:42:0a  txqueuelen 1000  (Ethernet)
        RX packets 23  bytes 1278 (1.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 156  bytes 23025 (22.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

在这里插入图片描述

nmcli connnection

nmcli connnection ...(用tab键可自动补齐)
nmcli connection  down westos(链接名)  #关闭链接
nmcli connnection  up  westos           #开启链接
nmcli connection  delete westos         #删除westos链接上的ip

nmcli connection add type ethernet con-name westos(链接名) ifname eth0(设备名)  autoconnect yes                   #动态添加(DHCP服务给什么ip就有什么ip)
nmcli connection add type ethernet con-name westos ifname eth0 ip4 172.25.254.130/24                              #静态添加(自行指定ip)
实验:
[root@node1 Desktop]# ifconfig eth0

在这里插入图片描述

##用Tab键补齐参数
[root@node1 Desktop]# nmcli connection 
add     delete  down    edit    help    load    modify  reload  show    up  
##关闭westos链接   
[root@node1 Desktop]# nmcli connection down westos
[root@node1 Desktop]# ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 52:54:00:00:42:0a  txqueuelen 1000  (Ethernet)
        RX packets 26  bytes 1404 (1.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 244  bytes 35128 (34.3 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
##开启westos链接
[root@node1 Desktop]# nmcli connection up westos
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/9)
[root@node1 Desktop]# ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.25.254.130  netmask 255.255.255.0  broadcast 172.25.254.255
        inet6 fe80::5054:ff:fe00:420a  prefixlen 64  scopeid 0x20<link>
        ether 52:54:00:00:42:0a  txqueuelen 1000  (Ethernet)
        RX packets 26  bytes 1404 (1.3 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 269  bytes 38811 (37.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

在这里插入图片描述

##删除westos链接上的ip
[root@node1 Desktop]# nmcli connection delete westos
[root@node1 Desktop]# ifconfig eth0

在这里插入图片描述

##添加动态ip    
[root@node1 Desktop]# nmcli connection add type ethernet con-name westos ifname eth0 autoconnect yes
[root@node1 Desktop]# ifconfig eth0

##先用图形方式删除ip
[root@node1 Desktop]# nm-connection-editor
##添加静态ip
[root@node1 Desktop]# nmcli connection add type ethernet  con-name westos ifname eth0 ip4 172.25.254.130/24
Connection 'westos' (d7144977-ddfc-4015-8532-7a648bcc2281) successfully added.
[root@node1 Desktop]# ifconfig eth0

在这里插入图片描述
更改(已存在)链接的ip:

DHCP(动态)--->  static(静态)
nmcli connection modify westos ipv4.addresses 172.25.254.100/24
nmcli connection modify westos ipv4.method manual
systemctl restart network

static(静态)---> DHCP(动态)
nmcli connection modify westos ipv4.method auto
systemctl restart network
实验:
@1静态-->动态
[root@node1 ~]# ifconfig eth0

在这里插入图片描述

[root@node1 ~]# nmcli connection modify westos ipv4.method auto
[root@node1 ~]# systemctl restart network
[root@node1 ~]# ifconfig eth0

在这里插入图片描述

@2动态-->静态
[root@node1 ~]# nmcli connection modify westos ipv4.addresses 172.25.254.130/24
[root@node1 ~]# nmcli connection modify westos ipv4.method manual
[root@node1 ~]# systemctl restart network
[root@node1 ~]# ifconfig eth0

在这里插入图片描述

3)文本的方式

 文本的方式与图形设定ip实质上是相同的
实验:
[root@node1 Desktop]# ifconfig eth0 

在这里插入图片描述

[root@node1 Desktop]# cd /etc/sysconfig/network-scripts/
[root@node1 network-scripts]# ls

在这里插入图片描述

##图形方式删除ip
[root@node1 network-scripts]# nm-connection-editor

在这里插入图片描述

##此时发现ifcfg-westos文件消失
[root@node1 network-scripts]# ls

在这里插入图片描述

3.1.设定动态ip与静态ip

@设定动态ip(DHCP)      

vim /etc/sysconfig/network-scripts/ifcfg-xxx  ##xxx表示可随意写
######################
DEVICE=设备名称        #接口使用的设备(真实存在的)
ONBOOT=yes            #网络服务开启时自动激活
BOOTPROTO=dhcp        #网卡工作模式为动态
NAME=链接名称          #可加可不加;链接名可随意取
实验:
##ifcfg-后边跟的名字可随意写
[root@node1 ~]# cd /etc/sysconfig/network-scripts/
[root@node1 network-scripts]# vim ifcfg-westos
#############################
DEVICE=eth0     #设备名
ONBOOT=yes      #网络服务开启时自动激活
BOOTPROTO=dhcp  #dhcp动态方式设定ip

在这里插入图片描述

##必须重启服务后,文本才会生效
[root@node1 ~]# systemctl restart network
[root@node1 ~]# ifconfig eth0

在这里插入图片描述

[root@node1 ~]# cd /etc/sysconfig/network-scripts/
[root@node1 network-scripts]# ls  

在这里插入图片描述

  ##发现图形中也生成了名为system-westos的链接
    [root@node1 network-scripts]# nm-connection-editor

在这里插入图片描述

[root@node1 network-scripts]# vim /etc/sysconfig/network-scripts/ifcfg-westos
#############################
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=dhcp
NAME=westos  ##设定链接名

在这里插入图片描述

[root@node1 network-scripts]# systemctl restart network
##此时发现图形中的链接名变更为westos
[root@node1 network-scripts]# nm-connection-editor 

在这里插入图片描述

[root@node1 network-scripts]# ifconfig eth0

在这里插入图片描述

[root@node1 network-scripts]# ls

在这里插入图片描述

##删除ifcfg-westos文件,此时图形中的westos链接并不会消失
[root@node1 network-scripts]# rm -rf ifcfg-westos
[root@node1 network-scripts]# nm-connection-editor 

在这里插入图片描述

  注意:此实验必须在虚拟机图形界面里做,不能ssh连接做此实验,因为是动态ip
@2设定静态ip(static)

vim /etc/sysconfig/network-scripts/ifcfg-xxx
###############
DEVICE=设备名称          #接口使用的设备(真实存在的)
ONBOOT=yes              #网络服务开启时自动激活
BOOTPROTO=none          #网卡工作模式为静态(static)
IPADDR=172.25.254.100   #设定IP
NETMASK=255.255.255.0   #子网掩码(PREFIX=24)
NAME=链接名称            #可加可不加;链接名可随意取
实验:
[root@node1 ~]# cd /etc/sysconfig/network-scripts/
[root@node1 network-scripts]# vim ifcfg-westos
#############################
DEVICE=eth0
ONBOOT=yes 
BOOTPROTO=none          ##静态方式设定ip(或者static)
IPADDR=172.25.254.130  
#NETMASK=255.255.255.0  ##子网掩码
PREFIX=24
NAME=westos    

在这里插入图片描述

[root@node1 network-scripts]# systemctl restart network
[root@node1 network-scripts]# ifconfig eth0

在这里插入图片描述

3.2.一个静态网卡设定多个ip

vim /etc/sysconfig/network-scripts/ifcfg-xxx
####################
DEVICE=设备名
ONBOOT=yes
BOOTPROTO=none
IPADDR0=172.25.254.133
PREFIX0=24
IPADDR1=192.168.0.130
PREFIX1=24
NAME=链接名
实验:
[root@node1 ~]# cd /etc/sysconfig/network-scripts/
[root@node1 network-scripts]# vim ifcfg-westos 
#############################
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=none
IPADDR0=172.25.254.130
PREFIX0=24
IPADDR1=172.25.254.230
PREFIX1=24
NAME=westos

在这里插入图片描述

  [root@node1 network-scripts]# systemctl restart network
 ##ifconfig无法查看到一个设备上的多个ip
[root@node1 network-scripts]# ifconfig eth0

在这里插入图片描述

##ip addr show专门用于查看多个ip
[root@node1 network-scripts]# ip addr show eth0

在这里插入图片描述

3.3.双网卡ip的设定

实验:
##先再添加一块网卡

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

    [root@node1 ~]# ifconfig

在这里插入图片描述

[root@node1 ~]# cd /etc/sysconfig/network-scripts/
[root@node1 network-scripts]# ls
[root@node1 network-scripts]# cp ifcfg-eth0 ifcfg-eth1
[root@node1 network-scripts]# ls

在这里插入图片描述

 [root@node1 network-scripts]# vim ifcfg-eth1 
 #############################
    DEVICE=eth1   ##设备名必须更改
    ONBOOT=yes
    BOOTPROTO=none
    IPADDR=172.25.254.230
    PREFIX=24
    NAME=westos   ##链接名可以不做更改

在这里插入图片描述

 [root@node1 network-scripts]# systemctl restart network
 [root@node1 network-scripts]# ifconfig

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值