From 55670593013bf4e3a318d9c295b460eb105be80f Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Mon, 21 Sep 2026 19:33:48 +0200 Subject: [PATCH] docs: add Linux commands articles --- infrastructure/linux/commands/misc/free.md | 56 +++++++++++++++++++ .../linux/commands/permissions/sudo.md | 52 +++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 infrastructure/linux/commands/misc/free.md create mode 100644 infrastructure/linux/commands/permissions/sudo.md diff --git a/infrastructure/linux/commands/misc/free.md b/infrastructure/linux/commands/misc/free.md new file mode 100644 index 0000000..612d58e --- /dev/null +++ b/infrastructure/linux/commands/misc/free.md @@ -0,0 +1,56 @@ +# freeコマンド + +`free`は、メモリ(RAM)とスワップ領域の使用状況を確認するコマンド + +## 基本構文 + +``` +free [OPTION]... +``` + +## 使用例 + +### 見やすい単位で表示 + +``` +free -h +``` +- `-h`(`--human`)オプションを使用 +- KiB, MiB, GiBなどの単位で表示 + +例: +``` +ubuntu@mail.unchioshi.net:~$ free + total used free shared buff/cache available +Mem: 24608576 2130904 22008672 42364 920764 22477672 +Swap: 0 0 0 +ubuntu@mail.unchioshi.net:~$ free -h + total used free shared buff/cache available +Mem: 23Gi 2.0Gi 20Gi 41Mi 899Mi 21Gi +Swap: 0B 0B 0B +ubuntu@mail.unchioshi.net:~$ +``` + +### 定期的に更新 + +``` +free -h -s 1 +``` +- `-s`(`--seconds`)オプションを使用 +- 指定した秒数ごとに表示 + +## 出力の見方 + +``` +ubuntu@mail.unchioshi.net:~$ free -h + total used free shared buff/cache available +Mem: 23Gi 2.0Gi 20Gi 41Mi 899Mi 21Gi +Swap: 0B 0B 0B +``` +- `total` ... メモリの総容量 +- `used` ... 使用中のメモリ +- `free` ... 完全に未使用のメモリ +- `shared` ... 主に共有メモリや`tmpfs`に使用されているメモリ +- `buff/cache` ... バッファやキャッシュに使用されているメモリ +- `available` ... 新しいアプリケーションが利用できるメモリの推定量 + diff --git a/infrastructure/linux/commands/permissions/sudo.md b/infrastructure/linux/commands/permissions/sudo.md new file mode 100644 index 0000000..b2b0533 --- /dev/null +++ b/infrastructure/linux/commands/permissions/sudo.md @@ -0,0 +1,52 @@ +# sudoコマンド + +`sudo` は、別のユーザーの権限(通常は root 権限)を使用してコマンドを実行するコマンド + + +## 基本構文 + +``` +sudo [-u ] [-s | -i] [ [...]] +``` +- `-u ` ... root以外のユーザの権限で実行するときに指定 +- `` ... 実行するコマンド +- `` ... ``に渡す引数 + +## 使用例 + +### システム用ディレクトリにファイルを作成する +- `/opt`,`/etc`,`/var`などのシステム用ディレクトリは通常rootが所有 +- 一般ユーザがそのようなディレクトリにファイルを作成する際には`sudo`を使用 +- 書き込み権限があれば`sudo`は不要 + +``` +sudo mkdir /opt/gitea +``` + +### postgresユーザとしてpsqlを実行する +- PostgreSQLをインストールすると、`postgres`という専用ユーザが作成される +- PostgreSQLでは、ローカル接続時にpeer認証が使用される +- `postgres`ユーザとして実行することで、DBの管理者ロールである`postgres`に接続できる + +``` +sudo -u postgres psql +``` +- `sudo` ... 別のユーザの権限でコマンドを実行 +- `-u postgres` ... 実行ユーザに`postgres`を指定 +- `psql` ... PostgreSQLのクライアントを起動 + + +### rootユーザとしてログイン + +``` +sudo -i +``` +- `-i`(`--login`)オプションを指定 +- rootユーザのログインシェルを起動する + +### root以外のユーザとしてログイン + +``` +sudo -u taro -i +``` +- root以外のユーザの権限で実行する場合は`-u`オプションを指定 \ No newline at end of file