【计算机网络】Linux网络的几个常用命令
📚 博主的专栏
🐧 Linux | 🖥️ C++ | 📊 数据结构 | 💡C++ 算法 | 🅒 C 语言 | 🌐 计算机网络
相关文章:计算机网络专栏
目录
ping(检测网络连通性)
netstat(网络状态、用来查看网络服务)
pidof [进程名](查看服务器的进程 id 时非常方便)
ping(检测网络连通性)
window和云服务器linux是否能连通
ping后面跟上目标网址对应的域名或者ip地址。
pupu@VM-8-15-ubuntu:~/computer-network/class_44/1.udp_echo_server$ ping www.baidu.com PING www.a.shifen.com (220.181.111.1) 56(84) bytes of data. 64 bytes from 220.181.111.1 (220.181.111.1): icmp_seq=1 ttl=251 time=2.82 ms 64 bytes from 220.181.111.1 (220.181.111.1): icmp_seq=2 ttl=251 time=2.83 ms 64 bytes from 220.181.111.1 (220.181.111.1): icmp_seq=3 ttl=251 time=2.83 ms ^C --- www.a.shifen.com ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2003ms rtt min/avg/max/mdev = 2.818/2.827/2.834/0.006 ms
默认ping开始后是不会停止的,因此可以加上选项c(count次数)num
pupu@VM-8-15-ubuntu:~/computer-network/class_44/1.udp_echo_server$ ping -c1 www.baidu.com PING www.a.shifen.com (110.242.70.57) 56(84) bytes of data. 64 bytes from 110.242.70.57 (110.242.70.57): icmp_seq=1 ttl=251 time=16.5 ms--- www.a.shifen.com ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 16.545/16.545/16.545/0.000 ms
netstat(网络状态、用来查看网络服务)
常用选项:
• n 拒绝显示别名, 能显示数字的全部转化成数字
• l 仅列出有在 Listen (监听) 的服务状态
• p 显示建立相关链接的程序名
• t (tcp)仅显示 tcp 相关选项• u (udp)仅显示 udp 相关选项
• a (all)显示所有选项, 默认不显示 LISTEN 相关
准备好一个udp或者tcp的服务器,开启服务器之后使用netstat【选项】可以查看是否运行
例如现在:“已有一个udp服务”:在开启服务器(端口号8899)后,这里就可以看到udp服务的相关信息以及看到相关进程(udpserver)的信息
pupu@VM-8-15-ubuntu:~/computer-network/class_44/1.udp_echo_server$ netstat -aup (Not all processes could be identified, non-owned process infowill not be shown, you would have to be root to see it all.) Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name udp 0 0 localhost:8899 0.0.0.0:* 3500381/./udpserver udp 0 0 localhost:domain 0.0.0.0:* - udp 0 0 VM-8-15-ubuntu:bootpc 0.0.0.0:* - udp 0 0 VM-8-15-ubuntu:ntp 0.0.0.0:* - udp 0 0 localhost:ntp 0.0.0.0:* - udp6 0 0 VM-8-15-ubuntu:ntp [::]:* - udp6 0 0 ip6-localhost:ntp [::]:* -
注意:当前只能查到(看到相关信息)我自己这个pupu身份启动的进程,其他是不允许我看到的,是系统本身自启动的,因此我们提权,想看到数字的ip地址就带n
pupu@VM-8-15-ubuntu:~/computer-network/class_44/1.udp_echo_server$ sudo netstat -aup Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name udp 0 0 localhost:8899 0.0.0.0:* 3500381/./udpserver udp 0 0 localhost:domain 0.0.0.0:* 826/systemd-resolve udp 0 0 VM-8-15-ubuntu:bootpc 0.0.0.0:* 824/systemd-network udp 0 0 VM-8-15-ubuntu:ntp 0.0.0.0:* 411559/ntpd udp 0 0 localhost:ntp 0.0.0.0:* 411559/ntpd udp6 0 0 VM-8-15-ubuntu:ntp [::]:* 411559/ntpd udp6 0 0 ip6-localhost:ntp [::]:* 411559/ntpd pupu@VM-8-15-ubuntu:~/computer-network/class_44/1.udp_echo_server$ sudo netstat -anup [sudo] password for pupu: Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name udp 0 0 127.0.0.1:8899 0.0.0.0:* 3500381/./udpserver udp 0 0 127.0.0.53:53 0.0.0.0:* 826/systemd-resolve udp 0 0 10.2.8.15:64 0.0.0.0:* 824/systemd-network udp 0 0 10.2.8.15:123 0.0.0.0:* 411559/ntpd udp 0 0 127.0.0.1:123 0.0.0.0:* 411559/ntpd udp6 0 0 fe80::5054:ff:fe45::123 :::* 411559/ntpd udp6 0 0 ::1:123 :::* 411559/ntpd pupu@VM-8-15-ubuntu:~/computer-network/class_44/1.udp_echo_server$
watch:n 是刷新的次数 ,可以定期检测,指定命令的执行
每个 1s 执行一次 netstat -aup $ watch -n 1 netstat -aup
pidof [进程名](查看服务器的进程 id 时非常方便)
想要关闭掉进程的时候需要知道进程的pid,之前使用的都是这样的方法:
pupu@VM-8-15-ubuntu:~/computer-network/class_44/1.udp_echo_server$ ps -ajx | grep udpserver 3485448 3506444 3506444 3485448 pts/8 3506444 S+ 1002 0:00 ./udpserver 3439936 3507463 3507462 3439936 pts/0 3507462 S+ 1002 0:00 grep --color=auto
我们可以直接使用pidof 指定进程的名称,就可以拿到,当前进程的pid:
pupu@VM-8-15-ubuntu:~/computer-network/class_44/1.udp_echo_server$ pidof udpserver 3506444
再直接kill掉进程:
kill -9 3506444
或者:管道是一个文件,当pidof将管道传给kill命令时,他是通过自己的标准输入文件描述符0,来把数据读到kill命令里面的,|命令是将命令行参数也就是进程pid放到kill命令的命令行参数,实际上xargs就相当于把从管道传过来的数据,传给后续命令的命令行参数上,拼接上。
pidof udpserver | xargs kill -9
结语:
随着这篇博客接近尾声,我衷心希望我所分享的内容能为你带来一些启发和帮助。学习和理解的过程往往充满挑战,但正是这些挑战让我们不断成长和进步。我在准备这篇文章时,也深刻体会到了学习与分享的乐趣。
在此,我要特别感谢每一位阅读到这里的你。是你的关注和支持,给予了我持续写作和分享的动力。我深知,无论我在某个领域有多少见解,都离不开大家的鼓励与指正。因此,如果你在阅读过程中有任何疑问、建议或是发现了文章中的不足之处,都欢迎你慷慨赐教。
你的每一条反馈都是我前进路上的宝贵财富。同时,我也非常期待能够得到你的点赞、收藏,关注这将是对我莫大的支持和鼓励。当然,我更期待的是能够持续为你带来有价值的内容。