LinuxからLinuxにパスワードなしでssh接続する

Linux
この記事は約5分で読めます。

Linuxから別のLinuxに対してssh接続したいときにパスワードを入力を省略して接続できるようにします。

これができるようになるとサーバー間のアクセスができるようになり、scpを使ったファイルのコピーなどが簡単にできます。

                 

設定手順

環境

接続元

  • Ubuntu 20.04.1 LTS(EPSON DIRECT Endeavor NP15)

接続先

  • Raspberry Pi OS(旧Rasbpian)(Raspberry Pi Model B Rev 2)

どちらもssh接続できている状態です。

                 

接続元:sshクライアントのインストール

接続元のLinuxにsshクライアントのパッケージがインストールされているか確認します。

$ apt list --installed | grep ssh

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

libssh-4/focal-updates,focal-security,now 0.9.3-2ubuntu2.1 amd64 [インストール済み、自動]
openssh-client/focal-updates,now 1:8.2p1-4ubuntu0.1 amd64 [インストール済み、自動]
openssh-server/focal-updates,now 1:8.2p1-4ubuntu0.1 amd64 [インストール済み]
openssh-sftp-server/focal-updates,now 1:8.2p1-4ubuntu0.1 amd64 [インストール済み、自動]
ssh-import-id/focal,focal,now 5.10-0ubuntu1 all [インストール済み、自動]

openssh-clientがインストール済みであることが確認できました。

ちなみに、”WARNING: apt does not have a stable CLI interface. Use with caution in scripts.”というメッセージは、「aptコマンドの本来の用途とは違う使い方をしているよ」という意味のようです。

接続元:秘密鍵と公開鍵の作成

ssh-keygenコマンドを実行し、接続時に必要となる秘密鍵と公開鍵を作成します。

$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/np15/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase):    ←何も入力せずにEnter
Enter same passphrase again:             ←何も入力せずにEnter
Your identification has been saved in /home/np15/.ssh/id_rsa
Your public key has been saved in /home/np15/.ssh/id_rsa.pub
The key fingerprint is:
~省略~
The key's randomart image is:
+---[RSA 3072]----+
~省略~
+----[SHA256]-----+

これで公開鍵(/home/np15/.ssh/id_rsa.pub)と秘密鍵(/home/np15/.ssh/id_rsa)が作成できました。

$ ll /home/np15/.ssh/id_rsa /home/np15/.ssh/id_rsa.pub
-rw------- 1 np15 np15 2590  2月  4 09:54 /home/np15/.ssh/id_rsa
-rw-r--r-- 1 np15 np15  563  2月  4 09:54 /home/np15/.ssh/id_rsa.pub
                 

接続先:/etc/ssh/sshd_configの編集

接続先の/etc/ssh/sshd_configの以下の箇所を編集します。

$ sudo vi /etc/ssh/sshd_config

変更前
#PubkeyAuthentication yes

変更後
PubkeyAuthentication yes
AuthorizedKeysFile      .ssh/authorized_keys

上書き保存したらsshdを再起動します。

$ service ssh restart
                 

接続先:.ssh/authorized_keysの編集

接続先のホームディレクトリにある.ssh/authorized_keysに、接続元で作成した公開鍵(/home/np15/.ssh/id_rsa.pub)の内容をコピーします。

設定は以上で終了です。

接続

さっそく接続してみます。

$ ssh pi@接続先IPアドレス
Linux pi 4.19.73+ #1267 Fri Sep 20 18:07:57 BST 2019 armv6l

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: 

無事、パスワード要求されることなく接続することができました。

                 

scp

scpでファイルコピーをしてみます。

$ scp -p /tmp/aaa pi@接続先IPアドレス:/tmp/
aaa                                                                 100%  676   214.6KB/s   00:00

こちらもパスワード入力なしでコピーに成功しました。

これで作成したプログラムのコピーなどが簡単にできるようになります。

相互にパスワードなしでssh接続するには、接続先と接続元を入れ替えて同じことを行えばよいです。

 

コメント

タイトルとURLをコピーしました