第7章:CentOS系统新装(配置主机名、IP、DNS等);配置网络(多网卡绑定技术、路由配置等)
CentOS系统新装
vm虚拟机需要注意网卡的工作模式以及是否接入到正确的vm虚拟网络中(vm---查看---网络编辑器)

更换主机名: 方法一:(CentOS7推荐使用该方法,CentOS6使用方法二) 1.hostnamectl set-hostname newname 2.重新连接or重新打开终端 3.修改/etc/hosts文件 方法二: 1.修改主机名文件(CentOS7:/etc/hostname;CentOS6:/etc/sysconfig/network) 2.激活主机名:hostname newname 3.重新连接or重新打开终端 4.修改/etc/hosts文件 注意:主机名修改后,要修改文件/etc/hosts(因为某些程序的运行会使用到主机名,若主机名没有和127.0.0.1正确对应,将会导致故障)

网络配置修改后,一般需要激活配置 方式1:service network restart #重启网卡服务 方式2:/etc/init.d/network reload #重新加载配置,命令有start | restart | stop | reload; 方式3:modprobe -r e1000;modprobe e1000 #重装网卡驱动 注意:service NetworkManager stop #关闭NetworkManager,否则NetworkManager可能会影响配置 chkconfig NetworkManager off #设置不开机启动

设置本地开机时,图形化界面自动登陆root用户 [root@yefeng28325 ~]# vim /etc/gdm/custom.conf [daemon] AutomaticLoginEnable=true #新增 AutomaticLogin=root #新增

主机名、DNS、端口等相关文件 /etc/hosts #本地DNS解析文件,getent hosts查看/etc/hosts内容 /etc/resolv.conf #本地NDS服务器配置文件 /etc/nsswitch.conf #配置passwd、shadow、hosts等生效顺序的文件,例如文件中hosts: files dns表示为本地/etc/hosts的优先级比dns高;若改为hosts: dns files后,则优先使用dns服务器的解析结果 /etc/services #该文件记录了各类协议使用的端口号

bash-completiontab键自动补全功能(centos7默认已安装) yum源和centos6镜像源没有bash-completion,需要安装epel源 yum install bash-completion
网络配置

多网卡bonding;网卡绑定的7种mode;案例:两块万兆bonding后带宽翻倍 ================================================================================================================== #cat ifcfg-bond0 DEVICE=bond0 TYPE=Bond ONBOOT=yes BOOTPROTO=static IPADDR=10.176.7.11 NETMASK=255.255.255.0 #cat /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 TYPE=Ethernet ONBOOT=yes BOOTPROTO=none MASTER=bond0 SLAVE=yes #cat /etc/sysconfig/network-scripts/ifcfg-eth1 DEVICE=eth1 TYPE=Ethernet ONBOOT=yes BOOTPROTO=none MASTER=bond0 SLAVE=yes #cat /proc/net/bonding/bond0 ----加载内核bonding模块, mode=0 是RR负载均衡模式 #cat /etc/modprobe.d/bonding.conf # modprobe bonding alias bond0 bonding options bond0 mode=0 miimon=100 //这一行也可以放到bond0配置文件中,比如:BONDING_OPTS="miimon=100 mode=4 xmit_hash_policy=layer3+4" 用iperf 多连接测试bonding后的带宽发现,发送端能用上两张网卡,但是接收队列只能使用一张物理网卡 ------------------------------------------------------------------------------------------------------------- 网卡绑定mode共有七种(0~6) bond0、bond1、bond2、bond3、bond4、bond5、bond6 常用的有三种 mode=0:平衡负载模式 (balance-rr),有自动备援,两块物理网卡和bond网卡使用同一个mac地址,但需要”Switch”支援及设定。 mode=1:自动备援模式 (balance-backup),其中一条线若断线,其他线路将会自动备援。 mode=6:平衡负载模式(balance-alb),有自动备援,不必”Switch”支援及设定,两块网卡是使用不同的MAC地址 Mode 4 (802.3ad): This mode creates aggregation groups that share the same speed and duplex settings, and it requires a switch that supports an IEEE 802.3ad dynamic link. Mode 4 uses all interfaces in the active aggregation group. For example, you can aggregate three 1 GB per second (GBPS) ports into a 3 GBPS trunk port. This is equivalent to having one interface with 3 GBPS speed. It provides fault tolerance and load balancing. 需要说明的是如果想做成mode 0的负载均衡,仅仅设置这里options bond0 miimon=100 mode=0是不够的,与网卡相连的交换机必须做特殊配置(这两个端口应该采取聚合方式),因为做bonding的这两块网卡是使用同一个MAC地址.从原理分析一下(bond运行在mode 0下): mode 0下bond所绑定的网卡的IP都被修改成相同的mac地址,如果这些网卡都被接在同一个交换机,那么交换机的arp表里这个mac地址对应的端口就有多 个, 那么交换机接受到发往这个mac地址的包应该往哪个端口转发呢?正常情况下mac地址是全球唯一的,一个mac地址对应多个端口肯定使交换机迷惑了。 所以 mode0下的bond如果连接到交换机,交换机这几个端口应该采取聚合方式(cisco称为 ethernetchannel,foundry称为portgroup), 因为交换机做了聚合后,聚合下的几个端口也被捆绑成一个mac地址.我们的解决办法是,两个网卡接入不同的交换机即可。 mode6模式下无需配置交换机,因为做bonding的这两块网卡是使用不同的MAC地址。 mod=5,即:(balance-tlb) Adaptive transmit load balancing(适配器传输负载均衡) 特点:不需要任何特别的switch(交换机)支持的通道bonding。在每个slave上根据当前的负载(根据速度计算)分配外出流量。如果正在接受数据的slave出故障了,另一个slave接管失败的slave的MAC地址。 该模式的必要条件:ethtool支持获取每个slave的速率. ------------------------------------------------------------------------------------------------------------- 案例:两块万兆bonding后带宽翻倍 #ethtool bond0 Settings for bond0: Supported ports: [ ] Supported link modes: Not reported Supported pause frame use: No Supports auto-negotiation: No Advertised link modes: Not reported Advertised pause frame use: No Advertised auto-negotiation: No Speed: 20000Mb/s Duplex: Full Port: Other PHYAD: 0 Transceiver: internal Auto-negotiation: off Link detected: yes [root@phy 16:55 /root] #cat /etc/sysconfig/network-scripts/ifcfg-bond0 DEVICE=bond0 BOOTPROTO=static TYPE="ethernet" IPADDR=100.1.1.2 NETMASK=255.255.255.192 ONBOOT=yes USERCTL=no PEERDNS=no BONDING_OPTS="miimon=100 mode=4 xmit_hash_policy=layer3+4" #cat /etc/modprobe.d/bonding.conf alias netdev-bond0 bonding #lsmod |grep bond bonding 137339 0 #cat ifcfg-bond0 DEVICE=bond0 BOOTPROTO=static TYPE="ethernet" IPADDR=100.81.131.221 NETMASK=255.255.255.192 ONBOOT=yes USERCTL=no PEERDNS=no BONDING_OPTS="miimon=100 mode=4 xmit_hash_policy=layer3+4" #cat ifcfg-eth1 DEVICE=eth1 TYPE="Ethernet" HWADDR=7C:D3:0A:E0:F7:81 BOOTPROTO=none ONBOOT=yes MASTER=bond0 SLAVE=yes PEERDNS=no RX_MAX=`ethtool -g "$DEVICE" | grep 'Pre-set' -A1 | awk '/RX/{print $2}'` RX_CURRENT=`ethtool -g "$DEVICE" | grep "Current" -A1 | awk '/RX/{print $2}'` [[ "$RX_CURRENT" -lt "$RX_MAX" ]] && ethtool -G "$DEVICE" rx "$RX_MAX"

多网卡绑定技术(bonding、team)及配置;网桥及配置网桥 bonding(CentOS6) bonding工作模式 mode 0:balance-rr 轮询模式 mode 1:active-backup 主备模式,只有一张网卡是活动的 mode 3: broadcast 广播模式,两张网卡均作出响应(例如ping bonding网卡时,会收到多份icmp reply) CentOS7推荐使用网络组network teaming,而不再是bonding ---------------------------------------------------------------------------- 配置步骤 1.删除ifcfg-IFACE文件 2.停止NM服务 service NetworkManager stop 3.创建bonding配置文件ifcfg-bond0(即创建逻辑接口) [root@yefeng network-scripts]# cat ifcfg-bond0 DEVICE=bond0 IPADDR=192.168.1.9 PREFIX=24 BONDING_OPTS="miimon=100 mode=3" #每100毫秒监测一次链路连接状态 4.创建ifcfg-IFACE文件(加入到逻辑接口) [root@yefeng network-scripts]# cat ifcfg-eth0 DEVICE=eth0 MASTER=bond0 SLAVE=yes #两台设备均为yes [root@yefeng network-scripts]# cat ifcfg-eth1 DEVICE=eth1 MASTER=bond0 SLAVE=yes #两台设备均为yes 5.重启网络服务 service network restart 6.查看逻辑接口工作状态 cat /proc/net/bonding/bond0 7.确认现象 [root@yefeng ~]# ping 192.168.1.9 PING 192.168.1.9 (192.168.1.9) 56(84) bytes of data. 64 bytes from 192.168.1.9: icmp_seq=1 ttl=64 time=0.190 ms 64 bytes from 192.168.1.9: icmp_seq=1 ttl=64 time=0.207 ms (DUP!) #因为使用mode3 所以受到双份的回包 64 bytes from 192.168.1.9: icmp_seq=2 ttl=64 time=0.211 ms 64 bytes from 192.168.1.9: icmp_seq=2 ttl=64 time=0.225 ms (DUP!) 64 bytes from 192.168.1.9: icmp_seq=3 ttl=64 time=0.204 ms 64 bytes from 192.168.1.9: icmp_seq=3 ttl=64 time=0.218 ms (DUP!) 删除bond0 0.lsmod 显示加载的所有驱动模块 1.禁用网卡 ifconfig bond0 down 2.删除模块 rmmod bonding/modprobe -r bonding 3.删除配置文件 ifcfg-bond0;还原eth0文件的配置 4.重启网卡 ---------------------------------------------------------------------------- CentOS7使用nmcli实现bonding 1.创建逻辑接口网络方案 nmcli connection add type bond con-name mybond0 ifname mybond0 mode broadcast 2.配置子接口加入到逻辑接口 nmcli connection add type bond-slave ifname eth0 master mybond0 nmcli connection add type bond-slave ifname eth1 master mybond0 3.先启动子接口 nmcli connection up bond-slave-eth0 nmcli connection up bond-slave-eth1 4.再启动逻辑接口 nmcli connection up mybond0 nmcli connection show 5.修改逻辑接口为静态IP nmcli connection modify mybond0 ipv4.method manual ipv4.addresses 192.168.1.7/24 6.确认现象 [root@yefeng ~]# ping 192.168.1.7 PING 192.168.1.7 (192.168.1.7) 56(84) bytes of data. 64 bytes from 192.168.1.7: icmp_seq=1 ttl=64 time=0.214 ms 64 bytes from 192.168.1.7: icmp_seq=1 ttl=64 time=0.220 ms (DUP!) 64 bytes from 192.168.1.7: icmp_seq=2 ttl=64 time=0.165 ms 64 bytes from 192.168.1.7: icmp_seq=2 ttl=64 time=0.172 ms (DUP!) 64 bytes from 192.168.1.7: icmp_seq=3 ttl=64 time=0.214 ms 64 bytes from 192.168.1.7: icmp_seq=3 ttl=64 time=0.221 ms (DUP!) ---------------------------------------------------------------------------- CentOS7推荐使用网络组network teaming,而不再是bonding 配置方法与nmcli实现bonding基本相同;network teaming支持使用json进行配置 1.创建逻辑接口网络方案(创建网络组接口) nmcli connection add type team con-name team0 ifname team0 config '{"runner": {"name": "activebackup"}}' nmcli connection add type team con-name team0 ifname team0 config '{"runner": {"name": "roundrobin"}}' nmcli connection add type team con-name team0 ifname team0 config '{"runner": {"name": "broadcast"}}' 2.配置子接口加入到逻辑接口(创建port接口) nmcli connection add type team-slave ifname eth0 master team0 nmcli connection add type team-slave ifname eth1 master team0 3.修改逻辑接口为静态IP nmcli connection modify team0 ipv4.addresses 192.168.1.7/24 ipv4.method manual 4.先启动子接口 nmcli connection up team-slave-eth0 nmcli connection up team-slave-eth1 5.再启动逻辑接口 nmcli connection up team0 6.确认现象 teamdctl team0 state #查看team0状态 [root@yefeng ~]# ping 192.168.1.7 PING 192.168.1.7 (192.168.1.7) 56(84) bytes of data. 64 bytes from 192.168.1.7: icmp_seq=1 ttl=64 time=0.338 ms 64 bytes from 192.168.1.7: icmp_seq=1 ttl=64 time=0.344 ms (DUP!) 64 bytes from 192.168.1.7: icmp_seq=2 ttl=64 time=0.199 ms 64 bytes from 192.168.1.7: icmp_seq=2 ttl=64 time=0.207 ms (DUP!) ---------------------------------------------------------------------------- CentOS7使用nmcli配置网桥(未进行实验) 注意:NetworkManager只支持以太网接口连接到网桥,不支持聚合接口 1.配置网桥 nmcli connection add type bridge con-name br0 ifname br0 nmcli connection modify br0 ipv4.method manual ipv4.addresses 192.168.1.7/24 nmcli connection add type bridge-slave con-name br0-port0 ifname eth0 master br0 nmcli connection add type bridge-slave con-name br0-port0 ifname eth1 master br0 2.查看网桥 cat /etc/sysconfig/network-scripts/ifcfg-br0 /etc/sysconfig/network-scripts/ifcfg-br0-port0 brctl show 3.删除网桥 brctl delbr br0 #删除网桥 brctl delif eth1 #删除网桥中的网卡

静态路由实验;配置动态路由 静态路由实验1: 实验环境:2台虚拟机都处于桥接模式;PC1 IP 10.0.0.1/24;PC2 IP 20.0.0.1/24 该实验现象有些特殊:连接在一台交换机上的2台PC,配置不同的网段,但是配置默认路由后,却可以ping通。 1.2台设备以指定出接口的方式添加默认路由 route add -net 0.0.0.0 netmask 0.0.0.0 dev eth1001 2.ping包测试正常 3.抓包:tcpdump -i eth1001 icmp 注意:tcpdump有点慢,需要等等 4.查看arp表,发现是有学习到arp表项的! -------------------------------------------------------------------- 静态路由实验2: 使用centos承担路由器角色 1.开启系统IP转发功能,0开启,1关闭 echo 1 > /proc/sys/net/ipv4/ip_forward 2.关闭防火墙 chkconfig iptables off #关闭开机启动防火墙 service iptables stop #关闭防火墙 traceroute tracepath ===================================================================== 配置动态路由(centos7) quagga包:支持多种路由协议:RIP/OSPF/BGP 一次简单的动态路由演示: 1.yum install quagga 2.rpm -ql quagga #找到其中一个文件/usr/share/doc/quagga-0.99.22.4/ospfd.conf.sample 3.将文件放入目录/etc/quagga cp /usr/share/doc/quagga-0.99.22.4/ospfd.conf.sample /etc/quagga/ospfd.conf 4.启动ospf进程:systemctl start ospfd 5.命令vtysh进入到ospf的配置界面(和思科的非常相似,命令也是可以补齐的) 6.ss -ntlp #可以看到ospf的进程信息 7./proc/sys/net/ipv4/ip_forward #centos系统的IP转发功能,0开启,1关闭 XORP有一个稳定但相当过时的BGP实现. Quagga,为您提供实际使用的最新版本(编写本文时为0.99.21),它在quagga.net上通常可靠,功能强大(现在具有多路径工作)并且在运行时易于(重新)配置. BIRD还具有良好的BGP实现,但由于重新配置需要编辑配置文件并指示守护进程重新加载(通过SIGHUP或birdc reload命令),因此管理起来很麻烦. 虚拟交换机技术:OVS vs Linux Bridge

网络测试工具;网络客户端工具;网络故障排查思路 ----------------------------------------------------------------------- 网络测试工具 1.测试网络连通性 hostname #显示主机名 mtr #测试网络连通性 ip route #查看路由表 arp -n #查看ARP表 ping -f Flood ping. DDOS攻击就是分布式的DOS攻击 linux ping默认TTL为64;windows的默认TTL为128 /proc/sys/net/ipv4/ip_default_ttl #设置默认TTL linux是支持ping ip地址对应的十进制数的,例如ping 3232243328 2.确定名称服务器使用(dns解析工具) nslookup host dig 3.路由跟踪 traceroute tracepath 4.抓包工具 tcpdump tcpdump -i eth1001 icmp #指定网卡,只抓icmp包 ----------------------------------------------------------------------- 网络客户端工具 ftp/lftp;子命令:get、mget、ls、help lftpget URL wget links URL #字符界面浏览器 ----------------------------------------------------------------------- 网络故障排查思路:当发现ping测试一个域名,无法ping通时 1.查看网络接口是否正常 ip link show 2.查看是否获取到ip ip add 3.到达网关是否正常 ping测试 4.确认dns配置 cat /etc/resolv.conf 5.确认网关设备到达外部网络是否正常 traceroute/ping

网卡相关常见操作(驱动的卸载与安装;网络服务开启与关闭) --------------------------------------------------------------- 查看网卡: dmesg |grep -i eth #可以看到网卡为e1000,但不明显 ethtool -i eth0 #可以看到网卡为e1000(driver对应的就是网卡名) lsmod #查看安装的驱动 卸载网卡驱动 modprobe -r e1000 rmmod e1000 安装网卡驱动 modprobe e1000 mii-tool 是查看,管理介质的网络接口的状态 mii-tool -v eth0 #查看网络接口的协商状态 mii-tool -F 100baseTx-FD #把网络接口eth0改为 1000Mb/s全双工的模式 ethtool 是用于查询及设置网卡参数的命令。 ethtool ethx #查询ethx网口基本设置 ethtool -s eth0 speed 100 duplex full #把网络接口eth0改为 1000Mb/s全双工的模式 --------------------------------------------------------------- 网卡接口关闭与激活 [root@localhost ~]# ifdown eth0 #关闭网络 [root@localhost ~]# ifup eth0 #启动网络 网络服务启动与关闭 方法一: [root@localhost ~]# service network stop #关闭网络服务 [root@localhost ~]# service network start #启动网络服务 [root@localhost ~]# service network restart #重启网络服务 方法二: [root@localhost ~]# /etc/init.d/network stop [root@localhost ~]# /etc/init.d/network start [root@localhost ~]# /etc/init.d/network restart 网卡状态查询 [root@localhost ~]# service network status 临时配置网卡信息,无需重启。 [root@localhost ~]# ifconfig eth0 10.1.1.10 netmask 255.0.0.0 查看网卡接口信息,默认列出所有接口 [root@localhost ~]# ifconfig 查看当前路由及网关信息 [root@localhost ~]# netstat -r

配置网卡别名;配置多个IP ------------------------------------------------ 配置网卡别名: ifconfig eth0:0 192.168.1.100/24 up ifconfig eth0:0 down if addr add 172.16.1.2/16 dev eth0 if addr add 172.16.1.2/16 dev eth0 label eth0:0 #加上label后,ifconfig命令也可以看到新增的ip if addr del 172.16.1.2/16 dev eth0 label eth0:0 ip addr flush dev eth0 label eth0:0 ------------------------------------------------ centos6中一张网卡配置多个IP: 配置临时测试IP: 1.ifconfig eth0:111 2.2.2.2/24(ifconfig,ip address均能查看到IP) 删除IP:ifconfig eth0:111 down 2.ip address add 3.3.3.3/24 dev eth0 (ip address能查看到IP,ifconfig却不行!) 删除IP:ip add del 3.3.3.3/24 dev eth0 配置永久IP: 目录/etc/sysconfig/network-scripts/下新增相应的文件,例如ifcfg-eth0:2
111111
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!