云计算百科
云计算领域专业知识百科平台

腾讯云海外服务器Window切换为linux系统(从Window DD 到 Linux)

腾讯云提示:不支持重装为该镜像,非中国大陆地域不支持Linux系统和Windows系统之间互转

买了腾讯云的海外服务器,重装系统的时候发现无法切换,直接dd到linux系统,以下是全过程。记录一下。

主要是用到一个开源项目,可以在系统之间切换。理论上支持所有的云服务器,开源项目地址:ttps://github.com/bin456789/reinstall

根据当前的系统选择命令

当前系统是Linux时

国外服务器:

curl -O https://raw.githubusercontent.com/bin456789/reinstall/main/reinstall.sh || wget -O reinstall.sh $_

国内服务器:

curl -O https://www.ghproxy.cc/https://raw.githubusercontent.com/bin456789/reinstall/main/reinstall.sh || wget -O reinstall.sh $_

当前系统是 Windows时

服务器注意,请先关闭 Windows Defender 的 实时保护 功能。该功能会阻止 certutil 下载任何文件。

国外服务器:

certutil -urlcache -f -split https://raw.githubusercontent.com/bin456789/reinstall/main/reinstall.bat

国内服务器:

certutil -urlcache -f -split https://www.ghproxy.cc/https://raw.githubusercontent.com/bin456789/reinstall/main/reinstall.bat

解决 Windows 7 下无法下载脚本:

由于不支持 TLS 1.2、SHA-256、根证书没有更新等原因,Vista,7 和 Server 2008 (R2) 可能无法自动下载脚本,因此需要手动下载,具体操作如下:

用 IE 下载 (先在 IE 高级设置里启用 TLS 1.2),或者通过远程桌面,将这两个文件保存到同一个目录

  • https://raw.githubusercontent.com/bin456789/reinstall/main/reinstall.bat
  • https://www.cygwin.com/setup-x86.exe

使用时运行下载的 reinstall.bat

下载安装环境

我的是国外服务器,当前是window环境,使用:

curl -O https://www.ghproxy.cc/https://raw.githubusercontent.com/bin456789/reinstall/main/reinstall.sh || wget -O reinstall.sh $_

最好是管理员启动CMD命令

接着在cmd中输入命令,这个命令其实就是下载reinstall.bat这个文件。

所以执行这个命令之前最好先看一下当前的cmd是在哪个路径下,最好cd到自己想要下载的目录里再去执行。

这里顺便提一下,如果切换不过去,直接使用d:这个命令强制切换盘符,然后再去cd d:/test这样子使用

显示命令成功完成才行,这里有可能计算机会有报错,提示文件不安全被拦截,需要关闭Windows Defender 的 实时保护 功能。该功能会阻止 certutil 下载任何文件。

执行完会生成一个bat文件在当前cmd所在的目录下。

如图所示,我的是在C:\\Users\\Administrator>目录下

然后选中以管理员省份运行

这时候就会自动下载了,运行了以后这个.bat文件实际上执行并自己下载了setup-x86.exe这个文件。并且启动这个exe文件下载了环境。

等待下载中

下载完成,然后就可以去使用命令去切换系统了。

选择安装的linux系统

环境好了这时候就可以再次运行bat,并且携带参数,执行命令选择系统了。众所周知linux有很多发行版本,比我我要安装ubuntu 22.04

我这里是:C:\\Users\\Administrator>reinstall.bat ubuntu 22.04 如下图所示

  • 用户名 root 默认密码 123@@@,首次开机可能要等几分钟才能成功登录
  • 安装最新版可不输入版本号

可选参数

  • –password PASSWORD 设置密码
  • –ssh-port PORT 修改 SSH 端口(安装期间观察日志用,也用于新系统)
  • –web-port PORT 修改 Web 端口(安装期间观察日志用)
  • –hold 2 安装结束后不重启,此时可以 SSH 登录修改系统内容,系统挂载在 /os (此功能不支持 Debian/Kali)

我这里就用默认密码。

其实这个时候当前目录下已经有reinstall.sh这个脚本了,也可以使用如bash reinstall.sh ubuntu 22这样的方式来安装ubuntu

bash reinstall.sh anolis 7|8
rocky 8|9
redhat 8|9 –img='http://xxx.com/xxx.qcow2'
almalinux 8|9
opencloudos 8|9
centos 9|10
oracle 7|8|9
fedora 40|41
nixos 24.11
debian 9|10|11|12
openeuler 20.03|22.03|24.03
alpine 3.18|3.19|3.20|3.21
opensuse 15.5|15.6|tumbleweed
ubuntu 16.04|18.04|20.04|22.04|24.04 [–minimal]
kali
arch
gentoo

等待一会,直接执行shutdown /r /t 0重启主机

然后使用默认的端口22,用户名root和密码123@@@去连接就能连上了,可能这时候还没完全好

提示:

To view logs run:

tail -fn+l /reinstall.log

这个时候等一等,也可以输入上面的命令看一下日志信息

显示Welcome to Ubuntu这个才是完成了。再去登录之前的window就无法进去了。

可能遇到的问题

我安装的时候出现了这个问题:

C:\\Users\\Administrator>C:\\Users\\Administrator\\reinstall.bat
The system cannot execute the specified program.
"Failed to install Cygwin."
C:\\Users\\Administrator>

可以尝试以下方案进行解决(手动安装的setup-x86_64.exe,然后在线运行了bat脚本):

  • 首先确认当前目录
  • cd C:\\Users\\Administrator

  • 检查setup-x86_64.exe是否存在
  • dir setup-x86_64.exe

  • 尝试手动下载Cygwin setup文件
  • certutil -urlcache -f -split "http://www.cygwin.com/setup-x86_64.exe" "setup-x86_64.exe"

  • 以管理员身份运行以下命令:
  • setup-x86_64.exe –allow-unsupported-windows –quiet-mode –only-site –site http://mirror.nju.edu.cn/sourceware/cygwin –root C:\\cygwin –local-package-dir %tmp%\\cygwin-local-package-dir –packages curl,cpio,p7zip,bind-utils,ipcalc,dos2unix,binutils,jq,xz,gzip,zstd,openssl,libiconv

    另外,请确保:

  • 使用管理员权限运行CMD
  • 暂时关闭杀毒软件
  • reinstall备份

    @echo off
    mode con cp select=437 >nul
    setlocal EnableDelayedExpansion

    set confhome=https://raw.githubusercontent.com/bin456789/reinstall/main
    set confhome_cn=https://www.ghproxy.cc/https://raw.githubusercontent.com/bin456789/reinstall/main
    rem set confhome_cn=https://jihulab.com/bin456789/reinstall/-/raw/main

    rem 65001 代码页会乱码

    rem 不要用 :: 注释
    rem 否则可能会出现 系统找不到指定的驱动器

    rem Windows 7 SP1 winhttp 默认不支持 tls 1.2
    rem https://support.microsoft.com/en-us/topic/update-to-enable-tls-1-1-and-tls-1-2-as-default-secure-protocols-in-winhttp-in-windows-c4bd73d2-31d7-761e-0178-11268bb10392
    rem 有些系统根证书没更新
    rem 所以不要用https
    rem 进入脚本目录
    cd /d %~dp0

    rem 检查是否有管理员权限
    fltmc >nul 2>&1
    if errorlevel 1 (
    echo Please run as administrator^^!
    exit /b
    )

    rem 有时 %tmp% 带会话 id,且文件夹不存在
    rem https://learn.microsoft.com/troubleshoot/windows-server/shell-experience/temp-folder-with-logon-session-id-deleted
    if not exist %tmp% (
    md %tmp%
    )

    rem 检查是否国内
    if not exist geoip (
    rem 部分地区 www.cloudflare.com 被墙
    call :download http://dash.cloudflare.com/cdn-cgi/trace %~dp0geoip || goto :download_failed
    )
    findstr /c:"loc=CN" geoip >nul
    if not errorlevel 1 (
    rem mirrors.tuna.tsinghua.edu.cn 会强制跳转 https
    set mirror=http://mirror.nju.edu.cn
    if defined confhome_cn (
    set confhome=!confhome_cn!
    ) else if defined github_proxy (
    echo !confhome! | findstr /c:"://raw.githubusercontent.com/" >nul
    if not errorlevel 1 (
    set confhome=!confhome:http://=https://!
    set confhome=!confhome:https://raw.githubusercontent.com=%github_proxy%!
    )
    )
    ) else (
    rem 服务器在美国 equinix 机房,不是 cdn
    set mirror=http://mirrors.kernel.org
    )

    rem pkgs 改动了才重新运行 Cygwin 安装程序
    set pkgs=curl,cpio,p7zip,bind-utils,ipcalc,dos2unix,binutils,jq,xz,gzip,zstd,openssl,libiconv
    set tags=%tmp%\\cygwin-installed-%pkgs%
    if not exist "%tags%" (
    rem win10 arm 支持运行 x86 软件
    rem win11 arm 支持运行 x86 和 x86_64 软件
    rem wmic os get osarchitecture 显示中文
    rem wmic ComputerSystem get SystemType 显示英文

    rem SystemType
    rem windows 11 24h2 没有 wmic
    rem 有的系统精简了 powershell
    where wmic >nul 2>&1
    if not errorlevel 1 (
    for /f "tokens=*" %%a in ('wmic ComputerSystem get SystemType ^| find /i "based"') do (
    set "SystemType=%%a"
    )
    ) else (
    for /f "delims=" %%a in ('powershell -NoLogo -NoProfile -NonInteractive -Command "(Get-WmiObject win32_computersystem).SystemType"') do (
    set "SystemType=%%a"
    )
    )

    rem BuildNumber
    for /f "tokens=3" %%a in ('reg query "HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion" /v CurrentBuildNumber') do (
    set /a BuildNumber=%%a
    )

    set CygwinEOL=1

    echo !SystemType! | find "ARM" > nul
    if not errorlevel 1 (
    if !BuildNumber! GEQ 22000 (
    set CygwinEOL=0
    )
    ) else (
    echo !SystemType! | find "x64" > nul
    if not errorlevel 1 (
    if !BuildNumber! GEQ 9600 (
    set CygwinEOL=0
    )
    )
    )

    rem win7/8 cygwin 已 EOL,不能用最新 cygwin 源,而要用 Cygwin Time Machine 源
    rem 但 Cygwin Time Machine 没有国内源
    rem 为了保证国内下载速度, cygwin EOL 统一使用 cygwin-archive x86 源
    if !CygwinEOL! == 1 (
    set CygwinArch=x86
    set dir=/sourceware/cygwin-archive/20221123
    ) else (
    set CygwinArch=x86_64
    set dir=/sourceware/cygwin
    )

    rem 下载 Cygwin
    if not exist setup-!CygwinArch!.exe (
    call :download http://www.cygwin.com/setup-!CygwinArch!.exe %~dp0setup-!CygwinArch!.exe || goto :download_failed
    )

    rem 安装 Cygwin
    set site=!mirror!!dir!
    start /wait setup-!CygwinArch!.exe ^
    –allow-unsupported-windows ^
    –quiet-mode ^
    –only-site ^
    –site !site! ^
    –root %SystemDrive%\\cygwin ^
    –local-package-dir %tmp%\\cygwin-local-package-dir ^
    –packages %pkgs% ^
    && type nul >"%tags%"

    if errorlevel 1 (
    echo "Failed to install Cygwin."
    exit /b 1
    )
    )

    rem 在c盘根目录下执行 cygpath -ua . 会得到 /cygdrive/c,因此末尾要有 /
    for /f %%a in ('%SystemDrive%\\cygwin\\bin\\cygpath -ua ./') do set thisdir=%%a

    rem 下载 reinstall.sh
    if not exist reinstall.sh (
    call :download_with_curl %confhome%/reinstall.sh %thisdir%reinstall.sh || goto :download_failed
    call :chmod a+x %thisdir%reinstall.sh
    )

    rem 为每个参数添加引号,使参数正确传递到 bash
    for %%a in (%*) do (
    set "param=!param! "%%~a""
    )

    rem 方法1
    %SystemDrive%\\cygwin\\bin\\dos2unix -q '%thisdir%reinstall.sh'
    %SystemDrive%\\cygwin\\bin\\bash -l -c '%thisdir%reinstall.sh !param!'

    rem 方法2
    rem %SystemDrive%\\cygwin\\bin\\bash reinstall.sh %*
    rem 再在 reinstall.sh 里运行 source /etc/profile
    exit /b

    :download
    rem bits 要求有 Content-Length 才能下载
    rem 据说如果网络设为“按流量计费” bits 也无法下载
    rem https://learn.microsoft.com/en-us/windows/win32/bits/http-requirements-for-bits-downloads
    rem certutil 会被 windows Defender 报毒
    rem windows server 2019 要用第二条 certutil 命令
    echo Download: %~1 %~2
    del /q "%~2" 2>nul
    if exist "%~2" (echo Cannot delete %~2 & exit /b 1)
    if not exist "%~2" certutil -urlcache -f -split "%~1" "%~2" >nul
    if not exist "%~2" certutil -urlcache -split "%~1" "%~2" >nul
    if not exist "%~2" exit /b 1
    exit /b

    :download_with_curl
    rem 加 –insecure 防止以下错误
    rem curl: (77) error setting certificate verify locations:
    rem CAfile: /etc/ssl/certs/ca-certificates.crt
    rem CApath: none
    echo Download: %~1 %~2
    %SystemDrive%\\cygwin\\bin\\curl -L –insecure "%~1" -o "%~2"
    exit /b

    :chmod
    %SystemDrive%\\cygwin\\bin\\chmod "%~1" "%~2"
    exit /b

    :download_failed
    echo Download failed.
    exit /b 1

    赞(0)
    未经允许不得转载:网硕互联帮助中心 » 腾讯云海外服务器Window切换为linux系统(从Window DD 到 Linux)
    分享到: 更多 (0)

    评论 抢沙发

    评论前必须登录!