linux中的网络配置

基本概念

什么是IP?

IP address : internet protocol address

ipv4 :  internet protocol version 4

IP是由32个bit组成。

11111110.11111110.11111110.11111110

254.254.254.254

什么是子网掩码?

子网掩码:用于标识ip中的网络位,可以说明子网网络的大小。

IP:      11111110.11111110.11111110.11111110

掩码:11111111.11111111.11111111.00000000

172.25.254.10 / 255.255.255.0

172.25.254.10 / 24             *** 24 = 255.255.255.0 ***

ip通信如何判定?

网络位一致,主机位不一致的2个ip可以直接通讯。

172.25.254.1 / 24   与   172.25.254.2 / 24   可以通信  原因:网络位相同,均为24

172.25.254.1 / 24   与   172.25.0.1 / 16       不能通信  原因:网络位置不同

 

网络设定工具


ping                     ## 检测网络是否通畅

ping  -c 1               ## ping 1次

ping  -w 1               ## 等待1秒

ping  -c1  -w1 ip        ## ping1次等待1秒


2.ifconfig                 ##查看或设定网络接口 

ifconfig                         ##查看或设定网络接口

ifconfig ens160              ##查看 ens160的信息

ifconfig device ip/24           ##设定

ifconfig device down             ##关闭

3.ip addr                         ##检测或这顶网络接口 

 

ip addr                                                      检测或这顶网络接口

ip addr show                                              检测

ip addr add ip/24 dev device                      设定

ip addr del dev ens160 ip/24                    删除

ip addr flush ens160                                刷新ens160的ip地址(删除所有)

 

更改ip步骤

ip addr flush ens160

ip addr add dev ens160 172.25.254.10/24

ip addr show ens160
五、图形方式设定ip             永久设定

1.设定   nm-connection-edit
2.更改

不建议用服务控制网络

systemctl restart NetworkManager

建议使用:

nmcli connection show

nmcli connection down 设置的链接名称 ##关闭ens160

nmcli connection up 设置的链接名称 ##开启ens160

六、命令方式设定网络

1.NetworkManager必须开启

2. nmcli的用法

<1>nmcli device   ##对设备

 

nmcli device connect ens160                        启用ens160网卡

nmcli device disconnect ens160                     关闭ens160网卡

nmcli device show ens160                             查看网卡信息

nmcli device status ens160                            查看网卡服务接口信息

  2.nmcli connection  对连接

nmcli connection show                                              查看连接

nmcli connection down 设定的连接名称                    关闭连接

nmcli connection up 设定的连接名称                          开启连接

nmcli connection delete 设定的连接名称                       删除连接

 

添加连接

nmcli connection add type ethernet con-name xxxxx ifname ens160 ip4 172.25.254.10/24

                                   类型                 连接名称          设备名称          地址    ip/子网掩码

 

 

更改连接

nmcli connection modify westos ipv4.addresses 172.25.254.10/24

nmcli connection down westos                                 关闭

nmcli connection up westos                                      开启

七、管理网络配置文件

1.网络配置目录

2.网络配置文件的名命规则

 

vim /etc/sysconfig/network-scripts/ifcfg-xxxx

DEVICE=ens16                            设备名称

BOOTPROTO=dhcp|static|none                      设备工作方式

ONBOOT=yes                            网络服务开启时自动激活网卡

IPADDR=                              IP地址

PREFIX=24                     子网掩码

NETMASK=255.255.255.0                     子网掩码

NAME=                                  接口名称

3.dhcp网络设定

vim /etc/sysconfig/network-scripts/ifcfg-ens160

DEVICE=ens160

ONBOOT=yes

BOOTPROTO=dhcp

1.nmcli connection reload                                  保存vim之后 重启连接

2.nmcli connection down ens160                       更改文件后,关闭

nmcli connection up ens160                               更改文件后,开启

4.静态网络设定文件

1.配置单个ip

vim /etc/sysconfig/network-scripts/ifcfg-ens160

DEVICE=ens160

ONBOOT=yes

IPADDR=172.25.254.100

NETMASK=255.255.255.0

BOOTPROTO=none

NAME=westos

1.编辑保存

nmcli connection reload                                      保存vim之后 重启连接

2.更改文件

nmcli connection down ens160                          更改文件后,关闭

nmcli connection up ens160                               更改文件后,开启

2.一块网卡上配置多个ip

vim /etc/sysconfig/network-scripts/ifcfg-ens160

DEVICE=ens160

ONBOOT=yes

IPADDR0=172.25.254.100

NETMASK0=255.255.255.0

BOOTPROTO=none

NAME=westos

IPADDR1=172.25.0.100

PREFIX1=24

 

1.编辑保存

nmcli connection reload                    保存vim之后 重启连接

2.更改文件

nmcli connection down ens160         更改文件后,关闭nmcli connection up ens160 ##更改文件后,开启ip addr show ens160

八、dhcp服务配置

1.在双网卡主机中使用镜像找到dhcp-server的安装包

rpm -ivh dhcp-server-4.3.6-30.el8.x86_64.rpm

2.用模板 生成配置文件

cp /usr/share/doc/dhcp-server/dh

vim /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; ##网关

}

39行之后全删除

 

 

 

 


 

[root@carry ~]# systemctl restart dhcpd

[root@carry ~]# systemctl status dhcpd

● dhcpd.service - DHCPv4 Server Daemon

Loaded: loaded (/usr/lib/systemd/system/dhcpd.service; disabled; vendor pres>

Active: active (running) since Fri 2020-05-08 21:26:42 CST; 1s ago

Docs: man:dhcpd(8)

man:dhcpd.conf(5)

Main PID: 2805 (dhcpd)

Status: "Dispatching packets..."

Tasks: 1 (limit: 11380)

Memory: 8.0M

CGroup: /system.slice/dhcpd.service

└─2805 /usr/sbin/dhcpd -f -cf /etc/dhcp/dhcpd.conf -user dhcpd -grou>

4.测试

建立ens160wei DHCP模式

在VMware编辑中选择虚拟网络编辑器

关闭本地DHCP分配ip服务

在Horizon_carry中重启网络服务,可以看到 ip GW dns 全部获取成功

在carry中 cat /var/lib/dhcpd/dhcpd.leases 也可以看到Horizon_carry获取成功

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值