2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

sshユーザー作成スクリプト

Posted at

はじめに

インフラ運用しているとちょこちょこ発生するユーザー作成をスクリプトにしてみました。

スクリプト概要

このスクリプトは、指定されたユーザー名のLinuxユーザーを作成し、そのユーザーのSSH公開鍵用のディレクトリとファイル(.ssh と authorized_keys)を準備する処理を行っています。

スクリプト

以下、スクリプトになります。

createUser.sh
#!/bin/bash

USER_NAME=$1

echo ${USER_NAME}

id ${USER_NAME}
if [ $? -eq 0 ];then
	echo "${USER_NAME} is already exist. Create is skipped."
else
	useradd ${USER_NAME}
	if [ $? -eq 0 ];then
		usermod -aG wheel ${USER_NAME}
	else
		echo "Error: Can't add user."
		exit 1
	fi
fi

if [ -d "/home/${USER_NAME}/.ssh" ];then
	echo ".ssh: This directory is already exist. - /home/${USER_NAME}/.ssh"
else
	echo ".ssh: Create directory."
	mkdir /home/${USER_NAME}/.ssh
	chmod 700 /home/${USER_NAME}/.ssh
fi

if [ -f "/home/${USER_NAME}/.ssh/authorized_keys" ];then
	echo "authorized_keys: This file is already exist. - /home/${USER_NAME}/.ssh/authorized_keys"
else
	echo "Create authorized_keys."
	touch /home/${USER_NAME}/.ssh/authorized_keys
	chmod 600 /home/${USER_NAME}/.ssh/authorized_keys
	chown -R ${USER_NAME} /home/${USER_NAME}/.ssh/
fi

ls -la /home/${USER_NAME}/.ssh/

exit 0

スクリプトの処理概要

スクリプトについてどのような処理を行なっているかを以下で説明します。

1. ユーザー名の取得と表示

bash
USER_NAME=$1
echo ${USER_NAME}
  • 引数で指定されたユーザー名を USER_NAME として取得
  • そのユーザー名を表示

2. ユーザーの存在確認

bash
id ${USER_NAME}
if [ $? -eq 0 ];then
    echo "${USER_NAME} is already exist. Create is skipped."
  • id コマンドでユーザーが既に存在するか確認
  • 存在していれば「作成をスキップ」と表示して以降の処理をスキップ

3. ユーザーの作成とwheelグループへの追加

bash
useradd ${USER_NAME}
if [ $? -eq 0 ];then
    usermod -aG wheel ${USER_NAME}
  • ユーザーが存在しない場合、useradd で新規作成
  • 作成成功時に、wheel グループ(sudo 権限を与える)に追加
  • 作成失敗時はエラーを出力して終了

4. .sshディレクトリの確認と作成

bash
if [ -d "/home/${USER_NAME}/.ssh" ];then
    echo ".ssh: This directory is already exist."
else
    mkdir /home/${USER_NAME}/.ssh
    chmod 700 /home/${USER_NAME}/.ssh
  • /home/ユーザー名/.ssh が存在するか確認
  • 存在しない場合、作成してパーミッションを 700 に設定(所有者のみアクセス可能)

5. authorized_keysファイルの確認と作成

bash
if [ -f "/home/${USER_NAME}/.ssh/authorized_keys" ];then
    echo "authorized_keys: This file is already exist."
else
    touch /home/${USER_NAME}/.ssh/authorized_keys
    chmod 600 /home/${USER_NAME}/.ssh/authorized_keys
    chown -R ${USER_NAME} /home/${USER_NAME}/.ssh/
  • authorized_keys ファイルが存在するか確認
  • 存在しない場合、作成し、パーミッションを 600 に設定(所有者のみ読み書き可能)
  • ディレクトリごとユーザーに所有権を設定

6. ディレクトリ内容の表示と終了

ls -la /home/${USER_NAME}/.ssh/
exit 0
  • .ssh ディレクトリの中身を表示
  • 正常終了

authorized_keys の追加

スクリプトではからのファイルを作成しています。
キー情報は利用者個々に用意していただく必要があるので共有してもらった共有キーを上書き保存して完了です。

おわりに

ちょっとしたことはスクリプト化などで自動化しておくことで作業内容の統一化もできて便利です。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?