Linux网络服务基础必备(网卡、主机名、路由)

    技术2025-07-16  7

    网络基本服务

    文章目录

    网络基本服务一、网卡信息1、配置文件2、查看网卡信息【1】主要命令【2】主要参数解释【3】ethtool 可查看网卡数据传输速率,工作模式【4】netstat 查看网络连接情况状态,用于实验【5】ss查看网络连接情况,用于工作实操 3、网卡的三种模式【1】桥接模式【2】仅主机模式【3】NAT模式 4、修改网卡IP【1】临时修改【2】永久修改【3】测试 二、认识主机名1、配置文件2、修改主机名【1】永久修改【2】临时修改 三、路由配置1、路由查询2、路由添加与删除【1】静态路由【2】默认路由

    一、网卡信息

    1、配置文件

    文件路径:/etc/sysconfig/network-scripts/ifcfg-网卡名

    [root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33 TYPE=Ethernet(网卡类型) PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=static(设置网络接口IP地址的配置方式) DEFROUTE=yes(是否设置默认路由,若为yes则可以在该文件通过IPADDR和NETMASK两个参数来设置网关) IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy NAME=ens33 UUID=41c7bb50-9989-42f5-8656-08e912d3340a DEVICE=ens33(网络接口名) ONBOOT=yes(设置网络接口是否在系统启动时激活)

    2、查看网卡信息

    【1】主要命令

    (1)ifconfig查看所有网卡信息

    [root@localhost ~]# ifconfig ens33 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.60.10 netmask 255.255.255.0 broadcast 192.168.60.255 inet6 fe80::46a1:7cc7:c89e:283a prefixlen 64 scopeid 0x20<link> ether 00:0c:29:79:c5:44 txqueuelen 1000 (Ethernet) RX packets 28 bytes 3613 (3.5 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 45 bytes 5665 (5.5 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

    (2)ip add 查看网卡的IP地址,可看状态(ip link show 网卡名)

    [root@localhost ~]# ip link show ens33 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000 link/ether 00:0c:29:79:c5:44 brd ff:ff:ff:ff:ff:ff
    【2】主要参数解释

    【3】ethtool 可查看网卡数据传输速率,工作模式
    [root@localhost ~]# ethtool ens33 Settings for ens33: Supported ports: [ TP ] Supported link modes: 10baseT/Half 10baseT/Full 100baseT/Half 100baseT/Full 1000baseT/Full Supported pause frame use: No Supports auto-negotiation: Yes Supported FEC modes: Not reported Advertised link modes: 10baseT/Half 10baseT/Full 100baseT/Half 100baseT/Full 1000baseT/Full Advertised pause frame use: No Advertised auto-negotiation: Yes Advertised FEC modes: Not reported Speed: 1000Mb/s Duplex: Full Port: Twisted Pair PHYAD: 0 Transceiver: internal Auto-negotiation: on MDI-X: off (auto) Supports Wake-on: d Wake-on: d Current message level: 0x00000007 (7) drv probe link Link detected: yes
    【4】netstat 查看网络连接情况状态,用于实验
    [root@localhost ~]# netstat -natpu Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 1394/dnsmasq tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1146/sshd tcp6 0 0 :::22 :::* LISTEN 1146/sshd udp 0 0 0.0.0.0:111 0.0.0.0:* 784/rpcbind udp 0 0 127.0.0.1:323 0.0.0.0:* 837/chronyd udp6 0 0 :::948 :::* 784/rpcbind 参数: LISTEN:监听中 ESTABLISHED:已确认 -a:显示所有活动的网络连接信息 -n:以IP地址形式显示 -r:显示路由表 -t:查看tcp协议相关的信息 -u:查看udp协议相关的信息 -p:显示进程号
    【5】ss查看网络连接情况,用于工作实操
    [root@localhost ~]# ss -natpu Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port udp UNCONN 0 0 *:948 *:* users:(("rpcbind",pid=784,fd=7)) tcp LISTEN 0 128 *:111 *:* users:(("rpcbind",pid=784,fd=8)) 参数: -a:显示所有活动的网络连接信息 -n:显示服务端口号 -r:显示服务名称,默认 -l:显示监听端口 -t:查看tcp协议相关的信息 -u:查看udp协议相关的信息 -p:显示进程号

    3、网卡的三种模式

    【1】桥接模式

    把虚拟机暴露在局域网环境下,可以直接被访问,但不能访问外网(如www.baidu.com)

    【2】仅主机模式

    把各个虚拟机包括宿主机之间构建成一个同一网段局域网,无法访问其他网段。

    【3】NAT模式

    那虚拟机连接到类似三层交换机的虚拟网卡上,不仅可以局域网之内通信,而且还可以外部通信互联网。

    4、修改网卡IP

    【1】临时修改

    一般用于网络调试 (1)物理网卡 命令格式: ifconfig 网卡名 IP地址 /子网掩码位数 ifconfig 网卡名 IP地址 netmask 子网掩码

    [root@localhost ~]# ifconfig ens33 192.168.60.88/24 [root@localhost ~]# ifconfig ens33 192.168.60.87 netmask 255.255.255.0

    (2)逻辑网卡子接口 命令格式: ifconfig 网卡名:子接口(0/1/2/3…)IP地址 /子网掩码位数 ifconfig 网卡名:子接口(0/1/2/3…) IP地址 netmask 子网掩码

    [root@localhost ~]# ifconfig ens33:0 192.168.60.86/24 [root@localhost ~]# ifconfig ens33:0 192.168.60.85 netmask 255.255.255.0

    启用网卡:ifup 网卡名 禁用网卡:ifdown 网卡名

    [root@localhost ~]#ifup ens33 [root@localhost ~]#ifdown ens33
    【2】永久修改

    修改配置文件

    [root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33 TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=static 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=ens33 UUID=41c7bb50-9989-42f5-8656-08e912d3340a DEVICE=ens33 ONBOOT=yes IPADDR=192.168.60.88 GATEWAY=192.168.60.2 NETMASK=255.255.255.0
    【3】测试

    二、认识主机名

    1、配置文件

    文件路径:/etc/sysconfig/network

    [root@localhost ~]# cat /etc/sysconfig/network # Created by anaconda

    2、修改主机名

    【1】永久修改

    命令格式:hostnamectl set-hostname 主机名 激活方式:su两次切换

    [root@localhost ~]# hostnamectl set-hostname zhangqing [root@localhost ~]# su zhangsan su: user zhangsan does not exist [root@localhost ~]# useradd zhangsan [root@localhost ~]# su zhangsan [zhangsan@zhangqing root]$ su root 密码: [root@zhangqing ~]#
    【2】临时修改

    命令格式:hostname 主机名 激活方式:bash

    [root@zhangqing ~]# hostname localhost [root@zhangqing ~]# bash [root@localhost ~]#

    三、路由配置

    1、路由查询

    显示主机名:route(默认)

    [root@localhost ~]# route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface default gateway 0.0.0.0 UG 0 0 0 ens33 default gateway 0.0.0.0 UG 100 0 0 ens33 192.168.60.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33 192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0

    显示IP地址:route -n

    [root@localhost ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.60.2 0.0.0.0 UG 0 0 0 ens33 0.0.0.0 192.168.60.2 0.0.0.0 UG 100 0 0 ens33 192.168.60.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33 192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0

    2、路由添加与删除

    【1】静态路由

    添加:route add -net 路由网段/几位子网掩码 gw 下一跳地址(一般是本机网关)

    [root@localhost ~]# route add -net 192.168.70.0/24 gw 192.168.60.2 [root@localhost ~]# route add -net 192.168.80.0 netmask 255.255.255.0 gw 192.168.60.2 [root@localhost ~]# route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface default gateway 0.0.0.0 UG 0 0 0 ens33 default gateway 0.0.0.0 UG 100 0 0 ens33 192.168.60.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33 192.168.70.0 gateway 255.255.255.0 UG 0 0 0 ens33 192.168.80.0 gateway 255.255.255.0 UG 0 0 0 ens33 192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0

    删除:route del 路由网段/掩码

    [root@localhost ~]# route del -net 192.168.70.0/24 [root@localhost ~]# route del -net 192.168.80.0/24 [root@localhost ~]# route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface default gateway 0.0.0.0 UG 0 0 0 ens33 default gateway 0.0.0.0 UG 100 0 0 ens33 192.168.60.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33 192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
    【2】默认路由

    添加:route add default gw 下一跳地址

    [root@localhost ~]# route add default gw 192.168.60.2 [root@localhost ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.60.2 0.0.0.0 UG 0 0 0 ens33 0.0.0.0 192.168.60.2 0.0.0.0 UG 100 0 0 ens33

    删除:route del default gw 下一跳地址

    [root@localhost ~]# route del default gw 192.168.60.2 [root@localhost ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.60.2 0.0.0.0 UG 100 0 0 ens33
    Processed: 0.015, SQL: 9