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?

scpコマンドの実行場所とファイル権限について検証してみた

0
Posted at

はじめに

scpコマンドはホームディレクトリ 例 (/home/ユーザ名/.ssh)フォルダでないと実行できない認識でしたが本当のことを知りたいと思い検証しました。

scp以外にも、SSHを利用してファイルを転送する方法としてsftpがあります。
sftpの使用方法については、次の記事も参考にしてください。

ディレクトリ内のファイルを差分転送したり、コピー元とコピー先を同期したりする場合は、rsyncを使用することもあります。

検証

ホームディレクトリからscpコマンドを実行

ホームディレクトリに移動

[test@server2 ~]$ pwd
/home/test

scpコマンドでserver2からserver1(192.168.179.8)にtextfileをコピーできました。

[test@server2 ~]$ scp /home/test/textfile test@192.168.179.8:/tmp
The authenticity of host '192.168.179.8 (192.168.179.8)' can't be established.
ECDSA key fingerprint is SHA256:9TSWgQQJ0GEAMYi8Z+er1c4WIZNIJx6vgHh8L6F+3F0.
ECDSA key fingerprint is MD5:f6:b5:e1:96:53:fb:e2:1f:cf:02:d5:6d:dc:a9:35:f1.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.179.8' (ECDSA) to the list of known hosts.
test@192.168.179.8's password:
Could not chdir to home directory /home/test: Permission denied
bash: /home/test/.bashrc: Permission denied
textfile                                                                                                                       100%    0     0.0KB/s   00:00
[test@server2 ~]$
[test@server2 ~]$

/etcに移動してscpコマンドを実行

カレントディレクトリを/etcへ移動

cd /etc

scpコマンドでserver2からserver1(192.168.179.8)にtextfileを/tmpフォルダにコピー。

[test@server2 etc]$ scp /home/test/textfile test@192.168.179.8:/tmp
test@192.168.179.8's password:
Could not chdir to home directory /home/test: Permission denied
bash: /home/test/.bashrc: Permission denied
textfile                                                                                                                       100%    0     0.0KB/s   00:00
[test@server2 etc]$

おまけ

読み取り権限がないファイルをコピー

rootユーザーのみ読み取れるログファイル(/var/log/secure)を
server1(192.168.179.8)の/tmpフォルダにコピーしたところ、権限不足で失敗

[test@server2 etc]$ scp /var/log/secure test@192.168.179.8:/tmp
test@192.168.179.8's password:
Could not chdir to home directory /home/test: Permission denied
bash: /home/test/.bashrc: Permission denied
/var/log/secure: Permission denied
[test@server2 etc]$

rootに昇格後、scpすると成功

[root@server2 ~]# scp /var/log/secure test@192.168.179.8:/tmp
The authenticity of host '192.168.179.8 (192.168.179.8)' can't be established.
ECDSA key fingerprint is SHA256:9TSWgQQJ0GEAMYi8Z+er1c4WIZNIJx6vgHh8L6F+3F0.
ECDSA key fingerprint is MD5:f6:b5:e1:96:53:fb:e2:1f:cf:02:d5:6d:dc:a9:35:f1.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.179.8' (ECDSA) to the list of known hosts.
test@192.168.179.8's password:
Could not chdir to home directory /home/test: Permission denied
bash: /home/test/.bashrc: Permission denied
secure                                        100% 3027     1.8MB/s   00:00
[root@server2 ~]#
[root@server2 ~]#

コピーファイルに読み取り権限を付与

/var/log/secureのログを/tmpにコピー

[root@server2 log]#  cd /var/log/
[root@server2 log]# cp -p ./secure /tmp

/tmp/secureをその他のユーザに読み取り権限を付与

[test@server01 /]$ chown test:test /tmp/secure
[test@server01 /]$ chmod 600 /tmp/secure

server2の/tmp/secureファイルをserver1(192.168.179.8)の/tmpフォルダにコピー

[test@server2 etc]$ scp /tmp/secure test@192.168.179.8:/tmp
test@192.168.179.8's password:
Could not chdir to home directory /home/test: Permission denied
bash: /home/test/.bashrc: Permission denied
secure                                                                                                                         100% 3027     3.5MB/s   00:00
[test@server2 etc]$

/tmp/secureファイルを終わったら削除

[test@server01 /]$ rm /tmp/secure

まとめ

scpコマンドは、ホームディレクトリや.sshディレクトリに移動しなくても、任意のディレクトリから実行できます。

コピー元ファイルや秘密鍵は、絶対パスと相対パスのどちらでも指定できます。ただし、絶対パスを使用すると、現在のディレクトリに左右されないため、パスの指定ミスを防ぎやすくなります。

scpでファイルを転送するには、scpコマンドを実行するユーザーに、コピー元ファイルの読み取り権限が必要です。

今回、一般ユーザーのtestでは/var/log/secureを読み取る権限がなかったため、転送に失敗しました。rootユーザーに切り替えると、ファイルを読み取れるようになったため、転送に成功しました。

また、転送先のtestユーザーのホームディレクトリに問題があったため、SSH接続時に警告が表示されました。ただし、転送先として/tmpを明示しており、testユーザーが/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?