Linux 命令行入门教程
61. 网络基础与本地解析
本教程共 90 篇 · 第 61 篇
61. 网络基础与本地解析
连不上网,先搞懂几个词:IP、端口、DNS、网关。
IP 是设备在网络里的地址,分 IPv4(如 192.168.1.10)和 IPv6。端口是程序通信的”门牌号”,比如网页 80、SSH 22。DNS 把 google.com 翻译成 IP。网关是你家网络通往外网的出口。
看本机网络,首选 ip(现代工具,取代老的 ifconfig):
ip addr show
它列出每张网卡、MAC 地址、分配的 IP。只看某张卡:
ip addr show eth0
老命令 ifconfig 也能用,但 Ubuntu 26.04 默认没装,需 sudo apt install net-tools。
hostname 看本机名,加 -I 直接列私有 IP:
hostname -I
本地解析靠 /etc/hosts——它比 DNS 优先。临时把某域名指向某 IP:
127.0.0.1 my.test
Note
/etc/hosts改的是本机解析,不影响别人。测试、屏蔽广告常用它。
小结:ip 看地址、hostname 看名字、/etc/hosts 做本地解析。连不上先查这三处。
参考来源
- GeeksforGeeks, ip command:https://www.geeksforgeeks.org/linux-unix/ip-command-in-linux-with-examples/
- GeeksforGeeks, ifconfig command:https://www.geeksforgeeks.org/linux-unix/ifconfig-command-in-linux-with-examples/