Ubuntu, ファイルシステムがマウントされていない場合に書き込みを失敗させる

Ubuntu 22.04 ext4ファイルシステム 外部ストレージなどのファイルシステムのマウントに失敗したとき、 定期実行スクリプトやDockerコンテナなどが、実際にはマウントされていないマウントポイント以下に書き込みしてしまう場合があります。 /etc/fstabからnofailの指定を外せば、マウントに失敗した場合にOSが起動しなくなることで誤った操作を防げますが、 外部ストレージと関係のない他のサービスが共存している、遠隔操作を前提とした運用をしている、などの理由で、OSは起動してほしい場合があります。 マウントポイントがext4ファイルシステムにある場合、chattrコマンドを使って、変更を禁止することができます。 /mnt/mystorageのマウントを解除した状態で、以下のように指定します。 chattr +i /mnt/mystorage これによりマウントしている間、immutable属性がなくなり、変更できるようになります。 mount /mnt/mystorage linux - Prevent the possiblity of writing data to an unmounted mount point directory - Server Fault chattr(1): change file attribs on file system - Linux man page

2023年12月9日 · aoirint

MinIO CLI(mcコマンド)でエイリアスが登録されているか確認する

$ mc --version mc version RELEASE.2023-12-02T11-24-10Z (commit-id=d920e2b34b22a15bca4cd081201d3b301c623d87) Runtime: go1.21.5 linux/amd64 Copyright (c) 2015-2023 MinIO, Inc. License GNU AGPLv3 <https://www.gnu.org/licenses/agpl-3.0.html> MinIO CLI(mcコマンド)では、サーバーURLや認証情報などをmc alias setコマンドで保存して、 設定したエイリアスを使って操作します。 mc cpやmc mirrorでは、{alias}/{bucket}/{object}のように操作対象のオブジェクトを指定します。 このときaliasが登録されていなかった場合、ファイルシステム上の相対パスとして扱われるため、意図しない操作が行われるおそれがあります。 エイリアスmyaliasが登録されているかどうかは、mc alias list {alias}コマンドの終了コードで判別できます。 終了コードが0の場合、エイリアスは存在します。終了コードが1の場合、エイリアスは存在しません。 mc alias list myalias echo $? シェルスクリプトでは、以下のようなコードを追加して、エイリアスが存在しなかった場合にスクリプトを異常終了させられます。 # set +e # check alias exists mc alias list myalias >/dev/null 2>/dev/null if [ $? != 0 ]; then echo "Error: mc alias myalias does not exist" > /dev/stderr exit 1 fi # set -e

2023年12月9日 · aoirint

Dockerコンテナ内の名前解決をプライベートIPアドレスにする

Docker Engine 24.0 Docker Compose 2.21 プライベートDNSを運用しているネットワークで、 Dockerコンテナからネットワーク内の別のホストに通信したいとき、 デフォルトではGoogle Public DNS8.8.8.8でホスト名がグローバルIPアドレスに解決されるため、 ISPやCloudflare Tunnelなどの外部を経由して通信することになり、非効率で危険な通信経路になります。 また、IPアドレスによるアクセス制限を設けている場合、 プライベートIPアドレスがソースとなる通信になるように通信経路を制御したい場合があります。 以下の設定により、コンテナ内でexample.comが192.168.0.50に解決されるようになります。 Docker sudo docker run --rm --add-host "example.com:192.168.0.50" hello-world Managing /etc/hosts - Docker run reference | Docker Docs Docker Compose services: app: image: hello-world extra_hosts: - "example.com:192.168.0.50" extra_hosts - Services top-level element | Docker Docs

2023年12月9日 · aoirint

Docker Registryをホストしてhtpasswdで認証する

registry - Official Image | Docker Hub Native basic auth - Restricting access - Deploy a registry server | CNCF Distribution htpasswdファイルの作成 htpasswdファイルを作成します。 registryイメージ(Distribution Registry)はbcrypt形式のパスワードのみサポートしているため、パスワードをbcrypt形式でハッシュ化する必要があります。 sudo apt install apache2-utils mkdir auth cd auth htpasswd -cB htpasswd myuser 永続化ディレクトリの作成 mkdir data docker-compose.ymlファイルの作成 services: registry: image: registry:2 restart: always environment: REGISTRY_AUTH: htpasswd REGISTRY_AUTH_HTPASSWD_PATH: /auth/htpasswd REGISTRY_AUTH_HTPASSWD_REALM: Registry Realm # ports: # - "0.0.0.0:5000:5000" volumes: - ./data:/var/lib/registry - ./auth:/auth コンテナの実行 sudo docker compose up -d コンテナのTCP 5000番ポートでDocker Registry HTTP APIがリッスンします。 このポート宛にCloudflaredやリバースプロキシを設定して、https://docker.example.comのようにサービスを公開します。 ...

2023年12月9日 · aoirint

Dockerコンテナのメモリ使用量を制限する

Docker Engine 24.0 Docker Compose 2.21 Docker # 100 MB sudo docker update --memory "100m" "$CONTAINER_ID" # 1 GB sudo docker update --memory "1g" "$CONTAINER_ID" # 制限を解除 sudo docker update --memory "0" "$CONTAINER_ID" Docker Compose app: image: hello-world deploy: resources: limits: memory: '1g' 参考 Update the limitation of memory/CPU for existing container in docker - Stack Overflow Runtime options with Memory, CPUs, and GPUs | Docker Docs Compose Deploy Specification | Docker Docs

2023年12月9日 · aoirint

DockerコンテナのCPU使用量を制限する

Docker Engine 24.0 Docker Compose 2.21 Docker # CPU 1個 sudo docker update --cpus "1" "$CONTAINER_ID" # CPU 0.01個(最小) sudo docker update --cpus "0.01" "$CONTAINER_ID" # 制限を解除 sudo docker update --cpus "0" "$CONTAINER_ID" Docker Compose app: image: hello-world deploy: resources: limits: cpus: '0.1' 参考 Update the limitation of memory/CPU for existing container in docker - Stack Overflow Runtime options with Memory, CPUs, and GPUs | Docker Docs Compose Deploy Specification | Docker Docs

2023年12月9日 · aoirint

Ubuntu 22.04, コマンドラインでパスワードをハッシュ化する(htpasswd bcrypt, doveadm SHA-512, openssl SHA-512)

htpasswd + bcrypt $ sudo apt install apache2-utils $ sudo apt list --installed apache2-utils apache2-utils/jammy-updates,now 2.4.52-1ubuntu4.6 amd64 [installed] $ htpasswd -nB myuser New password: password Re-type new password: password myuser:$2y$05$aw7xqaxQ207POKX8bavQleo52mb1jxRT7WenwvXqW21FA4wnygHjq password - Compute bcrypt hash from command line - Unix & Linux Stack Exchange doveadm + SHA-512 $ sudo apt install dovecot-core $ sudo apt list --installed dovecot-core dovecot-core/jammy-updates,now 1:2.3.16+dfsg1-3ubuntu2.2 amd64 [installed] $ doveadm pw -s SHA512-CRYPT Enter new password: password Retype new password: password {SHA512-CRYPT}$6$jtT1Mdke./dCtWSp$5ptqP0pgduBjHiRHCfh0nrWstAI46Jmytf88VlrJgpMsBPSNVhFG1cdgxkHVAXLporwb0d9pcYskAfFPqdtEy1 security - How to create SHA512 password hashes on command line - Unix & Linux Stack Exchange openssl + SHA-512 $ sudo apt list --installed openssl openssl/jammy-updates,jammy-security,now 3.0.2-0ubuntu1.12 amd64 [installed] $ openssl version OpenSSL 3.0.2 15 Mar 2022 (Library: OpenSSL 3.0.2 15 Mar 2022) $ openssl passwd -6 Password: password Verifying - Password: password $6$TmbOcErpIJRy.gWH$phJqAZzmdBj206D4kQEJNEq638KJyUwcnLbK61yBQeVsImjIg0SYXeClDjyMFCVCAvjqeNszhwtz1aUtTPKo70 security - How to create SHA512 password hashes on command line - Unix & Linux Stack Exchange LinuxでSHA-512のパスワードハッシュ作成方法まとめ #Python - Qiita

2023年11月1日 · aoirint

Ubuntu コマンドの実行を1度だけスケジュールする(atコマンド)

sudo apt install at コマンドは/bin/shで実行される。 次の18:00に1度だけコマンドを実行 echo 'wondershaper clear eno1' | sudo at 18:00 2023年6月29日 18:00に1度だけコマンドを実行 echo 'wondershaper clear eno1' | sudo at 18:00 2023-06-29 実行スケジュール(ジョブ一覧)を確認する sudo at -l ジョブ1番の実行スケジュールをキャンセルする sudo at -d 1 https://yuya-hirooka.hatenablog.com/entry/2021/01/22/215657

2023年6月28日 · aoirint

Ubuntu ネットワーク帯域を制限する(Wondershaper)

sudo apt install wondershaper # 上下300kbpsに制限 sudo wondershaper eno1 300 300 sudo wondershaper enp0s31f6 300 300 # 下り1000kbps、上り300kbpsに制限 sudo wondershaper eno1 1000 300 sudo wondershaper enp0s31f6 1000 300 # 帯域制限を解除 sudo wondershaper clear eno1 sudo wondershaper clear enp0s31f6 https://askubuntu.com/questions/20872/how-do-i-limit-internet-bandwidth rootユーザで/bin/shによるコマンド実行をスケジュール(atコマンド) echo "wondershaper eno1 300 300" | sudo at 18:00 2023-06-29 echo "wondershaper clear eno1" | sudo at 2:00 2023-06-29

2023年6月28日 · aoirint

Ubuntu ネットワーク通信量をターミナルからグラフで見る(Speedometer)

Ubuntu Server 22.04 sudo apt install speedometer 通信量を見たいネットワークデバイスの名前を調べる(eno1、enp0s31f6、enp3s0、eth0、wlan0など)。 ip addr eno1を目的のデバイスの名前に置き換えて、以下のコマンドを実行する。 speedometer -l -r eno1 -t eno1 -m $(( 1024 * 1024 * 3 / 2 )) # other examples speedometer -l -r enp0s31f6 -t enp0s31f6 -m $(( 1024 * 1024 * 3 / 2 )) speedometer -l -r enp3s0 -t enp3s0 -m $(( 1024 * 1024 * 3 / 2 )) speedometer -l -r eth0 -t eth0 -m $(( 1024 * 1024 * 3 / 2 )) speedometer -l -r wlan0 -t wlan0 -m $(( 1024 * 1024 * 3 / 2 )) 参考 networking - How to display network traffic in the terminal? - Ask Ubuntu

2023年5月19日 · aoirint