Skip to content

通过知识获得解放,通过技术获得自由

Menu
  • 专题目录
  • 液压相关
    • 液压技术
    • 液压相邻技术
    • 液压应用
  • 计算机相关
    • 计算机和软件
    • 网络和网站技术
  • 哲学
  • 关于本站
Menu

centos8 添加网卡和设置静态ip[0]

Posted on 2023年11月14日2025年5月6日 by

对于双网卡的centos8系统,在系统安装后再添加的网卡,需要手动配置网卡信息。

双网卡的系统应用,主要包括将一个网卡设置为外网,一个网卡设置为内网,并设置成DHCP服务器。将内网设备通过内网网卡连接,内网网卡NAT到外网网卡上网。

本文主要实现的技术包括:添加网卡,静态IP设置,DHCP,NAT上网,IPTABLES设置,PVE多vps的网口虚拟联网

在不需要单独配置网卡的驱动的情况下,网卡的配置主要包含以下部分:

  • 检查网络
ifconfig

  • 添加网卡配置文件(ens19为内网网卡,ens18为外网网卡系统默认)
uuidgen ens19 #生成ens19的UUID
cd /etc/sysconfig/network-scripts/
cp ifcfg-ens18 ifcfg-ens19
nano ifcfg-ens19
  • 设置为静态IP,修改如下:
TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
#BOOTPROTO="dhcp"
BOOTPROTO="static"
IPADDR="192.168.0.1"
GATEWAY="192.168.1.1"  #内网设备需要指定外网的网关,不指定本机就是网关
NETMASK="255.255.255.0"
DNS1="114.114.114.114"
#DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="ens19"
UUID="c98f8d72-2cb1-48fa-90aa-b24c603d096e"
DEVICE="ens19"
ONBOOT="yes"
  • 配置后重启,最终效果:

  • 安装dhcp服务器
yum install dhcp-server
ip add sh dev ens19 #绑定网卡
#cp /usr/share/doc/dhcp-server/dhcpd.conf.example /etc/dhcp/dhcpd.conf
nano /etc/dhcp/dhcpd.conf

修改如下

# DHCP Server Configuration file.
# see /usr/share/doc/dhcp-server/dhcpd.conf.example
# see dhcpd.conf(5) man page
#
######################################################
#Define the domain name of your DHCP server
option domain-name "PVE2020";

# Define the IP address or Hostname of your DNS server
#option domain-name-servers ns1.kifarunix-demo.com;

#If you have multiple DNS servers, specify them as;
# option domain-name-servers ns1.kifarunix-demo.com, ns2.kifarunix-demo.com;

# Define how long should an endpoint use the assigned IP address
default-lease-time 3600; # One hour

# Define the maximum lease time
max-lease-time 7200; # Two hours

# Other values you can use are 86400 (one day), 604800 (one week) and 2592000 (30 days).
# 
# Declare the DHCP server official for the local network
authoritative;# Change the default log file for DHCP server.
log-facility local7;

#### Subnet Declaration ####
# The subnet declaration includes a range of IP addresses that a DHCP server can assign to clients.
# and global options to apply for every DHCP client.
#
# Define the subnet of the DHCP server interface (Network address and subnet mask)
subnet 192.168.0.0 netmask 255.255.255.0 {
 # Define the broadcast address
 option broadcast-address 192.168.0.255;
 # Define the gateway
 option routers 192.168.0.1;
 option domain-name-servers 1.1.1.1;
 # Define the range of IP addresses to be leased
 range 192.168.0.2 192.168.0.120;
}

启动dhcpd

systemctl enable --now dhcpd
systemctl status dhcpd #检查状态

  • 开启NAT(实现内网网卡通过外网网卡上网)

本设置单次有效,系统重启即失效

nano /etc/sysctl.conf

添加如下内容:

net.ipv4.ip_forward=1

设置生效:

sysctl -p
  • 开启NAT+IP伪装

由于centos8默认没有安装iptables,因此需要手动安装

yum -y install iptables-services

单次启动测试

iptables -F
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -t nat -A POSTROUTING -o ens18 -j MASQUERADE  #ens18是可以上外网的网卡

检查路由表

route -n

保存规则

service iptables save
systemctl restart iptables

查看配置文件

nano /etc/sysconfig/iptables

开机启动

systemctl restart iptables
systemctl status iptables
systemctl enable iptables

 

  • 客户端检查

DHCP获取IP

联网测试

win10联网测试

  • 小结:

最终实现centos8作为路由器的功能:其中包括自动获取外部ip实现上网,内网网口的添加和静态IP设置,内网的DHCP服务器功能,并将内网设备通过NAT方式实现上网的功能。

 

  • 补充问题——扩容

以上设置对于两个网卡的实现没有问题,但是对于内网有多个设备需要联网的情况,实际上又不相同。可能的解决措施包括以下方式:

1)采用交换机进行扩充,实现一个一个网口的多个DHCP的分配,网段在同一个网段,最多容纳的设备254个

2)采用多个网卡的方式进行处理,限于网卡数量的限制,一般适合6个设备的有线联网,在dhcp前需要将多个网卡组成网桥,再dhcp,或者在每个网卡上单独的设置dhcp的网段,再用vlan实现内部的导通(在OPNsense防火墙简介和安装和pfsense简介和安装过程可证明)

3)将以上两种方式组合,实现254*6个设备的联网,但是本身的意义不大,毕竟出口的带宽就这么大。

参考资料:

  1. https://my.oschina.net/u/4357584/blog/3345662
  2. https://www.cnblogs.com/lilto/p/11876682.html
  3. http://blog.it780.com/?p=88
  4. https://www.helplib.cn/%E6%9C%88%E6%9C%88/install-and-setup-dhcp-server-on-centos-8
  5. https://www.linuxprobe.com/chapter-14.html
  6. https://www.cnblogs.com/starshine-zhp/p/12013961.html
  7. https://blog.csdn.net/yu132563/article/details/78926259

欢迎回来

希望本站对你有所帮助!

如有疑问请联系info@fanlog.org
2023 年 11 月
一二三四五六日
 12345
6789101112
13141516171819
20212223242526
27282930 
« 6 月    

AI辅助 (17)

© 2025 | Powered by Superbs Personal Blog theme
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept All”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent.
Cookie SettingsAccept All
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT