顯示具有 cygwin 標籤的文章。 顯示所有文章
顯示具有 cygwin 標籤的文章。 顯示所有文章

2024年11月7日 星期四

Windows 開發筆記 - 將 Windows 11 筆電作業系統重置/還原至乾淨的環境與建立本地帳號密碼登入方式

由於 Windows 筆電常拿來實驗軟體,環境裝得有點亂,把它重置一下,筆記一下流程,哪天應該會用到:

系統 -> 復原 -> 重設此電腦 -> 本機重新安裝 -> 變更設定 -> 是否清除資料 (是) -> 按下重設就開始進行






接著會花一點時間,例如 20-30 分鐘後,筆電重開機,此台筆電就進入顯示 "正在重設這部電腦 10%" ,依筆電規格運算時間不同,在這台大概還要運行 40 分鐘:



手上的筆電,還會問你要不要保留筆電相關 keys ,通常是推薦選 ESC 略過清除:

A configuration change was requested to clear this computer's TPM (Trusted Platform Module).

WARNING: Clearing erases information stored on the TPM.
You will lose all created keys and access to data encrypted by these keys.
Press [F9] to confirm, ESC to reject

The system will restart.

詢問 Claude.ai 提供的資訊:

這個訊息是關於 TPM (Trusted Platform Module,信賴平台模組) 的重要警告。讓我解釋一下:

TPM 是電腦主機板上的一個安全晶片,主要用來:
1. 存儲加密金鑰
2. 保護 Windows Hello 的生物識別資料(如指紋)
3. 保護 BitLocker 硬碟加密的金鑰
4. 確保開機過程的安全性

這個警告在告訴你:
- 系統正在請求清除 TPM 中的資料
- 如果你按下 F9 確認清除,你將會失去:
  - TPM 中儲存的所有加密金鑰
  - 使用這些金鑰加密的資料的存取權限
- 按 ESC 可以取消這個動作

建議:
1. 如果你正在重置電腦並打算重新開始使用,按 F9 沒有問題
2. 如果你的電腦有使用 BitLocker 加密,請確保你已經備份了重要資料
3. 如果你不確定要不要清除,建議先按 ESC,等確認沒有重要資料需要保留後再進行

清除後,系統會重新啟動,而 TPM 會重新初始化以供新的 Windows 安裝使用。

接著,就像筆電剛買回來初次啟動一樣,開始被問要怎樣登入系統。印象中只要一開始設定好連外網路後,由於 Windows 也都在推廣自家帳號跟雲端服務,選個人使用會一直卡在被要求註冊一個帳號或登入一個微軟帳號,這時可以使用 "為公司或學校設定",在這種組織流程中可以像以前 Windows 7 等等,直接創建本機帳號:

為公司或學校設定 -> 登入選項 -> 改為加入網域 -> 輸入






接著進入系統後,大概會繼續做:
  • 系統安全更新 -> 升級到最新
  • 系統 -> 遠端桌面 -> 啟用
  • 系統 -> 電源和電池 
    • -> 電源
      • -> 螢幕與睡眠 -> 插電時,在下列時間後讓裝置進入睡眠狀態:永不
      • -> 電源模式:最佳效能
  • 關閉 筆電關閉就進入休眠
    • Ctrl + R -> 輸入 control panel
      • -> 控制台 -> 硬體與音效 -> 電源選項 -> 選擇蓋上螢幕時的行為
        • 當我關閉筆記電腦螢幕時:一般電源 -> 不進行動做
  • 開始 選單 -> Microsoft Store
    • -> Python 3.12
    • -> Visual Studio Code
    • -> Brave Browser
    • -> Mozilla Firefox
  • 啟用系統內建 OpenSSH Server
    • Ctrl+R -> 輸入 services.msc -> 找尋 OpenSSH 伺服器 
             -> 按 右鍵 選 啟用
             -> 按 右鍵 選 內容 -> 啟動類型 -> 自動
    • 防火牆設定 -> Ctrl+R -> wf.msc -> 輸入規則 -> OpenSSH SSH Server (sshd) 
             -> 進階 -> 設定檔 -> 勾選 網域/私人/公用
  • 啟用 telnet 工具
    • 控制台 -> 程式集 -> 程式和功能 -> 開啟或關閉 Windows 功能 -> Telnet Client
  • 下載 CygWin 安裝更多工具
    • https://www.cygwin.com/install.html
      • 或是用 Windows 11 系統內建的 curl 來下載
        • C:\Users\User\Downloads>curl https://www.cygwin.com/setup-x86_64.exe > setup-x86_64.exe
    • 安裝 curl, wget, vim, git, cmake, clang, autossh 工具
    • 之後透過 command 遠端登入 Windows 筆電時,可以在靠指令切換到 CygWin 的環境:
      • C:\Users\User>C:\cygwin64\Cygwin.bat 

        User@PC ~
        $

        User@PC ~
        $ pwd
        /cygdrive/c/Users/User
  • 使用 winget 指令:learn.microsoft.com/zh-tw/windows/package-manager/winget/
    • 太久沒用 Windows ,沒想到 Microsoft 有推出自家牌 winget 套件管理工具,直接用他安裝工具
    • 安裝完記得要重新登入才會更新工具搜尋環境,但是,有些工具裝完的確找不到,需自行更新 PATH 資訊
    • 範例一:安裝 wget 指令
      • C:\Users\User>wget
      • 'wget' 不是內部或外部命令、可執行的程式或批次檔。
      • C:\Users\User>winget install wget
      • 找到 Wget [JernejSimoncic.Wget] 版本 1.21.4
      • 此應用程式已由其擁有者授權給您。
      • Microsoft 不負任何責任,也不會授與協力廠商封裝的任何授權。
      • 正在下載 https://eternallybored.org/misc/wget/1.21.4/64/wget.exe
      •   ██████████████████████████████  6.71 MB / 6.71 MB
      • 已成功驗證安裝程式雜湊
      • 正在啟動套件安裝...
      • 新增的命令列別名: "wget"
      • 已修改路徑環境變數;重新啟動命令介面以使用新值。
      • 已成功安裝
    • 範例二:安裝 git vim
      • C:\Users\User>winget install git vim
      • 找到多個符合輸入條件的套件。請精煉輸入。
      • 名稱   識別碼        來源
      • ----------------------------
      • My Git 9NLVK2SL2SSP  msstore
      • Git    Git.Git       winget
      • Git    Microsoft.Git winget
      • 找到多個套件: git
      • 找到多個符合輸入條件的套件。請精煉輸入。
      • 名稱            識別碼          來源
      • ---------------------------------------
      • Vim Cheat Sheet 9WZDNCRDMCWR    msstore
      • Vim             vim.vim         winget
      • Vim             vim.vim.nightly winget
      • 找到多個套件: vim
    • 大概就安裝這些工具:
      • C:\Users\User>winget install wget vim.vim Microsoft.Git GnuWin32.Grep CoreyButler.NVMforWindows Docker.DockerDesktop
        • 其中 vim 預設還沒更新到 PATH 環境,需要去找他出來用 "C:\Program Files\Vim\vim91\vim.exe" ,對應的還有 "C:\Program Files (x86)\GnuWin32\bin\grep.exe" 等等
如此,又變成可以遠端進去處理、測試的 Windows 節點了! 像是 Python Windows 的實驗環境就可以用:

C:\Users\User\Downloads>python -m venv venv  
C:\Users\User\Downloads>.\venv\Scripts\activate
C:\Users\User\Downloads>pip install grip 

2023年9月7日 星期四

Windows 開發筆記 - 開機自動啟動 SSH Reverse Tunnel / autossh @ Windows 11, cygwin

視窗鍵 + R 輸入 shell:startup

想說要善加利用 Windows 筆電,就想到把它規劃成算力單位後,以及思考如何自動化叫他做事。在資安角度上就可採用 SSH Reverse Tunnel 方案,讓筆電開機啟動後,建立一個連線到指定的機器候命。如此,在 Windows 筆上安裝了一些服務後,就可以用遠端 Port Forwarding 的方式存取到。

首先,要先找到 autossh 這種用法,不然單建立一個 ssh 掛了很麻煩的。雖然有一些 github 的可挑,但整體上要找一個夠信任的來源,最後選擇知名的 cygwin ,就順便安裝 autossh, tmux, vim, wget, lftp, git, zip, unzip 等,如此 autossh.exe 就搞定,位置在 C:\cygwin64\bin\autossh.exe

之前的文章已提到,我在 Windows 11 下已啟用 OpenSSH server 了,那我可以測試把 openssh server 服務建立反向的連線(須留意 cygwin 認定的使用者家目錄跟 Windows PowerShell 的不一樣,需要先建立好 keypair 等資料)

C:\cygwin64\bin\autossh.exe -M 0 -N -R 10022:localhost:22 ServerUser@RemotServer

如此,在指定的機器(RemoteServer)上,就可以測試連線:

$ telnet localhost 10022
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
SSH-2.0-OpenSSH_for_Windows_8.6
^C^C
Connection closed by foreign host.

對於 cygwin 方面,目前採用 Windows 內建的 OpenSSH server 方案,連入後是 Windows Powershell 環境,若想切換到 cygwin ,可以多打一下:

Microsoft Windows [版本 10.0.22621.2215]

(c) Microsoft Corporation. 著作權所有,並保留一切權利。


user@WINDOWS-DESKTOP C:\Users\user>c:\cygwin64\Cygwin.bat


user@windows-desktop ~

$ 


對於 cygwin 用法,正規的安裝軟體還是透過原本的 setup.exe 去擴充,然而,可以去下載 github.com/transcode-open/apt-cyg 來使用,他可以提供一些便利的 command line 安裝套件的方式(但實務上不幸踩過失敗),當作一個備用方式:

user@windows-desktop ~

$ curl -s https://raw.githubusercontent.com/transcode-open/apt-cyg/master/apt-cyg > apt-cyg


user@windows-desktop ~

$ chmod 700 ./apt-cyg 


user@windows-desktop ~

$ ./apt-cyg 

NAME

  apt-cyg - package manager utility


SYNOPSIS

  apt-cyg [operation] [options] [targets]


DESCRIPTION

  apt-cyg is a package management utility that tracks installed packages on a   

  Cygwin system. Invoking apt-cyg involves specifying an operation with any     

  potential options and targets to operate on. A target is usually a package    

  name, file name, URL, or a search string. Targets can be provided as command  

  line arguments.


OPERATIONS

  install

    Install package(s).


  remove

    Remove package(s) from the system.


  update

    Download a fresh copy of the master package list (setup.ini) from the       

    server defined in setup.rc.


  download

    Retrieve package(s) from the server, but do not install/upgrade anything.   


  show

    Display information on given package(s).


  depends

    Produce a dependency tree for a package.


  rdepends

    Produce a tree of packages that depend on the named package.


  list

    Search each locally-installed package for names that match regexp. If no    

    package names are provided in the command line, all installed packages will 

    be queried.


  listall

    This will search each package in the master package list (setup.ini) for    

    names that match regexp.


  category

    Display all packages that are members of a named category.


  listfiles

    List all files owned by a given package. Multiple packages can be specified 

    on the command line.


  search

    Search for downloaded packages that own the specified file(s). The path can 

    be relative or absolute, and one or more files can be specified.


  searchall

    Search cygwin.com to retrieve file information about packages. The provided 

    target is considered to be a filename and searchall will return the

    package(s) which contain this file.


  mirror

    Set the mirror; a full URL to a location where the database, packages, and  

    signatures for this repository can be found. If no URL is provided, display 

    current mirror.


  cache

    Set the package cache directory. If a file is not found in cache directory, 

    it will be downloaded. Unix and Windows forms are accepted, as well as      

    absolute or regular paths. If no directory is provided, display current     

    cache.


OPTIONS

  --nodeps

    Specify this option to skip all dependency checks.


  --version

    Display version and exit.


下一步回歸到正題 - 開機自動執行,則是可以參考微軟官方文件 - 新增的應用程式以在 Windows 10 啟動時自動執行。此例在 Windows 11 上,透過 視窗鍵+R 執行 shell:startup 可快速開啟對應的目錄位置 ( %HOME%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup ),只需在此目錄添加幾個 bat 檔案,開啟就會幫我運行這指令了,如:

00_ssh-reverse-tunnel_openssh-server.bat

內容:

C:\cygwin64\bin\autossh.exe -M 0 -N -R 10022:localhost:22 ServerUser@RemotServer

開啟後,他會佔一個小視窗。由於那台筆電本身就是閒置的,所以佔著小視窗反而更好觀測 autossh 運作是否正常

收工!