如何使用脚本更改 Ubuntu 中适配器的静态 IP 地址?

如何使用脚本更改 Ubuntu 中适配器的静态 IP 地址?

在 Ubuntu 22.04 中,我尝试更改单个网络接口的静态 IP 地址没有改变任何其他东西。此接口是以太网转 USB 转换器,用于连接到本地以太网设备(不用于连接到互联网)。我找到了以下脚本

#!/bin/bash

# Network interface name
INTERFACE="enx00e04c680202"

# Static IP address configuration
IP_ADDRESS="192.168.200.99"
NETMASK="255.255.255.0"
GATEWAY="192.168.200.220"
DNS_SERVER="8.8.8.8"

# Backup existing network configuration file
sudo cp /etc/netplan/01-network-manager-all.yaml /etc/netplan/01-network-manager-all.yaml.bak

# Create or modify the Netplan configuration file
sudo tee /etc/netplan/01-network-manager-all.yaml > /dev/null <<EOF
network:
  version: 2
  renderer: networkd
  ethernets:
    $INTERFACE:
      addresses: [$IP_ADDRESS/24]
      gateway4: $GATEWAY
EOF

# Apply the new network configuration
sudo netplan apply

这确实改变了接口的静态 IP 地址enx00e04c680202,但它以某种方式影响了其他配置,所以我无法再连接到互联网!

ifconfig 看起来像这样

enp84s0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        ether 7c:57:58:80:1c:61  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        device memory 0x54000000-540fffff  

enx00e04c680202: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.200.100  netmask 255.255.255.0  broadcast 192.168.200.255
        ether 00:e0:4c:68:02:02  txqueuelen 1000  (Ethernet)
        RX packets 281  bytes 12926 (12.9 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 41  bytes 5083 (5.0 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enxa0cec8c97ebc: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        ether a0:ce:c8:c9:7e:bc  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 1054  bytes 131881 (131.8 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1054  bytes 131881 (131.8 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

wlp0s20f3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.11  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 2a01:cb15:d7:c00:b8e9:ab1c:e07e:a0af  prefixlen 64  scopeid 0x0<global>
        inet6 2a01:cb15:d7:c00:1be3:f260:49cf:f265  prefixlen 64  scopeid 0x0<global>
        inet6 fe80::83c9:9667:953e:6ca1  prefixlen 64  scopeid 0x20<link>
        ether 3c:e9:f7:a7:1c:0f  txqueuelen 1000  (Ethernet)
        RX packets 32356  bytes 40427564 (40.4 MB)
        RX errors 0  dropped 32  overruns 0  frame 0
        TX packets 19027  bytes 3230853 (3.2 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

我看到以下界面:

  • enp84s0: 不知道这是什么
  • enx00e04c680202:我尝试使用简单的命令行命令更改接口的 IP 地址
  • enxa0cec8c97ebc:可能是已断开的 USB 转以太网接口的名称
  • wlp0s20f3:很可能是通过 WLAN 将计算机连接到互联网的无线接口

当然,我想保持与互联网的连接,并且我正在寻找的命令应该只改变192.168.200.100接口的地址enx00e04c680202

那么我该如何修复这个脚本,或者也许有一个更简单的命令来做到这一点?(它必须是一个命令行命令,因为我可能需要经常更改静态 IP 地址......)

我尝试了运行建议的解决方案

sudo /sbin/ip addr add 192.168.200.99/24 dev enx00e04c680202

ifconfig该接口的旧 IP 地址仍然显示。我也试过

sudo /sbin/ip link set dev enx00e04c680202 down
sudo /sbin/ip addr add 192.168.200.99/24 dev enx00e04c680202
sudo /sbin/ip link set dev enx00e04c680202 up

但似乎还是没有改变界面。最后我尝试了

sudo systemctl restart networking

这会给出错误

Failed to restart networking.service: Unit networking.service not found.

答案1

对我来说,以下脚本完全满足我的需要:

#!/bin/bash
INTERFACE=enx00e04c680202
sudo ifconfig $INTERFACE down
sudo ifconfig $INTERFACE $1 netmask 255.255.255.0 up

然后我调用这个脚本

./set_network_address.sh 192.168.200.99

相关内容