1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

OCI における新規ユーザー作成と SSH 接続の手順

Posted at

Oracle Cloud Infrastructure(OCI)では、新しくサーバを作成すると、opc という名前の初期ユーザーが自動的に作成されます。
クライアントはこの opc ユーザーを使用して SSH 接続を行うことができますが、
今回は opc ユーザー以外の新たなユーザーを作成して、そのユーザーアカウントを使用して SSH 接続を行う方法についてご紹介します。

前提条件

サーバOS:Oracle Linux 9(ホスト名は「ol9」)
新規作成するユーザー名:testuser

本手順

対象サーバの OS に SSH 接続後、rootに切り替えます。

[opc@ol9 ~]$ sudo su -
最終ログイン: 2024/02/23 () 15:48:20 JST 日時 pts/0
[root@ol9 ~]# 

testuser用の SSH キー・ペア(ここではtest.key)を作成します。

[root@ol9 ~]# ssh-keygen -t rsa -b 2048 -f test.key
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in test.key
Your public key has been saved in test.key.pub
The key fingerprint is:
SHA256:AxtSZnbfHR+b9YLeXBFHkD+PtoKfscpbOrVxTnbIPgw root@ol9
The key's randomart image is:
+---[RSA 2048]----+
|      = .     oB=|
|     = . . . o.oO|
|    . o   . o o++|
|     . +   . o +.|
|      . S   ..ooo|
|         .  E O o|
|           oo% o |
|         .ooo+*  |
|          ==+. . |
+----[SHA256]-----+
[root@ol9 ~]#

ls -lコマンドで SSH キー・ペアが作成されていることを確認します。

[root@ol9 ~]# ll
合計 8
-rw------- 1 root root 1811  2月 28 10:18 test.key
-rw-r--r-- 1 root root  390  2月 28 10:18 test.key.pub
[root@ol9 ~]# 

SSH キー・ペアの生成後、testuserを作成します。

[root@ol9 ~]# useradd testuser
# 何も表示されません
[root@ol9 ~]# 

こちらもtestuserが作成されていることを確認します。

[root@ol9 ~]# cat /etc/passwd | tail -3
pcp:x:981:981:Performance Co-Pilot:/var/lib/pcp:/usr/sbin/nologin
opc:x:1000:1000:Oracle Public Cloud User:/home/opc:/bin/bash
testuser:x:1001:1001::/home/testuser:/bin/bash
[root@ol9 ~]# 

同様に/home配下にtestuser用のディレクトリが作成されていることも確認します。

[root@ol9 ~]# ls -l /home
合計 4
drwx------. 6 opc      opc      4096  2月 16 13:52 opc
drwx------  2 testuser testuser   62  2月 28 10:25 testuser
[root@ol9 ~]# 

testuserのホームディレクトリ配下に、新たに.sshディレクトリを作成します。

[root@ol9 ~]# mkdir -p /home/testuser/.ssh
# 何も表示されません
[root@ol9 ~]# 
[root@ol9 ~]# ls -la /home/testuser
合計 12
drwx------  3 testuser testuser  74  2月 28 10:38 .
drwxr-xr-x. 4 root     root      33  2月 28 10:25 ..
-rw-r--r--  1 testuser testuser  18  1月 24  2023 .bash_logout
-rw-r--r--  1 testuser testuser 141  1月 24  2023 .bash_profile
-rw-r--r--  1 testuser testuser 492  1月 24  2023 .bashrc
drwxr-xr-x  2 root     root       6  2月 28 10:38 .ssh
[root@ol9 ~]# 

.ssh ディレクトリの作成後、作成した SSH キー・ペアをtestuserauthorized_keysにコピーします。

[root@ol9 ~]# cat test.key.pub > /home/testuser/.ssh/authorized_keys
# 何も表示されません
[root@ol9 ~]# 
[root@ol9 ~]# ls -la /home/testuser/.ssh
合計 4
drwxr-xr-x 2 root     root      29  2月 28 10:43 .
drwx------ 3 testuser testuser  74  2月 28 10:38 ..
-rw-r--r-- 1 root     root     390  2月 28 10:43 authorized_keys
[root@ol9 ~]# 

.sshディレクトリの所有者およびグループをtestuserに変更します。

[root@ol9 ~]# chown -R testuser:testuser /home/testuser/.ssh
# 何も表示されません
[root@ol9 ~]# 
[root@ol9 ~]# ls -lRa /home/testuser
/home/testuser:
合計 12
drwx------  3 testuser testuser  74  2月 28 10:38 .
drwxr-xr-x. 4 root     root      33  2月 28 10:25 ..
-rw-r--r--  1 testuser testuser  18  1月 24  2023 .bash_logout
-rw-r--r--  1 testuser testuser 141  1月 24  2023 .bash_profile
-rw-r--r--  1 testuser testuser 492  1月 24  2023 .bashrc
drwxr-xr-x  2 testuser testuser  29  2月 28 10:43 .ssh

/home/testuser/.ssh:
合計 4
drwxr-xr-x 2 testuser testuser  29  2月 28 10:43 .
drwx------ 3 testuser testuser  74  2月 28 10:38 ..
-rw-r--r-- 1 testuser testuser 390  2月 28 10:43 authorized_keys
[root@ol9 ~]# 

権限変更後、scp等を使用してクライアント端末に SSH 秘密キーの配置を行います。
配置後、以下のコマンドで SSH 秘密キーの権限を読み取り専用にします。

chmod 400 test.key

ローカルターミナルから以下のコマンドでtestuserを使用してSSH 接続を行い、ログインができたら作業は完了となります。

ssh -i test.key testuser@xx.xx.xx.xx
Last login: Wed Feb 28 10:51:51 2024 from xx.xx.xx.xx
[testuser@ol9 ~]$ 

Base DB の場合の追加設定

補足で、Base DB に新規ユーザーを付与して SSH 接続を行う場合、/etc/ssh/sshd_configの編集を行う必要があります。

[root@ol9 ~]# vim /etc/ssh/sshd_config
# 最下部に以下を追記
AllowUsers testuser

編集後、sshdの再起動を行い、作業は完了となります。

[root@ol9 ~]# systemctl restart sshd
# 何も表示されません
[root@ol9 ~]#

新規作成したユーザーに root 権限を付与する場合

visudoコマンドを使用して/etc/sudoersの編集を行います。

[root@ol9 ~]# visudo
# #includedir /etc/sudoers.d の下に以下を追記
testuser ALL=(ALL) NOPASSWD: ALL

参考サイト

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?