Linux网络设置

一.查看网络配置

1.1、查看网络接口信息——ifconfig

[root@qiaozhi ~]# ifconfig [网络接口]

[root@qiaozhi ~]# ifconfig 
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.32.96  netmask 255.255.255.0  broadcast 192.168.32.255
        inet6 fe80::82b:b82c:1bd1:7b59  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:93:b8:47  txqueuelen 1000  (Ethernet)
        RX packets 16826  bytes 22553653 (21.5 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8758  bytes 571540 (558.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.32.199  netmask 255.255.255.0  broadcast 192.168.32.255
        inet6 fe80::c2da:ceee:1c1b:5bd1  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:93:b8:51  txqueuelen 1000  (Ethernet)
        RX packets 938  bytes 85448 (83.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 60  bytes 5960 (5.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 72  bytes 5720 (5.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 72  bytes 5720 (5.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:22:44:30  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


ifconfig 只显示开启的网卡

[root@qiaozhi ~]# ifconfig ens37 down 把网卡ens33关闭
[root@qiaozhi ~]# ifconfig  这里就找不到ens37
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.32.96  netmask 255.255.255.0  broadcast 192.168.32.255
        inet6 fe80::82b:b82c:1bd1:7b59  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:93:b8:47  txqueuelen 1000  (Ethernet)
        RX packets 16906  bytes 22559886 (21.5 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8809  bytes 582686 (569.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 72  bytes 5720 (5.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 72  bytes 5720 (5.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:22:44:30  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@qiaozhi ~]# ifconfig ens37 up 开启网卡ens37
[root@qiaozhi ~]# ifconfig  
这里就ens37就有了
ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.32.199  netmask 255.255.255.0  broadcast 192.168.32.255
        inet6 fe80::c2da:ceee:1c1b:5bd1  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:93:b8:51  txqueuelen 1000  (Ethernet)
        RX packets 952  bytes 88351 (86.2 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 82  bytes 9300 (9.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

1.2、查看主机名称——hostname

查看当前主机名:hostname [主机名]

[root@qiaozhi ~]# hostname 
qiaozhi

创建主机名:[root@qiaozhi ~]# hostname whywhy 只是临时创建 重启过会就没有了

[root@qiaozhi ~]# hostname whywhy
[root@qiaozhi ~]# su 刷新
[root@whywhy ~]# 
[root@whywhy ~]# reboot
Connection closing...Socket close.

Connection closed by foreign host.

Disconnected from remote host(新建会话1) at 12:11:33.

Type `help' to learn how to use Xshell prompt.
[C:\~]$ 

Connecting to 192.168.32.96:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.

Last login: Sat Sep 25 12:12:04 2021
[root@qiaozhi ~]#  重启之后回到了qiaozhi主机名

1.3、查看路由表——route

[root@qiaozhi ~]# route
[root@qiaozhi ~]# route -n 可以将路由记录中的地址显示为数字形式

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         gateway         0.0.0.0         UG    100    0        0 ens37
default         gateway         0.0.0.0         UG    101    0        0 ens33
192.168.32.0    0.0.0.0         255.255.255.0   U     100    0        0 ens37
192.168.32.0    0.0.0.0         255.255.255.0   U     101    0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0



[root@qiaozhi ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.32.2    0.0.0.0         UG    100    0        0 ens37
0.0.0.0         192.168.32.2    0.0.0.0         UG    101    0        0 ens33
192.168.32.0    0.0.0.0         255.255.255.0   U     100    0        0 ens37
192.168.32.0    0.0.0.0         255.255.255.0   U     101    0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

1.4、查看网络连接情况——netstat

格式:netstat [选项]

选项作用
-a显示主机中所有活动的网络连接信息
-n以数字的形式显示相关的主机地址
-t查看TCP协议相关的信息
-u显示UDP协议相关的信息
-p显示与网络连接相关联的进程号、进程名称信息
[root@qiaozhi ~]# netstat -natp 
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd           
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      1475/dnsmasq        
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1170/sshd           
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      1169/cupsd          
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1434/master         
tcp        0      0 127.0.0.1:6010          0.0.0.0:*               LISTEN      2619/sshd: root@pts 
tcp        0     36 192.168.32.96:22        192.168.32.1:59802      ESTABLISHED 2619/sshd: root@pts 
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd           
tcp6       0      0 :::22                   :::*                    LISTEN      1170/sshd           
tcp6       0      0 ::1:631                 :::*                    LISTEN      1169/cupsd          
tcp6       0      0 ::1:25                  :::*                    LISTEN      1434/master         
tcp6       0      0 ::1:6010                :::*                    LISTEN      2619/sshd: root@pts 

二.测试网络连接

2.1、测试网络连通性——ping

[root@qiaozhi ~]]# ping [选项] 目标网络地址

[root@qiaozhi ~]# ping 192.168.32.96
PING 192.168.32.96 (192.168.32.96) 56(84) bytes of data.
64 bytes from 192.168.32.96: icmp_seq=1 ttl=64 time=0.054 ms
64 bytes from 192.168.32.96: icmp_seq=2 ttl=64 time=0.092 ms
64 bytes from 192.168.32.96: icmp_seq=3 ttl=64 time=0.035 ms
64 bytes from 192.168.32.96: icmp_seq=4 ttl=64 time=0.031 ms
^C  按ctrl +c 结束

[root@qiaozhi ~]]# ping -cn(n为数字) 目标网络地址 发送n个包到目标主机

[root@qiaozhi ~]# ping -c5 192.168.32.96 发送5个包到192.168.32.96
PING 192.168.32.96 (192.168.32.96) 56(84) bytes of data.
64 bytes from 192.168.32.96: icmp_seq=1 ttl=64 time=0.031 ms
64 bytes from 192.168.32.96: icmp_seq=2 ttl=64 time=0.035 ms
64 bytes from 192.168.32.96: icmp_seq=3 ttl=64 time=0.032 ms
64 bytes from 192.168.32.96: icmp_seq=4 ttl=64 time=0.032 ms
64 bytes from 192.168.32.96: icmp_seq=5 ttl=64 time=0.053 ms

--- 192.168.32.96 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4004ms
rtt min/avg/max/mdev = 0.031/0.036/0.053/0.010 ms

2.2、跟踪数据包——traceroute

[root@qiaozhi ~]# traceroute 目标网络地址

[root@qiaozhi ~]# traceroute www.baidu.com
traceroute to www.baidu.com (192.168.32.111), 30 hops max, 60 byte packets
 1  qiaozhi (192.168.32.96)  3034.370 ms !H  3034.240 ms !H  3034.225 ms !H

2.3、测试DNS域名解析——nslookup

[root@qiaozhi ~]# nslookup 目标主机地址 [DNS服务器地址]]
[root@qiaozhi ~]# dig 目标主机地址 [DNS服务器地址]

[root@qiaozhi ~]# 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.39



[root@qiaozhi ~]# dig www.baidu.com
; <<>> DiG 9.9.4-RedHat-9.9.4-50.el7 <<>> www.baidu.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 8407
;; 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.		729	IN	CNAME	www.a.shifen.com.
www.a.shifen.com.	284	IN	CNAME	www.wshifen.com.
www.wshifen.com.	264	IN	A	103.235.46.39

;; Query time: 54 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: 六 925 13:51:54 CST 2021
;; MSG SIZE  rcvd: 111

三.设置网络地址参数

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

  • 简单、快速,可直接修改运行中的网络参数

  • 一般只适合在调试网络的过程中使用

  • 系统重启以后,所做的修改将会失效
    固定设置——通过配置文件修改网络参数

  • 修改各项网络参数的配置文件

  • 适合对服务器设置固定参数时使用

  • 需要重载网络服务或者重启以后才会生效

四.使用网络配置命令

4.1、设置网络接口的IP地址、子网掩码

  • 修改网卡的IP地址、子网掩码:
    ifconfig 网络接口 ip地址 [netmask 子网掩码]
    ifconfig 网络接口 ip地址 [/子网掩码]
    在这里插入图片描述
  • 禁用或重新激活网卡
    ifconfig 网络接口 up 激活
    ifconfig 网络接口 down 禁用
  • 设置虚拟网络接口
    ifconfig 网络接口:序号 IP地址
    [root@qiaozhi ~]# ifconfig ens37:1 192.168.32.200/24

在这里插入图片描述

4.2、设置路由记录

  • 添加到指定网段的路由记录
    route add -net 网段地址 gw ip地址
    [root@localhost ~]# route add -net 192.168.100.0/24 gw 192.168.32.2
[root@localhost ~]# route add -net 192.168.100.0/24 gw 192.168.32.2
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.32.2    0.0.0.0         UG    100    0        0 ens33
192.168.32.0    0.0.0.0         255.255.255.0   U     100    0        0 ens33
192.168.100.0   192.168.32.2    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 -net 网段地址
    [root@localhost ~]# route del -net 192.168.100.0/24
[root@localhost ~]# route del -net 192.168.100.0/24
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.32.2    0.0.0.0         UG    100    0        0 ens33
192.168.32.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
  • 向路由表中添加默认网关记录
    route add default gw ip 地址
    [root@localhost ~]# route add default gw 192.168.32.14
[root@localhost ~]# route add default gw 192.168.32.14
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.32.14   0.0.0.0         UG    0      0        0 ens33
0.0.0.0         192.168.32.2    0.0.0.0         UG    100    0        0 ens33
192.168.32.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

在这里插入图片描述

  • 删除路由表中默认的网关记录
    route del default gw ip 地址
    [root@localhost ~]# route del default gw 192.168.32.14
[root@localhost ~]# route del default gw 192.168.32.14
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.32.2    0.0.0.0         UG    100    0        0 ens33
192.168.32.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

五.修改网络配置文件

5.1、网络接口配置文件

/etc/sysconfig/network-scripts/ 目录下

添加一块新的网卡
[root@localhost ~]#iifconfig
[root@localhost ~]# cd /etc/sysconfig/network-scripts
[root@localhost network-scripts]# cp ifcfg-ens33 ifcfg-ens37
[root@localhost network-scripts]# vim ifcfg-ens37
[root@localhost network-scripts]# systemctl restart network

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

5.2、主机名称配置文件

  • 方法一:
    通过/etc/hostname 文件来修改主机名 (仅识别第一行为主机名,并重启生效)
[root@localhost ~]# vim /etc/hostname 

why
#localhost.localdomain 在前面按下#表示注释
~                                                                                                                                                                                         
~                                                                                                                                                                                         
~  
重启后生效                                                                                                                                                                                                                                      
  • 方法二:
    通过hostnamectl set-hostname来改 [root@why ~]# hostnamectl set-hostname haoyunlai

在这里插入图片描述

5.3、域名解析配置文件

  • 配置文件位置:/etc/resolv.conf
[root@haoyunlai ~]# cat /etc/resolv.conf 
# Generated by NetworkManager
nameserver 8.8.8.8
[root@haoyunlai ~]# echo "DNS10=114.114.114.114" >> /etc/sysconfig/network-scripts/ifcfg-ens33
[root@haoyunlai ~]# systemctl restart network
[root@haoyunlai ~]# cat /etc/resolv.conf 
# Generated by NetworkManager
nameserver 8.8.8.8
nameserver 114.114.114.114

在这里插入图片描述

5.4、本地主机映射文件

[root@haoyunlai ~]# vim /etc/hosts
第三行插入 192.168.32.111 www.baidu.com
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值