4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

AlmaLinuxでsshを使えるようにする

Last updated at Posted at 2024-05-27

AlmaLinuxにssh接続ができるようにしてみましょう。

0.本稿について

本稿の途中で記載してある、この緑のラベルの事項は読み飛ばして大丈夫な補足です。読んで混乱するようなら読まないで。

本稿の途中で記載してある、この黄色のラベルの事項は読み飛ばすとうまくいかない可能性がある事を記載しています。

本稿の途中で記載してある、この赤色のラベルの事項は絶対に読んで欲しい事を記載しています。読み飛ばさないで。

  • 対象:AlmaLinuxのインストールを終わらせてssh接続したい人
  • 目標:sshのインストールと、パスワード認証でのssh接続ができるようになる
  • 想定:Almalinuxのインストールが終わっていて、クライアントからネットワークが接続できる状態である事
  • 環境:本稿ではAlmaLinux9.xを最小インストールし、opensshが入っていない環境を使用しています

正確さの省略について

本稿では概念をわかりやすくするために、簡略化した表現を使用し、一部専門的な詳細を省略している事があります。

本稿の手順ではroot権限で操作しています。root権限を持っていない場合は、sudo権限を持つユーザで、適宜sudo実行する必要があります。

sudoってなんぞ?な人は↓を参照ください。

1.sshのパッケージ確認

まずはsshが入っているか確認してみます。

opensshという名称でインストール済みパッケージを検索

dnf list installed|grep -i openssh

実行例

# dnf list installed|grep -i openssh
openssh.x86_64                       8.7p1-10.el9_0                   @baseos   
openssh-clients.x86_64               8.7p1-10.el9_0                   @baseos

opensshopenssh-clientsという2つのパッケージしか表示されませんでした。

opensshはsshの基本パッケージで、openssh-clientsはssh接続をする側のクライアントソフト用のパッケージです。

sshサーバとしてssh接続を受け付けるには、sshサーバソフト用のパッケージであるopenssh-serverが必要です。

この状態では、このAlmaLinuxから他のリモートホストへのssh接続はできますが、別の端末からこのAlmaLinuxへのssh接続はできません。
そのため、openssh-serverのインストールを行います。

私が今回用意した環境では、このようになっていましたが、場合によってはopensshのパッケージ自体が入っていなかったり、逆にopenssh-serverパッケージが最初から入っている場合があります。しかし、その環境ではそういうもの!という感じなので、「インストール直後でもう入ってるんだけど!なんで?」とか「何も入ってなかったんだけど!」とか考えるよりも、「こういう環境の場合は入ってた!/入ってなかった!」と覚えておくとよいでしょう。

2.openssh-serverのインストール

dnf install openssh-server

実行例

# dnf install openssh-server
Last metadata expiration check: 0:00:25 ago on Mon 27 May 2024 02:03:41 PM UTC.
Dependencies resolved.
=========================================================================
 Package              Arch        Version              Repository   Size
=========================================================================
Installing:
 openssh-server       x86_64      8.7p1-38.el9         baseos      459 k
Upgrading:
 openssh              x86_64      8.7p1-38.el9         baseos      457 k
 openssh-clients      x86_64      8.7p1-38.el9         baseos      712 k

Transaction Summary
=========================================================================
Install  1 Package
Upgrade  2 Packages

Total download size: 1.6 M
Is this ok [y/N]: y        ←yを入力してEnterを押下します
Downloading Packages:
(1/3): openssh-8.7p1-38.el9.x86_64.rpm   1.0 MB/s | 457 kB     00:00    
(2/3): openssh-server-8.7p1-38.el9.x86_6 811 kB/s | 459 kB     00:00    
(3/3): openssh-clients-8.7p1-38.el9.x86_ 1.0 MB/s | 712 kB     00:00    
-------------------------------------------------------------------------
Total                                    992 kB/s | 1.6 MB     00:01     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                 1/1 
  Running scriptlet: openssh-8.7p1-38.el9.x86_64                     1/5 
  Upgrading        : openssh-8.7p1-38.el9.x86_64                     1/5 
  Running scriptlet: openssh-server-8.7p1-38.el9.x86_64              2/5 
useradd warning: sshd's uid 74 outside of the SYS_UID_MIN 201 and SYS_UID_MAX 999 range.

  Installing       : openssh-server-8.7p1-38.el9.x86_64              2/5 
  Running scriptlet: openssh-server-8.7p1-38.el9.x86_64              2/5 
Created symlink /etc/systemd/system/multi-user.target.wants/sshd.service → /usr/lib/systemd/system/sshd.service.

  Upgrading        : openssh-clients-8.7p1-38.el9.x86_64             3/5 
  Running scriptlet: openssh-clients-8.7p1-38.el9.x86_64             3/5 
  Running scriptlet: openssh-clients-8.7p1-10.el9_0.x86_64           4/5 
  Cleanup          : openssh-clients-8.7p1-10.el9_0.x86_64           4/5 
  Cleanup          : openssh-8.7p1-10.el9_0.x86_64                   5/5 
  Running scriptlet: openssh-8.7p1-10.el9_0.x86_64                   5/5 
  Verifying        : openssh-server-8.7p1-38.el9.x86_64              1/5 
  Verifying        : openssh-8.7p1-38.el9.x86_64                     2/5 
  Verifying        : openssh-8.7p1-10.el9_0.x86_64                   3/5 
  Verifying        : openssh-clients-8.7p1-38.el9.x86_64             4/5 
  Verifying        : openssh-clients-8.7p1-10.el9_0.x86_64           5/5 

Upgraded:
  openssh-8.7p1-38.el9.x86_64     openssh-clients-8.7p1-38.el9.x86_64    
Installed:
  openssh-server-8.7p1-38.el9.x86_64                                     

Complete!

改めてパッケージを確認します。

# dnf list installed|grep -i openssh
openssh.x86_64                       8.7p1-38.el9                     @baseos   
openssh-clients.x86_64               8.7p1-38.el9                     @baseos   
openssh-server.x86_64                8.7p1-38.el9                     @baseos 

openssh-serverが入りました。これでssh接続を受け付けるために必要なパッケージはそろいました。

3.sshの有効化

openssh-serverをインストールした後は、1.有効にし2.開始する必要があります。

sshサーバの状態確認

systemctl is-active sshd

実行例

# systemctl is-active sshd
inactive

inactiveなので動いていません(=このAlmaLinuxへssh接続はできません)。

sshサーバの有効化と開始

systemctl enable sshd
systemctl start sshd

この2つのコマンドを実行することで、sshサーバを有効にし、sshサーバの稼働を開始することができます。なおこのコマンドは実行しても何も表示はされません。

わかりやすく確認

activeかどうかだけが知りたい場合は以下コマンドを実行します。activeとなっていれば稼働しています。

systemctl is-active sshd

実行例

# systemctl is-active sshd
active

もう少し詳細を確認

以下コマンドを実行すると、sshサービスの稼働状況について詳細な情報を表示できます。

systemctl status sshd

実行例

# systemctl status sshd
● sshd.service - OpenSSH server daemon
     Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2024-05-27 14:16:22 UTC; 58s ago
       Docs: man:sshd(8)
             man:sshd_config(5)
   Main PID: 282 (sshd)
      Tasks: 1 (limit: 100645)
     Memory: 1.8M
        CPU: 39ms
     CGroup: /system.slice/sshd.service
             └─282 "sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups"

May 27 14:16:22 ALC106 systemd[1]: Starting OpenSSH server daemon...
May 27 14:16:22 ALC106 sshd[282]: Server listening on 0.0.0.0 port 22.
May 27 14:16:22 ALC106 sshd[282]: Server listening on :: port 22.
May 27 14:16:22 ALC106 systemd[1]: Started OpenSSH server daemon.

本稿では1.有効化2.開始をする必要があると言いましたが、 2開始だけでもssh接続は可能になります。しかし、サーバの再起動を行うと停止状態になってしまいます。サーバ再起動後でも自動で開始してくれるようにするのが、1.有効化です。

4.ユーザの作成

このままでは、サーバ上にまだユーザが存在しないため、ssh接続してもPermissionDenyとか表示されて接続できません。そのためサーバ上にユーザを作成します。

rootユーザがいるじゃないかとssh root@HOSTNAME/IPなどでssh接続しようとしてもrootユーザはデフォルトではssh接続できないようになっています。

ユーザの作成

useradd yoyo

yoyoはユーザ名です。適宜お好きなユーザー名に変更してください。

ユーザのパスワード変更

passwd yoyo

実行例

# passwd yoyo
Changing password for user yoyo.
New password: 
Retype new password: 
passwd: all authentication tokens updated successfully.

5.ssh接続する

いよいよssh環境を構築したAlmaLinuxにssh接続を行います。
別の端末から先ほど作成したユーザ(私の場合はyoyoユーザ)でログインします。
今回はWindows端末からコマンドプロンプトでssh接続を行いました。

>ssh ユーザ名@IP/ホスト名

実行例

> ssh yoyo@192.168.1.46
The authenticity of host '192.168.1.46 (192.168.1.46)' can't be established.
ED25519 key fingerprint is SHA256:X9Jlq2XtglvDcwhfJEcZVxaEvlx5W9PGQPlgxHpmxww.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.1.46' (ED25519) to the list of known hosts.
yoyo@192.168.1.46's password:

初回接続時はyesと入力してエンターを押下します。

  • ここで行われているのはホスト認証です。つまり、ホスト(サーバ)が正しいものかどうかを聞いています。厳密に正しいかどうかを確認する方法は本稿では割愛します。
  • また、ここでyesを返す事で、接続元のknown_hostsファイルへこのホストが登録されます。

次にユーザに応じたパスワードを要求されます。
先ほど変更したパスワードを入力して、プロンプトが表示されたらログイン成功です!

6.蛇足

6.1 sshってなに?

AlmaLinuxに限りませんが、サーバ用途としてLinuxを利用する場合はキーボードやディスプレイなどはすべて取っ払ってしまい、本体に電源コードとLANケーブル位しか挿さない状態にします。これをヘッドレス(headless)サーバといいます。こうすることで、箱と数本のケーブルだけしかないため、部屋の片隅に放置場所の選定が楽になります。

しかしこれでは何かあった時の保守作業が何もできなくなるため、LAN経由で接続できるようにします。この時の接続手段としてよく使われるのがsshです。

※sshの細かい理屈などは書籍やWebで検索したら出てくるので本稿では割愛します。

6.2 sshのパッケージ

ssh接続をできるようにするには、ssh接続を受け入れるopenssh-server、ssh接続をしに行くために必要なopenssh-clients、両方で必要な基本機能が入っているopensshという3つのパッケージが必要です。

パッケージ名称 概説
openssh ssh接続に必要な基本的なパッケージ
openssh-server ssh接続を受けるのに必要なパッケージ
openssh-clients ssh接続をしに行くのに必要なパッケージ

なお、ssh接続の受け入れしか行わない場合はopenssh-clientsは不要ですし、ssh接続を受け入れず、クライアントとしてしか使わない場合はopenssh-serverは不要です。

そのため、あえてパッケージを削除して「ssh接続は受け付けるが、そこから多段sshはさせない」、「リモートホストへのssh接続は行うが、他からのssh接続を受け付けない」などというようにセキュリティ範囲を限定する事ができます。

# dnf list installed|grep -i openssh
openssh.x86_64                         8.7p1-38.el9                   @baseos
openssh-clients.x86_64                 8.7p1-38.el9                   @baseos
openssh-server.x86_64                  8.7p1-38.el9                   @baseos

6.3 sshのパッケージ詳細を見てみる

あまり見ることもないかもしれないので、パッケージの情報を参照してみます。

# dnf info openssh openssh-clients openssh-server
Last metadata expiration check: 0:33:26 ago on Mon 27 May 2024 02:03:41 PM UTC.
Installed Packages
Name         : openssh
Version      : 8.7p1
Release      : 38.el9
Architecture : x86_64
Size         : 1.9 M
Source       : openssh-8.7p1-38.el9.src.rpm
Repository   : @System
From repo    : baseos
Summary      : An open source implementation of SSH protocol version 2
URL          : http://www.openssh.com/portable.html
License      : BSD
Description  : SSH (Secure SHell) is a program for logging into and executing
             : commands on a remote machine. SSH is intended to replace rlogin and
             : rsh, and to provide secure encrypted communications between two
             : untrusted hosts over an insecure network. X11 connections and
             : arbitrary TCP/IP ports can also be forwarded over the secure channel.
             :
             : OpenSSH is OpenBSD's version of the last free version of SSH, bringing
             : it up to date in terms of security and features.
             :
             : This package includes the core files necessary for both the OpenSSH
             : client and server. To make this package useful, you should also
             : install openssh-clients, openssh-server, or both.

Name         : openssh-clients
Version      : 8.7p1
Release      : 38.el9
Architecture : x86_64
Size         : 2.0 M
Source       : openssh-8.7p1-38.el9.src.rpm
Repository   : @System
From repo    : baseos
Summary      : An open source SSH client applications
URL          : http://www.openssh.com/portable.html
License      : BSD
Description  : OpenSSH is a free version of SSH (Secure SHell), a program for logging
             : into and executing commands on a remote machine. This package includes
             : the clients necessary to make encrypted connections to SSH servers.

Name         : openssh-server
Version      : 8.7p1
Release      : 38.el9
Architecture : x86_64
Size         : 1.0 M
Source       : openssh-8.7p1-38.el9.src.rpm
Repository   : @System
From repo    : baseos
Summary      : An open source SSH server daemon
URL          : http://www.openssh.com/portable.html
License      : BSD
Description  : OpenSSH is a free version of SSH (Secure SHell), a program for logging
             : into and executing commands on a remote machine. This package contains
             : the secure shell daemon (sshd). The sshd daemon allows SSH clients to
             : securely connect to your SSH server.

以上です。お疲れ様でした

4
2
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
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?