Linux Tips

From misc notes
Jump to navigation Jump to search
  • たんなるメモ

いろいろお便利なコマンド

apt list '~c'
apt purge '~c'

apt --purge autoremove -y


  • たんなるメモ
npm --omit=dev --omit=optional ls --all
  • debian bookwormメモ

大きな変更。そろそろ来ると思ってたが rsyslog がなくなり systemd journal を使う。merged-/usr が必須。このあたりは時代の流れ。自分のOSSビルド環境で /bin から /usr/bin に変わったプログラムのシンボリックリンク切れが起きて結構はまったw

https://www.debian.org/releases/stable/i386/release-notes/ch-information.ja.html

python系の変更には注意して運用する。

5.2.2. python インタプリタは externally-managed としてマークされます

Debian が提供する python3 インタプリタパッケージ (python3.11 および pypy3) は、PEP-668 に従って externally-managed とマークされるようになりました。Debian で提供されるバージョンの python3-pip はこれに伴って、--break-system-packages オプションが指定されない限り、Debian の python インタプリタ環境へ手作業でのパッケージインストールを拒否します。

Debian ではパッケージ化されていない Python アプリケーション (あるいはバージョン) をインストールする必要がある場合、(pipx Debian パッケージにある) pipx を使ってのインストールを推奨します。pipx は他のアプリケーションやシステムの Python モジュールから隔離された環境をセットアップし、アプリケーションとその依存関係をその環境へインストールします。

Debian ではパッケージ化されていない Python ライブラリモジュール (あるいはバージョン) をインストールする必要がある場合、可能であれば virtualenv へインストールするのを推奨しています。virtualenv は Python 標準ライブラリモジュールの venv (python3-venv Debian パッケージ内)、あるいは Python サードパーティツールの virtualenv (virtualenv Debian パッケージにあります) で作成できます。例えば、pip install --user foo を実行するのではなく、任意の virtualenv にインストールするのに mkdir -p ~/.venvs && python3 -m venv ~/.venvs/foo && ~/.venvs/foo/bin/python -m pip install foo を実行します。

詳細については /usr/share/doc/python3.11/README.venv を参照してください。
  • avahi-browse や avahi-resolve が答え返さない問題

ホスト名に勝手にシリアル番号を付ける問題に対応した設定が原因だった。どちらが運用上いやなのかを考えて、avahi-browse や avahi-resolve は捨てて mdns-scan 使うw

勝手にシリアル番号追加問題の回避策

cache-entries-max=0
  • Ubuntu無題w
#!/bin/sh

systemctl stop snapd.socket
systemctl stop snapd
systemctl disable snapd
systemctl disable snapd.socket

systemctl stop snapd.failure.service
systemctl disable snapd.failure.service

systemctl stop snapd.snap-repair.service
systemctl disable snapd.snap-repair.service

systemctl stop snapd.mounts-pre.target
systemctl disable snapd.mounts-pre.target
  • 雑多なメモ

忘れてた。久しぶりに boot してこないと console をつなげたら...fsck中...

そのままメンテナンスモードからマルチユーザモードにしてしまったら、いわゆるデバイス番号が変わってたw UUID でマウントしてるから問題は起きなかったが、昔々大チョンボしたパターンw

おまじないは忘れずにw

tune2fs -c -1 -i 0 /dev/sda2
  • 雑多なメモ

binary file 探す。(今までは find でいろいろ駆使しながらやってたがこれがシンプルね)

grep -rIL .
  • 雑多なメモ

SSDを容量のギリギリまでつかうのなら discard 付けとけ。でないと遅杉。fstrim じゃぁ追いつかねぇw

  • make の j オプション

これにしておけば良いらしい

make -j$[$(nproc)+1]
  • smartmontools アプデ
update-smart-drivedb --trunk --no-verify

メジャーメーカーなのにまだ db にないのもあるな。(中華SSDもけっこうdbにはいってるw)

  • 雑多なメモ
pmd_set_huge: Cannot satisfy [mem 0xf8000000-0xf8200000] with a huge-page mapping due to MTRR override.

なんか kernel option に enable_mtrr_cleanup がいるっぽい。

ちょっと古いPC(Win11非対応機種の値崩れ品w)に限界までメモリー積んだ(DDR3驚くほど安いねw)ので、ちょっと古い(入手できる最新にはあげたが...)BIOSが原因っぽい。

  • BIOS update 後ブートしない

FAQらしい。EFI bootloader を再インストールする必要がある。外部メディアから resuce モードで boot してうんぬん。

https://wiki.debian.org/GrubEFIReinstall

楽な方法もあった。BIOS 関連のメニューに "Run UEFI Application" なる設定があれば、起動EFIパーティションにあるはずの grubx64.efi を指定して OS を boot して、grub-install /dev/sda, update-grub すればよい。BIOSいじってて、また boot しなくなって焦っていろいろやってみたら出来たw

  • USB 3.x のストレージなのに遅い

lsusb で確認しても 5Gbps なのでちゃんと認識はしている。5Gbps といえば ATA と遜色ないスピードなのに桁違いに遅い。いろいろホゲると... lsblk -t でみたら RQ-SIZE が 2 だ。これじゃパフォーマンスでない。美しい設定方法を探したがわからず(udev使うとかで出来るのかもしれないが)野蛮な方法で変更した。

echo 16 > /sys/block/sdX/queue/iosched/fifo_batch
echo 3 > /sys/block/sdX/queue/iosched/writes_starved
echo 5000 > /sys/block/sdX/queue/iosched/write_expire
echo 500 > /sys/block/sdX/queue/iosched/read_expire
echo 64 > /sys/block/sdX/queue/nr_requests
echo 128 > /sys/block/sdX/queue/read_ahead_kb
echo 0 > /sys/block/sdX/queue/add_random
#echo 0 > /sys/block/sdX/queue/iostats
  • disk増設した

macos で GUID なパーティション切ってたので gdisk でtype 変えた(Linux filesystem 8300) 。あとは UUID の確認は blkid

  • ionice

昨今の IO scheduler って mq-deadline らしくって、ionice が効かないとの事。また一つ賢くなったw

  • PCRE2 と PCRE3

ちょっと調べてたらビックリ。pcre3 が古いのね。pcre2 が最新だと思ってたら 3 が有ったのであれあれって調べたらなんと逆だ。perl3互換ということの3だろうが...

駄菓子菓子w 少し前の ubuntu のパッケージにリンクされているのは libpcre3-dev なのだ。 perl5 互換重要ということか... 新しめの ubuntu はほぼ pcre2 になっとる。

  • tcp_wrappers IPv6

IPv6 については CIDR 表記ができる

ALL : [3ffe:505:2:1::]/64
  • ccache 4.1 が遅い

compression がデフォで有効になっていた。非力な環境なので無効にした。

compression = false

すでに圧縮されちゃったキャッシュはこのコマンドで戻る。

ccache --recompress uncompressed

さらに、これはやりすぎかもしれないが...

sloppiness = include_file_mtime, include_file_ctime, time_macros, pch_defines, file_stat_matches, locale

やはりやりすぎだった。ある状況で間違ってビルドされた。いまは time_macros, pch_defines のみで検証中。

  • varnish 起動ミス

超ワークアラウンドだが、/etc/rc.local に書いておく

/usr/bin/install -o varnishlog -g varnish -d /run/varnish
/usr/bin/install -o varnishlog -g varnish -d /run/varnishlog
/usr/bin/install -o varnishlog -g varnish -d /run/varnishncsa
  • 壊れた symlink 一覧
find . -xtype l
  • icu めも
ICU Data Library Customizer
icupkg icudt57l.dat icudt62l.dat -w
  • めもめも
cat /sys/devices/system/cpu/vulnerabilities/l1tf
  • HTTP/2 化

hitch + varnish5 で簡単

curl -L https://packagecloud.io/varnishcache/varnish5/gpgkey | sudo apt-key add -
echo "deb https://packagecloud.io/varnishcache/varnish5/ubuntu xenial main" | sudo tee -a /etc/apt/sources.list.d/varnishcache_varnish5.list
sudo apt-get update
varnish6 はまだ安定しないので 5.x を使う

varnish の起動オプションに追加

-p feature=+http2 -p thread_pools=2 -p thread_pool_min=450 -p thread_pool_max=5000 -p syslog_cli_traffic=off -t 120

hitch.confに追加

alpn-protos = "h2, http/1.1"
  • https 化

そろそろヤバイので https 化した。現在の構成が apache2 + varnish なので、いろいろ調査した結果 hitch を使うことにした。

http://hitch-tls.org/

Ubuntu のパッケージは古かったので PPA で導入。

Ubuntu PPA aiming for latest hitch on latest ubuntu.
sudo add-apt-repository ppa:lasse-karstensen/hitch
sudo apt-get update
sudo apt-get -y install hitch

hitch + varnish + apache2 + Let's Encrypt の統合の参考はこれが良かった。

Let's Encrypt with Hitch and Varnish (CentOS7) Tutorial

Ubuntu のパッケージに letsencrypt というのがあるが、これは古くて hitch 対応してないらしいので PPA 導入。

https://launchpad.net/~certbot/+archive/ubuntu/certbot
sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
sudo apt-get -y install certbot

基本的には CentOS7 のドキュメントをなぞるのでよいが、 hitch.conf を作っておく。(手順にしたがって pem-file を追加する)

frontend = {
  host = "*"
  port = "443"
}
backend = "[127.0.0.1]:6086"    # 6086 is the default Varnish PROXY port.
workers = 2                     # number of CPU cores

backlog = 1024
keepalive = 3600
syslog = on

ciphers = "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH"

daemon = off
user = "_hitch"
group = "_hitch"

# Enable to let clients negotiate HTTP/2 with ALPN. (default off)
# alpn-protos = "h2, http/1.1"

# run Varnish as backend over PROXY; varnishd -a :80 -a localhost:6086,PROXY ..
write-proxy-v2 = on             # Write PROXY header
certbot-renew
certbot-renew.timer
が無いので renew は自分で crontab に書く
00 04 25 * * /usr/bin/certbot renew && systemctl restart hitch

certbot の --post-hook は、

--post-hook="systemctl restart hitch"

varnish で強制的に https に redirect する。

https://github.com/varnish/hitch/wiki/FAQ
https://varnish-cache.org/trac/wiki/VCLExampleRedirectInVCL

コンテンツ側の対処

https://www.mediawiki.org/wiki/Manual:$wgServer
https://ja.wordpress.org/plugins/really-simple-ssl/

HSTS

https://github.com/varnish/hitch/wiki/FAQ#hsts-with-hitch-and-varnish
A+にしたいのでやってみた

varnish h2 が安定したら HTTP/2 にする

https://packagecloud.io/varnishcache
varnish5 いれたら簡単にでけたw
  • varnish が 404 のまま
sub vcl_backend_response {
    # Don't cache 404 responses
    if ( beresp.status == 404 ) {
        set beresp.ttl = 3s;
    }
}
  • ubuntu 16.04 systemd 環境の varnish と logrotate
mkdir /run/varnish
chown varnishlog:varnish /run/varnish
Type=forking
ExecStartPre=/usr/bin/install -o varnishlog -g varnish -d /run/varnish
ExecStart=/usr/bin/varnishlog -D -a -w /var/log/varnish/varnish.log -P /run/varnish/varnishlog.pid
PIDFile=/run/varnish/varnishlog.pid
Type=forking
ExecStartPre=/usr/bin/install -o varnishlog -g varnish -d /run/varnish
ExecStart=/usr/bin/varnishncsa -D -a -w /var/log/varnish/varnishncsa.log -P /run/varnish/varnishncsa.pid
PIDFile=/run/varnish/varnishncsa.pid
  postrotate
    /bin/kill -HUP `cat /run/varnish/varnishlog.pid 2>/dev/null` 2> /dev/null || true
  endscript
  postrotate
    /bin/kill -HUP `cat /run/varnish/varnishncsa.pid 2>/dev/null` 2> /dev/null || true
  endscript


  • ccache

.bashrc に

USE_CCACHE=1
export USE_CCACHE
PATH="/usr/lib/ccache:$PATH"
export PATH
  • Fail2Ban

今更ながらですが、Fail2ban実験中。

いままで、つつかれるのは仕方がないと放置してたのですが...
  • たんなるメモ

OpenWrt / LEDE とかクロスコンパイル環境では、やたらと PATH が深くなりがち。このためにいろいろトラブルがでる。

execvp: printf: Argument list too long.
これはパス名がながくて、引数のサイズ制限をこえたということ。対処は PATH を短くするか、xargs つかってうまく逐次処理する。
getconf ARG_MAX
bad interpreter: No such file or directory
shellbang でもインタープリターのフルパス名でハマることもある。逃げ方は exec hoge 等のラッパかます等。
80桁


  • たんなるメモ
Use 'apt-get autoremove' to remove them.
  • VirtualBox + Ubuntu システム時刻

Windows のシステム時刻がローカルタイム(JST)なので、Ubuntu の /etc/default/rcS で

# assume that the BIOS clock is set to UTC time (recommended)
#UTC=yes
UTC=no
  • Ubuntu unattended-upgrades サードパーティ レポジトリの扱い

Origin と Suite を指定してくれてないものをどうすればよいのか...

Unattended-Upgrade::Allowed-Origins {
    "*:*";
};

大胆に *:* してテスト中。

    "Sensu:sensu";
    "LP-PPA-ondrej-php:${distro_codename}";
    "elastic:stable";


  • たんなるメモ
dpkg-reconfigure keyboard-configuration
  • Hey! MAN!!!

漢なら run as root B)

漢なら Disable Ubuntu command-not-found

.bashrc に

unset command_not_found_handle
  • メモリー系いろいろ
echo -17 > /proc/<PID>/oom_adj

sshd とかは起動スクリプトで -17 に設定されている

vm.overcommit_memory=2
vm.overcommit_ratio=90

overcommit_ratio は環境による。メモリー逼迫してる場合はこれやると危険かも。

  • service - run a System V init script

ふむ

man 8 service
service SCRIPT COMMAND

# service mysql restart

  • cron-apt
apt-get install cron-apt
/etc/cron-apt/config

APTCOMMAND=/usr/bin/aptitude
MAILTO="someone@somewhere"
/etc/cron-atp/action.d/5-upgrade

safe-upgrade -y -o APT::Get::Show-Upgraded=true
/etc/cron.d/cron-apt

実行時間適当に変えてくれてる
  • つかえるかな?
cpulimit
  • Cluster SSH

おもろい

Cluster SSH - Cluster Admin Via SSH
  • たんなるメモ
vm.swappiness=0
kernel.panic_on_oops=1
kernel.panic=1
mount -o barrier=0
調査中...
  • OOM Killer を避ける
# echo -17 > /proc/<pid of sshd>/oom_adj
Linux performance tuning & stabilization tips (mysqlconf2010)
デフォでなってるな
  • 127.0.1.1 の話と、localdomain の話
いろいろ厳密化してるなぁ
localhost. と hostname 名前解決の分離
Debian Reference - Network configuration
UbuntuNote - Ubuntuのメモ
めも - 127.0.1.1
Net::SMTPでSMTPのHELO/EHLOにデフォルトで不正なホスト名を使用
たぶん、この議論を引き起こした遠因はこれかな
SecuriTeam - Common DNS Misconfiguration can Lead to "same Site" Scripting
yebo blog: Same-Site Scripting
  • mount bind
ふむ リハビリ中
mount --bindが便利 - komamitsu.log
Linux/ディレクトリにディレクトリをマウントする - discypus
  • relatime
ほー知らんかった3...(かなりリハビリ必要やな)
relatimeがどこで実装されているのか調べてみた
最新のFedoraとUbuntuではrelatimeもnoatimeもいらない?
relatimeが標準に
  • ディスクI/Oとniceレベル
ほー知らんかった2...
ドライバに変数を渡すのにうってつけの方法
ほえほえ ionice
mediate disk access with ionice(1).
  • Linuxにおけるメモリの開放
ほー知らんかった...
第10回:メモリ管理で安定稼動
DB2 V9.1以降でSTMMを使用する場合のOSのメモリー関連設定の注意事項 (DM-08-009)
続 @ITのmeminfoの見方の説明が完全に間違っている件について
sysctl -w vm.drop_caches=1
  • Linux TCP Tuning
TCP Tuning Guide - Linux TCP Tuning
  • Jetty TCP Tuning
Jetty - Addressing High TCP Slot Usage
  • たんなるメモ15
わすれもの
/usr/bin/setterm -blank 0 -store > /dev/tty1
  • よけいなお世話:)
tune2fs -c -1 -i 0 デバイス

過去ログ

昔のものは、雑多なメモ