sshの公開鍵をauthorized_keysへ登録、その他諸々設定スクリプト

Debian

sshの公開鍵を登録する端末で一般ユーザーで実行するスクリプトです。
このスクリプトを実行すると /etc/ssh/sshd_config が修正されます。
修正箇所は以下の通りです。

  1. /etc/ssh/sshd_config: #Port 22 -> Port $SSH_PORT_NUM
  2. /etc/ssh/sshd_config: #PermitRootLogin prohibit-password -> PermitRootLogin no 
  3. /etc/ssh/sshd_config: #PubkeyAuthentication yes -> PubkeyAuthentication yes
  4. /etc/ssh/sshd_config: #PasswordAuthentication yes -> PasswordAuthentication no
  5. /etc/ssh/sshd_config: UsePAM yes -> UsePAM no

sshで使用するポート番号を$SSH_PORT_NUM で指定してからスクリプトを実行してください。
公開鍵(.pub)を cd 実行後の場所に配置してください。また、公開鍵(.pub)名をSSH_KEY_PUBで指定てください。
このスクリプトを実行後、端末は再起動します。

#!/bin/bash

### ssh_pub設定スクリプトVer1

### 令和4年7月14日作成

### 使い方
# 公開鍵(.pub)を cd 実行後の場所に配置してください
# nano debian11_ssh_pub.sh
# sudo chmod +x debian11_ssh_pub.sh
# ./debian11_ssh_pub.sh

### 設定内容ここから
# 1./etc/ssh/sshd_config: #Port 22 -> Port $SSH_PORT_NUM
# 2./etc/ssh/sshd_config: #PermitRootLogin prohibit-password -> PermitRootLogin no 
# 3./etc/ssh/sshd_config: #PubkeyAuthentication yes -> PubkeyAuthentication yes
# 4./etc/ssh/sshd_config: #PasswordAuthentication yes -> PasswordAuthentication no
# 5./etc/ssh/sshd_config: UsePAM yes -> UsePAM no
# 6.pub設定
### 設定内容ここまで

### 動作確認
# lsb_release -a
# No LSB modules are available.
# Distributor ID:	Debian
# Description:	Debian GNU/Linux 11 (bullseye)
# Release:	11
# Codename:	bullseye

### 環境に合わせた修正箇所ここから
# sshポート番号指定
SSH_PORT_NUM=2022
# .pub名
SSH_KEY_PUB="id_ed25519.pub"
### 環境に合わせた修正箇所ここまで

# ----------- 処理ここから -----------

# 1.ssh インストール
ssh -V &> /dev/null
if [ $? -ne 0 ] ; then
        echo "ssh インストールします"
        sudo apt -y install ssh

	# /etc/ssh/sshd_config バックアップ
	sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.org
else
        echo "ssh インストール済みです"

        # /etc/ssh/sshd_config.org がない場合の処理
	if [ ! -e /etc/ssh/sshd_config.org ]; then
		# /etc/ssh/sshd_config バックアップ
		sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.org
	fi
fi

# /etc/ssh/sshd_config 初期化
sudo cp /etc/ssh/sshd_config.org /etc/ssh/sshd_config

# /etc/ssh/sshd_config 編集
echo "/etc/ssh/sshd_config 編集"
# 1./etc/ssh/sshd_config #Port 22 -> Port $SSH_PORT_NUM
sudo sed -i -e 's/#Port 22/Port '$SSH_PORT_NUM'/' /etc/ssh/sshd_config

# 2./etc/ssh/sshd_config #PermitRootLogin prohibit-password -> PermitRootLogin no 
sudo sed -i -e 's/#PermitRootLogin prohibit-password/PermitRootLogin no/' /etc/ssh/sshd_config

# 3./etc/ssh/sshd_config #PubkeyAuthentication yes -> PubkeyAuthentication yes
sudo sed -i -e 's/#PubkeyAuthentication yes/PubkeyAuthentication yes/' /etc/ssh/sshd_config

# 4./etc/ssh/sshd_config #PasswordAuthentication yes -> PasswordAuthentication no
sudo sed -i -e 's/#PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config

# 5./etc/ssh/sshd_config UsePAM yes -> UsePAM no
sudo sed -i -e 's/UsePAM yes/UsePAM no/' /etc/ssh/sshd_config

# 6.pub設定
cd

# SSH_KEY_PUB がない場合の処理
if [ ! -e ~/$SSH_KEY_PUB ]; then
	echo $SSH_KEY_PUB がないため処理を終了します。
	exit 1
fi

# .ssh が存在または空でない場合は存在ディレクトリ名変更 $AUT_DIR.bak
if [ -d ./.ssh ]; then
        if [ -n "$(ls -A ./.ssh)" ]; then
                echo 既存の.sshディレクトリ名を.ssh.bakへ修正します。
                sudo mv ./.ssh ./.ssh.bak

		# .ssh.bak を削除する場合は下のコマンド rm -r ./.ssh.bak
		# の#を削除してからこのスクリプトを実行してください
		# rm -r ./.ssh.bak
        fi
fi

mkdir .ssh ; chmod 0700 .ssh ; cd ~/.ssh ; cat ~/$SSH_KEY_PUB > authorized_keys ; chmod 0600 authorized_keys ; rm ../$SSH_KEY_PUB

sudo reboot
# ----------- 処理ここまで -----------
タイトルとURLをコピーしました