動かざることバグの如し

近づきたいよ 君の理想に

Linuxでルーティングの設定が正しいか確認する方法

環境

やりたいこと

WireGuardでスタティックルーティング設定したのに全然pingが通らなくて困った

thr3a.hatenablog.com

結果から言うと受信側のルーティングの話で送信側は正しかったわけだが、それを確認するためのコマンドがあったのでメモ

$ ip route get 192.168.16.99
192.168.16.99 via 10.0.0.100 dev wg0 src 10.0.0.1 uid 0
    cache

当然認知していないルーティングの場合はデフォルトゲートウェイを通ろうとする

# ip route get 192.168.17.1
192.168.17.1 via 139.162.79.1 dev eth0 src 139.162.79.161 uid 0
    cache

参考リンク

RaspiでWireGuard VPN環境作ったのに外部から他のLANIPにアクセスできない件

状況

家の光回線が固定IPでないためポート開放して自宅サーバー、ってのができない

そこで固定IPを持つVPSサーバーを契約 WireGuardのVPNサーバーを設定

自宅にLANIPをもたせたRaspiを構築 WireGuardのクライアントインストールしてVPNの接続までは成功

だが

  • VPSサーバー → RaspiのWireGuard専用プライベートIP
  • VPSサーバー → Raspiの自宅ローカルIP

pingは通るが

  • VPSサーバー → 自宅のルーターのローカルIP(192.168.1.1とか)
  • VPSサーバー → 自宅のNASのローカルIP

pingが通らない

ローカルIPまでは到達してるが、その先の自宅ローカルネットワーク越しで通信ができない

解決策

以下のコマンドをRaspi上で実行する

iptables -t nat -A POSTROUTING -s 10.0.0.0/24 -o wlan0 -j MASQUERADE
iptables -t nat -A POSTROUTING -s WireGuard専用プライベートネットワーク -o RaspiのNIC -j MASQUERADE
  • -t nat natテーブルはNetwork Address Translationの通りネットワークアドレス変換を行う。
  • -A POSTROUTING POSTROUTINGチェインに新しいルールを追加 POSTROUTINGチェインとはルーティングの後に実行される送信時に送信元アドレス/ポートを変換するチェイン
  • -s パケットの送信元
  • -o パケットを送信するインタフェース
  • -j jampの略 MASQUERADEはパケットの送信元アドレスを修正する

WireGuardの起動時に自動でコマンド実行してほしいのでRaspiの設定は以下になった

[Interface]
PrivateKey = xxx
Address = 10.0.0.100/24
PostUp = iptables -t nat -A POSTROUTING -s 10.0.0.0/24 -o wlan0 -j MASQUERADE
PostDown = iptables -t nat -D POSTROUTING -s 10.0.0.0/24 -o wlan0 -j MASQUERADE
DNS = 94.140.14.14
MTU = 1420

[Peer]
PublicKey = xxxx
Endpoint = xxxxxx:51820
AllowedIPs = 10.0.0.0/24
PersistentKeepalive = 25

参考リンク

UbuntuにMPDをインストールしてリモート音楽再生する

環境

インストール

インストール

本来はapt get install mpdでインストールできるが、なんやかんやがあって自前ビルドしたものを使っている

thr3a.hatenablog.com

設定

# 再生したい音楽ファイルのパス
-music_directory       "/var/lib/mpd/music"
+music_directory        "/mnt/nas/MUSIC"

# デフォルトだとローカルでしか通信できないのでanyにして0.0.0.0にする
-bind_to_address       "localhost"
+bind_to_address        "any"

# エラーでうまくいかないときの調査用
-#log_level            "default"
+log_level          "verbose"

# 後述するが音楽ファイルのデータベースを自動更新するか
-#auto_update    "yes"
+auto_update    "yes"

# IPアドレスとか指定しなくてもクライアント側が自動検出してくれるシステム それくらい手動でやれ
-#zeroconf_enabled     "yes"
+zeroconf_enabled       "no"

# exception: Decoder plugin 'wildmidi' is unavailable: configuration file does not exist: /etc/timidity/timidity.cfg
# のエラーで起動しなかったので対応
+decoder {
+   plugin "wildmidi"
+   enabled "no"
+}
+

# スピーカー出力は無効化
-audio_output {
-  type        "alsa"
-  name        "My ALSA Device"
+#audio_output {
+#  type        "alsa"
+#  name        "My ALSA Device"
 #  device      "hw:0,0"  # optional
 #  mixer_type      "hardware"      # optional
 #  mixer_device    "default" # optional
 #  mixer_control   "PCM"     # optional
 #  mixer_index "0"       # optional
-}
+#}

# mpd streaming有効化
+audio_output {
+        type            "httpd"
+        name            "MPD Streaming"
+        encoder         "lame"          # optional, vorbis or lame
+        port            "6601"
+        bind_to_address "any"               # optional, IPv4 or IPv6
+#       quality         "5.0"                   # do not define if bitrate is defined
+        bitrate         "128"                   # do not define if quality is defined
+        format          "44100:16:1"
+        max_clients     "0"                     # optional 0=no limit
+        always_on "yes" # prevent MPD from disconnecting all listeners when playback is stopped
+        tags "yes" # httpd supports sending tags to listening streams.
+}

起動

設定反映

systemctl restart mpd

自動起動有効化

systemctl enable mpd

エラー対応

エラーレベルをverboseにしてにらめっこするしかない

いちいちsystemctl再起動してられるかって場合は

# mpd --no-daemon --stdout --verbose /etc/mpd.conf

でフォアグラウンドで起動するとログが全部標準出力で流れるので確認しやすい

/var/lib/mpd/playlistsがない

何故かデフォルトで作られてなかった mpdがアクセスできるようにするためにはユーザーmpd グループaudioにする必要がある

# mkdir /var/lib/mpd/playlists
#chown mpd:audio /var/lib/mpd/playlists

exception: Decoder plugin 'wildmidi' is unavailable

前述したがwildmidiとかいうデコーダプラグインが勝手に増えたが対応してないのでエラー 無効化したら起動した

Error “bind to ‘0.0.0.0:6600’ failed (continuing anyway, because binding to ‘[::]:6600’ succeeded)”

結論から言うと、問題ない

公式ドキュメントにも

This happens on Linux when /proc/sys/net/ipv6/bindv6only is disabled. MPD first binds to IPv6, and this automatically binds to IPv4 as well; after that, MPD binds to IPv4, but that fails. You can safely ignore this, because MPD works on both IPv4 and IPv6.

って書いてある。 https://mpd.readthedocs.io/en/latest/user.html#error-bind-to-0-0-0-0-6600-failed-continuing-anyway-because-binding-to-6600-succeeded

参考リンク

UbuntuでNASのsambaをマウントする

環境

やりたいこと

自宅のネットギアのNASUbuntuサーバーからマウントして読み書きしたい

やり方

CIFSとは、Common Internet File Systemの略で、Windowsのファイル共有の仕組みであるSMB(the Server Message Block)をLinuxでも使えるようなシステムらしい。

まずはインストール

apt-get install cifs-utils

いざマウント

mount -t cifs -o username=ユーザー名,password=パスワード,iocharset=utf8 //NASのIPアドレス/hoge/piyo /mnt/nas

オプションに ro をつけると読み込み専用になる

UbuntuでMPDをビルドする

環境

やりたいこと

Ubuntu公式が出しているmpdのバージョンが古くて動作しなかったので自前でビルドすることにした。日本語の記事が全然なかったのでメモ。

やり方

詳しくは公式ドキュメントを参照する

mpd.readthedocs.io

mesonのインストール

古き良きmake。。。かと思ったら違った。mesonとかいうPythonで書かれたビルドシステムらしい。GNOME界隈で人気とのこと。meson自体はpipでインストール可能。pipがそもそもインストールされてない場合は apt install python3-pip で入れる。

pip install meson==Meson 0.56.0

これでmesonコマンドが使えるようになる

ninjaのインストール

mesonはあくまでビルド補助ツールでmake的な実際のビルド作業はできない。そこでninjaというツールでビルドする。

https://github.com/ninja-build/ninja/releases からバイナリを適当にダウンロード

依存ライブラリのインストール

音楽系のビルドなので依存するライブラリパッケージが多い。ちゃんと公式ドキュメントに載ってるのでそれをインストールすればよかったのだが、見落としてたのでapt build-depで今回は一括インストールした。

apt-get build-dep mpd

ソースのダウンロード

公式サイト https://www.musicpd.org/download.html またはgithubからダウンロード 今回はgitからにした。

git clone https://github.com/MusicPlayerDaemon/MPD
cd MPD

configure

makeでいう./configure にあたる。 デフォルトだと設定ファイルが /usr/local/etc/mpd.conf になってしまうので、/etc/mpd.confにしたい場合は -Dsysconfdir='/etc'を追加する。設定ファイルの場所は meson configure output/release |grep sysconfdir で確認できる。

meson . output/release --buildtype=debugoptimized -Db_ndebug=true

いざビルド 結構時間かかる

ninja -C output/release

インストール

ninja -C output/release install

デフォルトでは /usr/local/bin/mpd にインストールされる。

確認

mpd -V
Music Player Daemon 0.24 (v0.23.6-73-ga360475c7)
Copyright 2003-2007 Warren Dukes <warren.dukes@gmail.com>
Copyright 2008-2021 Max Kellermann <max.kellermann@gmail.com>
This is free software; see the source for copying conditions.  There is NO
warranty; not even MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Database plugins:
 simple proxy upnp

Storage plugins:
 local udisks nfs curl

Neighbor plugins:
 upnp udisks

Decoders plugins:
 [mad] mp3 mp2
 [mpg123] mp3
 [vorbis] ogg oga
 [oggflac] ogg oga
 [flac] flac
 [opus] opus ogg oga
 [sndfile] wav aiff aif au snd paf iff svx sf voc w64 pvf xi htk caf sd2
 [audiofile] wav au aiff aif
 [dsdiff] dff
 [dsf] dsf
 [hybrid_dsd] m4a
 [faad] aac
 [mpcdec] mpc
 [wavpack] wv
 [modplug] 669 amf ams dbm dfm dsm far it med mdl mod mtm mt2 okt s3m stm ult umx xm
 [mikmod] amf dsm far gdm imf it med mod mtm s3m stm stx ult uni xm
 [sidplay] sid mus str prg P00
 [wildmidi] mid
 [fluidsynth] mid
 [adplug] amd d00 hsc laa rad raw sa2
 [ffmpeg] 16sv 3g2 3gp 4xm 8svx aa3 aac ac3 adx afc aif aifc aiff al alaw amr anim apc ape asf atrac au aud avi avm2 avs bap bfi c93 cak cin cmv cpk daud dct divx dts dv dvd dxa eac3 film flac flc fli fll flx flv g726 gsm gxf iss m1v m2v m2t m2ts m4a m4b m4v mad mj2 mjpeg mjpg mka mkv mlp mm mmf mov mp+ mp1 mp2 mp3 mp4 mpc mpeg mpg mpga mpp mpu mve mvi mxf nc nsv nut nuv oga ogm ogv ogx oma ogg omg opus psp pva qcp qt r3d ra ram rl2 rm rmvb roq rpl rvc shn smk snd sol son spx str swf tak tgi tgq tgv thp ts tsp tta xa xvid uv uv2 vb vid vob voc vp6 vmd wav webm wma wmv wsaud wsvga wv wve rtp:// rtsp:// rtsps://
 [gme] ay gbs gym hes kss nsf nsfe rsn sap spc vgm vgz
 [pcm]

Filters:
 libsamplerate soxr

Tag plugins:
 id3tag

Output plugins:
 shout null fifo sndio pipe alsa ao openal pulse jack httpd snapcast recorder

Encoder plugins:
 null vorbis opus lame wave flac

Archive plugins:
 [bz2] bz2
 [zzip] zip
 [iso] iso

Input plugins:
 file archive alsa qobuz curl ffmpeg nfs mms cdio_paranoia

Playlist plugins:
 extm3u m3u pls xspf asx rss soundcloud flac cue embcue

Protocols:
 file:// alsa:// cdda:// ftp:// ftps:// gopher:// hls+http:// hls+https:// http:// https:// mms:// mmsh:// mmst:// mmsu:// nfs:// qobuz:// rtmp:// rtmps:// rtmpt:// rtmpts:// rtp:// rtsp:// rtsps:// scp:// sftp:// smb:// srtp://

Other features:
 avahi dbus udisks epoll icu inotify ipv6 systemd tcp un

よいmpdライフを