www.shoeisha.co.jp
ユーザーとグループの管理
ユーザーアカウントと/etc/passwd
- ユーザーアカウント情報は
/etc/passwd
ファイルにプレーンテキストで保存されている
/etc/passwd-
とかがあったりする。バックアップ?
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
...
wand:x:1000:1000:wand,,,:/home/wand:/bin/bash
...
<ユーザ名>:<暗号化されたパスワード>:<UID>:<GID>:<GECOS>:<ホームディレクトリ>:<デフォルトシェル>
- ユーザ名
- 暗号化されたパスワード
- 現在ではシャドウパスワードが使用されているため'x'
- UID
- User ID
- 0: root
- 1-99: システム管理用
- 1000または500-: 一般ユーザ用
- GID
- Group ID
- プライマリグループ(基本グループ)のID
- ファイルやディレクトリを作成したとき、所持グループとしてデフォルトで適用されるもの
- GECOS
- コメント
- ユーザのフルネーム
- 部屋番号やビル名
- オフィスの電話番号
- その他コンタクト先(FAX番号とか)
- GEneral Comprehensive Operating System からきている
- ホームディレクトリ
- デフォルトシェル
|
/bin/false |
/sbin/noloin |
Telnet接続 |
x |
x |
SSH接続 |
x |
x |
FTP接続 |
x |
o |
SFTP接続 |
x |
x |
シェルの実行 |
x |
x |
/etc/passwd
はroot:root所有だが一般ユーザでも読み取り可能
ls -l /etc/passwd
-rw-r--r-- 1 root root 2372 2月 24 20:04 /etc/passwd
- cf.
/etc/shadow
はroot:shadow所有で、一般ユーザ読み取り不可能
ls -l /etc/shadow
-rw-r----- 1 root shadow 1308 2月 24 20:04 /etc/shadow
- シャドウパスワード
- 誰でも読み取り可能な
/etc/passwd
にパスワード情報を格納することはセキュリティ上危険
- root(とshadowグループ所属者)しか読み取りできない
/etc/shadow
にパスワード情報を格納するようになった
グループアカウントと/etc/group
- グループの設定は
/etc/group
に保存されている
root:x:0:
daemon:x:1:
bin:x:2:
...
wand:x:1000:
sambashare:x:128:
docker:x:999:wand
<グループ名>:<暗号化されたグループパスワード>:<GID>:<グループメンバー>
- グループ名
- 暗号化されたグループパスワード
- GID
- グループメンバー
- このグループをサブグループ(参加グループ)とするユーザー
- ユーザ名を
,
区切り
コマンドを用いたユーザーとグループの管理
useradd
コマンド
-c, --comment COMMENT 新アカウントの GECOS フィールド
-d パス
, --home-dir=パス
-d, --home-dir HOME_DIR 新アカウントのホームディレクトリ
-g グループ名/GID
, --gid=グループ名/GID
-g, --gid GROUP 新アカウントの主グループの名前または ID
- プライマリグループを指定
--gid
でもグループ名指定できる
passwd
にはGIDが記入される
-G グループ名/GID
, --groups=グループ名/GID
-G, --groups GROUPS 新アカウントの補助グループのリスト
- サブグループを指定する
- 複数指定するときは
,
区切り
sudo useradd --groups=wand,docker hoge
cat /etc/passwd
cat /etc/group
...
+ hoge:x:1001:1001::/home/hoge:/bin/sh
- wand:x:1000:
+ wand:x:1000:hoge
sambashare:x:128:
- docker:x:999:wand
+ docker:x:999:wand,hoge
+ hoge:x:1001:
-s パス
, --shell パス
-s, --shell SHELL 新アカウントのログインシェル
- デフォルトシェルを指定する
- システムアカウントや、ログインを想定しないアカウントでは
/bin/false
や/sbin/nologin
-D
, --defaults
-D, --defaults useradd のデフォルト設定を表示または変更
useradd -D
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/sh
SKEL=/etc/skel
CREATE_MAIL_SPOOL=no
-m
, --create-home
-m, --create-home ユーザのホームディレクトリを作成する
ホームディレクトリのデフォルトシェル
- ホームディレクトリ生成時、
.bashrc
とかも自動配置したい
/etc/skel/
以下に雛形を配置しておく
ls -lA /etc/skel
-rw-r--r-- 1 root root 220 5月 18 2017 .bash_logout
-rw-r--r-- 1 root root 3771 5月 18 2017 .bashrc
-rw-r--r-- 1 root root 807 4月 5 2018 .profile
-rw-r--r-- 1 root root 8980 4月 20 2016 examples.desktop
usermod
コマンド
- 既存のユーザアカウントを変更
- だいたい
useradd
とおなじ
【備忘録】プライマリグループを変更したら、過去に作成したファイルの所有グループも変わった話
- 昔、プライマリグループを
docker
グループにしてしまったwand
ユーザについて下記コマンドを実行
sudo usermod -g wand -G docker wand
- やや時間がかかった
- 昔
wand:docker
で作成したファイルが全部wand:wand
になっていた
-rw-r--r-- 1 wand wand 45180 2月 25 23:35 ch1.md
-rw-r--r-- 1 wand wand 9999 3月 31 20:56 ch11.md
...
-L
, --lock
-L, --lock このユーザアカウントをロックする
- パスワードをロックして一時的に無効化する
- 【補】
/etc/shadow
にて、暗号化パスワードの先頭に!
がつくよう
- 長期休暇中などに使う
-U
, --unlock
-U, --unlock このユーザアカウントのロックを解除する
passwd: パスワードをアンロックするとパスワードのないアカウントができます。
このアカウントのパスワードをアンロックするには usermod -p でパスワードを
設定すべきです。
userdel
コマンド
- ユーザアカウント消す
- ホームディレクトリ等も消すなら
-r
, --remove
passwd
コマンド
- パスワード変更
- パスワードは
/etc/shadow
に格納されている
/etc/shadow
は640, rootしか書き換えできない
- しかし
passwd
コマンドはroot所有でSUIDが付与されている
ls -l $(which passwd)
-rwsr-xr-x 1 root root 59640 1月 26 2018 /usr/bin/passwd
- rootの権限で実行されるため、一般ユーザも自分のパスワードを変更できる
- cf.
usermod
の-p
オプションによるパスワード変更はrootユーザしか行使できない
ls -l $(which usermod)
-rwxr-xr-x 1 root root 121920 1月 26 2018 /usr/sbin/usermod
-l
, --lock
-l, --lock 指定アカウントをロックする
-u
, --unlock
-u, --unlock 指定アカウントのロックを解除する
groupadd
コマンド
groupmod
コマンド
-g GID
, --gid=GID
-g, --gid GID グループの ID を GID に変更します
-n グループ名
, --new-name=グループ名
-n, --new-name NEW_GROUP 名前を NEW_GROUP に変更する
groupdel
コマンド
- グループ消す
- そのグループをプライマリグループとするユーザが居る場合、削除不可
能
groupdel wand
groupdel: ユーザ 'wand' のプライマリグループは削除できません。
id
コマンド
id wand
uid=1000(wand) gid=1000(wand) groups=1000(wand),999(docker)
getent
getent --help
使用法: getent [OPTION...] database [key ...]
管理データベースからエントリを取得します。
シャドウパスワード
- 現在、
/etc/passwd
の暗号化パスワードカラムにはx
が入る
- root:root 644
- プロンプトの表示、
ls -l
コマンド等のために640にはできない
- 代わりに
/etc/shadow
の第2列に保存される
- root:shadow 640
- 【補】ロック中は暗号化パスワード先頭に
!
がつく
ジョブスケジューリング
- メンテ作業
- 自動化してシステム管理コスト下げる
cron
: 定期実行
at
: 一回限り
cron
- 構成
crond
は1分ごとにcrontab
ファイルを調べる
ユーザーのcrontab
/var/spool/cron/
ディレクトリ以下に置かれる
- エディタで直接編集してはいけない
crontab
コマンドで編集する
- SGIDが設定されており、crontabグループ所属ユーザとして実行される
ls -l $(which crontab)
-rwxr-sr-x 1 root crontab 39352 11月 16 2017 /usr/bin/crontab
/var/spool/cron/crontabs/
はスティッキービットが立っている
- 誰でもファイル・ディレクトリを書き込めるが、所有者しか削除できないやつ
sudo ls -ld /var/spool/cron/crontabs/
drwx-wx--T 2 root crontab 4096 4月 12 19:37 /var/spool/cron/crontabs/
- この下に各ユーザのファイルを配置していく感じになる
-e
-e (edit user's crontab)
export EDITOR=emacsclient
crontab -e
- 環境変数
EDITOR
で指定のエディタでcrontabファイルを編集する
/tmp/crontab.Ndp3fo/crontab
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
- # 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
+ 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
no crontab for wand - using an empty one
Waiting for Emacs...
crontab: installing new crontab
- crontabs以下にwandファイルができている
sudo ls -la /var/spool/cron/crontabs/
drwx-wx--T 2 root crontab 4096 4月 12 19:46 .
drwxr-xr-x 3 root root 4096 1月 8 2018 ..
-rw------- 1 wand crontab 1087 4月 12 19:46 wand
-l
-l (list user's crontab)
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
-r
-r (delete user's crontab)
-i
-i (prompt before deleting user's crontab)
crontab -ri
crontab: really delete wand's crontab? (y/n)
sudo ls -la /var/spool/cron/crontabs/
drwx-wx--T 2 root crontab 4096 4月 12 19:54 .
drwxr-xr-x 3 root root 4096 1月 8 2018 ..
-u
crontab [ -u user ] [ -i ] { -e | -l | -r }
書式
# m h dom mon dow command
- min
- hour
- day of month
- month
- day of week
- command
例
15 23 * * * /usr/local/bin/backup
毎日23:15に実行
0 9,12 * * 1 /usr/local/bin/syscheck
毎週月曜の9時と12時
0 */2 * * * /usr/local/bin/syscheck
システムのcrontab
ls -l /etc/crontab
-rw-r--r-- 1 root root 722 8月 21 2017 /etc/crontab
/etc/crontab
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow user command
17 * * * * root cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#
user
カラムがある
/etc/cron.*/
ディレクトリ以下のファイルを呼び出す
ls /etc/cron.*
/etc/cron.d:
anacron php popularity-contest sysstat
/etc/cron.daily:
0anacron cracklib-runtime man-db sysstat
apport dpkg mlocate ubuntu-advantage-tools
apt-compat google-chrome passwd update-notifier-common
bsdmainutils logrotate popularity-contest
/etc/cron.hourly:
/etc/cron.monthly:
0anacron
/etc/cron.weekly:
0anacron man-db update-notifier-common
- daily,hourly,monthly,weekly
【参考】run-parts
【補】anacron
- スケジュールされた時間にシステムがOFFの場合、システムがONになった時点でタスクを実行してくれるらしい
- 1日1回以上の頻度
- 管理者のみ設定可能
atコマンド
オプション |
説明 |
at -d |
atrm のエイリアス |
at -r |
atrm のエイリアス |
at -l |
atq のエイリアス |
at -f ファイルパス |
STDINではなくファイルからジョブ指定 |
コマンド |
説明 |
atrm |
予約中のジョブをジョブ番号で指定して削除 |
atq |
予約中のジョブを表示 |
$ at 5:00 tomorrow
warning: commands will be executed using /bin/sh
at> /usr/local/sbin/backup
at> <EOT>
job 1 at Sat Apr 13 05:00:00 2019
atq
1 Sat Apr 13 05:00:00 2019 a wand
atrm 1
atq
- RDMSの自動増加キーよろしく、ジョブ番号は戻らないよう
$ at noon tomorrow
warning: commands will be executed using /bin/sh
at> /bin/sh
at> <EOT>
job 2 at Sat Apr 13 12:00:00 2019
at -d 2
日時指定書式の例
指定日時 |
書式 |
午後10時 |
22:00, 10pm |
正午(12:00) |
noon |
真夜中(0:00) |
midnight |
今日 |
today |
明日 |
tomorrow |
3日後 |
now + 3days |
2週間後の22:00 |
10pm + 2weeks |
at midnight today
at: refusing to create job destined in the past
cronとatのアクセス制御
共通
- ホワイトリスト優先
- allowファイルがあれば、そこに記述されたユーザのみが利用可能
- 次いでブラックリスト
- allowファイルがなく、denyファイルがあれば、そこに記述されていない全ユーザが利用可能
cronのアクセス制御
/etc/cron.allow
, /etc/cron.deny
- 両方無い場合、誰でも利用できる
atのアクセス制御
/etc/at.allow
, /etc/at.deny
- 両方無い場合、rootのみが利用できる
- デフォルトでは、空の
/etc/at.deny
が置かれ、全ユーザがat
コマンドを利用できるようになってるらしい
ls -l /etc/at.deny
-rw-r----- 1 root daemon 144 2月 20 2018 /etc/at.deny
sudo cat /etc/at.deny
alias
backup
bin
daemon
ftp
games
gnats
guest
irc
lp
mail
man
nobody
operator
proxy
qmaild
qmaill
qmailp
qmailq
qmailr
qmails
sync
sys
www-data
systemdによるスケジューリング
systemctl list-timers
NEXT LEFT LAST PASSED UNIT ACTIVATES
Fri 2019-04-12 21:09:00 JST 3min 11s left Fri 2019-04-12 20:39:41 JST 26min ago phpsessionclean.timer phpsessionclean.service
Fri 2019-04-12 22:01:18 JST 55min left Fri 2019-04-12 21:05:38 JST 10s ago anacron.timer anacron.service
Sat 2019-04-13 01:02:15 JST 3h 56min left Fri 2019-04-12 11:29:18 JST 9h ago apt-daily.timer apt-daily.service
Sat 2019-04-13 05:51:58 JST 8h left Fri 2019-04-12 17:10:04 JST 3h 55min ago motd-news.timer motd-news.service
Sat 2019-04-13 06:33:38 JST 9h left Fri 2019-04-12 06:50:54 JST 14h ago apt-daily-upgrade.timer apt-daily-upgrade.service
Sat 2019-04-13 17:23:02 JST 20h left Fri 2019-04-12 17:08:28 JST 3h 57min ago systemd-tmpfiles-clean.timer systemd-tmpfiles-clean.service
Mon 2019-04-15 00:00:00 JST 2 days left Mon 2019-04-08 00:57:00 JST 4 days ago fstrim.timer fstrim.service
n/a n/a Fri 2019-04-12 16:54:32 JST 4h 11min ago ureadahead-stop.timer ureadahead-stop.service
8 timers listed.
Pass --all to see loaded but inactive timers, too.
systemctl stop lpic.timer
- 書式は
man 7 systemd.time
で確認可能
systemd-run
コマンド
- サービスの実行ログは
journalctl
コマンドで確認する
journalctl -u apt-daily.service
...
4月 12 00:35:19 wand-ThinkPad-X240s systemd[1]: Starting Daily apt download activities...
4月 12 00:35:33 wand-ThinkPad-X240s systemd[1]: Started Daily apt download activities.
4月 12 11:29:18 wand-ThinkPad-X240s systemd[1]: Starting Daily apt download activities...
4月 12 11:29:19 wand-ThinkPad-X240s systemd[1]: Started Daily apt download activities.
ローカライゼーションと国際化
- ローカライゼーション(localization)
- ソフトウェアのメニューやメッセージを利用者の地域や国に合わせること
- 国際化(internationalization, i18n)
- ソフトウェアを最初から多言語・他地域対応するように作ること
カテゴリ
環境変数 |
説明 |
LC_CTYPE |
文字の種類やその比較・分類の規定 |
LC_COLLATE |
文字の照合や整列に関する規定 |
LC_MESSAGES |
メッセージ表示に使用する言語 |
LC_MONETARY |
通貨に関する規定 |
LC_NUMERIC |
数値の書式に関する規定 |
LC_TIME |
日付や時刻の書式に関する規定 |
LANGUAGE |
複数一括設定(: 区切り)、最高優先度 |
LC_ALL |
一括設定、高優先度 |
LANG |
一括設定、低優先度 |
locale
のマニュアルで調べられる
- 優先度
- GNU gettext
LANGUAGE
最優先
- ただし
LC_ALL
やLANG
も必要(他ライブラリで使用するため)
- スイス人なら
export LANGUAGE=sv:de
export LANG=sv_SE
LC_ALL
が設定されていれば、全カテゴリでその値が使用される
LANG
が設定されていれば、全カテゴリでその値が使用されるが、個別のカテゴリの設定で上書きできる
- 書式
<言語名>_<国家もしくは地域名>.<文字コード>
- 主なもの
locale
コマンド
locale
LANG=ja_JP.UTF-8
LANGUAGE=
LC_CTYPE="ja_JP.UTF-8"
LC_NUMERIC="ja_JP.UTF-8"
LC_TIME="ja_JP.UTF-8"
LC_COLLATE="ja_JP.UTF-8"
LC_MONETARY="ja_JP.UTF-8"
LC_MESSAGES="ja_JP.UTF-8"
LC_PAPER="ja_JP.UTF-8"
LC_NAME="ja_JP.UTF-8"
LC_ADDRESS="ja_JP.UTF-8"
LC_TELEPHONE="ja_JP.UTF-8"
LC_MEASUREMENT="ja_JP.UTF-8"
LC_IDENTIFICATION="ja_JP.UTF-8"
LC_ALL=
-a
, --all-locales
locale -a
C
C.UTF-8
POSIX
en_AG
en_AG.utf8
en_AU.utf8
en_BW.utf8
en_CA.utf8
en_DK.utf8
en_GB.utf8
en_HK.utf8
en_IE.utf8
en_IL
en_IL.utf8
en_IN
en_IN.utf8
en_NG
en_NG.utf8
en_NZ.utf8
en_PH.utf8
en_SG.utf8
en_US.utf8
en_ZA.utf8
en_ZM
en_ZM.utf8
en_ZW.utf8
ja_JP.utf8
-m
, --charmaps
-m, --charmaps
利用可能なキャラクタマップ名を書き込む
locale -m | head -5
ANSI_X3.110-1983
ANSI_X3.4-1968
ARMSCII-8
ASMO_449
BIG5
一時的に変更
LANG=C man ls
- 多くのディストリの標準はUTF-8
iconv
- つかえるやつ一覧
iconv -l
The following list contains all the coded character sets known. This does
not necessarily mean that all combinations of these names can be used for
the FROM and TO command line parameters. One coded character set can be
listed with several different names (aliases).
437, 500, 500V1, 850, 851, 852, 855, 856, 857, 858, 860, 861, 862, 863, 864,
865, 866, 866NAV, 869, 874, 904, 1026, 1046, 1047, 8859_1, 8859_2, 8859_3,
8859_4, 8859_5, 8859_6, 8859_7, 8859_8, 8859_9, 10646-1:1993,
...
-f, --from-code=NAME 元のテキストのエンコーディング
-t, --to-code=NAME 出力用のエンコーディング
iconv -f eucjp -t utf8 report.euc.txt > report.utf8.txt
- 標準出力に書き出されるのでリダイレクト
-o FILE
, --output=FILE
でファイルに書き出すこともできる
- ファイルの文字コードが不明な時は
nkf -g ファイル名
- 日本はUTCより9時間早いとかそういうやつ
/usr/share/zoneinfo/
ディレクトリ以下のバイナリファイルに格納されている
ls /usr/share/zoneinfo/
Africa Cuba GMT+0 Kwajalein Poland WET
America EET GMT-0 Libya Portugal Zulu
Antarctica EST GMT0 MET ROC iso3166.tab
Arctic EST5EDT Greenwich MST ROK leap-seconds.list
Asia Egypt HST MST7MDT Singapore localtime
Atlantic Eire Hongkong Mexico SystemV posix
Australia Etc Iceland NZ Turkey posixrules
Brazil Europe Indian NZ-CHAT UCT right
CET Factory Iran Navajo US zone.tab
CST6CDT GB Israel PRC UTC zone1970.tab
Canada GB-Eire Jamaica PST8PDT Universal
Chile GMT Japan Pacific W-SU
/etc/localtime
にコピーすることでタイムゾーン設定
ls -l /etc/localtime
lrwxrwxrwx 1 root root 30 2月 24 03:32 /etc/localtime -> /usr/share/zoneinfo/Asia/Tokyo
printenv TZ
export TZ='Asia/Tokyo'
- この値を全ユーザで利用するには
/etc/timezone
ファイルに記述する
ls -l /etc/timezone
-rw-r--r-- 1 root root 11 2月 24 03:32 /etc/timezone
/etc/timezone
Asia/Tokyo
- 上記の値は
tzselect
コマンドで生成できる
tzconfig
/etc/localtime
, /etc/timezone
まとめて変更
- 最近のディストリでは使用できないらしい
tzconfig
WARNING: the tzconfig command is deprecated, please use:
dpkg-reconfigure tzdata
- 代わりに
dpkg-reconfigure tzdata
を使用する(Debian系)