インフラエンジニアのためのLinuxコマンド

## 初めに

今回は、MAC OSに対応したUBUNTUの仮想環境であるMultipassを使用しました。
（Windowsでは、WSL2になります）
MAC OS上でLinuxコマンドの入力や実装ができるため、非常に便利です。
一番最後にMultipassのリンクを貼っておきますので、参考にしてください。

### 1. ポートの利用状況

コマンドを実行する際には、「sudo apt install net-tools」を使用して、インストールしましょう。

```
~$ netstat -anp
```

```
出力結果

(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      -
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      -
tcp        0      0 192.168.64.3:22         192.168.64.1:49193      ESTABLISHED -
tcp        0      0 192.168.64.3:22         192.168.64.1:49418      ESTABLISHED -
tcp        0      0 192.168.64.3:22         192.168.64.1:49179      ESTABLISHED -
tcp6       0      0 :::22                   :::*                    LISTEN      -
udp        0      0 127.0.0.53:53           0.0.0.0:*                           -
udp        0      0 192.168.64.3:68         0.0.0.0:*                           -
raw6       0      0 :::58                   :::*                    7           -
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node   PID/Program name     Path
unix  2      [ ACC ]     STREAM     LISTENING     11811    -                    /run/systemd/userdb/io.systemd.DynamicUser
unix  2      [ ACC ]     SEQPACKET  LISTENING     11841    -                    /run/udev/control
unix  2      [ ACC ]     STREAM     LISTENING     11821    -                    /run/lvm/lvmpolld.socket
unix  2      [ ]         DGRAM                    11824    -                    /run/systemd/journal/syslog
unix  2      [ ACC ]     STREAM     LISTENING     11826    -                    /run/systemd/fsck.progress
unix  13     [ ]         DGRAM                    11834    -                    /run/systemd/journal/dev-log
unix  2      [ ]         DGRAM                    23263    1055/systemd         /run/user/1000/systemd/notify
unix  2      [ ACC ]     STREAM     LISTENING     11836    -                    /run/systemd/journal/stdout
unix  2      [ ACC ]     STREAM     LISTENING     23266    1055/systemd         /run/user/1000/systemd/private
unix  9      [ ]         DGRAM                    11838    -                    /run/systemd/journal/socket
unix  2      [ ACC ]     STREAM     LISTENING     23717    1055/systemd         /run/user/1000/bus
unix  2      [ ACC ]     STREAM     LISTENING     23718    1055/systemd         /run/user/1000/gnupg/S.dirmngr
unix  2      [ ACC ]     STREAM     LISTENING     23719    1055/systemd         /run/user/1000/gnupg/S.gpg-agent.browser
unix  2      [ ACC ]     STREAM     LISTENING     23720    1055/systemd         /run/user/1000/gnupg/S.gpg-agent.extra
unix  2      [ ACC ]     STREAM     LISTENING     17248    -                    /var/snap/lxd/common/lxd/unix.socket
unix  2      [ ACC ]     STREAM     LISTENING     11823    -                    @/org/kernel/linux/storage/multipathd
unix  2      [ ACC ]     STREAM     LISTENING     23721    1055/systemd         /run/user/1000/gnupg/S.gpg-agent.ssh
unix  2      [ ACC ]     STREAM     LISTENING     23722    1055/systemd         /run/user/1000/gnupg/S.gpg-agent
unix  2      [ ACC ]     STREAM     LISTENING     23723    1055/systemd         /run/user/1000/pk-debconf-socket
unix  2      [ ACC ]     STREAM     LISTENING     23724    1055/systemd         /run/user/1000/snapd-session-agent.socket
unix  2      [ ACC ]     STREAM     LISTENING     13352    -                    /run/systemd/journal/io.systemd.journal
unix  2      [ ACC ]     STREAM     LISTENING     17243    -                    /run/dbus/system_bus_socket
unix  2      [ ACC ]     STREAM     LISTENING     17245    -                    /run/docker.sock
unix  2      [ ACC ]     STREAM     LISTENING     17250    -                    /run/snapd.socket
unix  2      [ ACC ]     STREAM     LISTENING     17252    -                    /run/snapd-snap.socket
unix  2      [ ACC ]     STREAM     LISTENING     17254    -                    /run/uuidd/request
unix  2      [ ACC ]     STREAM     LISTENING     19009    -                    /run/irqbalance//irqbalance655.sock
unix  2      [ ACC ]     STREAM     LISTENING     21556    -                    /run/containerd/containerd.sock.ttrpc
unix  2      [ ACC ]     STREAM     LISTENING     21558    -                    /run/containerd/containerd.sock
unix  2      [ ACC ]     STREAM     LISTENING     20863    -                    /var/run/docker/metrics.sock
unix  2      [ ACC ]     STREAM     LISTENING     21796    -                    /var/run/docker/libnetwork/1b59ba78122b.sock
unix  2      [ ACC ]     STREAM     LISTENING     17247    -                    @ISCSIADM_ABSTRACT_NAMESPACE
unix  3      [ ]         DGRAM                    11806    -                    /run/systemd/notify
unix  2      [ ACC ]     STREAM     LISTENING     11809    -                    /run/systemd/private
unix  3      [ ]         DGRAM                    17608    -
unix  3      [ ]         STREAM     CONNECTED     19794    -
unix  2      [ ]         DGRAM                    16169    -
unix  3      [ ]         STREAM     CONNECTED     18329    -                    /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     19884    -                    /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     23712    -                    /run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     17931    -
unix  3      [ ]         STREAM     CONNECTED     25634    1370/dbus-daemon
unix  3      [ ]         DGRAM                    17609    -
unix  3      [ ]         STREAM     CONNECTED     20720    -                    /run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     19894    -                    /run/systemd/journal/stdout
unix  2      [ ]         STREAM     CONNECTED     24284    -
unix  3      [ ]         DGRAM                    23264    1055/systemd
unix  2      [ ]         DGRAM                    23695    -
unix  2      [ ]         DGRAM                    19409    -
unix  3      [ ]         STREAM     CONNECTED     21564    -                    /run/systemd/journal/stdout
unix  3      [ ]         DGRAM                    13654    -
unix  3      [ ]         STREAM     CONNECTED     26369    -
unix  3      [ ]         STREAM     CONNECTED     21659    -
unix  3      [ ]         STREAM     CONNECTED     19295    -                    /run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     15175    -
unix  3      [ ]         STREAM     CONNECTED     17123    -                    /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     23267    1055/systemd
unix  3      [ ]         STREAM     CONNECTED     19296    -                    /run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     19291    -                    /run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     20722    -
unix  3      [ ]         STREAM     CONNECTED     20540    -
unix  3      [ ]         STREAM     CONNECTED     26370    -
unix  2      [ ]         DGRAM                    24307    -
unix  3      [ ]         STREAM     CONNECTED     23482    -
unix  3      [ ]         STREAM     CONNECTED     20813    -
unix  3      [ ]         STREAM     CONNECTED     20719    -
unix  3      [ ]         DGRAM                    16172    -
unix  3      [ ]         STREAM     CONNECTED     20865    -
unix  3      [ ]         STREAM     CONNECTED     19373    -
unix  3      [ ]         STREAM     CONNECTED     21536    -
unix  2      [ ]         DGRAM                    19276    -
unix  3      [ ]         STREAM     CONNECTED     20723    -
unix  2      [ ]         DGRAM                    19109    -
unix  3      [ ]         STREAM     CONNECTED     19986    -
unix  3      [ ]         STREAM     CONNECTED     17929    -
unix  3      [ ]         STREAM     CONNECTED     21537    -                    /run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     20084    -                    /run/systemd/journal/stdout
unix  2      [ ]         DGRAM                    25633    1370/dbus-daemon
unix  3      [ ]         STREAM     CONNECTED     19290    -
unix  3      [ ]         STREAM     CONNECTED     18993    -
unix  3      [ ]         STREAM     CONNECTED     16795    -
unix  2      [ ]         DGRAM                    19008    -
unix  3      [ ]         STREAM     CONNECTED     17930    -
unix  3      [ ]         STREAM     CONNECTED     13660    -                    /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     24532    -
unix  3      [ ]         STREAM     CONNECTED     14122    -                    /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     19014    -
unix  3      [ ]         DGRAM                    17607    -
unix  3      [ ]         STREAM     CONNECTED     25635    1370/dbus-daemon
unix  3      [ ]         STREAM     CONNECTED     19888    -                    /run/systemd/journal/stdout
unix  3      [ ]         DGRAM                    23265    1055/systemd
unix  2      [ ]         DGRAM                    23144    -
unix  3      [ ]         STREAM     CONNECTED     20626    -
unix  3      [ ]         STREAM     CONNECTED     19390    -                    /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     19424    -                    /run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     18744    -
unix  3      [ ]         STREAM     CONNECTED     19294    -                    /run/dbus/system_bus_socket
unix  2      [ ]         STREAM     CONNECTED     23653    -
unix  3      [ ]         DGRAM                    11808    -
unix  3      [ ]         STREAM     CONNECTED     18073    -
unix  3      [ ]         STREAM     CONNECTED     19289    -
unix  3      [ ]         STREAM     CONNECTED     25637    1370/dbus-daemon     /run/user/1000/bus
unix  3      [ ]         DGRAM                    11807    -
unix  3      [ ]         STREAM     CONNECTED     19293    -                    /run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     17046    -                    /run/systemd/journal/stdout
unix  2      [ ]         DGRAM                    13652    -
unix  3      [ ]         STREAM     CONNECTED     19885    -                    /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     18144    -
unix  3      [ ]         DGRAM                    16171    -
unix  3      [ ]         STREAM     CONNECTED     16796    -                    /run/systemd/journal/stdout
unix  2      [ ]         DGRAM                    13564    -
unix  3      [ ]         STREAM     CONNECTED     21542    -
unix  3      [ ]         STREAM     CONNECTED     21660    -                    /run/containerd/containerd.sock
unix  3      [ ]         STREAM     CONNECTED     20454    -
unix  3      [ ]         STREAM     CONNECTED     19205    -
unix  3      [ ]         STREAM     CONNECTED     14453    -
unix  3      [ ]         STREAM     CONNECTED     18580    -
unix  3      [ ]         STREAM     CONNECTED     18575    -
unix  3      [ ]         STREAM     CONNECTED     25622    -                    /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     24908    1055/systemd
unix  3      [ ]         STREAM     CONNECTED     21541    -
unix  3      [ ]         STREAM     CONNECTED     19203    -
unix  3      [ ]         STREAM     CONNECTED     19374    -                    /run/dbus/system_bus_socket
unix  2      [ ]         DGRAM                    18583    -
unix  2      [ ]         DGRAM                    17603    -
unix  3      [ ]         STREAM     CONNECTED     18328    -                    /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     19292    -                    /run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     17690    -
unix  3      [ ]         STREAM     CONNECTED     25621    1370/dbus-daemon
unix  3      [ ]         STREAM     CONNECTED     23689    -                    /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     18664    -
unix  2      [ ]         DGRAM                    19236    -
unix  3      [ ]         STREAM     CONNECTED     19352    -                    /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     19890    -                    /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     18331    -                    /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     19423    -
unix  3      [ ]         STREAM     CONNECTED     23246    1055/systemd
unix  2      [ ]         STREAM     CONNECTED     26263    -
unix  3      [ ]         STREAM     CONNECTED     19306    -                    /run/systemd/journal/stdout
unix  3      [ ]         STREAM     CONNECTED     19297    -                    /run/dbus/system_bus_socket
unix  3      [ ]         DGRAM                    16173    -
unix  2      [ ]         DGRAM                    25032    -
unix  3      [ ]         DGRAM                    13655    -
unix  3      [ ]         STREAM     CONNECTED     20866    -                    /run/containerd/containerd.sock
unix  2      [ ]         DGRAM                    19336    -
unix  3      [ ]         STREAM     CONNECTED     22115    -
unix  2      [ ]         DGRAM                    23256    1055/systemd
unix  3      [ ]         STREAM     CONNECTED     23481    -
unix  2      [ ]         DGRAM                    25431    -
unix  3      [ ]         STREAM     CONNECTED     17045    -
unix  3      [ ]         DGRAM                    16174    -
unix  3      [ ]         STREAM     CONNECTED     24531    -
unix  2      [ ]         DGRAM                    13354    -
unix  3      [ ]         DGRAM                    17610    -
unix  3      [ ]         STREAM     CONNECTED     22116    -                    /run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     19500    -
unix  2      [ ]         DGRAM                    17135    -
```
### 2. サーバーホスト名を確認する

「hostname」と「uname -n」の出力結果は同じなので、どちらでもいいかなと思います。


```
~$ hostname
~$ uname -n
~$ uname a

```
### 3. ルーティングテーブルの表示

```
~$ route print
```

```
出力結果

Usage: route [-nNvee] [-FC] [<AF>]           List kernel routing tables
       route [-v] [-FC] {add|del|flush} ...  Modify routing table for AF.

       route {-h|--help} [<AF>]              Detailed usage syntax for specified AF.
       route {-V|--version}                  Display version/author and exit.

        -v, --verbose            be verbose
        -n, --numeric            don't resolve names
        -e, --extend             display other/more information
        -F, --fib                display Forwarding Information Base (default)
        -C, --cache              display routing cache instead of FIB

  <AF>=Use -4, -6, '-A <af>' or '--<af>'; default: inet
  List of possible address families (which support routing):
    inet (DARPA Internet) inet6 (IPv6) ax25 (AMPR AX.25)
    netrom (AMPR NET/ROM) ipx (Novell IPX) ddp (Appletalk DDP)
    x25 (CCITT X.25)

```
上記のような長い出力結果が表示されましたが、おそらくオプションを指定欲しいのだと思います。
以下に、「-nNvee」を指定して実装します

```
~$route -nNvee
```

```
出力結果

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface    MSS   Window irtt
0.0.0.0         192.168.64.1    0.0.0.0         UG    100    0        0 enp0s1   0     0      0
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker   0     0      0
192.168.64.0    0.0.0.0         255.255.255.0   U     0      0        0 enp0s1   0     0      0
192.168.64.1    0.0.0.0         255.255.255.255 UH    100    0        0 enp0s1   0     0      0
```
### 4. サーバーコマンド

```
~$ w
```

```
出力結果

19:44:08 up  2:08,  1 user,  load average: 0.06, 0.05, 0.01
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
ubuntu   pts/0    192.168.64.1     19:18    0.00s  0.15s  0.03s w
```
### 5. 各種リソースの使用状況

```
~$vmstat
```

```
出力結果

19:44:08 up  2:08,  1 user,  load average: 0.06, 0.05, 0.01
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
ubuntu   pts/0    192.168.64.1     19:18    0.00s  0.15s  0.03s w
```
### 6. CPUの使用率を確認する

コマンドを実行する前に、「sudo apt install sysstat」を実行して、インストールする。

```
~$ iostat -c
```

```
出力結果

Linux 5.4.0-169-generic (UBUNTU) 	01/22/24 	_aarch64_	(2 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.11    0.01    0.12    0.03    0.00   99.73
```
### 7. コア毎のCPUの使用率を確認

```
~$mpstat -P ALL
```

```
出力結果

Linux 5.4.0-169-generic (UBUNTU) 	01/22/24 	_aarch64_	(2 CPU)

21:14:42     CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
21:14:42     all    0.10    0.01    0.10    0.03    0.00    0.01    0.00    0.00    0.00   99.75
21:14:42       0    0.10    0.01    0.10    0.03    0.00    0.01    0.00    0.00    0.00   99.75
21:14:42       1    0.09    0.01    0.09    0.03    0.00    0.01    0.00    0.00    0.00   99.76

```
### 8. メモリの空き容量を確認する

```
~$ free -m
```

```
出力結果

              total        used        free      shared  buff/cache   available
Mem:            969         194         246           1         529         685
Swap:             0           0           0

```

## 最後に

インフラで使用するコマンドは、未だ沢山ありますので、現在も学習を続けています。

## 参考リンク

https://heartbeats.jp/hbblog/2013/10/10-commands.html

https://multipass.run/docs/installing-on-macos