栏目分类:
子分类:
返回
终身学习网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
终身学习网 > IT > 系统运维 > 运维 > Linux

Linux网络设置

Linux 更新时间:发布时间: 百科书网 趣学号

目录

一、查看网络配置信息

1.查看网卡信息

2.查看和修改主机名

3.查看路由表条目

4.查看网络连接状况

5.获取socket(ip、端口)统计信息

6.测试网络连接性

7.跟踪数据包的路由途径

8.域名解析

 二、配置网络参数

1.修改网卡ip地址、子网掩码

(1)临时配置

(2)修改网卡配置文件

2.禁用或重启网卡

3.为网卡绑定虚拟接口

4. 添加或删除路由条目

(1)添加静态路由

(2)添加默认路由

(3)删除路由条目

(4)配置文件永久生效(需要重启)

5. 配置DNS服务器地址


一、查看网络配置信息

1.查看网卡信息

[root@localhost ~]# ifconfig [网卡名]

                                ip addr 或 ip a

ens33Centos7第一张默认网卡为ens33
lo回环网卡 代表本机
virbr0Linux KVM虚拟网卡
[root@localhost ~]#ifconfig
ens33: flags=4163  mtu 1500
        inet 192.168.116.10  netmask 255.255.255.0  broadcast 192.168.116.255
        inet6 fe80::7791:1d06:d2da:af8e  prefixlen 64  scopeid 0x20
        ether 00:0c:29:3b:4b:c3  txqueuelen 1000  (Ethernet)
        RX packets 110  bytes 14140 (13.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 143  bytes 25367 (24.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:6b:ab:7a  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@localhost ~]#ifconfig ens33
ens33: flags=4163  mtu 1500
        inet 192.168.116.10  netmask 255.255.255.0  broadcast 192.168.116.255
        inet6 fe80::7791:1d06:d2da:af8e  prefixlen 64  scopeid 0x20
        ether 00:0c:29:3b:4b:c3  txqueuelen 1000  (Ethernet)
        RX packets 160  bytes 18148 (17.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 169  bytes 28499 (27.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

mtu

代表最大传输单元,它的单位是字节。在我们常用的以太网中,MTU一般是1500,而无线路由器默认一般是 1492

  • 本地MTU值 > 网络MTU值 → 数据需要拆包,效率下降
  • 本地MTU值 < 网络MTU值 → 未发挥最大传输能力
  • 本地MTU值 = 网络MTU值 → 理想的本地MTU值
inet表示网络接口的IP地址
netmask表示网络接口的子网掩码
broadcast表示网络接口所在网络的广播地址
ether表示网络接口的物理地址(MAC地址)

2.查看和修改主机名

[root@localhost ~]# hostname [新主机名]      //查看主机名 [ 临时修改成新主机名 ]

[root@localhost ~]#hostnamectl set-hostname [新主机名]    //永久修改成新主机名

[root@localhost ~]#hostname
localhost.localdomain
[root@localhost ~]#hostname localhostmain
[root@localhost ~]#hostname
localhostmain

[root@localhost ~]#hostnamectl set-hostname localhostmain
[root@localhost ~]#hostname
localhostmain

3.查看路由表条目

[root@localhost ~]# route [-n]       //查看路由条目 [ 追加显示本机路由条目 ]

[root@localhost ~]#route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         gateway         0.0.0.0         UG    100    0        0 ens33
192.168.116.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

[root@localhost ~]#route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.116.2   0.0.0.0         UG    100    0        0 ens33
192.168.116.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

4.查看网络连接状况

[root@localhost ~]# netstat [选项]  

                                  -a        显示当前主机中所有活动的网络连接信息

                                  -n        以数字的形式显示相关的主机地址、端口等信息

                                  -r         显示路由表信息

                                  -l          显示处于监听状态的网络连接及端口信息

                                  -t          查看TCP协议相关的信息

                                  -u         显示UDP协议相关的信息

                                  -p         显示与网络连接相关联的进程号、进程名称信息(需root权限)

[root@localhost ~]# netstat
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0     48 localhostmain:ssh       192.168.116.1:52403     ESTABLISHED
tcp        0      0 localhostmain:ssh       192.168.116.1:52404     ESTABLISHED
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  5      [ ]         DGRAM                    10496    /run/systemd/journal/socket
unix  28     [ ]         DGRAM                    10498    /dev/log
unix  2      [ ]         DGRAM                    23422    /var/run/chrony/chronyd.sock
unix  2      [ ]         DGRAM                    12260    /run/systemd/shutdownd
unix  3      [ ]         DGRAM                    10476    /run/systemd/notify
unix  2      [ ]         DGRAM                    10478    /run/systemd/cgroups-agent
unix  3      [ ]         STREAM     CONNECTED     30403

[root@localhost ~]#netstat -ntap
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      1650/dnsmasq
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1254/sshd
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      1

5.获取socket(ip、端口)统计信息

[root@localhost ~]# ss [选项]

                                        -t    tcp 显示TCP协议的sockets
                                        -u    udp 显示 UDP协议的sockets
                                        -n    numeric 不解析服务的名称,如“22”端口不会显示成“ssh”
                                        -l    listening 只显示处于监听状态的端口
                                        -p    processes 显示监听端口的进程(Ubuntu 上需要sudo)
                                        -a    all 对TCP协议来说,既包含监听的端口,也包含建立的连接
                                        -r    resolve 把IP解释为域名,把端口号解释为协议名称

[root@localhost ~]#ss -ntlp
State       Recv-Q Send-Q Local Address:Port               Peer Address:Port     
LISTEN      0      5      192.168.122.1:53                       *:*                   users:(("dnsmasq",pid=1650,fd=6))
LISTEN      0      128          *:22                       *:*                   users:(("sshd",pid=1254,fd=3))
LISTEN      0      128    127.0.0.1:631                      *:*                   users:(("cupsd",pid=1256,fd=11))
LISTEN      0      100    127.0.0.1:25                       *:*                   users:(("master",pid=1569,fd=13))
LISTEN      0      128    127.0.0.1:6010                     *:*                   users:(("sshd",pid=2185,fd=9))
LISTEN      0      128          *:111                      *:*                   users:(("rpcbind",pid=773,fd=8))
LISTEN      0      128       [::]:22                    [::]:*                   users:(("sshd",pid=1254,fd=4))
LISTEN      0      128      [::1]:631                   [::]:*                   users:(("cupsd",pid=1256,fd=10))
LISTEN      0      100      [::1]:25                    [::]:*                   users:(("master",pid=1569,fd=14))
LISTEN      0      128      [::1]:6010                  [::]:*                   users:(("sshd",pid=2185,fd=8))
LISTEN      0      128       [::]:111                   [::]:*                   users:(("rpcbind",pid=773,fd=11))

6.测试网络连接性

[root@localhost ~]# ping [选项] 主机名(域名或ip地址)默认长ping,按 Crtl+c退出

[root@localhost ~]#ping www.baidu.com
PING www.wshifen.com (45.113.192.102) 56(84) bytes of data.
64 bytes from 45.113.192.102 (45.113.192.102): icmp_seq=1 ttl=128 time=285 ms
64 bytes from 45.113.192.102 (45.113.192.102): icmp_seq=2 ttl=128 time=291 ms
64 bytes from 45.113.192.102 (45.113.192.102): icmp_seq=3 ttl=128 time=248 ms

7.跟踪数据包的路由途径

[root@localhost ~]# traceroute  主机名(域名或ip地址)

[root@localhost ~]#traceroute 192.168.98.105
traceroute to 192.168.98.105 (192.168.98.105), 30 hops max, 60 byte packets
 1  gateway (192.168.116.2)  0.236 ms  0.166 ms  0.132 ms
 2  * * *
 3  * * *
 4  * * *
 5  * * *
 6  * * *
 7  * * *
 8  * * *

8.域名解析

[root@localhost ~]# nslookup 主机域名

或                             dig 主机域名

[root@localhost ~]#nslookup www.baidu.com
Server:         8.8.8.8
Address:        8.8.8.8#53

Non-authoritative answer:
www.baidu.com   canonical name = www.a.shifen.com.
www.a.shifen.com        canonical name = www.wshifen.com.
Name:   www.wshifen.com
Address: 103.235.46.40

[root@localhost ~]#dig www.baidu.com

; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7 <<>> www.baidu.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 46594
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;www.baidu.com.                 IN      A

;; ANSWER SECTION:
www.baidu.com.          496     IN      CNAME   www.a.shifen.com.
www.a.shifen.com.       57      IN      A       14.215.177.38
www.a.shifen.com.       57      IN      A       14.215.177.39

;; Query time: 78 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: 四 8月 11 12:08:07 CST 2022
;; MSG SIZE  rcvd: 101

 二、配置网络参数

临时配置——使用命令调整网络参数

  • 简单、快速,可直接修改运行中的网络参数
  • 一般只适合在调试网络的过程中使用
  • 系统重启以后,所做的修改将会失效

固定设置——通过配置文件修改网络参数 

  • 修改各项网络参数的配置文件
  • 适合对服务器设置固定参数时使用
  • 需要重载网络服务或者重启以后才会生效

1.修改网卡ip地址、子网掩码

(1)临时配置

[root@localhost ~]# ifconfig 网卡设备名 ip地址/子网掩码

[root@localhost ~]# ifconfig 网卡设备名 ip地址 netmask 子网掩码

[root@localhost ~]#ifconfig ens36 192.168.116.20/24
[root@localhost ~]#ifconfig ens36
ens36: flags=4163  mtu 1500
        inet 192.168.116.20  netmask 255.255.255.0  broadcast 192.168.116.255
        inet6 fe80::89d2:a0ed:c51:3469  prefixlen 64  scopeid 0x20
        ether 00:0c:29:3b:4b:cd  txqueuelen 1000  (Ethernet)
        RX packets 53  bytes 4074 (3.9 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 47  bytes 8120 (7.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

(2)修改网卡配置文件

[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33

[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE="Ethernet"    --设置网卡类型,“Ethernet”表示以太网
PROXY_METHOD="none"    
BROWSER_ONLY="no"      
BOOTPROTO="static"    --设置网卡的配置方式,“static”表示使用静态IP地址,“dhcp”时表示动态获取地址
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="d762dea5-cf23-4074-837a-1b1a7cd72ded"
DEVICE="ens33"    --设置网卡的名称
ONBOOT="yes"    --设置网卡是否在Linux操作系统启动时激活
IPADDR=192.168.116.10
NETMASK=255.255.255.0
GATEWAY=192.168.116.2 
DNS1=8.8.8.8

2.禁用或重启网卡

[root@localhost ~]# systemctl restart network     重启所有网卡

[root@localhost ~]# ifconfig 网卡设备名 up         启用网卡,不会更新ip

[root@localhost ~]# ifconfig 网卡设备名 down    禁用网卡,ifconfig会查看不到该网卡信息

[root@localhost ~]# ifup 网卡设备名               启用网卡,会更新ip

[root@localhost ~]# ifdown 网卡设备名          禁用网卡,仅是没有IP和流量,ifconfig能看到

3.为网卡绑定虚拟接口

[root@localhost ~]# ifconfig 网卡设备名:序号 ip地址/子网掩码

[root@localhost ~]#ifconfig

ens36: flags=4163  mtu 1500
        ether 00:0c:29:3b:4b:cd  txqueuelen 1000  (Ethernet)
        RX packets 90  bytes 7368 (7.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 80  bytes 12292 (12.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens36:1: flags=4163  mtu 1500
        inet 192.168.116.11  netmask 255.255.255.0  broadcast 192.168.116.255
        ether 00:0c:29:3b:4b:cd  txqueuelen 1000  (Ethernet)

4. 添加或删除路由条目

(1)添加静态路由

[root@localhost ~]# route add -net 网段地址 gw 下一跳地址(网关)[dev 网卡设备名]

                                                                                                             不加默认为ens33

                                                 -host 主机名

[root@localhostmain ~]#route add -net 192.168.110.0/24 gw 192.168.116.2 dev ens36
[root@localhostmain ~]#route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         gateway         0.0.0.0         UG    101    0        0 ens36
default         gateway         0.0.0.0         UG    102    0        0 ens33

//此条为添加的路由条目
192.168.110.0   gateway         255.255.255.0   UG    0      0        0 ens36

192.168.116.0   0.0.0.0         255.255.255.0   U     101    0        0 ens36
192.168.116.0   0.0.0.0         255.255.255.0   U     101    0        0 ens36
192.168.116.0   0.0.0.0         255.255.255.0   U     102    0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

(2)添加默认路由

 [root@localhost ~]# route add default gw 下一跳地址 dev 网卡设备名

 [root@localhost ~]# route add -net 0.0.0.0 netmask 0.0.0.0 gw 下一跳地址 dev 网卡设备名

                                                                                                       (网卡需要开启网关配置)

(3)删除路由条目

[root@localhost ~]# route del -net 网段地址 gw 下一跳地址(网关)[dev 网卡设备名]

                                                -host 主机名

(4)配置文件永久生效(需要重启)

[root@localhost ~]#vim /etc/sysconfig/static-routes
                                any net 网段/掩码 gw 下一跳地址
                                any host 主机地址 gw 下一跳地址

 [root@localhostmain ~]#vim /etc/sysconfig/network-scripts/route-网卡设备名

                                        目标网段/掩码 via 下一跳地址 dev 网卡设备名
                                        目标主机地址 via 下一跳地址 dev 网卡设备名

5. 配置DNS服务器地址

[root@localhostmain ~]#vim /etc/resolv.conf

nameserver 服务器地址

[root@localhostmain ~]#vim /etc/resolv.conf
# Generated by NetworkManager
search localdomain
nameserver 192.168.116.2
nameserver 8.8.8.8
nameserver 114.114.114.114

转载请注明:文章转载自 www.051e.com
本文地址:http://www.051e.com/it/1050310.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 ©2023-2025 051e.com

ICP备案号:京ICP备12030808号