這是我的拓樸:
My laptop ==> Ubuntu Desktop
| | |
User: root & saeed
My server ==> Ubuntu 20.04
| | |
Users: root & ubuntu (I deleted it as I explain below)
步驟:
- 我
tar zvcf back.tar.gz .
在我的筆記型電腦上運行用戶saeed
並建立文件。 - 我使用
root
路徑範例中的使用者/home/test
(不是任何使用者的主目錄)透過 SFTP 將此檔案上傳到我的伺服器。 - 我解壓縮
back.tar.gz
,/home/test
但我看到所有文件和目錄的所有者都是ubuntu:ubuntu
. - 我刪除了
ubuntu
用戶並嘗試了第三步,但得到了相同的結果。 - 我
back.tar.gz
在筆記型電腦中以另一個路徑提取,但所有文件的所有者都是saeed:saeed
.
為什麼會出現這種情況?
答案1
當以 root 身分提取時,預設tar
會保留原始所有者 ID。使用者saeed
恰好具有與另一台電腦上相同的 ID ubuntu
(並且可能是1000
)。您可以透過執行來檢查 ID id saeed
,或僅id
檢查目前使用者。
如果您不想保留使用者 ID,請不要以 root 身分提取或使用該--no-same-owner
選項。相關片段來自手動的:
--no-same-owner
Extract files as yourself (default for ordinary users).
對於一般使用者來說,可以實現相反的效果--same-owner
。
建立存檔時可以使用--owner
相關選項:--owner-map
--owner=NAME[:UID]
Force NAME as owner for added files. If UID is not
supplied, NAME can be either a user name or numeric UID.
In this case the missing part (UID or name) will be
inferred from the current host's user database.
When used with --owner-map=FILE, affects only those files
whose owner is not listed in FILE.
--owner-map=FILE
Read owner translation map from FILE. Empty lines are
ignored. Comments are introduced with # sign and extend
to the end of line. Each non-empty line in FILE defines
translation for a single UID. It must consist of two
fields, delimited by any amount of whitespace:
OLDUSR NEWUSR[:NEWUID]
OLDUSR is either a valid user name or a UID prefixed with
+. Unless NEWUID is supplied, NEWUSR must also be either
a valid user name or a +UID. Otherwise, both NEWUSR and
NEWUID need not be listed in the system user database.
As a result, each input file owned by OLDUSR will be
stored in archive with owner name NEWUSR and UID NEWUID.