#author("2024-02-27T06:31:38+00:00","default:mogamin","mogamin")
#author("2024-02-27T06:33:17+00:00","default:mogamin","mogamin")
* command snippet [#fdef793e]

#contents

** Windows [#d80b2e97]
→[[command snippet/windows]]

** user [#user]

*** ユーザとグループの一覧 [#rafe9a0c]

 cat /etc/passwd

 cat /etc/group

*** sudo ができる管理ユーザの追加の例 [#b41e60e7]

 sudo useradd -G adm,sudo USERNAME

*** パスワードの設定 [#q0de9365]

 sudo passwd USERNAME

かんたん。

 sudo usermod -p PASSWORD_ENCRYPTED USERNAME

自動化にはいい。PASSWORD_ENCRYPTED は

 openssl passwd

を実行し、プロンプトに沿ってパスワードを入れると出力できる。
以下は passwd と同じ動き:

 sudo usermod -p `openssl passwd` USERNAME

*** ユーザのログイン名を変更し、ホームディレクトリを引っ越す [#y9f49be4]

 sudo usermod -l CHANGE_TO -d /home/CHANGE_TO -m CHANGE_FROM

-l でログイン名変更、-d でホームディレクトリ変更、-m でホームディレクトリを変更と同時に移動する。
たぶん当然だけど CHANGE_FROM ユーザでこれを実行すべきではない。

** 今のセッションのコマンド履歴を忘れる [#l7e8322c]

 unset HISTFILE

** 文字列のハッシュ値を取得する [#gbaa248b]

  echo -n 'loremipsum' | sha256sum


** disk free [#df]

 df -h

'-h' は 61917736 を 60G とかになおしてくれる。

** parted [#parted]

パーティションの一覧表示

 parted -l

未使用スペースを含めて表示

 parted
 (parted) print free

** tar+gzip [#tar]

*** ディレクトリの圧縮 [#j6db2f31]

 tar -cvzf tgzFILE DIRCompressTo

*** ディレクトリの展開 [#gf16d085]

 tar -xvzf tgzFILE

** git [#git]

*** force pushed branch の pull [#i059c697]

 git pull --rebase

*** 直前の commit のハッシュを出力 [#n905f855]

 git rev-parse HEAD
 # b90383d07388fe8513e59a6deb1a2391146c6561

 git rev-parse --short @
 # b90383d07

 git rev-parse --short @~2
 # hash from 2 commits ago

 git rev-parse --short main
 # hash of latest commit on main

*** 直前の commit の author の修正 [#nf32094b]

 git commit --amend --author="mgmn <mgmnjp@example.com>" --no-edit

*** 直前の amend commit を取り消す (変更を維持) [#e7e9ff19]

 git reset --soft HEAD@{1}

`git reflog` した結果の `{n}` こ前に HEAD を移動している。

https://stackoverflow.com/questions/1459150/how-to-undo-git-commit-amend-done-instead-of-git-commit

*** いまのブランチ名を出力 [#gc049621]

 git rev-parse --abbrev-ref @

*** 上流が共通のよそのリポジトリのブランチに追従する [#zeb5275d]

 # ローカルのブランチを切る
 git branch yosono-fork
 
 # よその分化直前のコミットまで reset する
 git reset --hard (SHA1)
 
 # リモートの追加
 git remote add yoso https://example.com/yoso/yoso.git
 git fetch yoso
 
 # 上流ブランチの設定
 git branch --set-upstream-to yoso/master
 git pull

** ssh [#ssh]

*** 鍵ペアの生成 [#yf8ca5b2]

 ssh-keygen  # RSA3072

 ssh-keygen -t ed25519

***固まったセッションの強制終了 [#of2334f3]

 (Enter)
 ~.

** screen [#r46c236d]

*** 切断 [#h700e353]

 (Ctrl + a)
 k


** certbot [#certbot]

*** 証明書の取得 [#w3ec2f8b]

 certbot --non-interactive certonly --webroot -w /var/www/html -m "mgmnjp@example.com" --agree-tos -d "example.com" 

*** 証明書の取得 (staging server) [#w4055b4e]

 certbot --non-interactive certonly --webroot -w /var/www/html -m "mgmnjp@example.com" --agree-tos -d "example.com" --test-cert

staging server は production server よりも[[証明書の重複取得の回数制限:https://letsencrypt.org/ja/docs/rate-limits/]]がかなり[[緩和:https://letsencrypt.org/ja/docs/staging-environment/]]されている。

*** ストアしている証明書の一覧 [#t7abf0ad]

 certbot certificates

*** ストアしている証明書を削除する [#r8e6f814]

 sudo certbot delete --cert-name example.com

取得済みの証明書が「Certificate not yet due for renewal」で再取得できない場合はこれで設定を消すと通るようになる。

** openssl [#openssl]

*** 自己証明書の作成 [#x5580df6]

 openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 3650

*** RSA 鍵からパスワードを削除する [#jba2224e]

 openssl rsa -in key.pem -out key.pem

openssl rsa - Mister PKI
https://www.misterpki.com/openssl-rsa/

** curl [#curl]

*** verbose [#z85df100]

 curl -X GET -v https://...

*** 要求ヘッダを指定/追加する [#ed71acbc]

 curl --header "Accept: application/json,application/xml" https://...

 curl -H "Accept: application/json" -H "Accept-Language: ja" https://...

*** NTLM 認証をする [#gfac7aa9]

 curl --ntlm -u user:password https://...

** dig [#dig]

*** ルートサーバからの反復問い合わせとその応答をすべて表示する [#mfddbe9a]

 dig @recursive.ns.example.com mgmn.jp +trace

*** 再帰的動作を要求しない問い合わせを行う [#j86af4b8]

デフォルトでは `+recurse` を付加したときと同じ 再帰的動作を要求する問い合わせが行われる。

>&color(#006600){$ dig a mgmn.jp @authoritative.ns.example.com};~
; <<>> DiG 9.18.18-0ubuntu0.22.04.2-Ubuntu <<>> a mgmn.jp @authoritative.ns.example.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 1234
;; flags: qr aa &color(#ff0000){rd};((Recursion Desired フラグ)); QUERY: 1, ANSWER: 1, AUTHORITY: 2, ADDITIONAL: 1
;; &color(#ff0000){WARNING: recursion requested but not available};

`+norecurse` を付加すると、フルリゾルバから権威サーバに行われる問い合わせと同じように問い合わせる。
`+norecurse` を付加すると、フルリゾルバから権威サーバに行われる問い合わせと同じように、再帰的動作を要求せずに問い合わせる。
キャッシュサーバと権威サーバを兼ねているサーバ((このような共用サーバは攻撃に弱いため、いまは推奨されていない))に問い合わせるときは大切。

 dig a mgmn.jp @authoritative.ns.example.com +norecurse
 dig a mgmn.jp @authoritative.ns.example.com +norec

:thanks to |
DNSチュートリアル - JANOG53 Meeting in Hakata
https://www.janog.gr.jp/meeting/janog53/dnsbasic/

*** 回答を複数行表示する [#u1aabec0]

SOA レコードがとくに見やすくなる。

 dig mgmn.jp soa +multiline
 dig mgmn.jp soa +multi

** firewall-cmd [#firewall-cmd]

サービス名指定で許可・禁止

 sudo firewall-cmd --add-service=https --permanent

 sudo firewall-cmd --remove-service=ssh --permanent

ポート番号指定で許可・禁止

 sudo firewall-cmd --add-port=1234/tcp --permanent

 sudo firewall-cmd --remove-port=1234/tcp --permanent

** Mastodon [#mastodon]

下段は development 環境用。

*** 依存関係の更新 [#s43ac6ba]
 bundle && yarn --frozen-lockfile

*** DB のバックアップ [#eedc9a89]
 pg_dump -Fc mastodon_production -f ~/donbak/pg_backup.dump

 pg_dump -Fc mastodon_development -f ~/donbak/pg_backup.dump

*** DB のマイグレーション [#aae0edfd]
 RAILS_ENV=production bundle exec rails db:migrate

 RAILS_ENV=development bundle exec rails db:migrate

*** アセットのリビルド [#q75c2c36]
 RAILS_ENV=production bundle exec rails tmp:clear assets:clean assets:precompile

 RAILS_ENV=development bundle exec rails tmp:clear assets:clean assets:precompile

%%public/packs を消した場合は OS 再起動したほうがいいかも%%
より文明的なやり方をみつけたので mastodon-web の reload だけでよさそう

*** web と sidekiq の再起動 [#a3c93205]
 sudo systemctl daemon-reload && sudo systemctl reload mastodon-web && sudo systemctl restart mastodon-sidekiq
 # サービス稼働状態の確認
 sudo systemctl | grep mastodon

*** ユーザの追加 [#i6bdc7b6]
 RAILS_ENV=development bin/tootctl accounts create --email EMAIL --confirmed USERNAME

*** オーナユーザの追加 [#t5266537]
 RAILS_ENV=development bin/tootctl accounts create --role Owner --email EMAIL --confirmed USERNAME

*** 既存ユーザをオーナにする [#a5bd9e6d]
 RAILS_ENV=production bin/tootctl accounts modify --role Owner USERNAME

 RAILS_ENV=development bin/tootctl accounts modify --role Owner USERNAME

トップ   編集 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS