一、源起
?? 當前在研究goploy自動化開源部署工具,該工具部署在linux機器上,而要部署服務(wù)的目標服務(wù)器有一部分是windows server服務(wù)器,goploy自動化部署,使用rsync部署方式,底層依賴于ssh遠程連接目標服務(wù)器,所以,要實現(xiàn)自動化部署,必須先實現(xiàn)ssh遠程連接目標windows server服務(wù)器。下面將依次說明具體步驟。
二、使用ssh遠程連接Windows
1.先決條件
在開始之前,計算機必須滿足以下要求:
(1)至少運行 Windows Server 2019 或 Windows 10(內(nèi)部版本 1809)的設(shè)備。
(2)PowerShell 5.1 或更高版本。
(3)作為內(nèi)置管理員組成員的帳戶。
2.先決條件檢查
若要驗證環(huán)境,請打開提升的 PowerShell 會話并執(zhí)行以下操作:
鍵入 winver.exe ,然后按 Enter 查看 Windows 設(shè)備的版本詳細信息。
運行 $PSVersionTable.PSVersion。 驗證主要版本至少為 5,次要版本至少為 1。 詳細了解如何在 Windows 上安裝 PowerShell。
運行下面的命令(PowerShell)。 當你是內(nèi)置管理員組的成員時,輸出將顯示 True。
(New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
3.安裝適用于 Windows 的 OpenSSH
可以使用 Windows Server 2019 和 Windows 10 設(shè)備上的 Windows 設(shè)置安裝這兩個 OpenSSH 組件。
若要安裝 OpenSSH 組件:
(1)打開“設(shè)置”,選擇“應(yīng)用”,然后選擇“可選功能”。
(2)掃描列表,查看是否已安裝 OpenSSH。 如果未安裝,請在頁面頂部選擇“添加功能”,然后:
找到“OpenSSH 客戶端”,然后選擇“安裝”
找到“OpenSSH Server”,然后選擇“安裝”
(3)設(shè)置完成后,回到“應(yīng)用”和“可選功能”,并確認 OpenSSH 已列出。
(4)打開“服務(wù)”桌面應(yīng)用。 (選擇“開始”,在搜索框中鍵入 services.msc ,然后選擇“服務(wù)”應(yīng)用或按 ENTER。)
(5)在詳細信息窗格中,雙擊“OpenSSH SSH 服務(wù)器”。
(6)在“常規(guī)”選項卡上的“啟動類型”下拉菜單中,選擇“自動”。
(7)若要啟動服務(wù),請選擇“啟動”。
備注:
安裝 OpenSSH 服務(wù)器將創(chuàng)建并啟用一個名為 OpenSSH-Server-In-TCP 的防火墻規(guī)則。 這允許端口 22 上的入站 SSH 流量。 如果未啟用此規(guī)則且未打開此端口,那么連接將被拒絕或重置。
4.連接到 OpenSSH 服務(wù)器
Shell或PowerShell中依照下面格式“ssh domain\username@servername”輸入命令,樣例如下:
ssh administrator@192.168.13.142
正常輸入密碼即可連接上,然后就可以在控制臺輸入命令運行了。
參考鏈接:
適用于 Windows 的 OpenSSH 入門
三、OpenSSH for Windows 中基于密鑰的身份驗證
?? 下面是摘抄的ssh連接的全部內(nèi)容,如果只是關(guān)注linux使用ssh連接windows服務(wù)器,則只需要關(guān)注“部署公鑰”相關(guān)的部分。生成公私鑰可參見下面命令:
ssh-keygen -t ed25519 -C "your_email@example.com"
1.部署私鑰
PowerShell腳本說明:
# By default the ssh-agent service is disabled. Configure it to start automatically.
# Make sure you're running as an Administrator.
Get-Service ssh-agent | Set-Service -StartupType Automatic
# Start the service
Start-Service ssh-agent
# This should return a status of Running
Get-Service ssh-agent
# Now load your key files into ssh-agent
ssh-add $env:USERPROFILE\.ssh\id_ed25519
將密鑰添加到客戶端上的 ssh-agent 后,ssh-agent 會自動檢索本地私鑰并將其傳遞給 SSH 客戶端。
2.部署公鑰
?? 要使用上面創(chuàng)建的用戶密鑰,必須將公鑰 (.ssh\id_ed25519.pub) 的內(nèi)容作為文本文件放在服務(wù)器上。 文件的名稱和位置取決于用戶帳戶是本地管理員組的成員還是標準用戶帳戶。 以下部分涵蓋標準和管理用戶。特別提醒,下面的PowerShell腳本,都是需要在其他windows機器的PowerShell終端上執(zhí)行,實現(xiàn)遠程配置功能。
(1)標準用戶
?? 公鑰 (.ssh\id_ed25519.pub) 的內(nèi)容需放置在服務(wù)器上的一個名為 authorized_keys 的文本文件中,該文件位于 C:\Users\username.ssh\。 可以使用 OpenSSH scp 安全文件傳輸實用工具或使用 PowerShell 將密鑰寫入文件來復(fù)制公鑰。
以下示例將公鑰復(fù)制到服務(wù)器(其中“username”替換為你的用戶名)。 最初,你需要使用服務(wù)器的用戶帳戶的密碼。
PowerShell腳本說明:
# Get the public key file generated previously on your client
$authorizedKey = Get-Content -Path $env:USERPROFILE\.ssh\id_ed25519.pub
# Generate the PowerShell to be run remote that will copy the public key file generated previously on your client to the authorized_keys file on your server
$remotePowershell = "powershell New-Item -Force -ItemType Directory -Path $env:USERPROFILE\.ssh; Add-Content -Force -Path $env:USERPROFILE\.ssh\authorized_keys -Value '$authorizedKey'"
# Connect to your server and run the PowerShell using the $remotePowerShell variable
ssh username@domain1@contoso.com $remotePowershell
(2)管理用戶
公鑰 (.ssh\id_ed25519.pub) 的內(nèi)容需放置在服務(wù)器上的一個名為 administrators_authorized_keys 的文本文件中,該文件位于 C:\ProgramData\ssh\。 可以使用 OpenSSH scp 安全文件傳輸實用工具或使用 PowerShell 將密鑰寫入文件來復(fù)制公鑰。 此文件上的 ACL 需要配置為僅允許訪問管理員和系統(tǒng)。
以下示例將公鑰復(fù)制到服務(wù)器并配置 ACL(其中“username”替換為你的用戶名)。 最初,你需要使用服務(wù)器的用戶帳戶的密碼。
備注:此示例演示了創(chuàng)建 administrators_authorized_keys 文件的步驟。 這僅適用于管理員帳戶,并且在用戶配置文件位置內(nèi)的必須是用戶而不是每用戶文件。
PowerShell腳本說明:
# Get the public key file generated previously on your client
$authorizedKey = Get-Content -Path $env:USERPROFILE\.ssh\id_ed25519.pub
# Generate the PowerShell to be run remote that will copy the public key file generated previously on your client to the authorized_keys file on your server
$remotePowershell = "powershell Add-Content -Force -Path $env:ProgramData\ssh\administrators_authorized_keys -Value '$authorizedKey';icacls.exe ""$env:ProgramData\ssh\administrators_authorized_keys"" /inheritance:r /grant ""Administrators:F"" /grant ""SYSTEM:F"""
# Connect to your server and run the PowerShell using the $remotePowerShell variable
ssh username@domain1@contoso.com $remotePowershell
PowerShell實際樣例如下:
# Get the public key file generated previously on your client
$authorizedKey = Get-Content -Path C:\Users\tom\.ssh\id_ed25519.pub
# Generate the PowerShell to be run remote that will copy the public key file generated previously on your client to the authorized_keys file on your server
$remotePowershell = "powershell Add-Content -Force -Path $env:ProgramData\ssh\administrators_authorized_keys -Value '$authorizedKey';icacls.exe ""$env:ProgramData\ssh\administrators_authorized_keys"" /inheritance:r /grant ""Administrators:F"" /grant ""SYSTEM:F"""
# Connect to your server and run the PowerShell using the $remotePowerShell variable
ssh administrator@192.168.13.142 $remotePowershell
參考鏈接:
OpenSSH for Windows 中基于密鑰的身份驗證
四、實際驗證
上述步驟完成后,在配置好私鑰的Wndwos或Linux機器上都可以使用下面的命令ssh遠程連接Windows服務(wù)器,且不需要輸入密碼:
ssh administrator@192.168.13.142
五、配置rsync遠程同步文件
1.windows下安裝cwrsync客戶端
windows系統(tǒng)先下載cwrsync客戶端(下載鏈接),我使用的是6.2.9版本,如果兩邊機器都是windows的話,則需要都安裝cwrsync客戶端,如解壓之后統(tǒng)一放到“D:\programs\cwrsync”目錄下,
2.配置系統(tǒng)環(huán)境變量
在系統(tǒng)環(huán)境變量中,添加CWRSYNC_HOME,值為“D:\programs\cwrsync”,并將“%CWRSYNC_HOME%\bin”添加進系統(tǒng)環(huán)境變量Path中,并且確保在“%SYSTEMROOT%\System32\OpenSSH\”的上面,樣例如下:
確保系統(tǒng)優(yōu)先使用cwrsync自帶的ssh.exe可執(zhí)行文件。配置完畢后,遠程的windows服務(wù)器,需要重啟一下,rsync同步才會生效。
六、遠程linux服務(wù)器配置ssh遠程連接
重點是在遠程linux機器對應(yīng)帳號的.ssh文件夾下,將本機公鑰寫入authorized_keys文件中(文件屬性是600),PowerShell腳本命令如下:
$publicKey = Get-Content -Path "C:\Users\tom\.ssh\id_ed25519.pub"
ssh root@192.168.13.145 "mkdir -p ~/.ssh && echo `"$publicKey`" >> ~/.ssh/authorized_keys && chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys"
七、rsync執(zhí)行樣例
rsync使用方法可參考rsync 用法教程。
官方幫助文檔。
1.linux向windows同步數(shù)據(jù)樣例(默認是采取ssh模式)
如果目錄不存在則先創(chuàng)建目錄
directoryPath="D:/temp/rsync/receive"
remotePowershell="powershell if (!(Test-Path -Path $directoryPath -PathType Container)) { New-Item -ItemType Directory -Force -Path $directoryPath;Write-Host ""Directory created successfully."";} else { Write-Host ""Directory already exists."";}"
# Connect to your server and run the PowerShell using the $remotePowerShell variable
ssh administrator@192.168.13.180 $remotePowershell
從本地同步數(shù)據(jù)到遠端
rsync -e ssh -avn /root/backup/goploy/ administrator@192.168.13.180:/cygdrive/D/temp/rsync/receive
rsync -avz /root/backup/goploy/ administrator@192.168.13.180:/cygdrive/D/temp/rsync/receive
rsync -e ssh -avz /root/backup/goploy/ administrator@192.168.13.180:/cygdrive/D/temp/rsync/receive
rsync -e ssh -avzn /root/backup/goploy/ administrator@192.168.13.180:/cygdrive/D/temp/rsync/receive
2.windows向linux同步數(shù)據(jù)樣例
使用ssh輸入密碼方式遠程連接目標linux機器并配置公鑰PowerShell樣例如下:
$publicKey = Get-Content -Path "C:\Users\tom\.ssh\id_ed25519.pub"
ssh root@192.168.13.190 "mkdir -p ~/.ssh && echo `"$publicKey`" >> ~/.ssh/authorized_keys && chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys"
下面是將整個F:\temp\apk文件夾同步到無端/root/temp/what文件夾下:
ssh root@192.168.13.190 "[ ! -d /root/temp/what ] && mkdir -p /root/temp/what || echo 'already exists'"
rsync -r /cygdrive/F/temp/apk root@192.168.13.190:/root/temp/what
做整體同步:
rsync -avz /cygdrive/F/temp/apk/ --delete root@192.168.13.190:/root/temp/what
反過來,從遠程服務(wù)器拷貝文件到本地(將/root/temp/what目錄整體拷貝到本地):
rsync -avz root@192.168.13.190:/root/temp/what --delete /cygdrive/F/temp/apk_receive
2.windows向windows同步數(shù)據(jù)樣例
先創(chuàng)建文件夾,再同步數(shù)據(jù):文章來源:http://www.zghlxwxcb.cn/news/detail-753834.html
$directoryPath = "tom\storage"
$remotePowershell = "powershell if (!(Test-Path -Path $directoryPath -PathType Container)) { New-Item -ItemType Directory -Force -Path $directoryPath;Write-Host ""Directory created successfully."";} else { Write-Host ""Directory already exists."";}"
# Connect to your server and run the PowerShell using the $remotePowerShell variable
ssh administrator@192.168.13.180 $remotePowershell
同步數(shù)據(jù)樣例:
rsync -avz -e ssh -vvv /local/path username@remotehost:/remote/path文章來源地址http://www.zghlxwxcb.cn/news/detail-753834.html
rsync -avz -e ssh -vvv /cygdrive/F/temp/apk --delete administrator@192.168.13.180:/cygdrive/D/temp/apk
rsync -avz -e ssh -vvv /cygdrive/F/temp/apk administrator@192.168.13.180:/cygdrive/D/temp/apk
rsync -avz /cygdrive/F/temp/apk administrator@192.168.13.180:/cygdrive/D/temp/apk
到了這里,關(guān)于如何在Linux機器上使用ssh遠程連接Windows Server服務(wù)器并使用rsync同步文件的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!