ルギア君の戯言

雑多な記事。

su -c と sudo の細かい違い

あけましておめでとうございます。

気がついたのでメモ。

ただ、どういう役に立つかは知らん。

  • 注1: Linux です。OS X や他の UNIX は挙動が異なる場合あり。っていうかそもそも OS Xsu-c オプションを受け付けないし。
  • 注2: bashzsh など POSIX な shell を想定しています。

まず、とりあえず適当にファイルを開いておく。っていうかこの適当なFDに新しいファイル(や別の FD)をバインドすることができるのも今日知ったrz

$ temp=`mktemp`
$ exec 3<>$temp

自分のシェルが開いているファイルを確認。

$ ls -l /proc/$$/fd
total 0
lrwx------ 1 lfs lfs 64 Jan  1 19:42 0 -> /dev/pts/0
lrwx------ 1 lfs lfs 64 Jan  1 19:42 1 -> /dev/pts/0
lrwx------ 1 lfs lfs 64 Jan  1 19:42 2 -> /dev/pts/0
lrwx------ 1 lfs lfs 64 Jan  3 22:10 255 -> /dev/pts/0
lrwx------ 1 lfs lfs 64 Jan  1 19:42 3 -> /tmp/tmp.nKKIH7bnT0       ## 開いた

sudo で上と同じコマンドを起動してみる。

$ sudo sh -c 'ls -l "/proc/$$/fd"'
[sudo] password for lfs
total 0
lrwx------ 1 root root 64 Jan  3 22:26 0 -> /dev/pts/0
lrwx------ 1 root root 64 Jan  3 22:26 1 -> /dev/pts/0
lrwx------ 1 root root 64 Jan  3 22:26 2 -> /dev/pts/0
lr-x------ 1 root root 64 Jan  3 22:26 3 -> /proc/1821/fd         ## 開いたファイルはない。

今度は su で起動してみる。

$ su -c "sh -c 'ls -l "'/proc/$$/fd'"'"
Password: 
total 0
lrwx------ 1 root root 64 Jan  3 22:30 0 -> /dev/pts/0
lrwx------ 1 root root 64 Jan  3 22:30 1 -> /dev/pts/0
lrwx------ 1 root root 64 Jan  3 22:30 2 -> /dev/pts/0
lrwx------ 1 root root 64 Jan  3 22:30 3 -> /tmp/tmp.nKKIH7bnT0    ## 開いたまま
lr-x------ 1 root root 64 Jan  3 22:30 4 -> /proc/1828/fd

結論: su は開いたファイルを引き継ぐ。sudo は開いたファイルを引き継がない。

閉じる。

$ exec 3<&-
$ exec 3>&-
$ rm $temp

って、書いたけど、ちゃんとマニュアルに載ってました。このおバカちん。

$ man sudo

-C num, --close-from=num

Close all file descriptors greater than or equal to num before executing a command. Values less than three are not permitted. By default, sudo will close all open file descriptors other than standard input, standard output and standard error when executing a command. The security policy may restrict the user's ability to use this option. The sudoers policy only permits use of the -C option when the administrator has enabled the closefrom_override option.

まあ、確かに、この方が安全ではある。

NFSv4+Kerberos の設定にてこずった話

設定方法については、http://moimoitei.blogspot.jp/2007/11/nfsv4-kerberos.html あたりを参考にすればよろしいかと。詳細はそのうち書く…と思う。

  • サーバーは Ubuntu
  • クライアントは Mac
teostra:~ lugia% uname -a                                               [20:16]
Darwin teostra.local 15.0.0 Darwin Kernel Version 15.0.0: Sat Sep 19 15:53:46 PDT 2015; root:xnu-3247.10.11~1/RELEASE_X86_64 x86_64
teostra:~ lugia% kinit momonga                                          [20:15]
momonga@LOCAL's password: 
teostra:~ lugia% sudo mount -t nfs -o vers=4,sec=krb5 linaro-nano.local:/ tmp 
Password:
teostra:~ lugia% ls -la tmp                                             [20:16]
total 49
drwxr-xr-x   5 root   wheel  4096 10 13 21:08 .
drwxr-xr-x+ 62 lugia  staff  2108 11  4 20:05 ..
-rw-r--r--   1 root   wheel     0  9  5 13:52 .nilfs
drwxr-xr-x   3 root   wheel  4096  9 17 20:29 home
dr-xr-xr-x   1 root   wheel   148 11  2 20:32 root
drwxrwxrwx   5 root   wheel  4096 11  3 20:44 share
teostra:~ lugia% 

誰でもいいので、チケットを入手しておかないとマウントできない。

teostra:~ lugia% klist                                                  [21:36]
klist: krb5_cc_get_principal: No credentials cache file found
teostra:~ lugia% sudo mount -t nfs -o vers=4,sec=krb5 192.168.1.8:/ tmp [21:36]
mount_nfs: can't mount / from 192.168.1.8 onto /Users/lugia/tmp: RPC prog. not avail
teostra:~ lugia% sudo mount -t nfs -o vers=4,sec=krb5 linaro-nano.local:/ tmp
mount_nfs: can't mount / from linaro-nano.local onto /Users/lugia/tmp: RPC prog. not avail

サーバーを指定するときにIPアドレスではなく、ホスト名で指定する。

DNS まではなくても、/etc/hosts にあれば良い。

teostra:~ lugia% kinit momonga                                          [21:41]
momonga@LOCAL's password: 
teostra:~ lugia% sudo mount -t nfs -o vers=4,sec=krb5 192.168.1.8:/ tmp [21:41]
mount_nfs: can't mount / from 192.168.1.8 onto /Users/lugia/tmp: RPC prog. not avail
teostra:~ lugia% sudo mount -t nfs -o vers=4,sec=krb5 linaro-nano.local:/ tmp
teostra:~ lugia%                                                        [21:41]

NFS に httpd をインストール

Momonga Linux のビルド用の環境として、NFSchroot 環境 を用意したんだけど、NFSsetcap に対応してない (setcaplinux だけの機能と考えれば、当たり前か・・・。) ということで、インストールに失敗します。NFS にインストール、というのは、(パフォーマンス的に) 阿呆な考えなので、あまりしないだろうけど、AUFS や nilfs (後述) でも発生します。

NFS を使っている理由は SSD へのダメージが読めないのと、(Momonga を動かしている) VirtualBox の USB 接続が不安定であることと、机の上に置くことになる外付けHDDが邪魔。

[momonga@lunastra httpd]$ mount
192.168.1.8:/mnt/root on / type nfs4 (rw,relatime,vers=4.1,rsize=131072,wsize=131072,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=10.0.2.15,local_lock=none,addr=192.168.1.8)
devtmpfs on /dev type devtmpfs (rw,relatime,size=4080496k,nr_inodes=1020124,mode=755)
devpts on /dev/pts type devpts (rw,relatime,mode=600,ptmxmode=000)
tmpfs on /dev/shm type tmpfs (rw,relatime)
tmpfs on /tmp type tmpfs (rw,relatime)
sysfs on /sys type sysfs (rw,relatime)
proc on /proc type proc (rw,relatime)
[momonga@lunastra httpd]$ sudo yum install httpd
Loaded plugins: aliases, auto-update-debuginfo, changelog, dellsysid, download-
              : order, etckeeper, fastestmirror, filter-data, fs-snapshot, keys,
              : langpacks, list-data, local, merge-conf, post-transaction-
              : actions, presto, priorities, protectbase, ps, puppetverify,
              : refresh-packagekit, remove-with-leaves, rpm-warm-cache, show-
              : leaves, tmprepo, tsflags, upgrade-helper, verify, versionlock
Unable to connect to dbus
local-development                                        | 2.9 kB     00:00     
local-development-nonfree                                | 2.9 kB     00:00     
trunk                                                    | 3.6 kB     00:00     
Loading mirror speeds from cached hostfile
 * trunk: ftp.kddilabs.jp
Skipping filters plugin, no data
0 packages excluded due to repository protections
Resolving Dependencies
Skipping filters plugin, no data
--> Running transaction check
---> Package httpd.x86_64 0:2.4.16-1m.mo8 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package      Arch          Version              Repository                Size
================================================================================
Installing:
 httpd        x86_64        2.4.16-1m.mo8        local-development        1.0 M

Transaction Summary
================================================================================
Install  1 Package

Total download size: 1.0 M
Installed size: 3.6 M
Is this ok [y/d/N]: y
Downloading packages:
Loaded plugins: auto-update-debuginfo, dellsysid, etckeeper, fastestmirror, fs-
              : snapshot, langpacks, local, post-transaction-actions, presto,
              : priorities, protectbase, puppetverify, refresh-packagekit,
              : remove-with-leaves, rpm-warm-cache, upgrade-helper, versionlock
Unable to connect to dbus
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
etckeeper: pre transaction commit
  Installing : httpd-2.4.16-1m.mo8.x86_64                                   1/1 
Error unpacking rpm package httpd-2.4.16-1m.mo8.x86_64
error: unpacking of archive failed on file /usr/sbin/suexec: cpio: cap_set_file
etckeeper: post transaction commit
Unable to connect to dbus
  Verifying  : httpd-2.4.16-1m.mo8.x86_64                                   1/1 

Failed:
  httpd.x86_64 0:2.4.16-1m.mo8                                                  

Complete!
[momonga@lunastra pkgs]$ svn diff httpd/httpd.spec
Index: httpd/httpd.spec
===================================================================
--- httpd/httpd.spec    (revision 68789)
+++ httpd/httpd.spec    (working copy)
@@ -548,7 +548,7 @@
 %{_sbindir}/fcgistarter
 %{_sbindir}/apachectl
 %{_sbindir}/rotatelogs
-%caps(cap_setuid,cap_setgid+pe) %attr(510,root,%{suexec_caller_user}) %{_sbindir}/suexec
+%attr(6510,root,%{suexec_caller_user}) %{_sbindir}/suexec
 
 %dir %{_libdir}/httpd
 %dir %{_libdir}/httpd/modules
[momonga@lunastra pkgs]$ ../tools/OmoiKondara -f httpd
[momonga@lunastra pkgs]$ sudo rpm -ivh ../PKGS/RPMS/httpd-2.4.16-1m.mo8.x86_64.rpm 
Preparing...                ########################################### [100%]
   1:httpd                  ########################################### [100%]
[momonga@lunastra pkgs]$ ls -l /usr/sbin/suexec
-r-s--s--- 1 root apache 14280 Oct 16 23:53 /usr/sbin/suexec
[momonga@lunastra pkgs]$ getcap /usr/sbin/suexec
Failed to get capabilities of file `/usr/sbin/suexec' (Operation not supported)

一応、こんな感じでインストールは出来るけど、setcap による設定よりやや緩い権限設定になっちゃっているので、本当は、setcap できなかったら、fallback 出来るのが理想だとどっかの記事に書いてあった。

ちなみに、実際のファイルシステムは、下に示すように、btrfs です。(もともとは、nilfs でやってたのだが、nilfs も setcap をサポートしてないことがわかったので、急遽 btrfs に変更したわけだが、結局、NFS の方が問題だったので意味なし。)

linaro@linaro-nano:~/parallella-linux$ mount
  (省略)
/dev/sda1 on /mnt type nilfs2 (rw)
/dev/sda2 on /mnt/root type btrfs (rw)
nfsd on /proc/fs/nfsd type nfsd (rw)

httpd のビルドができているのは、tmpfs でビルドしたから。

おまけ。

(特に意味はない)

linaro@linaro-nano:~/parallella-linux$ sudo chmod ug-s /mnt/root/usr/sbin/suexec 
linaro@linaro-nano:~/parallella-linux$ sudo ls -l /mnt/root/usr/sbin/suexec
-r-x--x--- 1 root rtkit 14280 Oct 16 14:53 /mnt/root/usr/sbin/suexec
linaro@linaro-nano:~/parallella-linux$ sudo setcap cap_setuid,cap_setgid+pe /mnt/root/usr/sbin/suexec
linaro@linaro-nano:~/parallella-linux$ sudo ls -l /mnt/root/usr/sbin/suexec
-r-x--x--- 1 root rtkit 14280 Oct 16 14:53 /mnt/root/usr/sbin/suexec
linaro@linaro-nano:~/parallella-linux$ 
[momonga@lunastra pkgs]$ ls -l /usr/sbin/suexec
-r-x--x--- 1 root apache 14280 Oct 16 23:53 /usr/sbin/suexec
[momonga@lunastra pkgs]$ /usr/sbin/suexec
-bash: /usr/sbin/suexec: Permission denied

無題

PC使えるようになったことだし、環境整備もだいたい済んだから、TeX Live のパッケージ作業をやらねばな・・・。

おまけ (その1)

f:id:lugia:20150727231518p:plain

(原寸大)

仮想環境 (VirtualBox) に (Momonga) Linux を用意し、そこに Rosegarden と Jack をインストールして、ホストの Jack サーバーに転送した図。

ネットワーク負荷もそこまでないはずだし、CPUパワーも足りないとは思えないのだが、音飛びしてしまって実用的ではなかった。調整が必要だな。

おまけ (その2)

f:id:lugia:20150727213216p:plain

(原寸大)

  • MC 1.7.10
  • PureBDCraft x128
  • Shaders MOD
  • Sildurs Vibrant Shaders High
  • Mermaid MOD
  • グラブでキャプチャしたので 4K になってるけど、実際の「処理解像度」は 1920x1080…さすがに 4K での処理は無理ぽ
    • プレイ中は気にならないものの改めてよく見ると結構汚く見える不思議

Mac Pro

Twitter/Facebook のフォロワーさん達はきっと昨日の更新のお知らせで気づいたとは思うが、結局、

  1. それなりにお金をかけて信頼性・耐久性の高いものを買おうと思ったので、UNIX (Linux) が動かせる保証が欲しかった (ついでに Windows はいらない)
  2. Mac 以外の UNIX 系 OS を個人で買うのは非常に難しい。
    • もちろん、Linux でも良いのだが、結局 CentOSSuSE などを下敷きにしないと (現状) 安定性がいまいち (自分で言うか)

など、いろいろ考えた結果、Mac を買うことに。小さいし。

かなりいろんな OS を使っているが、実は Mac は初。

Mo のことはとりあえず仮想環境でやることにしよう。

Inkscape の公式バイナリは X11 が必要だったので、Homebrew で X11 のいらないバージョンをビルドした。ただ、それだと日本語入力ができないので、GtkIMCocoa もビルドして入れた。

一応 Formula: https://github.com/lugia-kun/homebrew/blob/gtkimcocoa/Library/Formula/gtkimcocoa.rb

即興で作ったものなので、いくつか問題があり、使う場合は以下の点に注意。

  • 肝心の IM モジュールは gtk+/gtk+3 の中にインストールされます。従って、
    • GtkIMCocoa の Celler の中はドキュメントしか入っていません。
    • GtkIMCocoa のアンインストール時にモジュールが削除されません。
  • gtk+gtk+3 の両方に対してビルドします。どちらかのみのビルドはできません。

アンインストールするときは、

$HOMEBREW_PREFIX/Cellar/gtk+/[version]/lib/gtk-2.0/2.10.0/immodules/im-cocoa.la
$HOMEBREW_PREFIX/Cellar/gtk+/[version]/lib/gtk-2.0/2.10.0/immodules/im-cocoa.so
$HOMEBREW_PREFIX/Cellar/gtk+3/[version]/lib/gtk-3.0/3.0.0/immodules/im-cocoa.la
$HOMEBREW_PREFIX/Cellar/gtk+3/[version]/lib/gtk-3.0/3.0.0/immodules/im-cocoa.so

を削除して、

$ gtk-query-immodules-2.0 --update-cache
$ gtk-query-immodules-3.0 --update-cache

を実行すれば良いかと。

f:id:lugia:20150621191445p:plain

追記 (2015/11/05)

GtkIMCocoa をいれなくてもよくなっているっぽい。理由は不明。

あれこれ

このタグもどうにかしたほうがいいな…。

まあいいや。

  1. 新しいPCを買いたい。
  2. コア数を多めにほしい (パッケージのビルドとか、Blenderレンダリングとか)
  3. コア数をできるだけ多くする方法を考える。
    1. Intel Xeon E5-2630 v3 (8-core 16-thread) × 2 で 16 コア (32 スレッド) (TDP 170W)
    2. AMD Opteron 6370P (16-core 16-thread) × 2 で 32 コア (32 スレッド) (TDP 198W)
    3. AMD FX-8370E (8-core 8-thread) × 5 (node) で 40 コア (40 スレッド) (TDP 495W)
    4. Intel Core i7 4790S (4-core 8-thread) × 5 (node) で 20 コア (40 スレッド) (TDP 325W)
    5. Parallella (2-core) × 25 (node) で 50 コア (50 スレッド) (250W)
  4. CPU自体の性能は悪くはなさそうなのだが、AMDだとDDR3になるからメモリの読み書き速度がなぁ…。
  5. 買うのをやめる。
  6. ウボァー

ところで。

● ntpd.service - Network Time Service
   Loaded: loaded (/usr/lib/systemd/system/ntpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2015-04-27 23:11:33 JST; 59min left
  Process: 4464 ExecStart=/usr/sbin/ntpd -u ntp:ntp $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 4466 (ntpd)
   CGroup: /system.slice/ntpd.service
           └─4466 /usr/sbin/ntpd -u ntp:ntp -g

なんで時計が1時間も進んでいたんだ…。というかブート時に起動してくれない…rz