请开启 JavaScript
Parallels Desktop虚拟机安装debian12系统后,如何设置静态ip,以供远程连接 – 老迟笔记

Parallels Desktop虚拟机安装debian12系统后,如何设置静态ip,以供远程连接

一、替换清华源

1、‌备份原配置文件‌

sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak

2、写入清华源配置‌

sudo tee /etc/apt/sources.list <<'EOF'
# 清华大学镜像源 [:ml-citation{ref="1" data="citationList"}2][:ml-citation{ref="5" data="citationList"}
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free non-free-firmware
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-backports main contrib non-free non-free-firmware
deb https://mirrors.tuna.tsinghua.edu.cn/debian-security bookworm-security main contrib non-free non-free-firmware
EOF

3、更新软件源

sudo apt update

二、安装OpenSSH服务

1、先确定是否安装openssh

sudo systemctl status ssh

2、安装OpenSSH服务

sudo apt install openssh-server

3、启动并验证服务

sudo systemctl start ssh
sudo systemctl enable ssh
sudo systemctl status ssh

三、固定ip

1、确定接口类型

ip a

查找类似 enp0s5、ens33 的接口名称(通常非 lo)

实例:

parallels@debian-gnu-linux-12:~$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: enp0s5: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:1c:42:80:0c:24 brd ff:ff:ff:ff:ff:ff
    inet 10.211.55.8/24 brd 10.211.55.255 scope global dynamic noprefixroute enp0s5
       valid_lft 1568sec preferred_lft 1568sec
    inet6 fdb2:2c26:f4e4:0:1ded:7fae:3984:7f4f/64 scope global temporary dynamic 
       valid_lft 604570sec preferred_lft 86086sec
    inet6 fdb2:2c26:f4e4:0:21c:42ff:fe80:c24/64 scope global dynamic mngtmpaddr noprefixroute 
       valid_lft 2591771sec preferred_lft 604571sec
    inet6 fe80::21c:42ff:fe80:c24/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

实例中的就是enp0s5

2、查看网关地址

ip route

实例:

parallels@debian-gnu-linux-12:~$ ip route
default via 10.211.55.1 dev enp0s5 proto dhcp src 10.211.55.8 metric 100 
10.211.55.0/24 dev enp0s5 proto kernel scope link src 10.211.55.8 metric 100 
169.254.0.0/16 dev enp0s5 scope link metric 1000 

这里的网关地址就是10.211.55.1

3、强制释放 DHCP 租约

sudo dhclient -r enp0s5  # 释放租约
sudo ip addr flush enp0s5  # 清除接口缓存
sudo rm /var/lib/dhcp/dhclient.leases  # 删除租约记录文件

4、编辑网络配置文件

Debian 12 默认使用传统配置文件 /etc/network/interfaces

sudo vi /etc/network/interfaces

将原有内容全部删除,替换为下面内容(替换 enp0s5 为你的实际接口名):

auto enp0s5
iface enp0s5 inet static
    address 10.211.55.101
    netmask 255.255.255.0
    gateway 10.211.55.1
    dns-nameservers 8.8.8.8
    up ip route del default via 10.211.55.1 || true  # 先删除旧路由
    up ip route add default via 10.211.55.1 dev enp0s5 metric 100  # 显式添加默认路由

关键参数说明‌:

address:目标静态 IP(需在局域网网段内)
gateway:路由器网关地址(通过 ip route | grep default 查询)
dns-nameservers:建议设置备用 DNS(如 8.8.8.8 114.114.114.114)

5、关闭冲突服务‌

Debian 12 默认同时运行 networking 和 NetworkManager,需禁用后者:

sudo systemctl stop NetworkManager
sudo systemctl disable NetworkManager

6、重启服务并验证

sudo systemctl restart networking
ip a show enp0s5  # 应显示静态IP 10.211.55.101
ping 10.211.55.1  # 测试网关连通性

7、持久化 DNS 配置

sudo vi /etc/resolv.conf

添加:

nameserver 8.8.8.8
options edns0 trust-ad

锁定配置防止覆盖:

sudo chattr +i /etc/resolv.conf