0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

linux tarコマンド展開時のファイル上書きについて

Last updated at Posted at 2025-07-12

はじめに

よくある運用作業で「サーバからログを取得してください」と依頼されることがあります。
その際、tarコマンドでアーカイブ&圧縮して取得するのが定番ですが、展開時に注意しないと重要なファイルが上書きされることがあります。

本記事では、同じユーザを別サーバに作成してtar展開した際の挙動を確認し、秘密鍵やauthorized_keysの上書きリスクについて紹介します。

ユーザ登録は以下の記事を参考にしました。

本記事でtarコマンドの注意点を再度確認しました。

🔧 前提環境
以下2台のLinuxサーバを用意。
test1とtest2サーバで同じユーザ「test」を作成します。

test1(鍵あり)

test2(鍵なし)

以下は同じフォルダ構成の場合は上書きする例です。

test1とtest2で同じユーザtestがあることを確認する

test1

[root@test1 ~]# useradd -u 1001 -g 1001 -d /home/test test
[root@test1 ~]#
[root@test1 ~]# cat /etc/passwd | grep test
test:x:1001:1001::/home/test:/bin/bash

test2

[root@test2 ~]# useradd -u 1001 -g 1001 -d /home/test test
[root@test2 ~]#
[root@test2 ~]# cat /etc/passwd | grep test
test:x:1001:1001::/home/test:/bin/bash

test1サーバのみ鍵を作成

今回はrsa方式でパスフレーズなしで鍵を作成される

[root@test1 ~]# su - test
Last login: Fri Jul 11 23:33:04 UTC 2025 on pts/1
[test@test1 ~]$
[test@test1 ~]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/test/.ssh/id_rsa):
Created directory '/home/test/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/test/.ssh/id_rsa
Your public key has been saved in /home/test/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:7LeQ7AExbgX27Z93euABc9SJZZo2g47zQtWRtT22PtA test@test1
The key's randomart image is:
+---[RSA 3072]----+
|      o      .==.|
|     . o .  oo*.+|
|      o o .o O +.|
|     . = .+ + * o|
|      + S+.. = E |
|     . +..o. .=  |
|        *...o..=.|
|       . +.. ..oo|
|        . .   .. |
+----[SHA256]-----+
[test@test1 ~]$

作成した秘密鍵でログイン出来るように公開鍵をauthorized_keysに登録

[test@test1 .ssh]$ cat id_rsa.pub > authorized_keys
[test@test1 .ssh]$
[test@test1 .ssh]$ chmod 600 authorized_keys
[test@test1 .ssh]$ ls -tlr
total 12
-rw-r--r--. 1 test testgrp  564 Jul 11 23:33 id_rsa.pub
-rw-------. 1 test testgrp 2590 Jul 11 23:33 id_rsa
-rw-------. 1 test testgrp  564 Jul 11 23:34 authorized_keys
[test@test1 .ssh]$
[test@test1 .ssh]$ pwd
/home/test/.ssh

homeディレクトリに移動

[root@test1 home]# cd /home

/home/testフォルダを.tar.gzでアーカイブする

[root@test1 home]# tar cvzf /tmp/test.tar.gz ./test
./test/
./test/.bash_logout
./test/.bash_profile
./test/.bashrc
./test/.bash_history

/tmpにtest.tar.gzが出来ていることを確認

# [root@test1 home]# ls -ltr /tmp/*tar.gz
-rw-r--r--. 1 root root 3264 Jul 11 23:41 /tmp/test.tar.gz

test1サーバで作成したtest.tar.gzを、WinSCPでローカルPCにダウンロードし、test2サーバのホームディレクトリにアップロード

[ec2-user@test2 ~]$ pwd
/home/ec2-user
[ec2-user@test2 ~]$ ls -tlr
total 4
-rw-rw-r--. 1 ec2-user ec2-user 3264 Jul 11 23:41 test.tar.gz
[ec2-user@test2 ~]$
[ec2-user@test2 ~]$
[ec2-user@test2 ~]$

現時点でtest2サーバには.sshフォルダがないことを確認

[test@test2 ~]$ pwd
/home/test
[test@test2 ~]$ ls -tlra
total 12
-rw-r--r--. 1 test testgrp 492 Jan 28  2023 .bashrc
-rw-r--r--. 1 test testgrp 141 Jan 28  2023 .bash_profile
-rw-r--r--. 1 test testgrp  18 Jan 28  2023 .bash_logout
drwxr-xr-x. 4 root root     34 Jul 11 23:29 ..
drwx------. 2 test testgrp  62 Jul 11 23:29 .
[test@test2 ~]$

/homeディレクトリに移動

# sudo su -
# cd /home

test.tar.gzファイルの中身を確認 

展開前にtar.gzの中身を確認すると安全です。

[root@test2 home]# tar tvf /home/ec2-user/test.tar.gz
drwx------ test/testgrp      0 2025-07-11 23:33 ./test/
-rw-r--r-- test/testgrp     18 2023-01-28 22:29 ./test/.bash_logout
-rw-r--r-- test/testgrp    141 2023-01-28 22:29 ./test/.bash_profile
-rw-r--r-- test/testgrp    492 2023-01-28 22:29 ./test/.bashrc
-rw------- test/testgrp    154 2025-07-11 23:38 ./test/.bash_history
drwx------ test/testgrp      0 2025-07-11 23:34 ./test/.ssh/
-rw------- test/testgrp   2590 2025-07-11 23:33 ./test/.ssh/id_rsa
-rw-r--r-- test/testgrp    564 2025-07-11 23:33 ./test/.ssh/id_rsa.pub
-rw------- test/testgrp    564 2025-07-11 23:34 ./test/.ssh/authorized_keys

test2サーバにtest1の/home/test.tar.gzを展開

展開前にpwdで現在のフォルダの場所を確認した方が安全です。

[root@test2 home]# pwd  現在はhomeディレクトリにいることを確認する
/home
[root@test2 home]#
[root@test2 home]# tar xvzf /home/ec2-user/test.tar.gz
./test/
./test/.bash_logout
./test/.bash_profile
./test/.bashrc
./test/.bash_history
./test/.ssh/
./test/.ssh/id_rsa
./test/.ssh/id_rsa.pub
./test/.ssh/authorized_keys

test2サーバに.sshフォルダが作成される

[root@test2 work]# ls -tlra /home/test
total 16
-rw-r--r--. 1 test testgrp 492 Jan 28  2023 .bashrc
-rw-r--r--. 1 test testgrp 141 Jan 28  2023 .bash_profile
-rw-r--r--. 1 test testgrp  18 Jan 28  2023 .bash_logout
drwxr-xr-x. 4 root root     34 Jul 11 23:29 ..
drwx------. 3 test testgrp  95 Jul 11 23:33 .
drwx------. 2 test testgrp  61 Jul 11 23:34 .ssh ←作成された
-rw-------. 1 test testgrp 154 Jul 11 23:38 .bash_history
[root@test2 work]#
[root@test2 work]# ls -tlr /home/test/.ssh
total 12
-rw-r--r--. 1 test testgrp  564 Jul 11 23:33 id_rsa.pub
-rw-------. 1 test testgrp 2590 Jul 11 23:33 id_rsa
-rw-------. 1 test testgrp  564 Jul 11 23:34 authorized_keys

以下は別のフォルダ構成の場合は上書きしない例

/root/workフォルダへ移動

[root@test2 work]# pwd
/root/work

現時点でファイルなし

[root@test2 work]# ls -tlr
total 0
[root@test2 work]#

/root/workフォルダ配下に/home/ec2-user/test.tar.gzを展開

[root@test2 work]# tar xvzf /home/ec2-user/test.tar.gz
./test/
./test/.bash_logout
./test/.bash_profile
./test/.bashrc
./test/.bash_history
./test/.ssh/
./test/.ssh/id_rsa
./test/.ssh/id_rsa.pub
./test/.ssh/authorized_keys
[root@test2 work]#

/root/workフォルダ配下にtestのフォルダが作成

[root@test2 work]# ls -tlr
total 0
drwx------. 3 test testgrp 95 Jul 11 23:33 test
[root@test2 work]#
[root@test2 work]# pwd
/root/work
[root@test2 work]#

まとめ:shamrock:

test1で /home/test を圧縮した tar アーカイブを、test2サーバの同じ /home ディレクトリで展開したため、既存の /home/test がアーカイブの内容で丸ごと上書きされてしまいました。
tar コマンドは、展開先に同じパスのディレクトリ構成があると、その中身を自動的に上書きします。
ログ取得時に上書きが気になる場合は、一度 /tmp 配下など別の安全な場所に展開してから必要なファイルだけを移動する方法がおすすめです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?