10 个 Linux 网络命令及其使用方法10 个 Linux 网络命令及其使用方法10 个 Linux 网络命令及其使用方法10 个 Linux 网络命令及其使用方法
  • 文章
  • 正则表达式
    • 工具
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容
发表 admin at 2025年2月28日
类别
  • 未分类
标签

10 个 Linux 网络命令及其使用方法

想知道如何在 Linux 中管理网络和 DNS 设置吗?这些命令会对您有所帮助。

Linux 与其他高级操作系统一样,支持网络通信。在 Linux 中,所有设置都存储在可访问的文件中。这样,可以编辑某些文件,并可以轻松地进行必要的设置。您可以从命令屏幕轻松管理用于网络和 DNS 设置的命令。

在本文中,您将找到有关 Linux 中最常用的基本网络命令以及如何编辑 DNS 设置的信息。

1. 对于网络连接和IP配置:ifconfig

ifconfig 命令允许您检查和更改系统中可用的网络连接和 IP 配置设置。

ifconfig

首先,您会看到 eth0 信息。 eth0 是显示以太网卡信息的部分。如果其他以太网卡连接到系统,它们的名称将分别为 eth1 和 eth2。

lo代表计算机本身,即localhost。

如果您使用的是无线网络环境,还可以看到wlan0信息。 wlan0 表示无线网卡。

您可以一一列出所需的信息,而不用这种方式使用 ifconfig 命令。例如,如果您只想列出以太网卡信息,ifconfig eth0命令就足够了。

 ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
       inet 10.0.2.15 netmask 255.255.255.0 broadcast 10.0.2.255
       inet6 fe00::000:00xx:xx00:0x00 prefixlen 64 scopeid 0x20<link>
       ether 08:00:00:00:0x:00 txqueuelen 1000 (Ethernet)
       RX packets 11 bytes 3310 (3.2 KiB)
       RX errors 0 dropped 0 overruns 0 frame 0
       TX packets 25 bytes 3050 (2.9 KiB)
       TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

例如,您在上面看到 eth0 的本地 IP 地址是 10.0.2.15。将此 IP 地址更改为 10.0.2.13:

sudo ifconfig eth0 10.0.2.13 

使用 ifconfig,您可以更改此处的信息,以及调整您在输出中遇到的信息卡。 ifconfig card_name down命令可以从显示中删除所需的卡,您可以使用ifconfig card_name up命令再次显示该卡。

sudo ifconfig lo down

从输出中可以看出,down 参数现在使 lo 卡不可见。

sudo ifconfig lo up

2.一种控制机制:ping

ping 命令检查目标和您的系统之间是否建立了通信。例如,您可以查询您与Google之间的通讯速度。

ping google.com

在执行此操作时,我在第 5 个查询包之后使用 Ctrl + C 组合结束了此过程。如果您使用 ping -c 4 google.com 这样的命令,则仅发送 4 个查询数据包。查询数据包数量耗尽后,该过程将终止。

ping -c 4 google.com
PING google.com (216.58.206.174) 56(84) bytes of data.
64 bytes from sof02s27-in-f14.1e100.net (216.58.206.174): icmp_seq=1 ttl=111 time=79.0 ms
64 bytes from sof02s27-in-f14.1e100.net (216.58.206.174): icmp_seq=2 ttl=111 time=73.5 ms
64 bytes from sof02s27-in-f14.1e100.net (216.58.206.174): icmp_seq=3 ttl=111 time=72.7 ms
64 bytes from sof02s27-in-f14.1e100.net (216.58.206.174): icmp_seq=4 ttl=111 time=75.1 ms
--- google.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3014ms
rtt min/avg/max/mdev = 72.710/75.089/79.030/2.432 ms

3.对于IP路由表:route

Route 命令允许您查看系统上 IP 路由表的内容。为此,您可以在控制台中键入 route -n 命令。

route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 10.0.2.1 0.0.0.0 UG 100 0 0 eth0
10.0.2.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0

这里的网关地址是从本地网络到互联网网络时使用的转发地址。

4.针对特定目标的转发跟踪:traceroute

使用 Traceroute,您的命令可以显示发送到特定目的地的数据包经过了哪些主机。这次,我们以目标地址 www.makeuseof.com 为例。

traceroute www.makeuseof.com 
traceroute to www.makeuseof.com (54.157.137.27), 30 hops max, 60 byte packets
1 10.0.2.1 (10.0.2.1) 0.430 ms 0.326 ms 0.414 ms
2 * * *
3 * * *
4 * * *
5 * * *
6 * * *
7 * * *
8 * * *
9 * * *
10 * * *
11 * * *

如您所见,为了能够定向到所需的地址,首先向地址 10.0.2.1 发出请求。您可以使用traceroute 命令查看此类路由路径并了解网络中发生的任何路由问题。

5. 域名信息:whois

whois查询包含域名建立时间、有效期等信息。让我们将 google.com 作为 whois 查询的示例。

whois google.com 
  Domain Name: GOOGLE.COM
  Registry Domain ID: 2138514_DOMAIN_COM-VRSN
  Registrar WHOIS Server: whois.markmonitor.com
  Registrar URL: http://www.markmonitor.com
  Updated Date: 2019-09-09T15:39:04Z
  Creation Date: 1997-09-15T04:00:00Z
  Registry Expiry Date: 2028-09-14T04:00:00Z
  Registrar: MarkMonitor Inc.
  Registrar IANA ID: 292
  Registrar Abuse Contact Email: abusecomplaints@markmonitor.com
  Registrar Abuse Contact Phone: +1.2083895740
  Domain Status: clientDeleteProhibited https://icann.org/epp#clientDeleteProhibited
  Domain Status: clientTransferProhibited https://icann.org/epp#clientTransferProhibited
  Domain Status: clientUpdateProhibited https://icann.org/epp#clientUpdateProhibited
  Domain Status: serverDeleteProhibited https://icann.org/epp#serverDeleteProhibited
  Domain Status: serverTransferProhibited https://icann.org/epp#serverTransferProhibited
  Domain Status: serverUpdateProhibited https://icann.org/epp#serverUpdateProhibited
  Name Server: NS1.GOOGLE.COM
  Name Server: NS2.GOOGLE.COM
  Name Server: NS3.GOOGLE.COM
  Name Server: NS4.GOOGLE.COM
  DNSSEC: unsigned
  URL of the ICANN Whois Inaccuracy Complaint Form: https://www.icann.org/wicf/

此外,您可以通过使用最好的网站免费查找 whois 信息来避免使用命令屏幕。

6. 获取有关目标地址:host 的信息

host 命令允许您获取有关目标地址的信息。通过host命令,可以从IP地址访问域名,也可以从域名访问IP地址。

7. 查看 DNS 记录:dig

dig 命令(域信息探索器)是一个常见的 Linux 网络命令,用于查找 DNS 记录。

dig google.com
; <<>> DiG 9.17.21-1-Debian <<>> google.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 64917
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;google.com. IN A
;; ANSWER SECTION:
google.com. 177 IN A 216.58.206.174
;; Query time: 3 msec
;; SERVER: 192.168.1.1#53(192.168.1.1) (UDP)
;; WHEN: Thu Apr 14 19:25:15 EDT 2022
;; MSG SIZE rcvd: 44

此外,您还可以使用 dig 命令检查 NS(名称服务器)记录。您可以使用-t NS命令来实现此目的。

dig google.com -t NS
; <<>> DiG 9.17.21-1-Debian <<>> google.com -t NS
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 41881
;; flags: qr rd ra; QUERY: 1, ANSWER: 4, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;google.com. IN NS
;; ANSWER SECTION:
google.com. 70621 IN NS ns3.google.com.
google.com. 70621 IN NS ns2.google.com.
google.com. 70621 IN NS ns4.google.com.
google.com. 70621 IN NS ns1.google.com.
;; Query time: 3 msec
;; SERVER: 192.168.1.1#53(192.168.1.1) (UDP)
;; WHEN: Thu Apr 14 19:34:38 EDT 2022
;; MSG SIZE rcvd: 100

8. IP-MAC地址匹配:arp

arp 命令显示保存 IP-MAC 地址映射的表。

arp 
Address HWtype HWaddress Flags Mask Iface
10.0.2.1 ether 52:54:00:12:35:00 C eth0
10.0.2.3 ether 08:00:27:66:24:25 C eth0

9. 对于系统建立的连接:tcpdump

tcpdump 命令允许您立即查看系统建立的连接以及与系统建立的连接。

# tcpdump 
tcpdump: verbose output suppressed, use -v[v]... for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), snapshot length 262144 bytes
19:44:59.746452 IP 10.0.2.15.48657 > homerouter.cpe.domain: 14329+ A? www.makeuseof.com. (37)
19:44:59.746574 IP 10.0.2.15.48657 > homerouter.cpe.domain: 39924+ AAAA? www.makeuseof.com. (37)
19:44:59.770573 IP 10.0.2.15.44391 > homerouter.cpe.domain: 44735+ A? content-signature-2.cdn.mozilla.net. (53)
19:44:59.770685 IP 10.0.2.15.44391 > homerouter.cpe.domain: 31664+ AAAA? content-signature-2.cdn.mozilla.net. (53)

另外,如果您想直接访问链接而不进行地址解析,可以使用tcpdump -n命令。

# tcpdump -n
tcpdump: verbose output suppressed, use -v[v]... for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), snapshot length 262144 bytes
19:47:41.327149 IP 10.0.2.15.43892 > 192.168.1.1.53: 44991+ A? content-signature-2.cdn.mozilla.net. (53)
19:47:41.327574 IP 10.0.2.15.43892 > 192.168.1.1.53: 46778+ AAAA? content-signature-2.cdn.mozilla.net. (53)
19:47:41.425041 IP 192.168.1.1.53 > 10.0.2.15.43892: 44991 5/0/0 CNAME d2nxq2uap88usk.cloudfront.net., A 18.66.248.5, A 18.66.248.40, A 18.66.248.112, A 18.66.248.105 (157)
19:47:41.425042 IP 192.168.1.1.53 > 10.0.2.15.43892: 46778 9/0/0 CNAME d2nxq2uap88usk.cloudfront.net., AAAA 2600:9000:224a:e400:a:da5e:7900:93a1, AAAA 2600:9000:224a:1c00:a:da5e:7900:93a1, AAAA 2600:9000:224a:9400:a:da5e:7900:93a1, AAAA 2600:9000:224a:8200:a:da5e:7900:93a1, AAAA 2600:9000:224a:c00:a:da5e:7900:93a1, AAAA 2600:9000:224a:8e00:a:da5e:7900:93a1, AAAA 2600:9000:224a:9a00:a:da5e:7900:93a1, AAAA 2600:9000:224a:a400:a:da5e:7900:93a1 (317)
19:47:42.470901 IP 10.0.2.15.44369 > 192.168.1.1.53: 28874+ A? safebrowsing.googleapis.com. (45)

10.DNS设置

与其他操作系统相比,在 Linux 上设置 DNS 相当容易。为此,您可以对存储 DNS 信息的 etc/resolv.conf 文件进行一些更改。

# cat etc/resolv.conf 
# Generated by NetworkManager
nameserver 192.168.1.1

如果您想在此处添加新的 DNS,可以使用 echo 命令。

# echo nameserver 8.8.8.8 > etc/resolv.conf
# echo nameserver 8.8.4.4 >> etc/resolv.conf 

添加这些内容后,当您再次浏览 etc/resolv.conf 文件时,您将看到所做的所需更改。

 # cat etc/resolv.conf 
nameserver 8.8.8.8
nameserver 8.8.4.4

在/etc/文件夹中,有另一个名为hosts的文件,它充当域名服务器。在系统中进行域名解析时会检查该文件。

# cat etc/hosts
127.0.0.1 localhost
127.0.1.1 kali
# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

使用 Linux 命令使网络管理更简单

Linux 是使用最广泛的操作系统之一。了解 Linux 中使用的正确命令将在时间和精力方面非常有益。配置 Linux 网络并查找网络中可能出现的任何问题时,可以使用 Linux 网络命令。

©2015-2025 艾丽卡 support@alaica.com