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.

linuxユーザ追加について検証してみた

Last updated at Posted at 2022-09-11

はじめに

LPICレベル1の勉強のため、linuxユーザ追加を検証してみました。
ついでに削除したユーザーの復元も試しました。

前提条件

VirtualboxでゲストOSを1台作成した

linuxユーザー追加の手順

現在の登録されているユーザーを確認した

# cat /etc/passwd
出力結果
#デフォルトに入っているユーザー
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin

グループを作成した

グループ名(testgroup) グループID(1000)を作成した

$ sudo  groupadd -g 1000 testgrp

グループ名(testgroup) グループID(1000)を確認した

$ sudo cat /etc/group | grep testgrp
testgroup:x:1000:

ユーザー作成した

ユーザー名はtest3で登録した
rootユーザーは必ず0であり、一般ユーザーは1000以降の番号が割り当てられるそうです。

新規ユーザーのuid(ユーザID)は1002で-g(グループID)1000を指定する。

$ sudo useradd -u 1002 -g 1000 -d /home/test3 test3

test3ユーザーを登録されていないことを確認した

$ sudo cat /etc/passwd | grep test3

補足情報 grep -eでユーザーを複数指定して表示も可能

$ sudo cat /etc/passwd | grep -e test -e test2 -e test3
test:x:1000:1000:test:/home/test:/bin/bash
test2:x:1001:1001::/home/test2:/bin/bash
test3:x:1002:1001::/home/test3:/bin/bash

パスワード設定した

事前のパスワード情報を確認した

vipw -sのコマンドでも、/etc/shadowファイルを編集モードで開きます。
パスワード設定前に、/etc/shadowを確認した

※パスワードが未設のため、パスワード情報が!!になっていることを確認した

$ sudo cat /etc/shadow  | grep test3
test3:!!:19821:0:99999:7:::

パスワードを設定した

$ sudo passwd test3
ユーザー test3 のパスワードを変更。
新しいパスワード:
新しいパスワードを再入力してください:
passwd: すべての認証トークンが正しく更新できました。

事後のパスワード情報を確認した

$ sudo cat /etc/shadow | grep test3
test3:$6$ezIhEb0s$T1l0C3cXdeUt3S8VObRtBxEYu.YpX3t/OPo4SN62xxXZS/z5c38yBl6qJv6FnoXKwcY167yTO1HYpWHuUjS9C0:19821:0:99999:7:::

ssh鍵を生成した

test3ユーザーにスイッチした

$ su - test3
Password:

ssh鍵のパスフレーズ指定で鍵を生成した。

ホームディレクトリに移動する

$ cd ~

鍵を生成する <パスフレーズ情報>は秘密鍵のパスワードです。任意な値をいれてください。

$ ssh-keygen -N <パスフレーズ情報>
Generating public/private rsa key pair.
Enter file in which to save the key (/home/test3/.ssh/id_rsa):
Created directory '/home/test3/.ssh'.
Your identification has been saved in /home/test3/.ssh/id_rsa.
Your public key has been saved in /home/test3/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:bKFZpqDU9PjaxN8pxoKeNLoFcftygBnIaNgxSEug3Q0 test3@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
|++o E            |
|B+.* =           |
|=+* * o +        |
|.. B = B .       |
|  = o B S        |
|   . B + . .     |
|    B = = o      |
|   = = o .       |
|  o.o            |
+----[SHA256]-----+

鍵が作成されたことを確認した

$ ls -tlra 

[test3@localhost ~]$ ls -ltra
合計 12
-rw-r--r--  1 test3 testgrp 231  4月  1  2020 .bashrc
-rw-r--r--  1 test3 testgrp 193  4月  1  2020 .bash_profile
-rw-r--r--  1 test3 testgrp  18  4月  1  2020 .bash_logout
drwxr-xr-x. 3 root  root     19  4月  8 23:57 ..
drwx------  2 test3 testgrp  38  4月  9 00:02 .ssh
drwx------  3 test3 testgrp  74  4月  9 00:02 .

鍵があることを確認する

cd .ssh

$ ls -tlr
合計 8
-rw-r--r-- 1 test3 testgrp  409  4月  9 00:02 id_rsa.pub ※公開鍵
-rw------- 1 test3 testgrp 1766  4月  9 00:02 id_rsa ※秘密鍵

公開鍵を登録します。

$ pwd 
/home/test3/.ssh
$ cat id_rsa.pub > authorized_keys

権限を変更した

[test3@localhost .ssh]$ chmod 600 authorized_keys
[test3@localhost .ssh]$ ls -tlr
合計 12
-rw-r--r--. 1 test3 testgroup  409  9月 11 09:10 id_rsa.pub
-rw-------. 1 test3 testgroup 1766  9月 11 09:10 id_rsa
-rw-------. 1 test3 testgroup  409  9月 11 09:54 authorized_keys

sudoの設定をした

test3ユーザーではsudo出来ないことを確認した

$ sudo su -
[sudo] test3 のパスワード:
test3 は sudoers ファイル内にありません。この事象は記録・報告されます。
[test3@localhost .ssh]$

rootユーザーでSSH接続した

$ test@DESKTOP-0S2U0NI:~$ ssh root@192.168.0.4
root@192.168.0.4's password:
Last login: Sun Sep 11 08:02:03 2022 from 192.168.0.14

sudoの設定ファイルを変更した

wheelグループに対して、すべてのroot権限が必要なコマンドをパスワード無しで実行できる設定のコメントアウトを外しました。

# vi /etc/sudoers

出力結果
## Allow root to run any commands anywhere
root    ALL=(ALL)       ALL

## Allows members of the 'sys' group to run networking, software,
## service management apps and more.
# %sys ALL = NETWORKING, SOFTWARE, SERVICES, STORAGE, DELEGATING, PROCESSES, LOCATE, DRIVERS

## Allows people in group wheel to run all commands
%wheel  ALL=(ALL)       ALL

## Same thing without a password
※以下は、wheelグループに対して、すべてのroot権限が必要なコマンドをパスワード無しで実行できる設定です。
以下の設定は、コメントアウトされていたため、先頭の#は削除しました。
%wheel  ALL=(ALL)       NOPASSWD: ALL

## Allows members of the users group to mount and unmount the
## cdrom as root
# %users  ALL=/sbin/mount /mnt/cdrom, /sbin/umount /mnt/cdrom

## Allows members of the users group to shutdown this system
# %users  localhost=/sbin/shutdown -h now

## Read drop-in files from /etc/sudoers.d (the # here does not mean a comment)
#includedir /etc/sudoers.d

idコマンドで現在の所属しているグループを確認した

現在はwheelグループに所属していないことを確認した

# id test3
uid=1002(test3) gid=1000(testgrp) groups=1000(testgrp)

wheelグループに所属する

以下のサイトを参考にしました。
https://atmarkit.itmedia.co.jp/ait/articles/1706/02/news014_2.html

# sudo gpasswd -a test3 wheel
Adding user test3 to group wheel

wheelグループに所属していることを確認した

# id test3
uid=1002(test3) gid=1000(testgrp) groups=1000(testgrp),10(wheel)

test3ユーザーにスイッチする

# su - test3

test3ユーザーではsudo出来ました。

※現在のユーザ名を確認
$ whoami
test3
$ sudo su -

sshの秘密鍵をPCに格納した

WinSCPを使用して秘密鍵をPCのドキュメント等に保存する

コマンドプロンプトを開く

※C:\Users\ユーザー名\OneDrive\ドキュメントは私の環境です。環境は個人の環境のため、
PCの任意の場所に秘密鍵を格納してください

test3ユーザーでssh接続した

C:\Users\ユーザー名\OneDrive\ドキュメント>ssh -i id_rsa test3@サーバーのIPアドレス
The authenticity of host '192.168.0.32 (192.168.0.32)' can't be established.
ED25519 key fingerprint is SHA256:DN0GsB0EvI0hPMIDSrGCaZiu3Jlb0al5bcnNLniwvXg.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.0.32' (ED25519) to the list of known hosts.
Enter passphrase for key 'id_rsa':<パスフレーズを入れる>
Last login: 
[test3@localhost ~]$
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?