10 个 Linux 网络命令以及如何使用它们

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

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

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

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

ifconfig

ifconfig 命令的输出

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

代表计算机本身,即localhost。

如果您使用的是无线网络环境,您还可以看到 无线局域网0 信息。 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&lt;link&gt;
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 命令更改本地 IP 地址

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

sudo ifconfig lo down

在 ifconfig 命令中使用 down 参数

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

sudo ifconfig lo up

将 up 参数与 ifconfig 命令一起使用

2.一种控制机制:ping

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

ping google.com

使用 ping 命令

在执行此操作时,我在第 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 路由表的内容。 您可以键入 路线-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 命令(域信息 groper)是一种常见的 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 &gt; homerouter.cpe.domain: 14329+ A? www.makeuseof.com. (37)
19:44:59.746574 IP 10.0.2.15.48657 &gt; homerouter.cpe.domain: 39924+ AAAA? www.makeuseof.com. (37)
19:44:59.770573 IP 10.0.2.15.44391 &gt; homerouter.cpe.domain: 44735+ A? content-signature-2.cdn.mozilla.net. (53)
19:44:59.770685 IP 10.0.2.15.44391 &gt; 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 &gt; 192.168.1.1.53: 44991+ A? content-signature-2.cdn.mozilla.net. (53)
19:47:41.327574 IP 10.0.2.15.43892 &gt; 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 &gt; 192.168.1.1.53: 28874+ A? safebrowsing.googleapis.com. (45)

10.DNS设置

与其他操作系统相比,在 Linux 上设置 DNS 非常简单。 为此,您可以对 等/resolv.conf 存储 DNS 信息的文件。

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

如果要在此处添加新的 DNS,可以使用 回声 命令。

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

完成这些添加后,当您浏览 等/resolv.conf 再次文件,您将看到进行了所需的更改。

 # cat etc/resolv.conf 
nameserver 8.8.8.8
nameserver 8.8.4.4

在 – 的里面 /ETC/ 文件夹,还有一个文件叫 主机 充当域名服务器。 在系统中进行域名解析时会检查该文件。

# 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 网络命令。