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

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> 
```

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) 環境下,也可以靠純指令做一點事了

2022年8月8日 星期一

PHP 開發筆記 - 在 Windows 10 上配置 cmd line 開發環境


這個故事說來話長,我本身不會在 Windows 開發,但為了同事只好配置環境出來,交叉比對運作的問題。就筆記一下,這次要做的事就弄到純 cmd line 開發環境:

安裝:

如此,在 cmd 就可以靠純指令工作,像是 composer install ,而在 cmd line 中有 vim 編輯器,可以快速編輯小東西。