Unix(及类Unix系统,如 Linux、macOS、BSD)配置网络的方法因发行版和版本不同而有差异。下面按常见场景给你一个清晰、实用的总结。
uname -a
cat /etc/os-release
常见系统:
ip addr # Linux 推荐
ifconfig # 传统(部分系统)
常见接口名:
eth0、ens33(Linux)en0(macOS)em0(FreeBSD)ip addr add 192.168.1.100/24 dev eth0
ip link set eth0 up
设置网关:
ip route add default via 192.168.1.1
设置 DNS(临时):
echo "nameserver 8.8.8.8" > /etc/resolv.conf
ping 192.168.1.1
ping www.baidu.com
编辑:
sudo vi /etc/netplan/01-netcfg.yaml
示例(DHCP):
network:
version: 2
ethernets:
eth0:
dhcp4: true
示例(静态 IP):
network:
version: 2
ethernets:
eth0:
dhcp4: no
addresses: [192.168.1.100/24]
gateway4: 192.168.1.1
nameservers:
addresses: [8.8.8.8, 114.114.114.114]
应用配置:
sudo netplan apply
nmcli device status
nmcli con add type ethernet ifname eth0 con-name static-eth0
nmcli con modify static-eth0 ipv4.addresses 192.168.1.100/24
nmcli con modify static-eth0 ipv4.gateway 192.168.1.1
nmcli con modify static-eth0 ipv4.dns "8.8.8.8"
nmcli con modify static-eth0 ipv4.method manual
nmcli con up static-eth0
vi /etc/sysconfig/network-scripts/ifcfg-eth0
示例:
DEVICE=eth0
BOOTPROTO=static
ONBOOT=yes
IPADDR=192.168.1.100
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=8.8.8.8
重启网络:
systemctl restart NetworkManager
vi /etc/rc.conf
ifconfig_em0="inet 192.168.1.100 netmask 255.255.255.0"
defaultrouter="192.168.1.1"
sudo networksetup -setmanual "Ethernet" 192.168.1.100 255.255.255.0 192.168.1.1
sudo networksetup -setdnsservers "Ethernet" 8.8.8.8
ip route
ping 网关
ping DNS
cat /etc/resolv.conf
nslookup www.google.com
ip link set eth0 up
| 功能 | 命令 |
|---|---|
| 查看 IP | ip addr |
| 启动接口 | ip link set eth0 up |
| 添加 IP | ip addr add IP/掩码 dev eth0 |
| 设置网关 | ip route add default via IP |
| Ubuntu 配置 | netplan |
| CentOS 配置 | nmcli |
| DNS | /etc/resolv.conf |
如果你愿意,可以告诉我:
我可以给你精确到命令级别的配置方案。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。