学習記録#30 250709

619ページから

634ページまで

useraddコマンド:
ユーザーアカウントを作成する
例)
コメント、ホームディレクトリ、デフォルトシェルを指定して新規ユーザーを作成
$ useradd -c "Linux User" -d /home/linux -s /bin/bash linuxuser

/etc/default/useraddを見ると、useraddのデフォルト値が見れる
# ls /etc/default/
dbus networkd-dispatcher useradd
# cat /etc/default/useradd
# Default values for useradd(8)
#
# The SHELL variable specifies the default login shell on your
# system.
# Similar to DSHELL in adduser. However, we use "sh" here because
# useradd is a low level utility and should be as general
# as possible
SHELL=/bin/sh
#
# The default group for users
# 100=users on Debian systems
# Same as USERS_GID in adduser
# This argument is used when the -n flag is specified.
# The default behavior (when -n and -g are not specified) is to create a
# primary user group with the same name as the user being added to the
# system.
# GROUP=100
#
# The default home directory. Same as DHOME for adduser
# HOME=/home
#
# The number of days after a password expires until the account
# is permanently disabled
# INACTIVE=-1
#
# The default expire date
# EXPIRE=
#
# The SKEL variable specifies the directory containing "skeletal" user
# files; in other words, files such as a sample .profile that will be
# copied to the new user's home directory when it is created.
# SKEL=/etc/skel
#
# Defines whether the mail spool should be created while
# creating the account
# CREATE_MAIL_SPOOL=yes

ホームディレクトリ作成時にファイルを配布したい場合は、事前に雛形として/etc/skelディレクトリに置いておく。
/etc/skelディレクトリにあるファイルは、新しいホームディレクトリ内にコピーされる

usermodコマンド:
既存のユーザーアカウントを変更する。
オプションはuseraddと共通しているものが多い
-gオプションではプライマリグループを変更し、-Gオプションではプライマリグループはそのままで所属するグループを変更する
例)
linuxuserさんがプライマリグループ以外に所属するグループをbprojectおよびstaffに変更している
$ usermod -G bproject:staff linuxuser
-Lオプションで、アカウントを一時的に無効化できる
例)
theodoreさんをロック
$ usermod -L theodore

userdelコマンド:
ユーザーアカウントを削除する
アカウント削除だけだとホームディレクトリが残るので、ホームディレクトリも消したいなら-rオプションを使う
例)
theodoreさんのアカウントとホームディレクトリ削除
$ userdel -r theodore

passwdコマンド:
パスワードを変更する
rootユーザー以外は自分のパスワードだけ変更できる
passwd
で、対話形式でパスワード変更できる
-lオプションでアカウントのロックもできる

groupaddコマンド:
グループを作成する
例)
$ groupadd staff

groupmodコマンド:
グループ情報を変更する
GIDとかグループ名を変更できる
例)
$ groupmod -g staff 1000
$ groupmod -n staff actor

groupdelコマンド:
グループを削除する
ただし、削除対象グループをプライマリグループとするユーザーが存在する場合は削除できない。プライマリじゃなかったら削除できる
例)
$ groupdel staff

idコマンド:
ユーザーIDや、ユーザーが所属しているグループのIDを調べる
例)
$ id root
uid=0(root) gid=0(root) groups=0(root)

getentコマンドで、ローカルホストやLDAPサーバー内にあるユーザー・グループ情報などを一括取得できる

Linuxでは、定期的に実行するジョブについてはcronを、1回限りのジョブの予約にはatコマンドを使ってスケジューリングする

cron:
定期的にジョブを実行するcronは、以下の2つから構成される
crond→スケジュールを管理するデーモン
crontabコマンド→スケジューリングを編集するコマンド
crondデーモンがcrontabファイルを調べて、実行すべきスケジュールが存在すればそのジョブを実行する

ユーザーのcrontabファイルは、/var/spool/cronディレクトリ以下に置かれる
例)
linuxuserさんのcrontabファイル
/var/spool/cron/linuxuser
エディタで開いて直接編集したりはできないので、crontabコマンドを使用する必要がある

編集エディタを開くには、crontab -e
を実行する

crontabファイルの書式
分 時 日 月 曜日 コマンド
15 23 * * * /usr/local/bin/backup.sh ←*にすると、すべての値にマッチする
0 */2 * * * /usr/local/bin/syscheck.sh ←*/[数字]で〜ごとが表現できる。これは2時間ごと

ユーザーとは別で、システムのcrontabファイルもある(/etc/crontab)
/etc/crontabファイルは、/etc/cron.*ディレクトリに置かれたファイルを呼び出すようになっている
また、実行ユーザー名を指定するフィールドも加わる