温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

unix如何配置网络

发布时间:2026-07-16 22:21:18 来源:亿速云 阅读:98 作者:小樊 栏目:系统运维

Unix(及类Unix系统,如 Linux、macOS、BSD)配置网络的方法因发行版和版本不同而有差异。下面按常见场景给你一个清晰、实用的总结。


一、快速判断你的系统类型

uname -a
cat /etc/os-release

常见系统:

  • Linux(Ubuntu / Debian / CentOS / Rocky / Alma)
  • macOS
  • FreeBSD / OpenBSD

二、临时配置网络(重启失效)

1️⃣ 查看网络接口

ip addr        # Linux 推荐
ifconfig       # 传统(部分系统)

常见接口名:

  • eth0ens33(Linux)
  • en0(macOS)
  • em0(FreeBSD)

2️⃣ 配置 IP 地址(Linux)

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

3️⃣ 测试网络

ping 192.168.1.1
ping www.baidu.com

三、永久配置网络(推荐)

✅ Ubuntu / Debian(netplan,现代版本)

编辑:

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

✅ CentOS / Rocky / Alma(NetworkManager)

方法一:nmcli(推荐)

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

✅ FreeBSD

vi /etc/rc.conf
ifconfig_em0="inet 192.168.1.100 netmask 255.255.255.0"
defaultrouter="192.168.1.1"

✅ macOS(命令行)

sudo networksetup -setmanual "Ethernet" 192.168.1.100 255.255.255.0 192.168.1.1
sudo networksetup -setdnsservers "Ethernet" 8.8.8.8

四、常见问题排查

1️⃣ 网络不通

ip route
ping 网关
ping DNS

2️⃣ DNS 不生效

cat /etc/resolv.conf
nslookup www.google.com

3️⃣ 接口未启动

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

如果你愿意,可以告诉我:

  • ✅ 具体系统(如 Ubuntu 22.04 / CentOS 7)
  • ✅ 是服务器还是虚拟机
  • ✅ 需要 DHCP 还是静态 IP

我可以给你精确到命令级别的配置方案。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI