技術メモのかけら

内容はもとより調べたことすら忘れてしまうので個人的なメモです。とにかく短く、結論だけ書いていきます。

ファイルを掴んでいるプロセスを調べる

lsof <ファイル名>でファイルを使用しているプロセルを特定できる。

$ sudo lsof /var/log/messages
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF      NODE NAME
rsyslogd 1055 root    3w   REG  253,0     7164 100839039 /var/log/messages

実行結果の4番目のフィールドが [0-9]w なのは書き込みモード、[0-9]uが読み書きモードで開かれているファイル。

応用として、書き込みモードで開かれているファイルの一覧取得方法。

$ sudo lsof|awk '$4~/^[0-9][wu][a-zA-Z]*$/{print $O}'

wとuの後には以下のようなロックモードがアルファベト1文字で表示されるので正規表現にその考慮を入れています。

The mode character is followed by one of these lock characters, describing the type of lock applied to the file:

                       N for a Solaris NFS lock of unknown type;
                       r for read lock on part of the file;
                       R for a read lock on the entire file;
                       w for a write lock on part of the file;
                       W for a write lock on the entire file;
                       u for a read and write lock of any length;
                       U for a lock of unknown type;
                       x for an SCO OpenServer Xenix lock on part      of the file;
                       X for an SCO OpenServer Xenix lock on the      entire file;
                       space if there is no lock.

または、/proc/<PID>/fd/にプロセルが使用しているファイルへのシンボリックリンクが貼られているので、ls -lで調べる方法もある。

$ ls -l /proc/5271/fd
total 0
lrwx------. 1 vagrant vagrant 64 Jan  7 15:34 0 -> /dev/pts/0
lrwx------. 1 vagrant vagrant 64 Jan  7 15:34 1 -> /dev/pts/0
lrwx------. 1 vagrant vagrant 64 Jan  7 15:34 2 -> /dev/pts/0
lrwx------. 1 vagrant vagrant 64 Jan  7 15:34 4 -> /home/vagrant/.hoge.txt.swp

CentOS7で試したけど他のOSだと少し違うかも。

awkのNF変数

awkの組み込みのNF変数には現在行のフィールド数が入っています。

$ cat /etc/redhat-release
CentOS Linux release 7.3.1611 (Core)

$ awk '{print NF}' /etc/redhat-release
5

これを利用して、後方からフィールドを参照できます

$ awk '{print $(NF),$(NF-1)}' /etc/redhat-release
(Core) 7.3.1611

また値をセットすることで現在行のフィールド数を操作できるので、先頭の3フィールド表示するようなことも可能です。

$ awk NF=3 /etc/redhat-release
CentOS Linux release

groupaddとuseraddのrオプションでシステムアカウントを作成する

groupaddとuseraddにrオプションを指定するとsystemアカウントになる

-r, --system create a system account

# groupadd -r r_group
# groupadd -r r_group2
# grep r_group /etc/group
r_group:x:998:
r_group2:x:997:

IDが振られる範囲は/etc/login.defsのSYS_GID_MINSYS_GID_MAXで定義されているが、IDは降順に振られるようである

#
# Min/max values for automatic gid selection in groupadd
#
GID_MIN                  1000
GID_MAX                 60000
# System accounts
SYS_GID_MIN               201
SYS_GID_MAX               999

普通のグループはGID_MINGID_MAXで定義されている通り、1000以降が振られる

# groupadd normal_group
# grep group /etc/group
normal_group:x:1000:

ユーザも同じ感じ

# useradd -r -g r_group r_user
# useradd -g normal_group normal_user
# grep user /etc/passwd
r_user:x:999:998::/home/r_user:/bin/bash
normal_user:x:1000:1000::/home/normal_user:/bin/bash

IDが振られる範囲は同じく/etc/login.defsで定義されている。

#
# Min/max values for automatic uid selection in useradd
#
UID_MIN                  1000
UID_MAX                 60000
# System accounts
SYS_UID_MIN               201
SYS_UID_MAX               999

普通のユーザとの違いは、/etc/shadowを見るとパスワードの有効期限がブランクになっている

r_user:!!:17528::::::
normal_user:!!:17528:0:99999:7:::

また、/etc/login.defsのCREATE_HOMEの設定を無視してホームディレクトリが作成されない

$ ls /home
normal_user

ただ、ログインできてしまう。
システムユーザとして使用するには、ログインシェルは使えないようにしたい。
ログインさせないために/sbin/nologinは知ってたけど、メッセージを返さない/bin/falseというのもあるらしい

# /sbin/nologin
This account is currently not available.
# echo $?
1
# /bin/false
# echo $?
1

本番環境ではメッセージを返さない/bin/falseデバッグ環境は /sbin/nologinとか使い分けると良さそう。

#  useradd -r -g r_group -s /bin/false r_user

途中まで打ったコマンドを保持しつつ別のコマンドを実行する方法

途中まで打ったけど、別のコマンドを打ちたくなった、でも打ちかけたのを消すのは惜しい。
そんな場合は Alt + q

$ docker exec -it  (あれ、コンテナIDなんだっけ?)

Alt + qを押すと一旦プロンプトがクリアされる。

$

別のコマンドを実行して、めでたくコンテナIDが分かった。打ちかけのコマンドが復元されるので続きをどうぞ。

$ docker ps 
CONTAINER ID        IMAGE                                 COMMAND                  CREATED             STATUS                   PORTS               NAMES
bf0c389358f9        centos:7.4.1708                       "/bin/bash"              9 hours ago         Exited (0) 9 hours ago                       34_redmine_1

$ docker exec -it

SQL中のVALUES

SELECT句が使える場所ならどこでもVALUESが使えるとのこと。

-- select 1,'one' union select 2,'two' union select 3,'three'と等価
postgres=# VALUES (1, 'one'), (2, 'two'), (3, 'three');
 column1 | column2
---------+---------
       1 | one
       2 | two
       3 | three
(3 rows)

asで別名をつければテーブルを指定できるところに使える。列名はデフォルトでcolumn1, column2のようになる。

postgres=# select column1,column2 from (values ('あいうえお',1),('かきくけこ',2),('さしすせそ',3)) as t;
  column1   | column2
------------+---------
 あいうえお |       1
 かきくけこ |       2
 さしすせそ |       3
(3 rows)

列名をつけることも可能です。

select no,hiragana from (values ('あいうえお',1),('かきくけこ',2),('さしすせそ',3)) as t(hiragana,no);

Re:dashのユーザ削除

GUIで操作できないので、コマンドから行う必要がある。
自分の場合はDockerでインストールしたので、下記コマンドを実行して削除。
画面で操作できるようにならないかな。

docker exec -it {コンテナID} ./manage.py users delete {メアド}