vagrant-export-ubuntu-box-in-win11

OS: win11
vagrant: 2.3.7
virtualbox: 7.0.10
ubuntu: jammy64 

在win11下 通过do-release-upgrade的方式升级并获得noble64 box文件
1)、启动vagrant: 
vagrant up ---> sudo do-release-upgrade 

vagrantfile内容如下: 
# -*- mode: ruby -*-
# vi: set ft=ruby :


Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/jammy64"
  config.vm.provision "shell", inline: <<-SHELL
    # 替换Ubuntu源为阿里云
    sudo sed -i 's/archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list
    sudo sed -i 's/security.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list
    
  SHELL
end
    
2)、升级完毕后重启 ,
将vagrant的公钥追加到/home/vagrant/.ssh/authorized_keys中,保证.ssh文件夹位700,
authorized_keys文件权限为600. 
https://raw.githubusercontent.com/hashicorp/vagrant/main/keys/vagrant.pub

/etc/ssh/sshd_config: 
PubkeyAuthentication yes
PasswordAuthentication no


# 清理APT缓存
#清理其他旧的文件:
sudo dpkg --get-selections  | grep linux
#sudo apt purge linux-image-5.15.0-144-generic linux-modules-5.15.0-144-generic linux-headers-5.15.0-144 linux-headers-5.15.0-144-generic

sudo apt autoremove -y
sudo apt clean

# 清理临时文件
sudo rm -rf /tmp/*

# 清理日志
sudo truncate -s 0 /var/log/*log
sudo rm -rf /var/log/*log
sudo rm -rf /var/log/apt/*
sudo rm -rf /var/log/journal/*

sudo dd if=/dev/zero of=/EMPTY bs=1M || true && sudo rm -rf /EMPTY 
[vagrant@ubuntu-jammy:.ssh]$ sudo dd if=/dev/zero of=/EMPTY bs=1M || true && sudo rm -rf /EMPTY
dd: error writing '/EMPTY': No space left on device
36526+0 records in
36525+0 records out
38300000256 bytes (38 GB, 36 GiB) copied, 115.549 s, 331 MB/s

history -c && exit



# 打包为新的box文件
vagrant package --output ubuntu-noble64.box

将ubuntu-noble64.box加入到box内: 
D:\data\programs\vms>vagrant box add ubuntu/noble64 ubuntu-noble64.box --force
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'ubuntu/noble64' (v0) for provider:
    box: Unpacking necessary files from: file://D:/data/programs/vms/ubuntu-noble64.box
    box:
==> box: Successfully added box 'ubuntu/noble64' (v0) for 'virtualbox'!

切换到另一个目录noble内:测试下这个box是否ok,vagrantfile如下:
# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
  
  config.vm.box = "ubuntu/noble64"
  
end

启动虚拟机: vagrant up ,启动信息如下: 
D:\data\programs\noble>Vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'ubuntu/noble64'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: noble_default_1758555203974_83684
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection aborted. Retrying...
    default: Warning: Connection reset. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: The guest additions on this VM do not match the installed version of
    default: VirtualBox! In most cases this is fine, but in rare cases it can
    default: prevent things such as shared folders from working properly. If you see
    default: shared folder errors, please make sure the guest additions within the
    default: virtual machine match the version of VirtualBox you have installed on
    default: your host and reload your VM.
    default:
    default: Guest Additions Version: 6.0.0 r127566
    default: VirtualBox Version: 7.0
==> default: Mounting shared folders...
    default: /vagrant => D:/data/programs/noble

可以尝试下能否连接上: vagrant ssh 
D:\data\programs\noble>Vagrant ssh
vagrant@127.0.0.1: Permission denied (publickey).

好吧,果然连不上,我们看下ssh-config信息: vagrant ssh-config 
D:\data\programs\noble>vagrant ssh-config
Host default
  HostName 127.0.0.1
  User vagrant
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile D:/data/vbox/boxes/ubuntu-VAGRANTSLASH-noble64/0/virtualbox/vagrant_private_key
  IdentitiesOnly yes
  LogLevel FATAL
  PubkeyAcceptedKeyTypes +ssh-rsa
  HostKeyAlgorithms +ssh-rsa

好吧,我们换种方式来访问: 
D:\data\programs\noble>ssh -i D:/data/vbox/boxes/ubuntu-VAGRANTSLASH-noble64/0/virtualbox/vagrant_private_key vagrant@127.0.0.1 -p 2222
The authenticity of host '[127.0.0.1]:2222 ([127.0.0.1]:2222)' can't be established.
ED25519 key fingerprint is SHA256:kKXFioPuL/U74TF7vMeNM/aN9hWfZpq72qzSSGH0d9Q.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '[127.0.0.1]:2222' (ED25519) to the list of known hosts.
Bad permissions. Try removing permissions for user: NT AUTHORITY\\Authenticated Users (S-1-5-11) on file D:/data/vbox/boxes/ubuntu-VAGRANTSLASH-noble64/0/virtualbox/vagrant_private_key.
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions for 'D:/data/vbox/boxes/ubuntu-VAGRANTSLASH-noble64/0/virtualbox/vagrant_private_key' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key "D:/data/vbox/boxes/ubuntu-VAGRANTSLASH-noble64/0/virtualbox/vagrant_private_key": bad permissions
vagrant@127.0.0.1: Permission denied (publickey).

看到没?too open了,我们修改下私钥(即: vagrant_private_key)权限,改成只读权限
右键属性-》安全-》高级-》禁用继承,选择 :“将已继承的权限转换为此对象的显式权限”,
然后在“权限条目”中,将其他几个用户全部删掉,只留当前用户(不一定是administrator,而是当前登录用户),
接着点击“编辑”-》全部删除-》点击“读取”-》确定。

现在我们再试下:  
D:\data\programs\noble>ssh -i d:\data\vbox\boxes\ubuntu-VAGRANTSLASH-noble64\0\virtualbox\vagrant_private_key vagrant@127.0.0.1 -p 2222
Welcome to Ubuntu 24.04.3 LTS (GNU/Linux 6.8.0-83-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/pro

 System information as of Mon Sep 22 15:48:18 UTC 2025

  System load:  0.0               Processes:               111
  Usage of /:   7.8% of 38.70GB   Users logged in:         0
  Memory usage: 19%               IPv4 address for enp0s3: 10.0.2.15
  Swap usage:   0%


Expanded Security Maintenance for Applications is not enabled.

0 updates can be applied immediately.

Enable ESM Apps to receive additional future security updates.
See https://ubuntu.com/esm or run: sudo pro status


Last login: Mon Sep 22 15:48:40 2025 from 10.0.2.2
[vagrant@ubuntu-jammy:~]$


OK成功了,真的是win下私钥文件的权限太open了,而 SSH 协议为了安全,会严格拒绝使用权限过松的私钥(即使密钥本身正确),最终表现为 Permission denied (publickey)

那么我们再试下:vagrant ssh如何了? 
D:\data\programs\noble>Vagrant ssh
Welcome to Ubuntu 24.04.3 LTS (GNU/Linux 6.8.0-83-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/pro

 System information as of Mon Sep 22 15:52:22 UTC 2025

  System load:  0.0               Processes:               115
  Usage of /:   7.8% of 38.70GB   Users logged in:         0
  Memory usage: 19%               IPv4 address for enp0s3: 10.0.2.15
  Swap usage:   0%


Expanded Security Maintenance for Applications is not enabled.

0 updates can be applied immediately.

Enable ESM Apps to receive additional future security updates.
See https://ubuntu.com/esm or run: sudo pro status


Last login: Mon Sep 22 15:48:57 2025 from 10.0.2.2
[vagrant@ubuntu-jammy:~]$

很好,也成功了,说明真的是私钥文件太open导致无法访问,这也说明我们的box文件导出成功了,现在我们把它上传到hashicorp上,blablabla