Linux勉強メモ
概要
Linuxを触るので久しぶりに再勉強した内容をメモしておく。
- 概要
- ssh
- 仮想マシン
- コンテナ
- ブートプロセス
- systemd
- ターゲット
- 主なターゲット
- ターゲットの居場所
- ターゲットの管理
- systemctl
- ターゲット
- プロセス
- プロセス関連のコマンド
- ジョブ
- GUI環境
- X Windos System
- 代表的なウィンドウマネージャ
- 統合デスクトップ環境
- 代表的な統合デスクトップ環境
- ディスプレイマネージャ
- X Window Systemの起動
- XサーバーとXクライアント
- ファイルの所有者と所有グループ
- アクセス権
- chmod
- umask
- SUID
- SGID
- スティッキービット
- SUID/SGID/スティッキービット
- ファイル管理
- アーカイブ
- ハードリンク、シンボリックリンク
- iノード (index node)
- ハードリンク
- シンボリックリンク
- シンボリックリンクのコピー
- シンボリックリンクのリンク元ファイルを削除した場合の挙動
- 正規表現
- ファイルの検索
- ディレクトリ構造
- 基本的なディレクトリ
- 「/bin」「/usr/bin」「/usr/local/bin」ディレクトリの使い分け
- 基本的なディレクトリ
- シェルの種類
- シェルの基本操作
- パッケージ
- パッケージの問題点
- パッケージ管理システム
- apt
- apt-getコマンド
- apt-getのリポジトリ
- apt-cacheコマンド
- apt-fileコマンド
- apt-getコマンド
- dpkg
- yum
- rpm
- デバイス管理
- udev
- ハードディスクのパーティション
- パーティションの種類
- LVM
- デバイスファイル
- パーティションの操作
- パーティションのテーブル
- ファイルシステム
- Btrfs
- ファイルシステムのマウント
- ネットワークの設定
- 設定ファイル
- nmcli コマンド
- ip コマンド
- ifconfig コマンド
- ifup/ifdown コマンド
- セキュリティ
- 不要なSUID/SGIDの削除
- 不要なポートを閉める
- 不要なサービスの停止
- TCP Wrappersによるアクセス制御
- firewalld サービス
- ログイン履歴の確認
ssh
https://www.codelab.jp/blog/?p=3569
sudo apt install openssh-server
$ systemctl status ssh
Unit ssh.service could not be found.
$ sudo apt install openssh-server
$ sudo systemctl enable ssh
$ sudo ssh-keygen
$ ssh-copy-id bluen@localhost
$ ls .ssh/authorized_keys
$ sudo systemctl start ssh
仮想マシン
https://note.com/vgnsz90ps/n/na8964293bf75
$ sudo apt install libvirt-clients
sudo apt install libvirt-clients qemu-utils qemu-kvm
sudo apt install libvirt-daemon
sudo apt install libvirt-daemon-system
コンテナ
- cgroups:コンテナが使用するハードウェアリソースの管理
- namespace
- docker
ブートプロセス
ブートプロセス=Linuxが起動するまでの流れ
systemd
- ユニット(設定ファイル)という単位で処理を管理する
- ユニットには種類がある
- ~.target
- ~.service
- ~.mount
- ~.device
- ~.swap
- ユニットには種類がある
- initが古くて、systemdが新しい
- initはSysvinitを使って起動処理を行っていた。Sysvinitはシェルスクリプトにまとめて起動処理が書かれており、変更が難しい
ターゲット
複数のユニットをまとめたもの
主なターゲット
- runlevel3.target
- ほとんどのプロセスが動作するモード(Webサーバ運用時に指定)
- multi-user.target
- CUIログインを行う
- graphical.target
- グラフィカルなログインを行う
- poweroff.target
- システムを終了する
- reboot.target
- システムの再起動を行う
ターゲットの居場所
/etc/systemd/system/
または
/lib/systemd/system/
ターゲットの管理
systemctl get-default
systemctl set-default ターゲット名
systemctl isolate ターゲット名
systemctl isolate poweroff.target
systemctl
systemctl
- isolate
- status
- is-active
- disable
- enable
- start
- restart
- reload
- stop
- set-default
- get-default
- reboot
- list-unit-files
プロセス
プロセス関連のコマンド
- ps
- プロセスの一覧表示
- top
- CPUやメモリの使用率順にプロセスを表示
- pstree
- プロセスの親子関係を表示
- kill
- プロセスの終了
- killall
- pkill
- pgrep
- 引数として指定したプロセス名に対してPIDを返す
$ sleep 60 &
[1] 692
$ pgrep sleep
692
$ ps a
PID TTY STAT TIME COMMAND
243 hvc0 Ss+ 0:00 /sbin/agetty -o -p -- \u --noclear --keep-baud console 115200,38400,9600 vt220
248 tty1 Ss+ 0:00 /sbin/agetty -o -p -- \u --noclear tty1 linux
611 pts/0 Ss 0:00 -bash
612 pts/1 Ss 0:00 /bin/login -f
657 pts/1 S+ 0:00 -bash
692 pts/0 S 0:00 sleep 60
709 pts/0 R+ 0:00 ps a
$ sleep 60 &
[1] 798
$ kill -STOP 798
$ pgrep -l sleep
798 sleep
[1]+ Stopped sleep 60
$ sleep 60 &
[1] 804
$ kill -TERM 804
$ pgrep sleep
[1]+ Terminated sleep 60
$ ps a
PID TTY STAT TIME COMMAND
263 hvc0 Ss+ 0:00 /sbin/agetty -o -p -- \u --noclear --keep-baud console 115200,38400,9600 vt220
270 tty1 Ss+ 0:00 /sbin/agetty -o -p -- \u --noclear tty1 linux
653 pts/0 Ss 0:00 -bash
654 pts/1 Ss 0:00 /bin/login -f
696 pts/1 S+ 0:00 -bash
725 pts/0 S 0:00 sleep 1000
726 pts/0 S 0:00 sleep 10004
727 pts/0 S 0:00 sleep 10003
730 pts/0 R+ 0:00 ps a
$ killall sleep
[2]- Terminated sleep 10004
[1]- Terminated sleep 1000
[3]+ Terminated sleep 10003
ジョブ
- フォアグラウンドジョブ
- バックグラウンドジョブ
コマンド + & でバックグラウンドジョブとして実行できる。
$ sleep 120 &
[2] 1483
$ ps
PID TTY TIME CMD
653 pts/0 00:00:00 bash
1482 pts/0 00:00:00 sleep
1483 pts/0 00:00:00 sleep
1484 pts/0 00:00:00 ps
jobsコマンドでジョブを表示し、bgコマンドでフォアグラウンドジョブをバックグラウンドジョブに変更できる。
$ jobs
[1]- Done sleep 12
[2]+ Running sleep 120 &
$ bg %2
-bash: bg: job 2 already in background
GUI環境
GUI環境を利用するのに、X Window Systemという仕組みが用いられる。
X Windos System
- 入出力装置をXサーバーが管理
- Xクライアントが、Xサーバーにディスプレイ表示するようリクエスト
- XクライアントのウィンドウマネージャがX Windows Systemの外観を制御する
代表的なウィンドウマネージャ
統合デスクトップ環境
下記のような様々なアプリケーションをまとめて提供するソフトウェア
- テキストエディタ
- 端末
- ファイルマネージャ
- ウィンドウマネージャ
- ディスプレイマネージャ
代表的な統合デスクトップ環境
ディスプレイマネージャ
GUI環境でユーザ認証を行うソフトウェア
X Window Systemの起動
$ startx
# systemctl isolate graphical.target
XサーバーとXクライアント
サーバー側がXクライアント、ユーザー側がXサーバー
ファイルの所有者と所有グループ
ファイルを作成されると、ユーザが所有者になり、ユーザが所属しているグループが所有グループとなる。
# ls -l
total 4
drwx------ 4 root root 4096 May 20 14:10 snap
-rw-r--r-- 1 root root 0 Jun 22 23:17 test.txt
# chown bluen test.txt
# ls -l
total 4
drwx------ 4 root root 4096 May 20 14:10 snap
-rw-r--r-- 1 bluen root 0 Jun 22 23:17 test.txt
# chgrp bluen test.txt
# ls -l
total 4
drwx------ 4 root root 4096 May 20 14:10 snap
-rw-r--r-- 1 bluen bluen 0 Jun 22 23:17 test.txt
アクセス権
- 読み取り可能
- 書き込み可能
- ファイルの書き込み(ファイルの内容変更)
- ディレクトリの書き込み(ファイル作成、削除)
- 実行可能
# ls -l
total 4
drwx------ 4 root root 4096 May 20 14:10 snap
-rw-r--r-- 1 bluen bluen 0 Jun 22 23:17 test.txt
# chmod o+x test.txt
# ls -l
total 4
drwx------ 4 root root 4096 May 20 14:10 snap
-rw-r--r-x 1 bluen bluen 0 Jun 22 23:17 test.txt
chmod
操作対象
- u: 所有者
- g: グループ
- o: その他のユーザー
- a: 全てのユーザー
操作内容
-
* : アクセス権を追加 -
* : アクセス権を削除 - = : アクセス権を指定
許可の種別
- r
- w
- x
- s
- t
umask
デフォルトのアクセス権
- ファイル:666
- ディレクトリ:777
umaskコマンドでデフォルトのアクセス権を変更
# umask
0022
# touch test2.txt
# ls -l
-rw-r--r-- 1 root root 0 Jun 22 23:32 test2.txt
umask値が0022なので、666 - 022 = 644となっていることがわかる。
つまり、-rw-r--r--となる。
# umask 0026
# umask
0026
# touch test3.txt
# ls -l
-rw-r--r-- 1 root root 0 Jun 22 23:32 test2.txt
-rw-r----- 1 root root 0 Jun 22 23:36 test3.txt
umask値を0026に変更して、ファイルを作成すると、666 - 026 = 640、つまり、-rw-r-----となる。
SUID
実行権を持つユーザによって実行された場合、ファイルの所有者の権限で実行される。
$ ls -l /usr/bin/passwd
-rwsr-xr-x 1 root root 59976 Nov 24 2022 /usr/bin/passwd
- 実行権のところが
sになっている。 - passwdコマンドの場合はrootが所有者なので、root権限で実行される。
SGID
- 実行権を持つユーザによって実行された場合、ファイルを所有するグループの権限で実行される。
- ディレクトリに設定することも可能
$ chmod g+s test.txt
$ ls -l
-rw-r-Sr-- 1 bluen bluen 0 Jun 22 23:13 test.txt
スティッキービット
書き込み権限はあっても、ファイルを削除できないようにできる。
$ ls -ld /tmp
drwxrwxrwt 7 root root 4096 Jun 23 06:55 /tmp
$ ls -l
drwxr-xr-x 2 bluen bluen 4096 Jun 23 06:58 sampledir
$ chmod o+t sampledir
$ ls -l
drwxr-xr-t 2 bluen bluen 4096 Jun 23 06:58 sampledir
SUID/SGID/スティッキービット
- SUID: 4000
- SGID: 2000
- スティッキービット: 1000
ファイル管理
- ls
- file
$ cat test.txt
ttthhh
$ file test.txt
test.txt: ASCII text
$ ls -lR
.:
total 16
drwxr-xr-x 3 bluen bluen 4096 Jun 23 07:15 dir1
drwxr-xr-t 2 bluen bluen 4096 Jun 23 06:58 sampledir
drwx------ 3 bluen bluen 4096 May 14 20:05 snap
-rw-r--r-- 1 bluen bluen 7 Jun 23 07:05 test.txt
./dir1:
total 4
drwxr-xr-x 3 bluen bluen 4096 Jun 23 07:15 dir2
./dir1/dir2:
total 4
drwxr-xr-x 2 bluen bluen 4096 Jun 23 07:15 dir3
./dir1/dir2/dir3:
total 0
- rm
-rでディレクトリ内もすべて削除
- touch
- ファイルのタイムスタンプを変更する
$ touch -t 202201010000 test.txt
$ ls -l
-rw-r--r-- 1 bluen bluen 7 Jan 1 2022 test.txt
アーカイブ
複数のファイルを1つのアーカイブファイルにまとめる
$ tar -cf tests.tar test.txt test2.txt
$ ls -l
-rw-r--r-- 1 bluen bluen 7 Jan 1 2022 test.txt
-rw-r--r-- 1 bluen bluen 0 Jun 23 08:30 test2.txt
-rw-r--r-- 1 bluen bluen 10240 Jun 23 08:31 tests.tar
ハードリンク、シンボリックリンク
iノード (index node)
- ファイルの属性情報がまとめられたデータ
- ファイルがディスク上のどこに書き込まれているか記録されている
$ ls -li
total 28
5260 drwxr-xr-x 3 bluen bluen 4096 Jun 23 07:15 dir1
5252 drwxr-xr-t 2 bluen bluen 4096 Jun 23 06:58 sampledir
24896 drwx------ 3 bluen bluen 4096 May 14 20:05 snap
5489 -rw-r--r-- 1 bluen bluen 7 Jan 1 2022 test.txt
5245 -rw-r--r-- 1 bluen bluen 0 Jun 23 08:30 test2.txt
5501 -rw-r--r-- 1 bluen bluen 10240 Jun 23 08:31 tests.tar
- 先頭の数字列がiノード番号
ハードリンク
- 見た目上のファイルは別だが、iノードが共通しており、同一のファイル実体につながっている
- 部屋(ファイル実体)は1つで、ドア(ハードリンク)が複数あるイメージ
- ハードリンクはファイル実体とつながりがある
$ touch linksource.txt
$ ls
linksource.txt
$ ln linksource.txt hardlink1.txt
$ ls
hardlink1.txt linksource.txt
$ cat hardlink1.txt
$ vi hardlink1.txt
$ cat hardlink1.txt
tekesaku
$ cat linksource.txt
tekesaku
$ ls -li
5496 -rw-r--r-- 2 bluen bluen 9 Jun 23 18:18 hardlink1.txt
5496 -rw-r--r-- 2 bluen bluen 9 Jun 23 18:18 linksource.txt
851 lrwxrwxrwx 1 bluen bluen 14 Jun 23 18:21 symboliclink1.txt -> linksource.txt
- ハードリンクファイルに記載した内容が、リンク元ファイルにも変更されていることがわかる
シンボリックリンク
- 部屋(ファイル実体)は1つで、ドア(リンク元ファイル)も1つで、ドアに複数の廊下(シンボリックリンク)がつながっているイメージ
- シンボリックリンクはファイル実体と直接的なつながりはないが、リンク元ファイルを介してつながりを持っている
$ ln -s linksource.txt symboliclink1.txt
$ cat symboliclink1.txt
tekesaku
$ ls
hardlink1.txt linksource.txt symboliclink1.txt
$ ls -li
5496 -rw-r--r-- 2 bluen bluen 9 Jun 23 18:18 hardlink1.txt
5496 -rw-r--r-- 2 bluen bluen 9 Jun 23 18:18 linksource.txt
851 lrwxrwxrwx 1 bluen bluen 14 Jun 23 18:21 symboliclink1.txt -> linksource.txt
シンボリックリンクのコピー
$ cp -d symboliclink1.txt symboliclink2.txt
$ cat symboliclink2.txt
tekesaku
シンボリックリンクのリンク元ファイルを削除した場合の挙動
$ rm linksource.txt
$ ls
hardlink1.txt symboliclink1.txt symboliclink2.txt
$ cat symboliclink1.txt
cat: symboliclink1.txt: No such file or directory
$ cat hardlink1.txt
tekesaku
-
シンボリックリンクのリンク元ファイルを削除すると、シンボリックリンクファイルは消えないが参照先ファイルが消えているので、
cat: symboliclink1.txt: No such file or directoryというエラーになる。 -
一方、ハードリンクファイルは引き続きファイルの実体を見ることができる。
-
ls -lで権限の最初の桁、ないし、ls -Fでシンボリックリンクファイルを見分けることができる。 -
ls -iで表示されるいノード番号が一致していればハードリンク。
$ ls -i
851 hardlink.txt 851 source.txt
正規表現
*: 0文字以上の文字列にマッチ?: 任意の1文字にマッチ[]:[]にあるどれか1文字にマッチ{,}:,で区切られたいずれかの文字列にマッチ
ファイルの検索
- find
$ find /home -name "*".txt
/home/hoge/hardlink.txt
/home/hoge/source.txt
-
locate
- ファイル名DB(リスト)をもとに、ファイルを検索する
- 事前にupdatedbコマンドを使って、データベースを作成しておく
- データベースを作るのに時間がかかる
-
which
- コマンドファイルを検索して、絶対パスを表示
$ which find
/usr/bin/find
- whereis
- コマンドファイルを検索して、パスを表示
$ whereis find
find: /usr/bin/find /usr/share/man/man1/find.1.gz /usr/share/info/find.info-1.gz /usr/share/info/find.info-2.gz /usr/share/info/find.info.gz
- type
$ type find
find is hashed (/usr/bin/find)
$ type cd
cd is a shell builtin
$ type whereis
whereis is hashed (/usr/bin/whereis)
$ type locate
locate is /usr/bin/locate
$ type ls
ls is aliased to `ls --color=auto'
ディレクトリ構造
- Linuxディストリビューションでは、ファイルシステム階層標準 Filesystem Hierarchy Standard (FHS) としてディレクトリ構造が標準化されている
- rootディレクトリ配下に、ディレクトリがぶら下がっている
基本的なディレクトリ
- /bin
- 一般ユーザが実行可能案コマンドが配置されている
- /sbin
- rootユーザ のみが実行可能なコマンドが配置されている
- /etc
- 設定情報やスクリプトファイルが配置されている
- /dev
- /lib
- ライブラリファイル
- /media
- 外付けHDD、USB
- /proc
- システム状態がファイルの形をとって保管される
- /home
- ユーザごとのホームディレクトリ
- /root
- rootユーザのディレクトリ
- /boot
- カーネルなどの起動に必要なファイルが配置
- /tmp
- 一時的に必要なファイルの置き場
- /var
- ログファイルなど頻繁に更新されるファイルが配置
- /usr コマンドやライブラリが配置
- /usr/bin
- ユーザが一般的に使うコマンドが配置
- /usr/sbin
- システムを管理するためのコマンドが配置
- /usr/lib
- /usr/local
- /usr/share/man
- /usr/bin
「/bin」「/usr/bin」「/usr/local/bin」ディレクトリの使い分け
参考:https://linuc.org/study/knowledge/544/
-
/bin
- シングルユーザモードでも利用できるコマンドを配置
- シングルユーザモードは、基本的にOSが壊れて正常に起動できないなど非常時に利用するもの
- /bin には、ごく基本的かつ非常時に利用するコマンドが置かれる
-
/usr/bin
-
/usr/local/bin
シェルの種類
シェルの基本操作
- Ctrl + A
- Ctrl + E
- Ctrl + D
- Ctrl + H
- Ctrl + L
- Ctrl + C
- Ctrl + S
- Ctrl + Q
- Ctrl + Z
パッケージ
パッケージの問題点
- 依存:パッケージAのファイルをパッケージBが利用している
- 競合:パッケージAのファイルと、パッケージBのファイルが共存できない(バージョン違いなど)
⇒ パッケージ管理システム
パッケージ管理システム
apt
- Advanced Packaging Tool
- 多機能で煩雑なdpkgにかわり、操作性を向上させたパッケージ管理システム
apt-getコマンド
- パッケージのインストールや削除にはroot権限が必要
サブコマンド
- autoclean
- ダウンロードしたパッケージの元ファイル(アーカイブ)を削除
- autoremove
- 使われていないパッケージを削除
- download
- インストールはせず、ダウンロードするだけ
- install
- インストールまたはアップグレードする
- remove
- パッケージをアンインストール
- update
- リポジトリから最新の情報を取得
- dist-upgarade
- パッケージを最新にアップグレード
- upgrade
- 全てのパッケージのなかから、パッケージ追加削除が必要のないものをアップグレード
apt-getのリポジトリ
$ cat /etc/apt/sources.list.d/ondrej-ubuntu-php-jammy.list
deb https://ppa.launchpadcontent.net/ondrej/php/ubuntu/ jammy main
# deb-src https://ppa.launchpadcontent.net/ondrej/php/ubuntu/ jammy main
debはパッケージファイルを取得する、という意味deb-srcだとソースファイルを取得する、という意味http...は、リポジトリのURIjammyの部分は、バージョン名mainは公式がサポートしているという意味universeだとコミュニティによるメンテナンスmultiverseは制限がある等
$ cat /etc/apt/sources.list
# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://archive.ubuntu.com/ubuntu/ jammy main restricted
# deb-src http://archive.ubuntu.com/ubuntu/ jammy main restricted
## Major bug fix updates produced after the final release of the
## distribution.
deb http://archive.ubuntu.com/ubuntu/ jammy-updates main restricted
# deb-src http://archive.ubuntu.com/ubuntu/ jammy-updates main restricted
## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team. Also, please note that software in universe WILL NOT receive any
## review or updates from the Ubuntu security team.
deb http://archive.ubuntu.com/ubuntu/ jammy universe
# deb-src http://archive.ubuntu.com/ubuntu/ jammy universe
deb http://archive.ubuntu.com/ubuntu/ jammy-updates universe
# deb-src http://archive.ubuntu.com/ubuntu/ jammy-updates universe
## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team, and may not be under a free licence. Please satisfy yourself as to
## your rights to use the software. Also, please note that software in
## multiverse WILL NOT receive any review or updates from the Ubuntu
## security team.
deb http://archive.ubuntu.com/ubuntu/ jammy multiverse
# deb-src http://archive.ubuntu.com/ubuntu/ jammy multiverse
deb http://archive.ubuntu.com/ubuntu/ jammy-updates multiverse
# deb-src http://archive.ubuntu.com/ubuntu/ jammy-updates multiverse
## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.
deb http://archive.ubuntu.com/ubuntu/ jammy-backports main restricted universe multiverse
# deb-src http://archive.ubuntu.com/ubuntu/ jammy-backports main restricted universe multiverse
deb http://security.ubuntu.com/ubuntu/ jammy-security main restricted
# deb-src http://security.ubuntu.com/ubuntu/ jammy-security main restricted
deb http://security.ubuntu.com/ubuntu/ jammy-security universe
# deb-src http://security.ubuntu.com/ubuntu/ jammy-security universe
deb http://security.ubuntu.com/ubuntu/ jammy-security multiverse
# deb-src http://security.ubuntu.com/ubuntu/ jammy-security multiverse
apt-cacheコマンド
- パッケージの情報を調べたり検索する
サブコマンド
- search
- 指定したキーワードが含まれたパッケージを検索
- show
- 指定したパッケージの情報を表示
- shopkg
- 指定したパッケージの詳細情報を表示
- depends
- 指定したパッケージの依存関係を表示
$ apt-cache depends tree
tree
Depends: libc6
apt-fileコマンド
- 指定したファイルを含むパッケージを検索する
- 使用する前にインストールが必要
apt install apt-file apt-file updateを事前に実行する
$ apt-file search "/usr/bin/sudo"
sshuttle: /usr/bin/sudoers-add
sudo: /usr/bin/sudo
sudo: /usr/bin/sudoedit
sudo: /usr/bin/sudoreplay
sudo-ldap: /usr/bin/sudo
sudo-ldap: /usr/bin/sudoedit
sudo-ldap: /usr/bin/sudoreplay
dpkg
- インストールは行えるが、ダウンロードは行えない
$ sudo dpkg -iE tree_1.7.0-5_amd64.deb
オプション
- -i
- パッケージをインストール
- -r
- 設定ファイルは残して、パッケージをアンインストール
- -P
- 設定ファイルも含めて、完全にアンインストール
- -l
- インストール済みのパッケージを表示
- -L
- 指定したパッケージからインストールされたファイルを一覧表示
- -S
- 指定したファイルがどのパッケージからインストールされたか
$ dpkg -l
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name Version Architecture Description
+++-====================================-============================================-============-====================>
ii acl 2.3.1-1 amd64 access control list >
ii adduser 3.118ubuntu5 all add and remove users>
ii adwaita-icon-theme 41.0-1ubuntu1 all default icon theme o>
ii alsa-topology-conf 1.2.5.1-2 all ALSA topology config>
ii alsa-ucm-conf 1.2.6.3-1ubuntu1.6 all ALSA Use Case Manage>
ii apache2 2.4.52-1ubuntu4.5 amd64 Apache HTTP Server
ii apache2-bin 2.4.52-1ubuntu4.5 amd64 Apache HTTP Server (>
ii apache2-data 2.4.52-1ubuntu4.5 all Apache HTTP Server (>
ii apache2-utils 2.4.52-1ubuntu4.5 amd64 Apache HTTP Server (>
ii apparmor 3.0.4-2ubuntu2.2 amd64 user-space parser ut>
ii apport 2.20.11-0ubuntu82.5 all automatically genera>
ii apport-symptoms 0.24 all symptom scripts for >
ii apt 2.4.9 amd64 commandline package >
ii apt-file 3.2.2 all search for files wit>
ii apt-utils 2.4.9 amd64 package management r>
ii aspell 0.60.8-4build1 amd64 GNU Aspell spell-che>
ii aspell-en 2018.04.16-0-1 all English dictionary f>
ii at-spi2-core 2.44.0-3 amd64 Assistive Technology>
...
$ dpkg -L acl
/.
/bin
/bin/chacl
/bin/getfacl
/bin/setfacl
/usr
/usr/share
/usr/share/doc
/usr/share/doc/acl
/usr/share/doc/acl/copyright
/usr/share/man
/usr/share/man/man1
/usr/share/man/man1/chacl.1.gz
/usr/share/man/man1/getfacl.1.gz
/usr/share/man/man1/setfacl.1.gz
/usr/share/man/man5
/usr/share/man/man5/acl.5.gz
/usr/share/doc/acl/changelog.Debian.gz
$ dpkg -S /usr/share/doc/acl
acl: /usr/share/doc/acl
$ dpkg -s acl
Package: acl
Status: install ok installed
Priority: optional
Section: utils
Installed-Size: 200
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Architecture: amd64
Multi-Arch: foreign
Version: 2.3.1-1
Depends: libacl1 (= 2.3.1-1), libc6 (>= 2.34)
Description: access control list - utilities
This package contains the getfacl and setfacl utilities needed for
manipulating access control lists. It also contains the chacl IRIX
compatible utility.
Original-Maintainer: Guillem Jover <guillem@debian.org>
Homepage: https://savannah.nongnu.org/projects/acl/
yum
# yum info bash
Last metadata expiration check: 0:02:34 ago on Sat 24 Jun 2023 10:54:13 AM JST.
Installed Packages
Name : bash
Version : 5.1.8
Release : 4.el9
Architecture : x86_64
Size : 7.4 M
Source : bash-5.1.8-4.el9.src.rpm
Repository : @System
From repo : anaconda
Summary : The GNU Bourne Again shell
URL : https://www.gnu.org/software/bash
License : GPLv3+
Description : The GNU Bourne Again shell (Bash) is a shell or command language
: interpreter that is compatible with the Bourne shell (sh). Bash
: incorporates useful features from the Korn shell (ksh) and the C shell
: (csh). Most sh scripts can be run by bash without modification.
Available Packages
Name : bash
Version : 5.1.8
Release : 6.el9
Architecture : x86_64
Size : 1.7 M
Source : bash-5.1.8-6.el9.src.rpm
Repository : baseos
Summary : The GNU Bourne Again shell
URL : https://www.gnu.org/software/bash
License : GPLv3+
Description : The GNU Bourne Again shell (Bash) is a shell or command language
: interpreter that is compatible with the Bourne shell (sh). Bash
: incorporates useful features from the Korn shell (ksh) and the C shell
: (csh). Most sh scripts can be run by bash without modificationp
rpm
- Red Hatが開発したパッケージ管理システム
$ rpm -qa
bash-5.1.8-4.el9.x86_64
...
オプション
- -i
- パッケージをインストール
- -U
- アップグレードまたはインストール
- -F
- アップグレード
- -e
- アンインストール
- -q
- 調べる系、オプションと併用
- -qa
- インストールされているパッケージを一覧表示
デバイス管理
-
ハードウェアはデバイスファイルを通じて管理する
-
デバイスファイルは
/dev配下に保存される -
デバイスファイルは
udevという仕組みで自動的に作成される -
ハードウェア情報は
/procに置かれている -
/sysには、システム情報にアクセスできるファイルが置かれている -
/proc/cpuinfo- CPUの情報
-
/proc/interrupts- 割り込み処理に関する情報
-
/proc/ioports- デバイス間で通信を行うためのアドレス情報
-
/proc/meminfo- メモリ情報
-
/proc/swaps -
/proc/bus/usb/デバイス名- USBデバイス情報
-
/proc/bus/pci/デバイス名
デバイス情報の参照
- cat /proc/cpuinfo
- lscpu
- lspci
- lsusb
- lsmem
udev
udevの仕組み
- デバイスを接続する
- カーネルが
/sysディレクトリにデバイス情報を作成する udevデーモンが、/devディレクトリにデバイスファイルを作成する- デバイス情報が、D-Busという仕組みでアプリケーションに伝えられる
ハードディスクのパーティション
パーティションの種類
LVM
- Logical Volume Manager
- 物理的に異なるボリューム(HDDなど)を仮想的に1つのボリュームとして管理する
- 論理ボリュームはサイズを変更したり、他のディスクに移動できる。
- スナップショットで容易にバックアップが作成できる
デバイスファイル
- 1番目のHHD(SSD, USB):
/dev/sda - 2番目のHHD:
/dev/sdb - 3番目のHHD:
/dev/sdc - 4番目のHHD:
/dev/sdd - 1番目のCD/DVDドライブ:
/dev/sr0 - 1番目のテープドライブ:
/dev/st0
パーティションの操作
fdiskコマンド
$ sudo fdisk -l
[sudo] password for bluen:
Disk /dev/ram0: 64 MiB, 67108864 bytes, 131072 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk /dev/ram1: 64 MiB, 67108864 bytes, 131072 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk /dev/ram2: 64 MiB, 67108864 bytes, 131072 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
<中略>
Disk /dev/sda: 363.32 MiB, 380968960 bytes, 744080 sectors
Disk model: Virtual Disk
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk /dev/sdb: 1 GiB, 1073745920 bytes, 2097160 sectors
Disk model: Virtual Disk
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk /dev/sdc: 1 TiB, 1099511627776 bytes, 2147483648 sectors
Disk model: Virtual Disk
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk /dev/sdd: 1 TiB, 1099511627776 bytes, 2147483648 sectors
Disk model: Virtual Disk
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
$ sudo fdisk /dev/sdd
Welcome to fdisk (util-linux 2.37.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
This disk is currently in use - repartitioning is probably a bad idea.
It's recommended to umount all file systems, and swapoff all swap
partitions on this disk.
The device contains 'ext4' signature and it will be removed by a write command. See fdisk(8) man page and --wipe option for more details.
Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x0ef9f134.
Command (m for help): m
Help:
DOS (MBR)
a toggle a bootable flag
b edit nested BSD disklabel
c toggle the dos compatibility flag
Generic
d delete a partition
F list free unpartitioned space
l list known partition types
n add a new partition
p print the partition table
t change a partition type
v verify the partition table
i print information about a partition
Misc
m print this menu
u change display/entry units
x extra functionality (experts only)
Script
I load disk layout from sfdisk script file
O dump disk layout to sfdisk script file
Save & Exit
w write table to disk and exit
q quit without saving changes
Create a new label
g create a new empty GPT partition table
G create a new empty SGI (IRIX) partition table
o create a new empty DOS partition table
s create a new empty Sun partition table
Command (m for help): q
パーティションのテーブル
- Master Boot Record
- BIOSシステムで用いられるパーティションテーブル
- マザーボードのブートローダを起動した後に最初に読み込まれる
- 扱える容量は2TB
- 作成できる基本パーティションは4個
GUID パーティションテーブル
ファイルシステム
種類
Btrfs
- 仮想ボリュームを作成できる(ストレージプール)
- 複数の物理ボリュームをまたがった1つのファイルシステムを作成できる(マルチデバイスファイルシステム)
- スナップショットによるバックアップ
- ファイルシステムを分割してサブボリューム作成
ファイルシステムのマウント
# mount -t ext4 /dev/sdc1 /data
- マウントしたいファイルシステム(+デバイス)とマウントポイントを指定する
- mountコマンドは、rootユーザーしか行えない
- デバイスまたはマウントポイントを指定しない場合、
/etc/fstabファイルが参照される
ネットワークの設定
設定ファイル
/etc/hostname- ホスト名を記述
/etc/hosts- ホスト名とIPアドレスの対応を記述
$ cat /etc/hostname
DESKTOP-7IGG3DT
$ cat /etc/hosts
# This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf:
# [network]
# generateHosts = false
127.0.0.1 localhost
127.0.1.1 DESKTOP-7IGG3DT. DESKTOP-7IGG3DT
192.168.10.121 host.docker.internal
192.168.10.121 gateway.docker.internal
127.0.0.1 kubernetes.docker.internal
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
nmcli コマンド
- NetworkManagerを介してネットワークを管理するコマンド
- NetworkManagerはRedHat系で採用されているサブシステム
$ nmcli general status
STATE CONNECTIVITY WIFI-HW WIFI WWAN-HW WWAN
disconnected unknown enabled enabled enabled enabled
$ nmcli general hostname
DESKTOP-7IGG3DT
$ nmcli networking connectivity
unknown
$ nmcli radio wifi
enabled
$ nmcli radio wwan
enabled
$ nmcli device
DEVICE TYPE STATE CONNECTION
virbr0 bridge unmanaged --
eth0 ethernet unmanaged --
lo loopback unmanaged --
$ nmcli device show eth0
GENERAL.DEVICE: eth0
GENERAL.TYPE: ethernet
GENERAL.HWADDR: 00:15:5D:CF:DB:33
GENERAL.MTU: 1500
GENERAL.STATE: 10 (unmanaged)
GENERAL.CONNECTION: --
GENERAL.CON-PATH: --
WIRED-PROPERTIES.CARRIER: on
IP4.ADDRESS[1]: 172.27.63.153/20
IP4.GATEWAY: 172.27.48.1
IP4.ROUTE[1]: dst = 0.0.0.0/0, nh = 172.27.48.1, mt = 0
IP4.ROUTE[2]: dst = 172.27.48.0/20, nh = 0.0.0.0, mt = 0
IP6.ADDRESS[1]: fe80::215:5dff:fecf:db33/64
IP6.GATEWAY: --
IP6.ROUTE[1]: dst = fe80::/64, nh = ::, mt = 256
$ nmcli device wifi list
$ nmcli connection show
ip コマンド
$ ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000
link/ether 00:15:5d:cf:db:33 brd ff:ff:ff:ff:ff:ff
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default qlen 1000
link/ether 52:54:00:a4:b0:85 brd ff:ff:ff:ff:ff:ff
$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:15:5d:cf:db:33 brd ff:ff:ff:ff:ff:ff
inet 172.27.63.153/20 brd 172.27.63.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::215:5dff:fecf:db33/64 scope link
valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
link/ether 52:54:00:a4:b0:85 brd ff:ff:ff:ff:ff:ff
inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
valid_lft forever preferred_lft forever
$ ip route
default via 172.27.48.1 dev eth0 proto kernel
172.27.48.0/20 dev eth0 proto kernel scope link src 172.27.63.153
192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1 linkdown
ifconfig コマンド
$ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.27.63.153 netmask 255.255.240.0 broadcast 172.27.63.255
inet6 fe80::215:5dff:fecf:db33 prefixlen 64 scopeid 0x20<link>
ether 00:15:5d:cf:db:33 txqueuelen 1000 (Ethernet)
RX packets 6160 bytes 8986715 (8.9 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1598 bytes 146271 (146.2 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:a4:b0:85 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ifup/ifdown コマンド
- ネットワークインターフェイスを有効・無効にする
セキュリティ
不要なSUID/SGIDの削除
- findコマンドで、SUID/SGIDが設定されているファイルを検索する
- 不要なSUID/SGIDが付与されていたら、
chmod u-s <ファイル名>でパーミッションを取り上げる。SGIDの場合は、g-s
$ sudo find / -perm -u+s -ls
1666 32 -rwsr-xr-x 1 root root 30872 Feb 26 2022 /usr/bin/pkexec
1817 56 -rwsr-xr-x 1 root root 55672 Feb 21 2022 /usr/bin/su
1818 228 -rwsr-xr-x 1 root root 232416 Apr 4 03:00 /usr/bin/sudo
1289 72 -rwsr-xr-x 1 root root 72712 Nov 24 2022 /usr/bin/chfn
1893 36 -rwsr-xr-x 1 root root 35192 Feb 21 2022 /usr/bin/umount
1596 40 -rwsr-xr-x 1 root root 40496 Nov 24 2022 /usr/bin/newgrp
1580 48 -rwsr-xr-x 1 root root 47480 Feb 21 2022 /usr/bin/mount
1635 60 -rwsr-xr-x 1 root root 59976 Nov 24 2022 /usr/bin/passwd
1412 36 -rwsr-xr-x 1 root root 35200 Mar 23 2022 /usr/bin/fusermount3
1295 44 -rwsr-xr-x 1 root root 44808 Nov 24 2022 /usr/bin/chsh
1433 72 -rwsr-xr-x 1 root root 72072 Nov 24 2022 /usr/bin/gpasswd
26960 60 -rwsr-xr-x 1 root root 60360 Sep 5 2021 /usr/bin/inetutils-traceroute
26010 416 -rwsr-xr-- 1 root dip 424512 Feb 25 2022 /usr/sbin/pppd
不要なポートを閉める
引用:https://linuc.org/study/knowledge/356/
実は、ポートの塞ぎ方については大きく分けて二つの方法があります。
一つは、「アプリケーション側の対応を行う」です。そもそも、「ポートが開く」とはどういうことなのかを考えてみましょう。たとえば、ApacheなどのWebサーバアプリケーションを起動すると、(特別な設定をしない限り)Well known portsである80番ポートが開き、80番ポートでの通信の待ちうけを開始します。
これを考えると、サーバアプリケーションを停止すれば、自動的にポートが閉じます。「余計なアプリケーションを起動してはいけない」というお話しにつながりますね。
もう一つは、「通信を制限する」という方法です。代表的なのが「パケットフィルタリング」という手法です。パケットフィルタリングを利用すると、特定のポートの通信、特定のIPアドレスの通信をシャットアウトすることができます。Linuxでは「iptables」というコマンドによってパケットフィルタリングを行うことができます。iptablesコマンドを利用すると、どのような通信を遮断するのかなどを細かく指定できるので、「あるホストから、特定のポートを遮断する」といったこともできます。
どのポートが開いているか?を調べるには、「netstatコマンド」「lsofコマンド」「nmapコマンド」などのコマンドがあります。ここではそれぞれのコマンドについての詳細は割愛しますが、netstatとlsofは「自分がどのポートを開けているか?」を知るために、nmapは「他ホストがどのポートを開けているか?」を知るために使われます。パケットフィルタリングを施した場合、netstatやlsofではポートが空いているように見える場合もあるので注意が必要になります。
その他参考:
https://network-beginners-handbook.com/netstat/
https://milestone-of-se.nesuke.com/sv-basic/linux-basic/ss-netstat/
https://qiita.com/s-yoshida/items/38e9830bd84a3bbe25b9
https://www.linuxmaster.jp/linux_skill/2009/02/linux-4.html
不要なサービスの停止
自動起動の状態
- 次のコマンドで、systemdパス内(
/etc/systemd/system/または/lib/systemd/system/)の利用可能なユニットをすべて表示し、自動起動状態を確認する systemctl list-unit-files --type=service- 不要なサービスが自動起動になっている場合は、
systemctl disable <ユニット名>で自動起動を無効にする
$ systemctl list-unit-files --type=service
UNIT FILE STATE VENDOR PRESET
apache-htcacheclean.service disabled enabled
apache-htcacheclean@.service disabled enabled
apache2.service disabled enabled
apache2@.service disabled enabled
apparmor.service enabled enabled
<中略>
wpa_supplicant@.service disabled enabled
x11-common.service masked enabled
201 unit files listed.
サービスの状態確認
- systemdがロードしようとしたユニットをすべて表示し、状態を確認する
- ロードしようとしなかったものは表示されない
systemctl list-units --all --type=service
$ sudo systemctl list-units --type=service
UNIT LOAD ACTIVE SUB DESCRIPTION >
apport.service loaded active exited LSB: automatic crash report generation
blk-availability.service loaded active exited Availability of block devices
console-getty.service loaded active running Console Getty
<中略>
wpa_supplicant.service loaded active running WPA supplicant
LOAD = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB = The low-level unit activation state, values depend on unit type.
45 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.
TCP Wrappersによるアクセス制御
TCP Wrappersはtcpdで動いているらしいが、tcpdが動いてなくても、sshdでも使用できる模様。
/etc/hosts.allowと/etc/hosts.denyに設定を入れることでアクセス制御ができる。
$ cat /etc/hosts.deny
# /etc/hosts.deny: list of hosts that are _not_ allowed to access the system.
# See the manual pages hosts_access(5) and hosts_options(5).
#
# Example: ALL: some.host.name, .some.domain
# ALL EXCEPT in.fingerd: other.host.name, .other.domain
#
# If you're going to protect the portmapper use the name "rpcbind" for the
# daemon name. See rpcbind(8) and rpc.mountd(8) for further information.
#
# The PARANOID wildcard matches any host whose name does not match its
# address.
#
# You may wish to enable this to ensure any programs that don't
# validate looked up hostnames still leave understandable logs. In past
# versions of Debian this has been the default.
# ALL: PARANOID
sshd: ALL
# ssh bluen@localhost
kex_exchange_identification: read: Connection reset by peer
Connection reset by 127.0.0.1 port 22
firewalld サービス
- firewalldはLinuxでパケットフィルタリング等のネットワーク操作を行なうソフトウェア
- https://knowledge.sakura.ad.jp/22269/#Firewalld
- https://access.redhat.com/documentation/ja-jp/red_hat_enterprise_linux/7/html/security_guide/sec-using_firewalls#sec-Zones
$ sudo apt install firewalld
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed
<中略>
Processing triggers for dbus (1.12.20-2ubuntu4.1) ...
Processing triggers for libc-bin (2.35-0ubuntu3.1) ...
$ systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2023-06-30 08:44:38 JST; 1min 3s ago
Docs: man:firewalld(1)
Main PID: 2096 (firewalld)
Tasks: 2 (limit: 4559)
Memory: 30.0M
CGroup: /system.slice/firewalld.service
└─2096 /usr/bin/python3 /usr/sbin/firewalld --nofork --nopid
$ cat /lib/systemd/system/firewalld.service
[Unit]
Description=firewalld - dynamic firewall daemon
Before=network-pre.target
Wants=network-pre.target
After=dbus.service
After=polkit.service
Conflicts=iptables.service ip6tables.service ebtables.service ipset.service nftables.service
Documentation=man:firewalld(1)
[Service]
ExecStart=/usr/sbin/firewalld --nofork --nopid
ExecReload=/bin/kill -HUP $MAINPID
# supress to log debug and error output also to /var/log/messages
StandardOutput=null
StandardError=null
Type=dbus
BusName=org.fedoraproject.FirewallD1
KillMode=mixed
[Install]
WantedBy=multi-user.target
Alias=dbus-org.fedoraproject.FirewallD1.service
ログイン履歴の確認
Red Hat系では/var/log/secure、Debian系では/var/log/auth.logで、SSH接続やサーバーログインのログを確認できる。
$ cat /var/log/auth.log
<中略>
Jun 30 08:17:03 <ホスト名> sshd[1450]: refused connect from 127.0.0.1 (127.0.0.1)
Jun 30 08:35:32 <ホスト名> sshd[1656]: refused connect from 127.0.0.1 (127.0.0.1)