Linuxにsshで接続する

自宅に使っていないPCがあったのでLinuxをインストールし色々と実験(遊び)できるようにしようかと。メインのPCは別のMacとしたいのでまずはSSHで接続できるように設定します。

構成

接続先のLinuxです。

bookstore@bookstoreUbuntu:~$ cat /etc/os-release
NAME="Ubuntu"
VERSION="20.04.1 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.1 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal

SSHで接続できるようにする

sshで接続するには接続先にsshサーバをインストールし起動させる必要があります。Ubuntuはインストール時点でsshサーバが入っていないのでまずはsshサーバをインストールします。

bookstore@bookstoreUbuntu:~$ sudo apt install openssh-server

インストールするとsshデーモンが起動しssh接続できるようになります。

bookstore@bookstoreUbuntu:~$ systemctl status sshd
● ssh.service - OpenBSD Secure Shell server
     Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2020-09-06 11:20:41 JST; 28min ago
       Docs: man:sshd(8)
             man:sshd_config(5)
    Process: 876 ExecStartPre=/usr/sbin/sshd -t (code=exited, status=0/SUCCESS)
   Main PID: 904 (sshd)
      Tasks: 1 (limit: 19011)
     Memory: 5.1M
     CGroup: /system.slice/ssh.service
             └─904 sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups

 9月 06 11:20:41 bookstoreUbuntu systemd[1]: Starting OpenBSD Secure Shell server...
 9月 06 11:20:41 bookstoreUbuntu sshd[904]: Server listening on 0.0.0.0 port 22.
 9月 06 11:20:41 bookstoreUbuntu sshd[904]: Server listening on :: port 22.
 9月 06 11:20:41 bookstoreUbuntu systemd[1]: Started OpenBSD Secure Shell server.

クライアントPCから接続してみましょう。

$ ssh bookstoreUbuntu
Welcome to Ubuntu 20.04.1 LTS (GNU/Linux 5.4.0-45-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

0 updates can be installed immediately.
0 of these updates are security updates.

Last login: Sun Sep  6 11:20:59 2020 from 192.168.50.126

接続できましたね。

公開鍵暗号を使ってSSH接続する

公開鍵暗号を使えばsshで接続する際にパスワードの入力を省くことができます。クライアントで秘密鍵・公開鍵を作成し、接続先に公開鍵を渡します。

鍵の作成と渡し方はこちらを参考にしました。

https://qiita.com/kazokmr/items/754169cfa996b24fcbf5

ここで詳しく書きませんが、基本は下記のコマンドでできます。

// キーペアの作成
$ ssh-keygen -t rsa

// 公開鍵の転送&設定
$ ssh-copy-id <ユーザ名>@<接続先>

SSH接続の詳細設定

実験的な環境であれば上記のステップで十分ですがもう少し細かい設定項目も見ていきます。sshサーバの設定は /etc/ssh/sshd_config で確認、設定を行うことができるようです。各項目についての説明はこちらに記載されているみたいです。

https://man.openbsd.org/sshd_config

デフォルトで設定ファイルが生成されていて、いくつかコメントアウトされているものもあります。コメントアウトされているものはデフォルトの値です。デフォルト値を変更するにはコメントアウトし、値を変更すればよさそうです。

パスワード入力による認証を無効化し、公開暗号を使った認証のみにしたいので以下 PasswordAuthentication をnoに変更します。

# To disable tunneled clear text passwords, change to no here!
PasswordAuthentication no
#PermitEmptyPasswords no

sshdを再起動します。

bookstore@bookstoreUbuntu:~$ systemctl restart sshd
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to restart 'ssh.service'.
Authenticating as: bookstore,,, (bookstore)
Password:
==== AUTHENTICATION COMPLETE ===

これでパスワードによるssh接続を無効にすることができました。