• Windows ↔ Kubuntu 双向切换
  • 固定 GUID(Windows 端)+ 自动识别 Boot ID(Kubuntu 端)
  • 去掉快速重启(改为正常重启)
  • Windows 快捷方式 + Kubuntu 全局快捷键配置

 

在双系统环境中,我们可以用一个 Python 脚本,实现 在 Windows 和 Kubuntu 之间一键切换并重启,无需进入 BIOS 手动选择启动项。


一前提条件

  • 已安装 WindowsKubuntu 双系统(UEFI 模式)
  • 两个系统都已安装 Python 3
  • Windows 端有管理员权限
  • Kubuntu 端有 sudo 权限
  • 已安装 efibootmgr(Kubuntu 端)

二获取启动项信息

1. 在 Kubuntu 中获取 Windows Boot ID (1.没使用了,后面改用名字获取)

sudo efibootmgr -v

找到类似:

Boot0001* Windows Boot Manager ...

记录 0001(你的 Windows Boot ID)。


2. 在 Windows 中获取 Kubuntu GUID

  1. 启动到 Windows
  2. 以管理员身份打开 命令提示符 (CMD)
  3. 输入:
    bcdedit /copy {bootmgr} /d "Kubuntu"
    
  4. 系统会返回一个 GUID,例如:
    已将项复制到 {c78ef8fb-91aa-11f0-8d5b-ff08951ac907}
    
    记录下来
    Kubuntu GUID = {c78ef8fb-91aa-11f0-8d5b-ff08951ac907}
    
  5. 设置 Kubuntu 启动路径:
    bcdedit /set {c78ef8fb-91aa-11f0-8d5b-ff08951ac907} path \EFI\UBUNTU\SHIMX64.EFI
    
    出现“操作成功完成”即可。

 


三Python 脚本

保存为 reboot_to_other_os.py

import platform
import subprocess
import time
import sys
import ctypes

# ===== 配置区 =====
# Windows 下查到的 Kubuntu GUID
KUBUNTU_GUID = "{c78ef8ff-91aa-11f0-8d5b-ff08951ac907}"
# =================

def is_admin_windows():
    try:
        return ctypes.windll.shell32.IsUserAnAdmin()
    except:
        return False

def get_win_boot_id_linux():
    """自动获取 Windows Boot Manager 的 Boot ID"""
    try:
        result = subprocess.run(["efibootmgr" "-v"] capture_output=True text=True check=True)
        for line in result.stdout.splitlines():
            if "EFI\\MICROSOFT\\BOOT\\BOOTMGFW.EFI" in line.upper():
                return line.split("*")[0].replace("Boot" "").strip()
    except subprocess.CalledProcessError:
        pass
    return None

def switch_to_kubuntu():
    print("🔄 Windows → Kubuntu")
    if not is_admin_windows():
        print("❌ 请以管理员身份运行")
        sys.exit(1)
    try:
        subprocess.run(
            ["bcdedit" "/set" "{bootmgr}" "bootsequence" KUBUNTU_GUID]
            check=True
        )
        print(f"✅ 已设置下一次启动为 Kubuntu ({KUBUNTU_GUID})")
        countdown_and_reboot("Windows")
    except subprocess.CalledProcessError:
        print("❌ 切换失败,请检查 GUID 或 Secure Boot 设置")
        sys.exit(1)

def switch_to_windows():
    print("🔄 Kubuntu → Windows")
    win_boot_id = get_win_boot_id_linux()
    if not win_boot_id:
        print("❌ 未找到 Windows Boot Manager 启动项")
        sys.exit(1)
    try:
        subprocess.run(["sudo" "efibootmgr" "-n" win_boot_id] check=True)
        print(f"✅ 已设置下一次启动为 Windows (Boot ID: {win_boot_id})")
        countdown_and_reboot("Linux")
    except subprocess.CalledProcessError:
        print("❌ 切换失败,请检查是否已配置免密 sudo")
        sys.exit(1)

def countdown_and_reboot(system_type):
    for i in range(5 0 -1):
        print(f"   {i} 秒后重启..." end="\r")
        time.sleep(1)
    print("\n🚀 正在重启...")
    if system_type == "Windows":
        subprocess.run(["shutdown" "/r"])
    elif system_type == "Linux":
        subprocess.run(["sudo" "reboot"])

if __name__ == "__main__":
    sysname = platform.system()
    if sysname == "Windows":
        switch_to_kubuntu()
    elif sysname == "Linux":
        switch_to_windows()
    else:
        print(f"⚠ 不支持的系统类型:{sysname}")
        sys.exit(1)

四免密 sudo(Kubuntu 端)

sudo visudo

添加:

你的用户名 ALL=(ALL) NOPASSWD: /usr/bin/efibootmgr /usr/sbin/reboot

五快捷键配置

1. Windows 端

  1. 创建快捷方式:
    python "C:\路径\reboot_to_other_os.py"
    
  2. 右键 → 属性 → 快捷方式 → 高级 → 勾选 以管理员身份运行
  3. 设置快捷键(如 Ctrl+Alt+K

2. Kubuntu 端

  1. 系统设置 → 快捷键 → 自定义快捷键
  2. 新建 → 全局快捷键 → 命令/URL
  3. 触发器:Ctrl+Alt+W
  4. 动作:
    python3 /home/你的用户名/reboot_to_other_os.py
    

六使用方法

  • Windows → Kubuntu:按 Ctrl+Alt+K(或双击快捷方式)
  • Kubuntu → Windows:按 Ctrl+Alt+W

七双系统时间不同步
 

核心解决方案:让 Linux 使用本地时间

推荐方法:通过一条命令让 Linux 将硬件时钟视为本地时间,与 Windows 保持一致。

 
  1. 在 Linux 终端执行(需管理员权限):

     

    sudo timedatectl set-local-rtc 1 --adjust-system-clock
    

    这条命令会:
    • 将硬件时钟从 UTC 模式切换为本地时间模式。
    • 自动调整系统时间以保持当前时间不变。
  2. 验证设置

     

    timedatectl status
    

    检查输出中的RTC in local TZ是否显示为yes。若显示yes,说明设置成功。
     
    yys@yys-defaultstring:~/mobile$ sudo timedatectl set-local-rtc 1 --adjust-system-clock [sudo] password for yys: yys@yys-defaultstring:~/mobile$ timedatectl status Local time: 二 2025-09-16 16:59:42 CST Universal time: 二 2025-09-16 08:59:42 UTC RTC time: 二 2025-09-16 08:59:42 Time zone: Asia/Shanghai (CST, +0800) System clock synchronized: yes NTP service: active RTC in local TZ: yes Warning: The system is configured to read the RTC time in the local time zone. This mode cannot be fully supported. It will create various problems with time zone changes and daylight saving time adjustments. The RTC time is never updated, it relies on external facilities to maintain it. If at all possible, use RTC in UTC by calling 'timedatectl set-local-rtc 0'.

     

  3. 重启系统
    完成后重启电脑,Windows 的时间应恢复正常。

八注意事项

  • Windows 必须以管理员身份运行脚本
  • Kubuntu 必须配置免密 sudo,否则会卡在输入密码
  • GUID 和 Boot ID 必须正确
  • 建议 BIOS 中保留两个启动项,防止误操作