Mac上的圖形和命令行完美統(tǒng)一谱轨,一直是我用Mac的最大動(dòng)力,簡(jiǎn)直是生產(chǎn)力神器橄浓。雖然是蘋果前員工,不過說實(shí)話再M(fèi)acOS的更新只停留在換個(gè)Dark Mode皮膚這種水平上了亮航,倒是PadOS感覺有些看頭荸实。但它老冤家Windows近來的一些更新讓我重新對(duì)Windows重新燃起興趣。
Windows上的圖形界面很友好缴淋,但CMD等命令行功能相比Linux和Mac來說實(shí)在太弱了准给。不過隨著Win10開始內(nèi)置WSL,可以直接在Windows上跑Linux重抖,例如Ubuntu等露氮,再也不用安裝Cygwin等第三方軟件。再加上.NET和PowerShell等開源并支持跨平臺(tái)钟沛,單看這幾招畔规,微軟新CEO把MS重新送上全球市值第一的寶座絕對(duì)是杠杠的。
PowerShell 6 開始恨统,核心部分已經(jīng)可以除了Windows之外的Linux/Mac上多平臺(tái)使用了油讯,而且有了Remote遠(yuǎn)程登錄功能。這個(gè)在我看來簡(jiǎn)直是一個(gè)開啟新時(shí)代的功能延欠,終于可以用命令行遠(yuǎn)程登錄Windows陌兑!
以下主要用Win10,以管理員Administrator
這個(gè)用戶為例由捎,要給這個(gè)用戶設(shè)置好密碼
實(shí)現(xiàn)這些主要用以下步驟:
- 安裝PowerShell 6
- Win10上開啟并OpenSSH
安裝PowerShell 6
Win10自帶有PowerShell兔综,不過要開啟遠(yuǎn)程登錄,要用6以上版本狞玛,在這里下載安裝到默認(rèn)位置即可软驰,https://github.com/PowerShell/PowerShell
要方便下面OpenSSH的設(shè)置,創(chuàng)建一下快捷方式心肪,因?yàn)镺penSSH里路徑不能用空格锭亏,以管理員運(yùn)行PowerShell,并輸入以下命令:
mklink /D c:\pwsh "C:\Program Files\PowerShell\6"
安裝配置OpenSSH
用PowerShell安裝很簡(jiǎn)單
Get-WindowsCapability -Online | ? Name -like 'OpenSSH*'
# This should return the following output:
Name : OpenSSH.Client~~~~0.0.1.0
State : NotPresent
Name : OpenSSH.Server~~~~0.0.1.0
State : NotPresent
如果有就不用硬鞍,沒有的話用以下命令裝一下
# 安裝OpenSSH Client
Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0
# 安裝 OpenSSH Server
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
# Both of these should return the following output:
Path :
Online : True
RestartNeeded : False
這樣安裝到C:\Windows\System32\OpehSSH
下慧瘤,不過配置文件在C:\ProgramData\ssh
。修改下C:\ProgramData\ssh\sshd_config
固该,
# 打開密碼登錄
PasswordAuthentication yes
# 開啟非對(duì)稱密鑰登錄
PubkeyAuthentication yes
# 之前務(wù)必已經(jīng)創(chuàng)建好PowerShell6所在的快捷方式
Subsystem powershell c:\pwsh\pwsh.exe -sshs -NoLogo -NoProfile
記得在防火墻里打開22端口或關(guān)閉锅减,不然連接不上。
啟動(dòng)SSH服務(wù)
Start-Service sshd
# SSH自動(dòng)運(yùn)行
Set-Service -Name sshd -StartupType 'Automatic'
# 看PowerShell有沒打開
sshd -T | select-string 'subsystem'
subsystem sftp sftp-server.exe
subsystem powershell c:\pwsh\pwsh.exe -sshs -NoLogo -NoProfile
在Mac上登錄Windows
在Mac或Linux上安裝好PowerShell6后伐坏,開啟
# 以下假設(shè)192.168.1.12就是Windows機(jī)器的IP
$session = New-PSSession -HostName "192.168.1.12" -UserName Administrator
Enter-PSSession $session
# 正常到這里你已經(jīng)登錄Windows怔匣!
[Administrator@192.168.1.12]: PS C:\Users\Administrator>