はじめに
scpコマンドは非推奨ですが、今回、scp コマンドの動作について検証を行いました。
通常、scp コマンドは踏み台サーバ上のファイルを対象サーバへコピーする用途でよく利用していますが、対象サーバ上のファイルを踏み台サーバへコピーする方法については知識が不足していたため、あらためて検証を実施しました。
scpコマンドは以下の記事を参考にしました。
https://qiita.com/chihiro/items/142ebe6980a498b5d4a7
scp [オプション] コピー元パス 保存先パス
前提条件
踏み台サーバ(192.168.0.23/24):パブリックネットワークに接続されているサーバ
対象サーバ(192.168.0.20/24):プライベートネットワークのみに接続されているサーバ
踏み台サーバから対象サーバへの接続はパスワード認証で行い、SSH接続が可能である。
踏み台サーバにあるファイルを対象サーバにコピー
踏み台サーバ(192.168.0.23)上の text ファイルを、対象サーバ(192.168.0.20)の /tmp ディレクトリへコピーする作業を実施する。
[test@localhost .ssh]$ ip a | grep inet | grep enp
inet 192.168.0.23/24 brd 192.168.0.255 scope global noprefixroute enp0s3
[test@localhost .ssh]
[test@localhost .ssh]$ scp ./test test@192.168.0.20:/tmp
test@192.168.0.20's password:
test 100% 0 0.0KB/s 00:00
[test@localhost .ssh]$
踏み台サーバ(192.168.0.23)から、対象サーバ(192.168.0.20)の /tmp ディレクトリにファイルがコピーされたことを確認する。
[test@localhost .ssh]$ ip a | grep inet | grep enp
inet 192.168.0.23/24 brd 192.168.0.255 scope global noprefixroute enp0s3
[test@localhost .ssh]$
[test@localhost .ssh]$ ssh test@192.168.0.20 "ls -l /tmp/test"
test@192.168.0.20's password:
-rw-rw-r--. 1 test test 0 9月 28 09:18 /tmp/test
対象サーバにあるファイルを踏み台サーバにコピー
対象サーバ(192.168.0.20)において、/etc/nagios/ 配下に .cfg ファイルが3件存在することを確認する
[root@localhost nagios]# ip a | grep inet | grep enp
inet 192.168.0.20/24 brd 192.168.0.255 scope global noprefixroute enp0s3
[root@localhost nagios]#
[root@localhost nagios]# ls -tlr /etc/nagios
合計 16
-rw-r--r--. 1 root root 12835 3月 12 2021 nrpe.cfg
-rw-r--r--. 1 root root 0 9月 28 07:56 test1.cfg
-rw-r--r--. 1 root root 0 9月 28 07:56 test2.cfg
対象サーバ(192.168.0.20)に存在する Nagios 設定ファイルを、踏み台サーバ(192.168.0.23)の /tmp ディレクトリへコピーする
[root@localhost tmp]# ip a | grep inet | grep enp
inet 192.168.0.23/24 brd 192.168.0.255 scope global noprefixroute enp0s3
[root@localhost tmp]#
[root@localhost tmp]# scp test@192.168.0.20:/etc/nagios/*.cfg /tmp
test@192.168.0.20's password: パスワード入力する
nrpe.cfg 100% 13KB 4.1MB/s 00:00
test1.cfg 100% 0 0.0KB/s 00:00
test2.cfg 100% 0 0.0KB/s 00:00
[root@localhost tmp]#
一括でダウンロードできました。
[root@localhost tmp]# ip a | grep inet | grep enp
inet 192.168.0.23/24 brd 192.168.0.255 scope global noprefixroute enp0s3
[root@localhost tmp]#
[root@localhost tmp]# ls -ltr /tmp
合計 16
drwx------. 3 root root 17 9月 28 07:49 systemd-private-023639d322994ab6b92e7dd214b75e49-chronyd.service-anxJCm
-rw-r--r--. 1 root root 12835 9月 28 09:13 nrpe.cfg
-rw-r--r--. 1 root root 0 9月 28 09:13 test1.cfg
-rw-r--r--. 1 root root 0 9月 28 09:13 test2.cfg
scp コマンドにおいて -r オプションを指定し、ディレクトリ単位でのコピーを実施した。
対象サーバ(192.168.0.20)に存在する Nagiosフォルダを、踏み台サーバ(192.168.0.23)の /tmp ディレクトリへコピーする
[test@localhost ~]$ ip a | grep inet | grep enp
inet 192.168.0.23/24 brd 192.168.0.255 scope global noprefixroute enp0s3
[test@localhost ~]$
[test@localhost ~]$ scp -r test@192.168.0.20:/etc/nagios/ /tmp
test@192.168.0.20's password:
nrpe.cfg 100% 13KB 4.3MB/s 00:00
test1.cfg 100% 0 0.0KB/s 00:00
test2.cfg 100% 0 0.0KB/s 00:00
[test@localhost ~]$
[test@localhost ~]$ ls -tlr /tmp
合計 0
drwx------. 3 root root 17 9月 28 07:49 systemd-private-023639d322994ab6b92e7dd214b75e49-chronyd.service-anxJCm
drwxrwxr-x. 2 test test 56 9月 28 09:35 nagios ←nagiosフォルダがコピーされた
[test@localhost ~]$ ls -tlr /tmp/nagios
合計 16
-rw-r--r--. 1 test test 12835 9月 28 09:35 nrpe.cfg
-rw-r--r--. 1 test test 0 9月 28 09:35 test1.cfg
-rw-r--r--. 1 test test 0 9月 28 09:35 test2.cfg
[test@localhost ~]$
scpコマンドはコピーコマンドのため、対象サーバ(192.168.0.20)にあるnagiosの設定ファイルが更新や削除はされないことを確認
[root@localhost nagios]# pwd
/etc/nagios
[root@localhost nagios]# ip a | grep inet | grep enp
inet 192.168.0.20/24 brd 192.168.0.255 scope global noprefixroute enp0s3
[root@localhost nagios]#
[root@localhost nagios]# ls -tlr /etc/nagios
合計 16
-rw-r--r--. 1 root root 12835 3月 12 2021 nrpe.cfg
-rw-r--r--. 1 root root 0 9月 28 07:56 test1.cfg
-rw-r--r--. 1 root root 0 9月 28 07:56 test2.cfg
まとめ
複数の対象サーバにある設定ファイルを、1つのサーバへコピーすることが可能であることを確認しました。
scp コマンドは非常に便利であり、覚えておくと有用です。
今回の検証を通じて、ファイル単位のコピーとディレクトリ単位のコピーの両方が実行できることを確認しました。
また、scp コマンドは従来よく利用していた「踏み台サーバ上のファイルを対象サーバへコピーする」用途だけでなく、対象サーバ上のファイルを踏み台サーバへコピーすることも可能であることが分かりました。
なお、scp コマンドはだいぶ前から非推奨とされているため、今後は SFTP および rsync を利用した方法についても検証を進めていきます。