LoginSignup
1
3

More than 5 years have passed since last update.

【Linux】パスワード無しでscpを実行する方法

Last updated at Posted at 2018-10-27

crontabで定期的にscpをする時にSSHログインのパスワードを尋ねられると困る(expectやsshpassを使う必要がある)ので、パスワード無しの公開鍵認証を用いることにしました。

サーバ

1.データ転送用のユーザを追加

# useradd datatransfer

2.SSHデーモンの設定ファイル(例:/etc/ssh/sshd_config)に下記を追加(コメントアウトを外す)

/etc/ssh/sshd_config
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys

3.このユーザのパスワード認証を無効に設定(任意)

/etc/ssh/sshd_config
Match User datatransfer
PasswordAuthentication no

4.SSHデーモンの再起動

# service sshd restart

ローカルホスト

1.認証用の鍵ペアを作成

ssh-keygen -t rsa

※パスワードを設定してしまうとパスワードを尋ねられるので入力しない

Enter passphrase (empty for no passphrase)
Enter same passphrase again:

2.生成した公開鍵(例:id_rsa.pub)をsshd_configのAuthorizedKeysFileで指定したファイルに追記

3.scp時に-iオプションで秘密鍵を指定

scp -i [秘密鍵] [コピー元] [コピー先]
1
3
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
1
3