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

2026年5月13日 星期三

[Windows] 設置 OpenSSH 預設使用 PowerShell 7 環境 @ Windows 11

最近在處理 AI 自動化工作,開發了一些 claude skills ,發現最後需要在 Windows 11 運行 Claude Code CLI ,發現不同筆電的環境容易有問題,研究一陣子後,就決定統一都用 PowerShell 7 吧,也可以把 Windows 11 內建的 OpenSSH server 的 shell 也更換過去。

需要先理解 Windows 11 內建有 Powershell 5 ,但他不能拿來當 ssh 登入使用。這是預設採用 cmd 環境,改壞無法 ssh 登入 Windows 11 可以用這段還原,這是在 Powershell 運行:

New-ItemProperty `
  -Path "HKLM:\SOFTWARE\OpenSSH" `
  -Name DefaultShell `
  -Value "C:\Windows\System32\cmd.exe" `
  -PropertyType String `
  -Force

Restart-Service sshd

運行完,可用 Get-ItemProperty HKLM:\SOFTWARE\OpenSSH | Select-Object DefaultShell 查看預設的情況。

連續動作:

```
Windows PowerShell
著作權(C) Microsoft Corporation。保留擁有權利。

安裝最新的 PowerShell 以取得新功能和改進功能!https://aka.ms/PSWindows

PS C:\WINDOWS\system32> New-ItemProperty `
>>   -Path "HKLM:\SOFTWARE\OpenSSH" `
>>   -Name DefaultShell `
>>   -Value "C:\Windows\System32\cmd.exe" `
>>   -PropertyType String `
>>   -Force

DefaultShell : C:\Windows\System32\cmd.exe
PSPath       : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\OpenSSH
PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE
PSChildName  : OpenSSH
PSDrive      : HKLM
PSProvider   : Microsoft.PowerShell.Core\Registry

PS C:\WINDOWS\system32>
PS C:\WINDOWS\system32> Restart-Service sshd
PS C:\WINDOWS\system32> Get-ItemProperty HKLM:\SOFTWARE\OpenSSH | Select-Object DefaultShell

DefaultShell
------------
C:\Windows\System32\cmd.exe
```

後續安裝完 Powershell 7 且更正確是要安裝 PowerShell 7 MSI 套件,直接在 MS Store 安裝會是另一個:

winget install --id Microsoft.PowerShell --installer-type wix

這時安裝完路徑會顯示在 `C:\Program Files\PowerShell\7\pwsh.exe` ,這版就可以當 ssh shell 來用了:

```
PS C:\WINDOWS\system32> Get-Command pwsh

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Application     pwsh.exe                                           7.6.1.0    C:\Program Files\PowerShell\7\pwsh.exe


PS C:\WINDOWS\system32> New-ItemProperty `
>>   -Path "HKLM:\SOFTWARE\OpenSSH" `
>>   -Name DefaultShell `
>>   -Value "C:\Program Files\PowerShell\7\pwsh.exe" `
>>   -PropertyType String `
>>   -Force

DefaultShell : C:\Program Files\PowerShell\7\pwsh.exe
PSPath       : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\OpenSSH
PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE
PSChildName  : OpenSSH
PSDrive      : HKLM
PSProvider   : Microsoft.PowerShell.Core\Registry

PS C:\WINDOWS\system32>
PS C:\WINDOWS\system32> Restart-Service sshd
```

如此遠端到 Windows 11 時,登入後會看到版本資訊:

```
PowerShell 7.6.1
PS C:\Users\user> 
```

2024年6月29日 星期六

ThinkPad E15 開機無法偵測到 SSD 排除 - 2101: Detection error on Storage Device0 (M.2 Main Slot)

這台是 ThinkPad E15 2020年版,是疫情期間買給長輩用的平價筆電,在 2024.06 開機讀不到硬碟,由於已經沒有保固,再加上想要送回廠修理的方式,全台只有四個點且週末、假日不營業,實在不太方便,就乾脆自己修吧!


給原廠修的主因是想保有正版 Windows,省去煩惱一些資安問題,而自己處理則是可以加強隱私保護,確保沒人可以存取硬碟內的資訊,像是自己物理破壞掉也行。

研究了一下,據說 Windows 8 以後的筆電,會把 Windows 序號已經綁定在韌體上(BIOS?),因此筆電外殼也不會再貼序號貼紙,就這樣,只需看看能不能取得正版 Windows ISO 即可重新安裝:

首先,先研究一下如何拆筆電後蓋殼子,隨意找一下,發現有個 Youtube 影片教學快速看一下,剛好家裡也有個塑膠墊片就拿來開了!第一次沒經驗也把塑膠墊片弄得變形 XD 整體上需要一點點技巧的,可直接看官網 Support 教學影片:Lenovo Support 拆解與更換影片 > 底蓋拆卸

先買了一個 SSD 外接盒(非必要)想試試看救資料,結果失敗 Orz 看來 SSD 真的一壞了就難靠個人救了,要也得交給專業的來。回過頭來,也買一個特價中 MSI M371 1TB NVMe M.2 SSD 來使用,接著使用微軟官網下載的 ISO 檔,實際測試安裝都是失敗告收 XD 使用 Windows 10 或 11 的 x64 ISO 製成的 USB 安裝碟,永遠卡在要指定驅動程式的步驟:


原先還誤以為 SSD 沒抓到,跑去研究更新 BIOS 等事情,最終回想起 Lenovo USB Recovery Creator 工具,透過 Lenovo USB Recovery Creator 工具可以找到符合自己筆電的還原檔案(總共約11GB)並製作成還原隨身碟,必須找另一台 Windows 環境來運行該程式。整體上花 10分鐘下載資料,製作 USB 開機碟還要花個 20 分鐘。



終於,可以用 USB 還原隨身碟安裝 Windows 10 了:


以上步驟筆記:
經過了上述流程,終於筆電開起來是一個 Windows 10 Home 環境,接著就是一連串的 Windows 更新(數小時),預計最後要升級到 Windows 11 才收工,努力了 Orz 這時記得要先調整電源方案,調整成使用電源供電中不休眠。

當 Windows 10 安全更新至最新後,一直沒法收到 Windows 11 通知,只有一些 Windows 11 的簡介,像是跳轉到微軟官網看。後來透過以下方式可以更主動升級到 Windows 11:

2023年9月20日 星期三

Windows 開發筆記 - 使用 Command Line / CMD / ssh 與 PHP 8.2 / Composer / Git / VIM 開發環境 @ Windows 11

幫同事看了一下 Windows 開發環境,要在此環境使用 PHP8.2 與 PHP Laravel v10 framework,由於之前採用 xampp 管理套件被環境變數卡住。這些問題描述,瞬間拉回到學生時代在那邊設置 Windows %PATH% 環境變數 XD 我也忘了那時在幹嘛?推論是配置 Java 環境吧

目前就把手邊的 Windows 11 筆電拿來遠端,但懶得打開它。並且實際在 command line 測試會碰到幾個問題。

1. 從 https://windows.php.net/download/ 下載 VS16 x64 Non Thread Safe ,並解壓在 C:\php 目錄中,必須在設置 php.ini 。想要知道自己的 php.ini 位置,可以用 php.exe --ini

C:\php>php.exe --ini 
Configuration File (php.ini) Path: 
Loaded Configuration File:         (none)
Scan for additional .ini files in: (none)
Additional .ini files parsed:      (none)

接著把 C:\php\php.ini-development 複製到 C:\php\php.ini 使用:

C:\php>copy php.ini-development php.ini     
複製了         1 個檔案。

後續就編輯 php.ini 開啟一些項目,在此就靠 C:\cygwin64\bin\vim.exe 當編輯器(若碰到滑鼠圈選文字難複製,記得關掉滑鼠模式 :set mouse-=a),主要打開一些 php.ini 註解:

; Directory in which the loadable extensions (modules) reside.
; https://php.net/extension-dir
;extension_dir = "./"
; On windows:
extension_dir = "ext"
; ...
extension=curl
extension=fileinfo
extension=gd
extension=intl
extension=mbstring
extension=exif
extension=mysqli
extension=openssl
extension=sqlite3

C:\php>php.exe --ini 
Configuration File (php.ini) Path: 
Loaded Configuration File:         C:\php\php.ini
Scan for additional .ini files in: (none)
Additional .ini files parsed:      (none)

2. 下載 composer 後,預設會失敗:

C:\php>php.exe composer.phar self-update

In Factory.php line 648:
                                                                                                                          The openssl extension is required for SSL/TLS protection but is not available. If you can not enable the openssl extension, you can   
   disable this error, at your own risk, by setting the 'disable-tls' option to true.                          

self-update [-r|--rollback] [--clean-backups] [--no-progress] [--update-keys] [--stable] [--preview] [--snapshot] [--1] [--2] [--2.2] [--set-channel-only] [--] [<version>]

設置後:

C:\php>php.exe composer.phar self-update 
You are already using the latest available Composer version 2.6.3 (stable channel).

3. 在管理專案時,透過 C:\php\php.exe composer.phar install 時,會需要 GIT 指令,解法就是去官方安裝一下,安裝完的目錄位置在 C:\Program Files\Git 位置

C:\>"C:\Program Files\Git\bin\git.exe" --version
git version 2.42.0.windows.2

4. 回過頭來,更新環境變數 %PATH% 

C:\>git
'git' 不是內部或外部命令、可執行的程式或批次檔。
C:\>echo %PATH%
C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\Docker\Docker\resources\bin;C:\WINDOWS\system32\config\systemprofile\AppData\Local\Microsoft\WindowsApps;C:\Users\user\AppData\Local\Microsoft\WindowsApps;
C:\>set PATH=%PATH%;C:\Program Files\Git\bin\
C:\>git --version
git version 2.42.0.windows.2

如此在 Windows 的 command line (PowerShell) 環境下,也可以靠純指令做一點事了

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 運作是否正常

收工!

2023年8月31日 星期四

[macOS] 從 Macbook 遠端登入 Windows 筆電並使用 PowerShell 工作環境 @ macOS 13.2.1

由於想善用 Windows 筆電的資源(算力),先來試試看能不能打通 macOS command line 遠端登入進 Windows 11 筆電。查了一下資料,看來真的也有人有這種需求 XD

原理:
  1. Windows 11 安裝 OpenSSH Server
  2. macOS 安裝 PowerShell Core 資源 (非必要)
其中一還滿常見,二就有點怕怕,所幸資源算是微軟提供(?),有興趣可以裝 pkg 檔案,裝完在 macos command line 用 pwsh 指令就切換到 PowerShell 工作環境,目前只需做 (1) 就能完成需求,以下是在 Windows 11 筆電下運行指令,建議在上述微軟官方文件上複製使用,在此純做筆記:

PS C:\Users\user> Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH*'

Name  : OpenSSH.Client~~~~0.0.1.0
State : Installed

Name  : OpenSSH.Server~~~~0.0.1.0
State : NotPresent

PS C:\Users\user> ssh -V
OpenSSH_for_Windows_8.6p1, LibreSSL 3.4.3

PS C:\Users\user> Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0

Path          :
Online        : True
RestartNeeded : False

PS C:\Users\user> Start-Service sshd
PS C:\Users\user> Set-Service -Name sshd -StartupType 'Automatic'
PS C:\Users\user> if (!(Get-NetFirewallRule -Name "OpenSSH-Server-In-TCP" -ErrorAction SilentlyContinue | Select-Object Name, Enabled)) {
>>     Write-Output "Firewall Rule 'OpenSSH-Server-In-TCP' does not exist, creating it..."
>>     New-NetFirewallRule -Name 'OpenSSH-Server-In-TCP' -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22
>> } else {
>>     Write-Output "Firewall rule 'OpenSSH-Server-In-TCP' has been created and exists."
>> }
Firewall rule 'OpenSSH-Server-In-TCP' has been created and exists.

PS C:\Users\user> ssh localhost
user@localhost's password:

如此筆電重啟後,也會自動啟動 OpenSSH Server 。而 macOS 要遠端進去一樣把它當成 linux server 即可,靠 ssh 加上帳密遠端登入。

另外,常會使用 ping 來查看機器健康情況,而 Windows 11 默認關閉(也是滿好的資安規劃),在此要人工開啟:

系統 -> 隱私權與安全性 -> Windows 安全性 -> 防火牆與網路保護 -> 進階設定 -> 輸入規則 ->  核心網路診斷 - ICMP 回應要求 -> 啟動


可以自行依照需求開啟全部 ICMP 回應或部分情境。 

最後,莫忘筆電的電源節源設計,預設應該是 5分鐘就會進入休眠,可以把它調整成插電就永不進入休眠,以及要衝刺算力也可以調整電源策略。