建议收藏chatGPT说的Ubuntu下常用网络命令合集

Linux系统
340
0
0
2023-04-21

写在前面

这个是当下最流行最时髦的AI神器chatGPT和我一起合作写的一篇通用技术文章,请读者笑纳!

chatGPT说

咚咚咚,咚咚咚,嘿嘿;咚咚咚,咚咚咚,嘿嘿;AI等一会,我来发答案,看图!!!

img

命令演示

ifconfig:查看当前系统的网络接口配置信息,包括IP地址、子网掩码、网关等信息;

img

如果你发现用不了,那你要不装下包,或者ip addr其实查出来也差不多

img

ping:用于测试网络连接,可以测试远程主机是否可达、延迟等信息;

感觉良好,能ping通啊,linux系统默认是一直ping下去,你可以按CTRL +C把它掐掉。

img

traceroute:跟踪数据包在网络中的路径和路由,用于定位网络故障;

墙墙外很通畅,速度很快

img

墙墙内也就这样吧,感觉良好

img

netstat:查看当前系统的网络连接和网络统计信息;

一页放不下,贴日志吧

ubuntu@VM-0-16-ubuntu:~$ netstat
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 VM-0-16-ubuntu:60768    169.254.0.138:8186      ESTABLISHED
tcp        0      0 VM-0-16-ubuntu:43352    169.254.0.55:5574       ESTABLISHED
tcp        0      0 VM-0-16-ubuntu:35514    169.254.0.4:http        TIME_WAIT  
tcp        0      0 VM-0-16-ubuntu:43354    169.254.0.55:5574       ESTABLISHED
tcp        0     52 VM-0-16-ubuntu:ssh      124.160.67.3:2439       ESTABLISHED
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  2      [ ]         DGRAM                    8407869  /run/user/500/systemd/notify
unix  2      [ ]         DGRAM                    25793    /usr/local/qcloud/YunJing/conf/ydrpc_3
unix  2      [ ]         DGRAM                    201820   @/usr/local/qcloud/YunJing/conf/ydrpc_3@
unix  3      [ ]         DGRAM                    14415    /run/systemd/notify
unix  2      [ ]         DGRAM                    14428    /run/systemd/journal/syslog
unix  11     [ ]         DGRAM                    14446    /run/systemd/journal/dev-log
unix  8      [ ]         DGRAM                    14450    /run/systemd/journal/socket
unix  3      [ ]         STREAM     CONNECTED     8408699  /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     22162    /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     21873    
unix  3      [ ]         STREAM     CONNECTED     25094    /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     8407840  
unix  2      [ ]         DGRAM                    21122    
unix  3      [ ]         STREAM     CONNECTED     14878    /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     23987    
unix  3      [ ]         STREAM     CONNECTED     22161    
unix  3      [ ]         STREAM     CONNECTED     19897    
unix  3      [ ]         STREAM     CONNECTED     21910    
unix  3      [ ]         STREAM     CONNECTED     8409252  
unix  3      [ ]         STREAM     CONNECTED     15992    
unix  3      [ ]         STREAM     CONNECTED     21119    /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     22519    /run/systemd/journal/stdout
unix  3      [ ]         DGRAM                    14417    
unix  3      [ ]         STREAM     CONNECTED     18366    
unix  3      [ ]         DGRAM                    8407870  
unix  3      [ ]         STREAM     CONNECTED     21628    /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     8610754  
unix  3      [ ]         STREAM     CONNECTED     19646    /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     8409253  
unix  3      [ ]         STREAM     CONNECTED     21292    /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     21118    
unix  3      [ ]         DGRAM                    8407871  
unix  3      [ ]         STREAM     CONNECTED     21909    
unix  2      [ ]         DGRAM                    8408695  
unix  3      [ ]         STREAM     CONNECTED     8611103  /var/run/dbus/system_bus_socket
unix  3      [ ]         DGRAM                    16040    
unix  3      [ ]         DGRAM                    16039    
unix  3      [ ]         STREAM     CONNECTED     21874    /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     19644    
unix  2      [ ]         DGRAM                    8407847  
unix  3      [ ]         STREAM     CONNECTED     8616394  
unix  3      [ ]         STREAM     CONNECTED     8616395  /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     22802    
unix  3      [ ]         STREAM     CONNECTED     8609648  /run/systemd/journal/stdout
unix  2      [ ]         DGRAM                    8407850  
unix  3      [ ]         STREAM     CONNECTED     8610532  
unix  2      [ ]         DGRAM                    19642    
unix  2      [ ]         DGRAM                    21629    
unix  3      [ ]         STREAM     CONNECTED     17178    
unix  3      [ ]         STREAM     CONNECTED     22803    /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     17179    /run/systemd/journal/stdout
unix  2      [ ]         DGRAM                    18158    
unix  3      [ ]         STREAM     CONNECTED     19643    
unix  3      [ ]         DGRAM                    18994    
unix  3      [ ]         STREAM     CONNECTED     184145   
unix  2      [ ]         DGRAM                    18983    
unix  3      [ ]         DGRAM                    14416    
unix  2      [ ]         DGRAM                    21930    
unix  3      [ ]         STREAM     CONNECTED     22424    
unix  2      [ ]         DGRAM                    9077544  
unix  3      [ ]         STREAM     CONNECTED     21449    
unix  2      [ ]         DGRAM                    19606    
unix  3      [ ]         DGRAM                    18992    
unix  3      [ ]         STREAM     CONNECTED     18974    
unix  3      [ ]         STREAM     CONNECTED     20453    
unix  3      [ ]         STREAM     CONNECTED     8615773  
unix  2      [ ]         DGRAM                    19391    
unix  2      [ ]         DGRAM                    200967   
unix  3      [ ]         STREAM     CONNECTED     22796    /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     21291    
unix  3      [ ]         STREAM     CONNECTED     18976    /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     184143   
unix  3      [ ]         STREAM     CONNECTED     19340    
unix  3      [ ]         DGRAM                    18993    
unix  3      [ ]         STREAM     CONNECTED     21527    /run/systemd/journal/stdout
unix  3      [ ]         DGRAM                    18995    
unix  3      [ ]         STREAM     CONNECTED     22518    
unix  3      [ ]         STREAM     CONNECTED     184146   
unix  3      [ ]         STREAM     CONNECTED     184144   
unix  3      [ ]         STREAM     CONNECTED     21526    
unix  3      [ ]         STREAM     CONNECTED     22425    /var/run/dbus/system_bus_socket
unix  2      [ ]         DGRAM                    26774    
unix  3      [ ]         STREAM     CONNECTED     21450    /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     8616393  /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     19645    /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     19092    
unix  3      [ ]         STREAM     CONNECTED     15487    /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     20673    
unix  3      [ ]         STREAM     CONNECTED     19647    /var/run/dbus/system_bus_socket
unix  2      [ ]         DGRAM                    19614    
unix  3      [ ]         STREAM     CONNECTED     20674    /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     8616416  /run/user/500/bus
unix  2      [ ]         DGRAM                    13956    
unix  3      [ ]         STREAM     CONNECTED     19617    
unix  3      [ ]         STREAM     CONNECTED     8616396  
unix  3      [ ]         STREAM     CONNECTED     8616414  
unix  3      [ ]         STREAM     CONNECTED     8616413  
unix  3      [ ]         STREAM     CONNECTED     19094    /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     15486    
unix  2      [ ]         DGRAM                    8616408  
unix  2      [ ]         DGRAM                    14867    
ubuntu@VM-0-16-ubuntu:~$ 

nslookup:用于查询域名的解析信息,包括IP地址、域名服务器等信息;

墙墙外很通畅,速度很快

img

墙墙内也就这样吧,感觉良好

img

dig:用于查询DNS服务器的信息,可以查看域名的详细解析信息;

贴日志吧

ubuntu@VM-0-16-ubuntu:~$ dig www.google.com

; <<>> DiG 9.11.3-1ubuntu1.18-Ubuntu <<>> www.google.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 22782
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

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

;; ANSWER SECTION:
www.google.com.         295     IN      A       142.251.42.164

;; Query time: 5 msec
;; SERVER: 127.0.0.53#53(127.0.0.53)
;; WHEN: Tue Mar 14 23:52:26 CST 2023
;; MSG SIZE  rcvd: 59

ubuntu@VM-0-16-ubuntu:~$ dig www.zhengjiangtao.cn

; <<>> DiG 9.11.3-1ubuntu1.18-Ubuntu <<>> www.zhengjiangtao.cn
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 59614
;; flags: qr rd ra; QUERY: 1, ANSWER: 5, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 65494
;; QUESTION SECTION:
;www.zhengjiangtao.cn.          IN      A

;; ANSWER SECTION:
www.zhengjiangtao.cn.   229     IN      CNAME   ataola.github.io.
ataola.github.io.       3229    IN      A       185.199.110.153
ataola.github.io.       3229    IN      A       185.199.109.153
ataola.github.io.       3229    IN      A       185.199.108.153
ataola.github.io.       3229    IN      A       185.199.111.153

;; Query time: 0 msec
;; SERVER: 127.0.0.53#53(127.0.0.53)
;; WHEN: Tue Mar 14 23:52:35 CST 2023
;; MSG SIZE  rcvd: 143

ubuntu@VM-0-16-ubuntu:~$ 

route:用于显示和操作系统的路由表,可以添加、删除、修改路由规则;

添加删除修改略

img

iptables:用于配置和管理系统的防火墙规则,可以限制或允许特定的网络流量;、

我觉得chatGPT说的真好,我觉得引用她说的话,下面是她给我的原话。

iptables是一个用于配置和管理Linux操作系统防火墙的工具,可以限制或允许特定的网络流量,提高系统的安全性。以下是iptables的使用方法:

查看当前iptables规则:使用命令sudo iptables -L可以查看当前系统的iptables规则,包括已有的规则和默认的规则。

添加iptables规则:使用命令sudo iptables -A可以添加新的iptables规则,例如:

sudo iptables -A INPUT -s 192.168.1.0/24 -p tcp --dport 22 -j ACCEPT

上面的命令表示,允许来自192.168.1.0/24网段的主机使用SSH协议(TCP端口为22)连接到本机。

删除iptables规则:使用命令sudo iptables -D可以删除指定的iptables规则,例如:

sudo iptables -D INPUT -s 192.168.1.0/24 -p tcp --dport 22 -j ACCEPT

上面的命令表示,删除允许来自192.168.1.0/24网段的主机使用SSH协议(TCP端口为22)连接到本机的规则。

保存iptables规则:使用命令sudo iptables-save可以将当前的iptables规则保存到文件中,例如:

sudo iptables-save > /etc/iptables/rules.v4

上面的命令表示,将当前的iptables规则保存到文件/etc/iptables/rules.v4中。

加载iptables规则:使用命令sudo iptables-restore可以从文件中加载iptables规则,例如:

sudo iptables-restore < /etc/iptables/rules.v4

上面的命令表示,从文件/etc/iptables/rules.v4中加载iptables规则。

需要注意的是,iptables的规则配置比较复杂,需要了解网络协议和端口等相关知识。建议在进行iptables规则配置时,先备份当前的规则,避免出现不必要的问题。同时,也建议在实际操作中,使用特权用户或者root用户进行执行。

img

ssh:用于远程登录到其他计算机,进行网络管理和操作;

使用ssh root@ip地址既可以登录到其他的主机上,默认端口是22可以不写,配置ssh密码可以不输入。

img

curl:用于向远程服务器发送HTTP请求,可以获取网页内容、下载文件等

curl和wget互为姊妹,可以对照着学学

img

以上就是今天的全部内容,希望能在运维路上帮到你,让我们下期再见!