Open5

【メモ】Linux

あぷしあぷし

ディレクトリ

Filesystem Hierarchy Standard

Linux(などのUNIX系OS)の標準的なディレクトリ構成を定めた標準仕様。
https://www.pathname.com/fhs/pub/fhs-2.3.html

基本のディレクトリ構成(ParrotOS)

各OSの改装はman hierコマンドで確認可能。

ディレクトリ名 概略
/ ルートディレクトリ。ファイルシステムの最上位の位置
/bin 基本となる実行ファイルやコマンド(のバイナリ)
/boot OSの起動に必要なファイル
/dev デバイスファイル
/etc システム固有の設定ファイル
/home ユーザーごとのホームディレクトリ
/lib 必須の共有ライブラリやカーネルモジュール
/lib32 32ビット用の共有ライブラリ
/lib64 64ビット用の共有ライブラリ
/libx32 x32(x86_64ベースでアドレス長32bit)用の共有ライブラリ
/media CD/DVD, リムーバブルメディアのマウントポイント
/mnt 一時的なファイルシステムのマウントポイント
/opt アドオンのアプリやデータ
/proc システムの統計情報、現在実行中のプロセス情報、カーネルパラメータの一部
/root rootユーザーのホームディレクトリ
/run 現在実行されているプロセスに関するデータ
/sbin 基本となるシステム管理者用のコマンド(のバイナリ)
/srv システム固有のサーバー用データ
/sys Linuxカーネルに関するデータ
/tmp 一時的なファイル。再起動時にクリアされることもある。
/usr 各ユーザーが共通して利用するプログラム、ライブラリ、文書、カーネルソース
/var メールやログといった時間の経過とともに変化するデータ

/usr

/usr
├── bin/(必須) 大部分のユーザーコマンド
|   ├── perl/(例:オプション) 
|   ├── python/(例:オプション) 
|   ├── tcl/(例:オプション) 
|   └ など 
├── include/(必須) C言語で使うヘッダファイル
├── lib/(必須) ライブラリファイル
├── local/(必須) パッケージシステム管理外
|   ├── bin/(必須) ローカルにインストールしたプログラム
|   ├── games/(必須) ゲーム
|   ├── include/(必須) C言語用ヘッダファイル
|   ├── lib/(必須) ライブラリとプログラム
|   ├── man/(必須) /local/bin用manページ
|   ├── sbin/(必須) 管理者用プログラム
|   ├── share/(必須) アーキテクチャに依存しないデータ
|   └ src/(必須) ソースコード
├── sbin/(必須) システム用バイナリファイル
├── share/(必須) アーキテクチャに依存しないデータ
|   ├── man/(必須) manページ
|   └ misc/(必須) さまざまなデータ
├── X11R6/(オプション) X Window System
├── games/(オプション) ゲームと教育用
├── lib<qual>/(オプション) ライブラリの代替形式
└ src/(オプション) ソースコード
└ linux/ Linux Kernelのソースコード

参考

あぷしあぷし

パーミッション

drwxr-x--- 4 ubuntu   ubuntu   4096 Oct 15 08:46 ./
drwxr-xr-x 3 root     root     4096 Oct 15 07:25 ../
-rw-r--r-- 1 ubuntu   ubuntu    220 Jan  6  2022 .bash_logout
-rw-r--r-- 1 ubuntu   ubuntu   3771 Jan  6  2022 .bashrc
drwx------ 2 ubuntu   ubuntu   4096 Oct 15 07:27 .cache/
-rw------- 1 ubuntu   ubuntu     20 Oct 15 08:37 .lesshst
-rw-r--r-- 1 ubuntu   ubuntu    807 Jan  6  2022 .profile
drwx------ 2 ubuntu   ubuntu   4096 Oct 15 07:25 .ssh/
-rw-r--r-- 1 ubuntu   ubuntu      0 Oct 15 07:36 .sudo_as_admin_successful
-rw------- 1 ubuntu   ubuntu   1529 Oct 15 08:46 .viminfo
-rwx------ 1 www-data www-data 1371 Oct 15 08:36 server.crt*
-rwx------ 1 www-data www-data 1082 Oct 15 08:32 server.csr*
-rwx------ 1 www-data www-data 1704 Oct 15 08:36 server.key*

各桁の意味

先頭からの番地 意味
1 ファイル種別
2-4 ファイルの所有者に対する権限
5-7 ファイルの所有グループに対する権限
8-10 その他に対する権限

ファイル種別

種別 意味
- ファイル
d ディレクトリ
l シンボリックリンク

権限

モード(英字) モード(数字) 意味
r 4 読み取り権限
w 2 書き込み権限
x 1 実行権限
- - 権限なし

権限付与

chmodコマンドを使用する
chmod の 第1引数で <a/g/u/o><-/=/+><r/w/x> の形式で指定
例1: chmod u+w <ファイル> で ユーザーに対して書込み権限を付与
例2: chmod go=rw <ファイル> で グループとその他は読み込み・書込み権限を持つように設定

変更方法

オプション 内容
= 指定した権限にする
+ 指定した権限を付与する
- 指定した権限を除去する

変更対象

オプション 内容
a すべてのユーザ
g グループ
u ユーザー
o その他

参考

あぷしあぷし

/etc/sudoersファイルの書式

<ユーザ名> <ホスト>=(<権限>) <コマンド>
%<グループ名> <ホスト>=(<権限>) <コマンド>
対象 説明
<ホスト> 設定ファイルを使い回す場合、どのサーバーで設定を反映するかの指示
<権限> どのユーザーに成り代わってコマンドを実行するか。(<ユーザー>:<グループ>) のようにも記述可能。
<コマンド> 許可するコマンドをフルパスで指定

どの項目もALLを指定することで、それぞれすべてのホスト、ユーザー、グループ、コマンドの許可が可能

記述例

root ALL=(ALL:ALL) ALL
%sudo ALL=(ALL) NOPASSWD:ALL

参考

  • ハッキング・ラボのつくりかた P269~
あぷしあぷし

エディタの変更

デフォルトのエディタの変更

$ sudo update-alternatives --config editor

There are 4 choices for the alternative editor (providing /usr/bin/editor).

  Selection    Path                Priority   Status
------------------------------------------------------------
* 0            /bin/nano            40        auto mode
  1            /bin/ed             -100       manual mode
  2            /bin/nano            40        manual mode
  3            /usr/bin/vim.basic   30        manual mode
  4            /usr/bin/vim.tiny    10        manual mode

Press <enter> to keep the current choice[*], or type selection number: 3

sudoで実行する必要あり。
Vimを使いたい場合、「3」を入力してエンター

一時的な変更

EDITOR=vim sudo -E visudo

-Eオプションにより特権ユーザー時に環境変数を引き継ぐ。

参考

あぷしあぷし

標準入力・標準出力・標準エラー出力

ファイルディスクリプタとの対応

ファイルディスクリプタ 対応する入出力 英名
0 標準入力 stdin
1 標準出力 stdout
2 標準エラー出力 stderr

リダイレクト (>, >>, <)

%A% > %B% で %A%からの入力を%B%に出力する。
引数には ファイルディスクリプタ と デバイスファイルが指定できる。

  • 1>
    標準出力の内容をリダイレクトする。1は省略可能。
┌─[parrot@parrot][~/exprience/2]
└──╼ $cat 1> hello.txt
hello fron keyboard # キーボードから入力しエンター、その後 Ctrl + D で EOF を送信して入力終了
┌─[parrot@parrot][~/exprience/2]
└──╼ $cat hello.txt 
hello fron keyboard # 入力した内容がファイルに書き込まれている

キーボードからの入力を受け付けて、hello.txtに入力内容を保存する。

  • 2>
    標準エラー出力の内容をリダイレクトする。
┌─[parrot@parrot][~/exprience/2]
└──╼ $rm parrot-archive-keyring_2024.12_all.deb 
┌─[][parrot@parrot][~/exprience/2]
└──╼ $cat -k hoge 2> error.txt
┌─[][parrot@parrot][~/exprience/2] # なにも表示されない
└──╼ $cat error.txt 
cat: invalid option -- 'k'
Try 'cat --help' for more information. # エラーの出力がファイルに書き込まれている

複数列挙することも可能

┌─[parrot@parrot][~/exprience/2]
└──╼ $ls
error.txt  hello.txt
┌─[parrot@parrot][~/exprience/2]
└──╼ $cat -k hello.txt 1> result.txt 2> error.txt 
┌─[][parrot@parrot][~/exprience/2]
└──╼ $cat result.txt 
┌─[parrot@parrot][~/exprience/2] # result.txt には 何も書き込まれていない
└──╼ $cat error.txt
cat: invalid option -- 'k'
Try 'cat --help' for more information. # error.txt に エラー出力が書き込まれている
┌─[parrot@parrot][~/exprience/2]
└──╼ $cat hello.txt 1> result.txt 2> error.txt 
┌─[parrot@parrot][~/exprience/2]
└──╼ $cat result.txt 
hello fron keyboard
┌─[parrot@parrot][~/exprience/2]
└──╼ $cat error.txt 
┌─[parrot@parrot][~/exprience/2]
└──╼ $

&>でも同等のことが可能。

command &> output.txt # command 1> output.txt 2> output.txt と同等
  • 0<
    ファイルの内容を標準入力に入力する。0は省略可能。
┌─[parrot@parrot][~/exprience/2]
└──╼ $echo hello, hoge > hello.txt 
┌─[parrot@parrot][~/exprience/2]
└──╼ $cat hello.txt 
hello, hoge
┌─[parrot@parrot][~/exprience/2]
└──╼ $cat > result.txt < hello.txt 
┌─[parrot@parrot][~/exprience/2]
└──╼ $cat result.txt 
hello, hoge # hello.txtの「hello, hoge」を result.txtに書き込んだ
  • >>
    >はファイルを上書きするのに対して>>は追記を行う。

デバイスファイルも指定可能で、別端末へ出力をリダイレクトすることも可能。
端末のデバイスファイル名は ttyコマンドで確認可能。

# /dev/pts/0
┌─[parrot@parrot][~/exprience/2]
└──╼ $cat < hello.txt > /dev/pts/1
┌─[parrot@parrot][~/exprience/2]
└──╼ $
# /dev/pts/1
┌─[parrot@parrot][~]
└──╼ $hello, hoge
  • n>&o
    「ファイルディスクリプタn」を「ファイルディスクリプタoの出力先」と同じにする
  • n<&o
    「ファイルディスクリプタn」を「ファイルディスクリプタoの入力先」と同じにする
┌─[parrot@parrot][~/exprience/2]
└──╼ $find /root -name "README.license" # 非ルートユーザーで /root ディレクトリをfindする
find: ‘/root/.config/mozc’: Permission denied
find: ‘/root/.local/share/keyrings’: Permission denied
find: ‘/root/.local/share/nvim’: Permission denied
/root/Desktop/README.license
find: ‘/root/.cache’: Permission denied
find: ‘/root/.ssh’: Permission denied
find: ‘/root/.dbus’: Permission denied
find: ‘/root/.synaptic’: Permission denied
┌─[][parrot@parrot][~/exprience/2]
└──╼ $find /root -name "README.license" > /dev/null # 標準出力をnullデバイスに捨てる
find: ‘/root/.config/mozc’: Permission denied
find: ‘/root/.local/share/keyrings’: Permission denied
find: ‘/root/.local/share/nvim’: Permission denied
find: ‘/root/.cache’: Permission denied
find: ‘/root/.ssh’: Permission denied
find: ‘/root/.dbus’: Permission denied
find: ‘/root/.synaptic’: Permission denied
┌─[][parrot@parrot][~/exprience/2]
└──╼ $find /root -name "README.license" 2> /dev/null # 標準エラー出力をnullデバイスに捨てる
/root/Desktop/README.license
┌─[][parrot@parrot][~/exprience/2]
└──╼ $find /root -name "README.license" > /dev/null 2>&1 # 標準出力をnullデバイスに捨て、stderrをstdoutの出力先(= /dev/null)と同じにする
┌─[][parrot@parrot][~/exprience/2] # なにも表示されない
└──╼ $
┌─[][parrot@parrot][~/exprience/2]
└──╼ $find /root -name "README.license" 2>&1 1> /dev/null # stderrをstdoutの出力先(= ターミナル端末)にし、stdoutを/dev/nullに捨てる)
find: ‘/root/.config/mozc’: Permission denied
find: ‘/root/.local/share/keyrings’: Permission denied
find: ‘/root/.local/share/nvim’: Permission denied
find: ‘/root/.cache’: Permission denied
find: ‘/root/.ssh’: Permission denied
find: ‘/root/.dbus’: Permission denied
find: ‘/root/.synaptic’: Permission denied # stdoutに出力される内容が表示されない

メモ

catを引数なしで実行すると、標準入力から受け付け、Enter押下で入力内容を標準出力に吐き出す。

cat
hello fron stdin # キーボードからの入力。
hello fron stdin # Enterを入力するとcatコマンドが受け付けた文字列を出力

参考

  • ハッキング・ラボのつくりかた P380〜390