LoginSignup
10
5

More than 3 years have passed since last update.

Arch Linux における OpenSSH の設定

Last updated at Posted at 2020-02-24

インストール

openssh パッケージをインストールします。

# pacman -Syu openssh

以下のコマンドを実行することで、コンピュータの起動時に自動的に SSH サーバーが起動するようになります。

# systemctl enable sshd

クライアントの設定

~/.ssh ディレクトリを作成し、秘密鍵と公開鍵のペアを生成します。

# mkdir .ssh
# chmod 700 .ssh
# cd .ssh
# ssh-keygen -t rsa -b 4096 -C "{email}"

何らかの方法でサーバーに公開鍵を転送します。

scp ~/.ssh/id_rsa.pub {user}@{host}:/home/{user}/id_rsa.pub

サーバーへの接続時にユーザー名・ポート番号・秘密鍵の指定を省略するため、以下の内容で ~/.ssh/config ファイルを作成します。

~/.ssh/config
Host {host}
  HostName {host}
  IdentitiesOnly yes
  IdentityFile ~/.ssh/id_rsa
  Port {port}
  User {user}

サーバーの設定

クライアントが作成した公開鍵を ~/.ssh/authorized_keys に追加し、パーミッションを設定します。

% mkdir ~/.ssh
% cat ~/id_rsa.pub >> ~/.ssh/authorized_keys
% rm -f ~/id_rsa.pub
% chmod 700 ~/.ssh
% chmod 600 ~/.ssh/authorized_keys

セキュリティ強化のため、下記の設定を行います。

/etc/ssh/sshd_config
# 不正アクセス防止のため、ポート番号をデフォルトの 22 から変更する。
# 衝突の可能性が高い Well Known Ports と Ephemeral Port の使用は避ける。
# 具体的には 1024-32767 の範囲で選択することを推奨。
Port {port}

# root ユーザでのログインは完全に禁止する。
# 管理者権限が必要な場合は一般ユーザでログインして sudo を使用する。
PermitRootLogin no

# 安全性の低いパスワードに認証は禁止し、常に秘密鍵を使用する。
PasswordAuthentication no

設定を反映するため、sshd を再起動します。

# systemctl restart sshd

使い方

ssh コマンドでサーバーにログインします。

% ssh {host}

ファイルを転送するには scp コマンドを使用します。

% scp {localPath} {host}:{remotePath}
10
5
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
10
5