Ubuntu 24.04 配置大量ip及ssh远程

温馨提醒:我是在虚拟机上测试的

  • 网段:192.168.233.0/24
  • 网关:192.168.233.2
  • 已有主 IP:192.168.233.3
  • 需要附加 IP 范围:192.168.233.4 ~ 192.168.233.253,一共 250 个附加 IP。

需求:外部任意访问该段任意 IP3-253,都可以 ssh 连接本机

步骤 1:确认网卡名称

bash 复制代码
ip addr

我这里的网卡名是 ens33;如果你的是eth0或者其他的话,下面所有配置把ens33替换成eth0或者其他的

把/etc/netplan/ 目录下的文件内容改成下面这种

然后 netplan apply

步骤 2:禁用 cloud‑init 网络

bash 复制代码
cd /etc/cloud/cloud.cfg.d
nano 99-disable-network-config.cfg

再把network: {config: disabled}这串写进上面的文件里面

步骤 3:禁用NetworkManager,启动systemd‑networkd

ubuntu 24.04 默认使用的是NetworkManager服务

bash 复制代码
systemctl stop NetworkManager
systemctl disable NetworkManager

systemctl start systemd-networkd
systemctl enable systemd-networkd

步骤 4:编写脚本

由于192.168.233.4 ~ 192.168.233.253 ip数太多了,不可能一个一个去敲,所以脚本会方便一点

bash 复制代码
nano /usr/local/bin/ipconf.sh

再把下面的这串写进去【ip网段那些换成你自己的就行】

bash 复制代码
#!/bin/bash
NET_CONF="/run/systemd/network/20-ens33.network"
INTERFACE="ens33"
PRIMARY_IP="192.168.233.3/24"
GW="192.168.233.2"
SUBNET_BASE="192.168.233"
START_IP=4
END_IP=253

mkdir -p /run/systemd/network

cat > ${NET_CONF} <<EOF
[Match]
Name=${INTERFACE}

[Network]
Address=${PRIMARY_IP}
Gateway=${GW}
DNS=223.5.5.5
DNS=8.8.8.8
KeepConfiguration=static
EOF

for ((i=START_IP;i<=END_IP;i++));do
echo "Address=${SUBNET_BASE}.${i}/24" >> ${NET_CONF}
done
networkctl reload

赋予权限

bash 复制代码
chmod +x /usr/local/bin/ipconf.sh

步骤 5:创建 systemd 服务,在 networkd 启动前生成配置

bash 复制代码
nano /etc/systemd/system/ipconf.service
bash 复制代码
[Unit]
Description=Generate ens33 large ip range config before networkd start
Before=systemd-networkd.service

[Service]
Type=oneshot
ExecStart=/usr/local/bin/ipconf.sh

[Install]
WantedBy=multi‑user.target

启用并执行

bash 复制代码
systemctl daemon-reload
systemctl enable --now ipconf.service
systemctl restart systemd-networkd

步骤 6:测试验证

先模拟网卡up,down的情况

bash 复制代码
ip link set ens33 down
ip addr

从上面结果来看,ip没了

bash 复制代码
ip link set ens33 up
ip addr

我们查看一下/run/systemd/network/20‑ens33.network 这个文件【我们在创建ipconf.sh文件里面有写到】

我们从外面ping 192.168.233.3 ~ 192.168.233.253 任意几个ip是可以通的

重启机器情况

重启机器后,ip依然还在,外面也是能正常ping 通

步骤7:ssh测试验证

安装一下ssh【如果已经安装ssh了,这一步可忽略】

bash 复制代码
apt update
apt install -y openssh-server

安装完成后,/etc/ssh下面会多出sshd_config服务端配置文件

启动并开机自启 ssh 服务

bash 复制代码
systemctl start ssh
systemctl enable ssh

外部测试ssh

如果要允许root远程的话,编辑 /etc/ssh/sshd_config,然后将

PermitRootLogin prohibit-password

改为

PermitRootLogin yes

记得把PermitRootLogin前面的#去掉

修改完重启 ssh

systemctl restart ssh

相关推荐
奔跑的大白啊7 小时前
多容器共享目录权限踩坑记
linux·ubuntu·共享目录·linux权限·docker 容器化部署
91刘仁德11 小时前
IP协议详解:从IP协议头到网段划分、路由与NAT
linux·服务器·网络·网络协议·tcp/ip
酣大智14 小时前
H3C MSR3600 交换机配置 SSH
网络·ssh·智能路由器
马六六i15 小时前
市面上正规的IP驱动产业新场景新工具有哪些
大数据·网络·python·tcp/ip
缘友一世17 小时前
银河护卫队在steam(linux Proton GE-Proton10-34)上启动无窗口问题修复
linux·ubuntu·steam·proton
刘胡子大叔18 小时前
repo 与 index:站点目录该怎么切
ubuntu·php
XiaoMaqqqq1 天前
市面上正规的IP驱动产业新场景新工具哪家强
运维·python·网络协议·tcp/ip
ShyanZh1 天前
【Python3基础】19-Socket 与 TCP、UDP 编程
python·网络协议·tcp/ip·udp
zhangrelay2 天前
用ROS2Lyrical实验报告册思路重做蓝桥云课ROS1实验并改进
linux·笔记·学习·ubuntu·机器人
2301_777998342 天前
TCP协议:滑动窗口与流量控制
服务器·网络·网络协议·tcp/ip·php