Linux中的网络配置 1

一、什么是 IP ADDRESS

  1. internet  protocol  ADDRESS        ##网络进程地址 
  2.  
  3. ipv4    internet  protocol  version 4 
  4.  
  5. ip是由32个01组成
  6.  
  7. 11111110.11111110.11111110.11111110 = 254.254.254.254
  8. —————————————————————————— ————————
  9.          网络位              主机位

二、子网掩码

  1. 用来划分网络区域 
  2.  
  3. 子网掩码非0的位对应的ip上的数字表示这个ip的网络位 
  4.  
  5. 子网掩码0位对应的数字是ip的主机位 
  6.  
  7. 网络位表示网络区域 
  8.  
  9. 主机位表示网络区域里某台主机 
  10.  
  11. 11111111.11111111.11111111.00000000 = 255.255.255.0 = 24 
  12. —————————————————————————— ————————
  13.          网络位              主机位

三、ip通信判定

  1.  
  2. 网络位一致,主机位不一致的2个IP可以直接通讯 
  3.  
  4. 172.25.254.10/24    24=255.255.255.0 
  5.  
  6. 172.25.254.20/24 
  7.  
  8. 172.25.0.1/16
  9.  
  10. 前两个可以直接通讯,最后一个与其他俩个不能直接通讯
  11.  

四、网络设定工具        ##临时设定

1.ping     ##检测网络是否通畅

  1. ping  ip        ##检测网络是否通畅 
  2. ping -c1 ip        ##ping 1 次  c代表次数
  3. ping -w1 ip        ##等待1秒  w代表时间
  4. ping -c1 -w1 ip         ##ping1次等待1秒 


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

  1. ifconfig                ##查看或设定网络接口 
  2. ifconfig  ens160        ##查看 ens160的信息
  3. ifconfig device ip/24   ##设定 
  4. ifconfig device down    ##关闭 
  5. ifconfig device up      ##开启
     

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

  1. ip addr             ##检测或这顶网络接口 
  2. ip addr show                 ##检测 
  3. ip addr add ip/24 dev device     ##设定 
  4. ip addr del dev ens160 ip/24     ##删除 
  5. ip addr flush ens160         ##刷新ens160的ip地址(删除所有)

  1. 更改ip步骤 
  2. ip addr flush ens160 
  3. ip addr add  dev ens160 172.25.254.10/24
  4. ip addr show ens160  

五、图形方式设定ip       ##永久设定

1.设定

  1. 设定:
  2. nm-connection-editor

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.网络配置目录

etc/sysconfig/network-scripts/ 


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

  1. vim  /etc/sysconfig/network-scripts/ifcfg-xxxx 
  2. DEVICE=ens160                 ##设备名称 
  3. BOOTPROTO=dhcp|static|none         ##设备工作方式 
  4. ONBOOT=yes                 ##网络服务开启时自动激活网卡 
  5. IPADDR=                 ##IP地址 
  6. PREFIX=24                 ##子网掩码 
  7. NETMASK=255.255.255.0             ##子网掩码 
  8. NAME=                     ##接口名称

3.dhcp网络设定

  1. vim /etc/sysconfig/network-scripts/ifcfg-ens160 
  2. DEVICE=ens160 
  3. ONBOOT=yes 
  4. BOOTPROTO=dhcp
  5.  
  6. <1>nmcli connection reload  ##保存vim之后 重启连接
  7. <2>nmcli connection down ens160 ##更改文件后,关闭
  8.    nmcli connection up ens160   ##更改文件后,开启

4.静态网络设定文件

<1>配置单个ip

  1. vim /etc/sysconfig/network-scripts/ifcfg-ens160 
  2.  DEVICE=ens160 
  3.  ONBOOT=yes 
  4.  IPADDR=172.25.254.100 
  5.  NETMASK=255.255.255.0 
  6.  BOOTPROTO=none 
  7.  NAME=westos
  8. <1>编辑保存
  9.     nmcli connection reload  ##保存vim之后 重启连接
  10. <2>更改文件
  11.     nmcli connection down ens160 ##更改文件后,关闭
  12.     nmcli connection up   ens160 ##更改文件后,开启

<2>一块网卡上配置多个ip

  1. vim /etc/sysconfig/network-scripts/ifcfg-ens160 
  2. DEVICE=ens160 
  3. ONBOOT=yes 
  4. IPADDR0=172.25.254.100 
  5. NETMASK0=255.255.255.0 
  6. BOOTPROTO=none 
  7. NAME=westos 
  8. IPADDR1=172.25.0.100 
  9. PREFIX1=24
  10.  
  11. <1>编辑保存
  12.     nmcli connection reload  ##保存vim之后 重启连接
  13. <2>更改文件
  14.     nmcli connection down ens160 ##更改文件后,关闭
  15.     nmcli connection up   ens160 ##更改文件后,开启
  16. ip addr show ens160

八、dhcp服务配置

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

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

2.用模板 生成配置文件

  1. cp /usr/share/doc/dhcp-server/dhcpd.conf.example /etc/dhcp/dhcpd.conf 

  2. ##用模板 生成配置文件


3.修改配置文件

  1. vim /etc/dhcp/dhcpd.conf
  2. # dhcpd.conf 
  3. # # 
  4. # Sample configuration file for ISC dhcpd 
  5. #
  6. # option definitions common to all supported networks... 
  7. option domain-name "westos.com";                 ##域名
  8. option domain-name-servers 114.114.114.114;     ##dns
  9. default-lease-time 600; ##默认租约 
  10. max-lease-time 7200; ##最长租约
  11. # Use this to enble / disable dynamic dns updates globally. 
  12. #ddns-update-style none;
  13. # If this DHCP server is the official DHCP server for the local 
  14. # network, the authoritative directive should be uncommented. 
  15. #authoritative;
  16. #Use this to send dhcp log messages to a different log file (you also 
  17. # have to hack syslog.conf to complete the redirection). log-facility local7;
  18. # No service will be given on this subnet, but declaring it helps the 
  19. # DHCP server to understand the network topology. 
  20. 27 删除 
  21. 28 删除
  22. # This is a very basic subnet declaration.
  23. subnet 172.25.254.0 netmask 255.255.255.0 {                 ##子网设定 
  24.     range 172.25.254.160 172.25.254.200;                     ##IP地址池    
  25.         option routers 172.25.254.250;                         ##网关    
  26.         } 
  27. 39行之后全删除

  1. [root@carry ~]# systemctl restart dhcpd
  2. [root@carry ~]# systemctl status dhcpd
  3. ● dhcpd.service - DHCPv4 Server Daemon
  4.    Loaded: loaded (/usr/lib/systemd/system/dhcpd.service; disabled; vendor pres>
  5.    Active: active (running) since Fri 2020-05-08 21:26:42 CST; 1s ago
  6.      Docs: man:dhcpd(8)
  7.            man:dhcpd.conf(5)
  8.  Main PID: 2805 (dhcpd)
  9.    Status: "Dispatching packets..."
  10.     Tasks: 1 (limit: 11380)
  11.    Memory: 8.0M
  12.    CGroup: /system.slice/dhcpd.service
  13.            └─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、付费专栏及课程。

余额充值