在 Azure Cloud Shell 中保存檔案
第一次啟動 Cloud Shell 時,系統會提示您選取您的記憶體選項。 如果您想要儲存每次使用 Cloud Shell 時可以使用的檔案,您必須建立新的或選擇現有的記憶體資源。 Cloud Shell 會使用 Microsoft Azure 檔案儲存體 共用,跨會話保存檔案。
Cloud Shell 記憶體的運作方式
Cloud Shell 會透過下列兩種方法保存檔案:
- 建立磁碟映像以包含目錄
$HOME
的內容。 磁碟映像會儲存至https://storageaccountname.file.core.windows.net/filesharename/.cloudconsole/acc_user.img
。 Cloud Shell 會自動同步處理此磁碟映像的變更。 - 將檔案共用掛接為
clouddrive
目錄中$HOME
的 。/home/<User>/clouddrive
路徑會對應至storageaccountname.file.core.windows.net/filesharename
。
注意
目錄中 $HOME
的所有檔案,例如 SSH 金鑰,都會保存在您的使用者磁碟映射中,該映像會儲存在掛接的檔案共用中。 使用最佳做法來保護目錄中的資訊 $HOME
和掛接的檔案共用。
保護記憶體存取
為了安全性,每個用戶都應該建立自己的記憶體帳戶。 針對 Azure 角色型存取控制 (RBAC),使用者必須在記憶體帳戶層級具有參與者存取權或更高許可權。
Cloud Shell 會在指定的訂用帳戶內,在記憶體帳戶中使用 Azure 檔案共用。 由於繼承的許可權,訂用帳戶中具有足夠訪問許可權的使用者可以存取訂用帳戶中包含的記憶體帳戶和檔案共用。
用戶應藉由在記憶體帳戶或訂用帳戶層級設定許可權,來鎖定其檔案的存取權。
Cloud Shell 儲存體帳戶包含 Cloud Shell 使用者在其主目錄中建立的檔案,其中可能包含敏感性資訊,包括存取令牌或認證。
使用 Azure 資源原則限制資源建立
儲存體 Cloud Shell 中建立的帳戶會以ms-resource-usage:azure-cloud-shell
標記。
如果您想要禁止使用者在 Cloud Shell 中建立記憶體帳戶,請建立 由此特定標籤觸發的 Azure 資源 原則。
管理 Cloud Shell 記憶體
使用 clouddrive
命令
Cloud Shell 包含命令行工具,可讓您變更 Cloud Shell 中 Azure 檔案儲存體 共用。 執行 clouddrive
以查看可用的命令。
Group
clouddrive :Manage storage settings for Azure Cloud Shell.
Commands
mount :Mount a file share to Cloud Shell.
unmount :Unmount a file share from Cloud Shell.
掛接新的 clouddrive
clouddrive mount
使用 命令來變更 Cloud Shell 所使用的共用。
注意
如果您要掛接新的共用,則會為您的 $HOME
目錄建立新的使用者映像。 $HOME
先前的映像會保留在先前的檔案共用中。
使用下列參數執行 clouddrive mount
命令:
clouddrive mount -s mySubscription -g myRG -n storageAccountName -f fileShareName
如需詳細資訊,請執行 clouddrive mount -h
。
Command
clouddrive mount :Mount an Azure file share to Cloud Shell.
Mount enables mounting and associating an Azure file share to Cloud Shell.
Cloud Shell will automatically attach this file share on each session start-up.
Note: This command does not mount storage if the session is Ephemeral.
Cloud Shell persists files with both methods below:
1. Create a disk image of your $HOME directory to persist files within $HOME.
This disk image is saved in your specified file share as 'acc_sean.img'' at
'//<storageaccount>.file.storage.windows.net/<fileshare>/.cloudconsole/acc_sean.img'
2. Mount specified file share as 'clouddrive' in $HOME for file sharing.
'/home/sean/clouddrive' maps to '//<storageaccount>.file.storage.windows.net/<fileshare>'
Arguments
-s | --subscription id [Required]:Subscription ID or name.
-g | --resource-group group [Required]:Resource group name.
-n | --storage-account name [Required]:Storage account name.
-f | --file-share name [Required]:File share name.
-d | --disk-size size :Disk size in GB. (default 5)
-F | --force :Skip warning prompts.
-? | -h | --help :Shows this usage text.
取消掛接 clouddrive
您可以隨時卸除 Cloud Shell 檔案共用。 因為 Cloud Shell 需要使用掛接的檔案共用,Cloud Shell 會提示您在下一個工作階段中建立並掛接另一個檔案共用。
- 執行
clouddrive unmount
。 - 確認並確認提示。
卸除的檔案共享會繼續存在,直到您手動刪除它為止。 卸除之後,Cloud Shell 就不會再搜尋後續會話中的此檔案共用。 如需詳細資訊,請執行 clouddrive unmount -h
、
Command
clouddrive unmount: Unmount an Azure file share from Cloud Shell.
Unmount enables unmounting and disassociating a file share from Cloud Shell.
All current sessions will be terminated. Machine state and non-persisted files will be lost.
You will be prompted to create and mount a new file share on your next session.
Your previously mounted file share will continue to exist.
Note: This command does not unmount storage if the session is Ephemeral.
Arguments
None
警告
雖然執行此命令不會刪除任何資源,但手動刪除對應至 Cloud Shell 的資源群組、儲存器帳戶或檔案共用會清除您的目錄磁碟映像和檔案 $HOME
共用中的任何檔案。 此動作無法復原。
使用 PowerShell 命令
取得目前檔案共享的相關信息
Get-CloudDrive
使用 PowerShell 中的 命令來取得文件共用之資源的相關信息。
PS /home/user> Get-CloudDrive
FileShareName : cs-user-microsoft-com-xxxxxxxxxxxxxxx
FileSharePath : //cs7xxxxxxxxxxxxxxx.file.core.windows.net/cs-user-microsoft-com-xxxxxxxxxxxxxxx
MountPoint : /home/user/clouddrive
Name : cs7xxxxxxxxxxxxxxx
ResourceGroupName : cloud-shell-storage-southcentralus
StorageAccountName : cs7xxxxxxxxxxxxxxx
SubscriptionId : 78a66d97-7204-4a0d-903f-43d3d4170e5b
卸除檔案共用
您可以隨時使用 Dismount-CloudDrive
Cmdlet 卸除 Cloud Shell 檔案共用。
卸除 會 clouddrive
終止目前的會話。
Dismount-CloudDrive
Do you want to continue
Dismounting clouddrive will terminate your current session. You will be prompted to create and
mount a new file share on your next session
[Y] Yes [N] No [S] Suspend [?] Help (default is "Y"):