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 キー・ペアをtestuser
のauthorized_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
参考サイト