<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ja">
	<id>https://wiki.archlinux.jp/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Takemaro</id>
	<title>ArchWiki - 利用者の投稿記録 [ja]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.archlinux.jp/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Takemaro"/>
	<link rel="alternate" type="text/html" href="https://wiki.archlinux.jp/index.php/%E7%89%B9%E5%88%A5:%E6%8A%95%E7%A8%BF%E8%A8%98%E9%8C%B2/Takemaro"/>
	<updated>2026-04-12T20:28:29Z</updated>
	<subtitle>利用者の投稿記録</subtitle>
	<generator>MediaWiki 1.44.3</generator>
	<entry>
		<id>https://wiki.archlinux.jp/index.php?title=Man_%E3%83%9A%E3%83%BC%E3%82%B8&amp;diff=39996</id>
		<title>Man ページ</title>
		<link rel="alternate" type="text/html" href="https://wiki.archlinux.jp/index.php?title=Man_%E3%83%9A%E3%83%BC%E3%82%B8&amp;diff=39996"/>
		<updated>2025-03-15T16:40:33Z</updated>

		<summary type="html">&lt;p&gt;Takemaro: 未訳の部分2箇所を翻訳&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Lowercase title}}&lt;br /&gt;
[[Category:コマンドライン]]&lt;br /&gt;
[[ar:Man page]]&lt;br /&gt;
[[en:Man page]]&lt;br /&gt;
[[es:Man page]]&lt;br /&gt;
[[id:Man page]]&lt;br /&gt;
[[ko:Man page]]&lt;br /&gt;
[[pt:Man page]]&lt;br /&gt;
[[ru:Man page]]&lt;br /&gt;
[[zh-hans:Man page]]&lt;br /&gt;
{{Related articles start}}&lt;br /&gt;
{{Related|コンソールのカラー出力#man}}&lt;br /&gt;
{{Related articles end}}&lt;br /&gt;
&#039;&#039;&#039;man ページ&#039;&#039;&#039; (&amp;quot;マニュアルページ&amp;quot;の略) は Arch Linux を含む、ほとんどの主要な UNIX ライクなオペレーティングシステムで最初からインストールされているドキュメントです。man ページを表示するには {{Ic|man}} コマンドを使います。&lt;br /&gt;
&lt;br /&gt;
man ページのカバーする範囲の如何を問わず、man ページは完結型のドキュメントとして作られており、必然的に、関連する話題を述べるときに他の man ページを参照するということが控えられています。このことはハイパーリンクを使用する info ファイルと対照的であり、GNU は info ファイルで伝統的な man ページ形式を置き換えようとしています。&lt;br /&gt;
&lt;br /&gt;
== インストール ==&lt;br /&gt;
&lt;br /&gt;
Arch Linux では、{{Pkg|man-db}} が &#039;&#039;man&#039;&#039; を実装しており、&#039;&#039;man&#039;&#039; で使用されるデフォルトのページャは [[Core utilities#Essentials|less]] です。{{Pkg|mandoc}} も使用できます。&lt;br /&gt;
&lt;br /&gt;
{{Pkg|man-pages}} は Linux と POSIX.1 の両方の man pages を提供します。([https://www.kernel.org/doc/man-pages/download.html] を参照)。&lt;br /&gt;
&lt;br /&gt;
各種プロジェクトにより翻訳された man ページもあります:&lt;br /&gt;
&lt;br /&gt;
* {{Pkg|man-pages-cs}} チェコ語&lt;br /&gt;
* {{Pkg|man-pages-de}} ドイツ語&lt;br /&gt;
* {{Pkg|man-pages-es}} スペイン語&lt;br /&gt;
* {{Pkg|man-pages-fr}} フランス語&lt;br /&gt;
* {{Pkg|man-pages-it}} イタリア語&lt;br /&gt;
* {{AUR|man-pages-ja}} 日本語&lt;br /&gt;
* {{Pkg|man-pages-nl}} オランダ語&lt;br /&gt;
* {{Pkg|man-pages-pl}} ポーランド語&lt;br /&gt;
* {{Pkg|man-pages-pt_br}} ブラジルポルトガル語&lt;br /&gt;
* {{Pkg|man-pages-ro}} ルーマニア語&lt;br /&gt;
* {{AUR|man-pages-ru}} ロシア語&lt;br /&gt;
* {{AUR|man-pages-tr}} トルコ語&lt;br /&gt;
* {{Pkg|man-pages-zh_cn}} 中国語簡体字&lt;br /&gt;
* {{Pkg|man-pages-zh_tw}} 中国語繁体字&lt;br /&gt;
&lt;br /&gt;
いくつかのアプリケーションを使用して、マニュアルページを表示できます:&lt;br /&gt;
&lt;br /&gt;
* {{App|GNOME Help|Help viewer for [[GNOME]]. It can show man pages via {{ic|yelp man:&amp;lt;name&amp;gt;}} or the undocumented {{ic|Ctrl+L}} keybinding from an existing window.|https://wiki.gnome.org/Apps/Yelp|{{Pkg|yelp}}}}&lt;br /&gt;
* {{App|KHelpCenter|Application to show [[KDE]] Applications&#039; documentation. Man pages are in &#039;&#039;UNIX manual pages&#039;&#039; or by running {{ic|khelpcenter man:&amp;lt;name&amp;gt;}}.|https://userbase.kde.org/KHelpCenter|{{Pkg|khelpcenter}}}}&lt;br /&gt;
* {{App|[[Wikipedia:Konqueror|Konqueror]]|KDE file manager and web browser. It can show man pages via {{ic|man:&amp;lt;name&amp;gt;}}.|https://konqueror.org/|{{Pkg|konqueror}}}}&lt;br /&gt;
* {{App|xman|Provides a categorized look at man pages.|https://xorg.freedesktop.org/|{{pkg|xorg-xman}}}}&lt;br /&gt;
&lt;br /&gt;
== Man ページの表示 ==&lt;br /&gt;
man ページを読むには、次を入力してください:&lt;br /&gt;
&lt;br /&gt;
 $ man &#039;&#039;page_name&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
マニュアルは複数のセクションに分類されています:&lt;br /&gt;
# 一般的なコマンド&lt;br /&gt;
# システムコール (カーネルによって提供されている関数)&lt;br /&gt;
# ライブラリコール (C ライブラリの関数)&lt;br /&gt;
# 特殊ファイル (通常は {{ic|/dev}} 以下に存在します) とドライバー&lt;br /&gt;
# ファイルのフォーマットと規則&lt;br /&gt;
# ゲーム&lt;br /&gt;
# その他 (約束事など)&lt;br /&gt;
# システム管理コマンド (通常は root 権限を必要とします) とデーモン&lt;br /&gt;
&lt;br /&gt;
基本的に Man ページはページの名前で呼ばれ、括弧で囲まれたセクション番号が後ろに付きます。{{man|1|man}} と {{man|7|man}} のように、同じ名前の man ページが存在することも時々あります。この場合は、man を呼び出すときにセクション番号と man ページの名前を指定します。例:&lt;br /&gt;
&lt;br /&gt;
 $ man 5 passwd&lt;br /&gt;
&lt;br /&gt;
上記のコマンドを実行すると {{Ic|passwd}} ユーティリティではなく、{{Ic|/etc/passwd}} についての man ページが表示されます。&lt;br /&gt;
&lt;br /&gt;
または、ページ名の後にピリオドで区切ってセクション番号を付けても同じことができます:&lt;br /&gt;
&lt;br /&gt;
 $ man passwd.5&lt;br /&gt;
&lt;br /&gt;
== マニュアルの検索 ==&lt;br /&gt;
{{Ic|man}} ユーティリティを使えば man ページを表示することはできますが、そもそも読みたいマニュアルページの名前を知らない場合に問題が起こります。幸いに、{{Ic|-k}} または {{Ic|--apropos}} オプションを使うことで、キーワードを指定してマニュアルページの説明文を検索することができます。&lt;br /&gt;
&lt;br /&gt;
検索機能は専用のキャッシュを使っています。デフォルトでは、キャッシュが作成されていないために、何を検索しても&#039;&#039;該当するページは見つからない&#039;&#039;という結果になってしまいます。次を実行することでキャッシュを生成または更新することが可能です:&lt;br /&gt;
 # mandb&lt;br /&gt;
新しい man ページをインストールしたら上のコマンドを実行するようにしてください。&lt;br /&gt;
&lt;br /&gt;
キャッシュを作成したら検索をすることができます。例えば、&amp;quot;password&amp;quot; に関する man ページを検索するには:&lt;br /&gt;
&lt;br /&gt;
 $ man -k password&lt;br /&gt;
&lt;br /&gt;
または:&lt;br /&gt;
&lt;br /&gt;
 $ man --apropos password&lt;br /&gt;
&lt;br /&gt;
上記のコマンドは以下のように {{Ic|apropos}} コマンドを実行するのと同じです:&lt;br /&gt;
&lt;br /&gt;
 $ apropos password&lt;br /&gt;
&lt;br /&gt;
デフォルトでは、指定したキーワードは正規表現として解釈されます。&lt;br /&gt;
&lt;br /&gt;
ページ全体でキーワードが検索されるように細かく検索して欲しい場合、{{ic|-K}} オプションを使います:&lt;br /&gt;
 $ man -K password&lt;br /&gt;
&lt;br /&gt;
== ページ幅 ==&lt;br /&gt;
man ページの幅は {{Ic|MANWIDTH}} 環境変数で制御できます。&lt;br /&gt;
&lt;br /&gt;
ターミナルのカラム数が小さすぎる場合 (例: ウィンドウの幅が小さい場合)、間違ったところで改行されることがあります。{{Ic|man}} を実行するときに MANWIDTH を設定することで読みやすくすることが可能です:&lt;br /&gt;
&lt;br /&gt;
{{Hc|~/.bashrc|&amp;lt;nowiki&amp;gt;&lt;br /&gt;
man() {&lt;br /&gt;
    local width=$(tput cols)&lt;br /&gt;
    [ $width -gt $MANWIDTH ] &amp;amp;&amp;amp; width=$MANWIDTH&lt;br /&gt;
    env MANWIDTH=$width \&lt;br /&gt;
    man &amp;quot;$@&amp;quot;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;}}&lt;br /&gt;
&lt;br /&gt;
== ローカルの man ページを読む ==&lt;br /&gt;
&lt;br /&gt;
標準のインターフェイスの代わりに、{{Pkg|lynx}} や [[Firefox]] などのブラウザを使って man ページを見ることで info ページの利点 (ハイパーリンク) を得ることができます。代替手段としては、以下のようなものがあります。&lt;br /&gt;
&lt;br /&gt;
=== ブラウザで表示できる HTML に変換する ===&lt;br /&gt;
==== mandoc ====&lt;br /&gt;
{{Pkg|mandoc}} パッケージをインストールしてください。例えば {{ic|free(1)}} のページを変換するなら:&lt;br /&gt;
 $ gunzip -c /usr/share/man/man1/free.1.gz | mandoc -Thtml -Ostyle=style.css 1&amp;gt; free.html&lt;br /&gt;
&lt;br /&gt;
{{ic|free.html}} ファイルをお好きなブラウザで開いて下さい。&lt;br /&gt;
&lt;br /&gt;
==== man2html ====&lt;br /&gt;
まず、[[公式リポジトリ]]から {{Pkg|man2html}} をインストールします。&lt;br /&gt;
&lt;br /&gt;
そして、man ページを変換:&lt;br /&gt;
 $ man free | man2html -compress -cgiurl man$section/$title.$section$subsection.html &amp;gt; ~/man/free.html&lt;br /&gt;
&lt;br /&gt;
他の使い方として印刷しやすいように {{Ic|man2html}} でプレーンテキストにすることもできます:&lt;br /&gt;
 $ man free | man2html -bare &amp;gt; ~/free.txt&lt;br /&gt;
&lt;br /&gt;
==== man -H ====&lt;br /&gt;
Arch のリポジトリに入っている man の GNU 実装には HTML で表示する機能が付いています:&lt;br /&gt;
 $ man -H free&lt;br /&gt;
&lt;br /&gt;
使用されるブラウザは {{ic|BROWSER}} [[環境変数]]によって決まります。{{ic|-H}} オプションにバイナリを渡すことで上書きできます。&lt;br /&gt;
&lt;br /&gt;
==== roffit ====&lt;br /&gt;
[[AUR]] から {{AUR|roffit}} をインストールしてください。&lt;br /&gt;
&lt;br /&gt;
man ページを変換するには:&lt;br /&gt;
&lt;br /&gt;
 $ gunzip -c /usr/share/man/man1/free.1.gz | roffit &amp;gt; free.html&lt;br /&gt;
&lt;br /&gt;
=== PDF に変換する ===&lt;br /&gt;
man ページは基本的に印刷ができるようになっています。man ページは troff で書かれており、troff はもともと組版言語だからです。{{Pkg|ghostscript}} をインストールしていれば、man ページを PDF に変換するのはとても簡単に行えます: {{ic|&amp;lt;nowiki&amp;gt;man -t &amp;lt;manpage&amp;gt; | ps2pdf - &amp;lt;pdf&amp;gt;&amp;lt;/nowiki&amp;gt;}}。どんな風になるかは [https://www.google.com/search?q=manpage+pdf+troff&amp;amp;num=100&amp;amp;hl=en&amp;amp;prmd=imvns&amp;amp;source=lnms&amp;amp;tbm=isch&amp;amp;sa=X&amp;amp;ei=5BZpUI3oH6rI2AXvx4CoAw&amp;amp;ved=0CAoQ_AUoAQ&amp;amp;biw=1321&amp;amp;bih=1100 google で画像検索] してみればわかります。これは万人向けとは言えないでしょう。&lt;br /&gt;
&lt;br /&gt;
{{Note|基本的にフォントは決まったサイズの Times に制限されています。ハイパーリンクは存在しません。man ページによってはターミナルでの表示に特化されていることがあり、PS や PDF のような形だと上手くいかないことがあります。}}&lt;br /&gt;
&lt;br /&gt;
== オンラインの Man ページ ==&lt;br /&gt;
man ページのオンラインデータベース:&lt;br /&gt;
* [https://jlk.fjfi.cvut.cz/arch/manpages/ Arch Linux の man ページ] ([https://man.kusakata.com 日本語訳]) ArchWiki の[[テンプレート:Man|man ページ]] のリンク先です。&lt;br /&gt;
* [http://man7.org/linux/man-pages/index.html man7.org] - Arch Linux の {{pkg|man-pages}} で使われています。&lt;br /&gt;
* [https://manned.org/ manned.org の様々な Linux ディストリビューションや BSD などのマニュアルページ集]&lt;br /&gt;
* [https://linux.die.net/man/ linux.die.net]&lt;br /&gt;
* [https://man.cx/ man.cx]&lt;br /&gt;
* [https://manpages.debian.org/ Debian GNU/Linux の man ページ]&lt;br /&gt;
* [https://manpages.ubuntu.com/ Ubuntu の manpage リポジトリ]&lt;br /&gt;
* [https://leaf.dragonflybsd.org/cgi/web-man DragonFlyBSD マニュアルページ]&lt;br /&gt;
* [https://www.freebsd.org/cgi/man.cgi FreeBSD の man ページ]&lt;br /&gt;
* [http://netbsd.gw.com/cgi-bin/man-cgi NetBSD マニュアルページ]&lt;br /&gt;
* [https://www.openbsd.org/cgi-bin/man.cgi OpenBSD マニュアルページ]&lt;br /&gt;
* [http://man.cat-v.org/plan_9/ Plan 9 マニュアル — Volume 1]&lt;br /&gt;
* [http://man.cat-v.org/inferno/ Inferno マニュアル — Volume 1]&lt;br /&gt;
* [https://www.unix.com/man-page/OpenSolaris/1/man/ UNIX と Linux フォーラムの man ページリポジトリ]&lt;br /&gt;
&lt;br /&gt;
{{pkg|man-pages}} は POSIX.1 の man pages を提供していますが([https://www.kernel.org/doc/man-pages/download.html] を参照)、公式のオンラインリファレンスも存在することに注意してください。&lt;br /&gt;
&lt;br /&gt;
* [https://pubs.opengroup.org/onlinepubs/9699919799/ POSIX.1-2017]&lt;br /&gt;
&lt;br /&gt;
[https://gist.github.com/rixx/6cb5fa38f694009ad0bd50c275bb61f2 オンライン man ページの比較表]もあります。&lt;br /&gt;
&lt;br /&gt;
{{Warning|ディストリビューションによっては、Arch が使っているのとは異なる man ページを使用していることがあります (パッチを適用していたり単純に古くなっているなど)。オンラインの man ページを使用するときはそのことに注意してください。}}&lt;br /&gt;
&lt;br /&gt;
==特筆すべき man ページ==&lt;br /&gt;
&lt;br /&gt;
以下は読む価値があるページの部分的なリストであり、様々なことを深く理解するのに役立ちます。リファレンスとして役立つページもあります (ascii テーブルなど)。&lt;br /&gt;
&lt;br /&gt;
* {{man|7|ascii}}&lt;br /&gt;
* {{man|7|boot}}&lt;br /&gt;
* {{man|7|charsets}}&lt;br /&gt;
* {{man|1|chmod}}&lt;br /&gt;
* {{man|7|credentials}}&lt;br /&gt;
* {{man|5|fstab}}&lt;br /&gt;
* {{man|7|hier}}&lt;br /&gt;
* {{man|1|systemd}}&lt;br /&gt;
* {{man|1p|locale}}, {{man|5|locale}}, {{man|7|locale}}&lt;br /&gt;
* {{man|3|printf}}&lt;br /&gt;
* {{man|5|proc}}&lt;br /&gt;
* {{man|7|regex}}&lt;br /&gt;
* {{man|7|signal}}&lt;br /&gt;
* {{man|5|term}}, {{man|7|term}}&lt;br /&gt;
* {{man|5|termcap}}&lt;br /&gt;
* {{man|5|terminfo}}&lt;br /&gt;
* {{man|7|utf-8}}&lt;br /&gt;
&lt;br /&gt;
他のページは、[http://man7.org/linux/man-pages/dir_section_7.html カテゴリ 7 (miscellaneous) ページ] を見て下さい:&lt;br /&gt;
 $ man -s 7 -k &amp;quot;.*&amp;quot; &lt;br /&gt;
&lt;br /&gt;
Arch Linux 特有のページ:&lt;br /&gt;
* {{man|5|alpm-hooks}}&lt;br /&gt;
* {{man|7|archlinux}}&lt;br /&gt;
* {{man|3|libalpm}}&lt;br /&gt;
* {{man|8|makepkg}}&lt;br /&gt;
* {{man|5|makepkg.conf}}&lt;br /&gt;
* {{man|1|makepkg-template}}&lt;br /&gt;
* {{man|8|mkinitcpio}}&lt;br /&gt;
* {{man|8|pacman}}&lt;br /&gt;
* {{man|5|pacman.conf}}&lt;br /&gt;
* {{man|8|pacman-conf}}&lt;br /&gt;
* {{man|8|pacman-key}}&lt;br /&gt;
&lt;br /&gt;
== 参照 ==&lt;br /&gt;
&lt;br /&gt;
* [https://wiki.gentoo.org/wiki/Man_page man page - Gentoo Wiki] - Gentoo wiki の記事&lt;br /&gt;
* [https://linuxtidbits.wordpress.com/2013/08/21/wtfm-write-the-fine-manual-with-pod2man-text-converter/ Write The Fine Manual with pod2man]&lt;br /&gt;
* [https://manpages.bsd.lv/history.html History of UNIX Manpages]&lt;/div&gt;</summary>
		<author><name>Takemaro</name></author>
	</entry>
	<entry>
		<id>https://wiki.archlinux.jp/index.php?title=Dm-crypt/%E3%83%87%E3%83%90%E3%82%A4%E3%82%B9%E3%81%AE%E6%9A%97%E5%8F%B7%E5%8C%96&amp;diff=39963</id>
		<title>Dm-crypt/デバイスの暗号化</title>
		<link rel="alternate" type="text/html" href="https://wiki.archlinux.jp/index.php?title=Dm-crypt/%E3%83%87%E3%83%90%E3%82%A4%E3%82%B9%E3%81%AE%E6%9A%97%E5%8F%B7%E5%8C%96&amp;diff=39963"/>
		<updated>2025-03-10T12:41:22Z</updated>

		<summary type="html">&lt;p&gt;Takemaro: /* plain モードでデバイスを暗号化 */ 未訳部分を最新の英語版から翻訳&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Lowercase title}}&lt;br /&gt;
[[Category:保存データ暗号化]]&lt;br /&gt;
[[Category:ファイルシステム]]&lt;br /&gt;
[[en:Dm-crypt/Device encryption]]&lt;br /&gt;
このセクションではコマンドラインから &#039;&#039;dm-crypt&#039;&#039; を利用して手動でシステムを暗号化する方法を説明しています。&lt;br /&gt;
&lt;br /&gt;
== 準備 ==&lt;br /&gt;
{{pkg|cryptsetup}} を使用する前に、{{ic|dm_crypt}} [[カーネルモジュール]]がロードされていることを確認してください。&lt;br /&gt;
&lt;br /&gt;
== Cryptsetup の使用方法 ==&lt;br /&gt;
&#039;&#039;Cryptsetup&#039;&#039; は暗号化デバイスを作成・管理する &#039;&#039;dm-crypt&#039;&#039; を使うためのコマンドラインツールです。後に Linux カーネルの &#039;&#039;&#039;d&#039;&#039;&#039;evice-&#039;&#039;&#039;m&#039;&#039;&#039;apper と &#039;&#039;&#039;crypt&#039;&#039;&#039;ographic モジュールを使用する別の暗号化もサポートするように拡張されました。最も著しい拡張は Linux Unified Key Setup (LUKS) の拡張で、dm-crypt をセットアップするのに必要な情報を全てディスク自体に保存してパーティションとキーの管理を抽象化することで使いやすさを増しています。device-mapper によってアクセスされるデバイスはブロックデバイスと呼ばれます。詳しくは[[ディスク暗号化#ブロックデバイスの暗号化]]を見て下さい。&lt;br /&gt;
&lt;br /&gt;
ツールは以下のように使います:&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup &#039;&#039;action&#039;&#039; &#039;&#039;options&#039;&#039; &#039;&#039;device&#039;&#039; &#039;&#039;dmname&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
オプションや暗号化モードにはデフォルト値が存在し、コマンドラインで何も指定しなかった場合はデフォルト値が使われます。オプションや暗号化モードのデフォルトパラメータを確認するには以下のコマンドを実行:&lt;br /&gt;
&lt;br /&gt;
 $ cryptsetup --help &lt;br /&gt;
&lt;br /&gt;
オプションの完全なリストは man ページで確認できます。暗号化モードやアクションによって必要なパラメータは変わるため、以下のセクションでは違いについて説明しています。ブロックデバイス暗号化は高速ですが、速度は非常に重要な問題です。ブロックデバイスの暗号を設定後に変更することは難しいため、それぞれのパラメータについて &#039;&#039;dm-crypt&#039;&#039; のパフォーマンスをチェックすると良いでしょう:&lt;br /&gt;
&lt;br /&gt;
 $ cryptsetup benchmark &lt;br /&gt;
&lt;br /&gt;
上記のコマンドはインストールするときにアルゴリズムや鍵長を決める手がかりになります。特定の AES 暗号が非常に高速に処理される場合、おそらくその暗号は CPU によるハードウェア支援の恩恵に与かっています。&lt;br /&gt;
&lt;br /&gt;
{{Tip|テストしたいときは[[:カテゴリ:仮想化|仮想マシン]]の仮想ハードドライブで練習すると良いでしょう。}}&lt;br /&gt;
&lt;br /&gt;
=== Cryptsetup のパスフレーズとキー ===&lt;br /&gt;
暗号化されたブロックデバイスはキーによって保護されます。キーは以下のいずれかです:&lt;br /&gt;
&lt;br /&gt;
*パスフレーズ、[[セキュリティ#パスワード]]を見て下さい。&lt;br /&gt;
*キーファイル、[[#キーファイル]] を見て下さい。&lt;br /&gt;
&lt;br /&gt;
どちらのタイプのキーも最大サイズが決められています: パスフレーズは512文字まで、キーファイルは 8192kB までです。&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;LUKS&#039;&#039; の重要な特徴として、キーは LUKS によって暗号化されたデバイスのマスターキーを解除するのに使われ、root 権限で変えることができるということです。他の暗号化モードでは設定後にキーを変更することはできません。暗号化にマスターキーを使わないためです。詳しくは[[ディスク暗号化#ブロックデバイスの暗号化]]を見て下さい。&lt;br /&gt;
&lt;br /&gt;
== dm-crypt の暗号化オプション ==&lt;br /&gt;
&#039;&#039;Cryptsetup&#039;&#039; は &#039;&#039;dm-crypt&#039;&#039; で使用できる様々な暗号化モードをサポートしています。&lt;br /&gt;
* {{ic|--type luks}} - LUKS フォーマットのデフォルトバージョン ({{Pkg|cryptsetup}} &amp;lt; 2.1.0 ではLUKS1、{{Pkg|cryptsetup}} ≥ 2.1.0 では LUKS2) を使用。&lt;br /&gt;
* {{ic|--type luks1}} - LUKS の古いバージョンである LUKS1 を使用。&lt;br /&gt;
* {{ic|--type luks2}} - LUKS に追加の拡張が導入された現在のバージョンである LUKS2 を使用。&lt;br /&gt;
** {{ic|--hw-opal-only}} - TCG OPAL 標準をサポートするドライブでハードウェアベースの暗号化を使用。[[自己暗号化ドライブ]]や {{man|8|cryptsetup|SED (SELF ENCRYPTING DRIVE) OPAL EXTENSION}} も参照して下さい。&lt;br /&gt;
** {{ic|--hw-opal}} - OPAL によるハードウェア暗号化と dm-crypt によるソフトウェア暗号化の両方を使用。&lt;br /&gt;
*{{ic|--type plain}} - dm-crypt plain モードを使用。&lt;br /&gt;
*{{ic|--type loopaes}} - loopaes legacy モードを使用。&lt;br /&gt;
*{{ic|--type tcrypt}} - [[Truecrypt]] 互換モードを使用。&lt;br /&gt;
* {{ic|--type bitlk}} - [[Wikipedia:ja:BitLocker|BitLocker]] 互換モードを使用。 {{man|8|cryptsetup|BITLK (WINDOWS BITLOCKER COMPATIBLE) EXTENSION}} も参照して下さい。&lt;br /&gt;
&lt;br /&gt;
暗号やハッシュの基本的な暗号化オプションは全てのモードで使うことができ、カーネルの暗号化バックエンド機能を利用します。使用できるオプションは以下のコマンドで確認できます:&lt;br /&gt;
&lt;br /&gt;
 $ less /proc/crypto &lt;br /&gt;
&lt;br /&gt;
{{Tip|リストが短い場合、{{ic|cryptsetup benchmark}} を実行すればモジュールのロードが行われます。}}&lt;br /&gt;
&lt;br /&gt;
以下では LUKS モードと plain モードの暗号化オプションを紹介します。記事の中で例として使っているオプションを並べていますが、利用できるオプションの全てではないので注意してください。&lt;br /&gt;
&lt;br /&gt;
=== LUKS モードの暗号化オプション ===&lt;br /&gt;
LUKS 暗号化モードで新しい dm-crypt デバイスをセットアップする &#039;&#039;cryptsetup&#039;&#039; のアクションは &#039;&#039;luksFormat&#039;&#039; です。名前にはフォーマットとありますが、実際にはデバイスをフォーマットするのではなく、LUKS デバイスヘッダーを設定して指定された暗号オプションを使ってマスター鍵を暗号化します。&lt;br /&gt;
&lt;br /&gt;
デフォルトパラメータ ({{ic|cryptsetup --help}}で確認できます) で新しい LUKS デバイスを作成する場合、単に以下のコマンドを実行します:&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup luksFormat &#039;&#039;device&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
cryptsetup 2.4.0 の場合、これは以下と同等です:&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup luksFormat --type luks2 --cipher aes-xts-plain64 --hash sha256 --iter-time 2000 --key-size 256 --pbkdf argon2id --use-urandom --verify-passphrase &#039;&#039;device&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
以下の表でデフォルト値とサンプルにコメントを付けて説明しています:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; style=&amp;quot;text-align:left&amp;quot; | オプション&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; style=&amp;quot;text-align:left&amp;quot; | Cryptsetup 2.1.0 のデフォルト&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; style=&amp;quot;text-align:left&amp;quot; | 例&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; style=&amp;quot;text-align:left&amp;quot; | コメント&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;row&amp;quot; style=&amp;quot;text-align:right&amp;quot; | --cipher, -c&lt;br /&gt;
| {{ic|aes-xts-plain64}}&lt;br /&gt;
| {{ic|aes-xts-plain64}}&lt;br /&gt;
| [https://www.kernel.org/pub/linux/utils/cryptsetup/v1.6/v1.6.0-ReleaseNotes バージョン 1.6.0]からデフォルトが AES [[ディスク暗号化#暗号と利用形態|暗号]]と [[wikipedia:Disk encryption theory#XEX-based tweaked-codebook mode with ciphertext stealing (XTS)|XTS]] を使用するよう変わりました ([https://gitlab.com/cryptsetup/cryptsetup/wikis/FrequentlyAskedQuestions#5-security-aspects FAQ] の 5.16 も参照)。以前のデフォルトである  {{ic|--cipher aes-cbc-essiv}} は、[[wikipedia:Disk encryption theory#Cipher-block chaining (CBC)|既知の問題]]があり、[https://www.jakoblell.com/blog/2013/12/22/practical-malleability-attack-against-cbc-encrypted-luks-partitions/ 実際の攻撃]も存在するため、使用しないことをお勧めします。&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;row&amp;quot; style=&amp;quot;text-align:right&amp;quot; | --key-size, -s&lt;br /&gt;
| {{ic|256}} (XTSでは {{ic|512}})&lt;br /&gt;
| {{ic|512}}&lt;br /&gt;
| XTS モードの暗号では 512 ビットがデフォルトです。[[wikipedia:Disk encryption theory#XEX-based tweaked-codebook mode with ciphertext stealing (XTS)|XTS はキーを半分に割る]]ので、これは AES-256 が使われることを意味します。&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;row&amp;quot; style=&amp;quot;text-align:right&amp;quot; | --hash, -h&lt;br /&gt;
| {{ic|sha256}}&lt;br /&gt;
| {{ic|sha512}}&lt;br /&gt;
| [[ディスク暗号化#暗号メタデータ|鍵導出]] で使用されるハッシュアルゴリズム。リリース 1.7.0 でデフォルト設定が {{ic|sha1}} から {{ic|sha256}} に変更されました。セキュリティ上の理由ではなく SHA1 が使用できないシステムでも動作するようにするためです [https://www.kernel.org/pub/linux/utils/cryptsetup/v1.7/v1.7.0-ReleaseNotes]。{{ic|sha1}} でも十分セキュアであるため古いバージョンの &#039;&#039;cryptsetup&#039;&#039; と互換性を維持する目的で使用できます [https://gitlab.com/cryptsetup/cryptsetup/wikis/FrequentlyAskedQuestions#5-security-aspects]。&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;row&amp;quot; style=&amp;quot;text-align:right&amp;quot; | --iter-time, -i&lt;br /&gt;
| {{ic|2000}}&lt;br /&gt;
| {{ic|5000}}&lt;br /&gt;
| PBKDF2 パスフレーズを処理するのに費やす時間 (ミリ秒単位)。&amp;quot;&#039;&#039;PBKDF2 の反復回数を高く保持したままユーザーが許容できる数値にするため&#039;&#039;&amp;quot;、リリース 1.7.0 からデフォルト値が {{ic|1000}} から {{ic|2000}} に変更されています [https://www.kernel.org/pub/linux/utils/cryptsetup/v1.7/v1.7.0-ReleaseNotes]。&#039;&#039;luksFormat&#039;&#039; や &#039;&#039;luksAddKey&#039;&#039; などパスフレーズを設定・変更する LUKS 操作のみに関連するオプションです。パラメータとして 0 を指定するとコンパイル時のデフォルトが選択されます。&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;row&amp;quot; style=&amp;quot;text-align:right&amp;quot; | --use-{u,}random&lt;br /&gt;
| {{ic|--use-&#039;&#039;&#039;u&#039;&#039;&#039;random}}&lt;br /&gt;
| {{ic|--use-random}}&lt;br /&gt;
| [[乱数生成|乱数生成器]]の選択。現在は {{ic|/dev/random}} と {{ic|/dev/urandom}} は等価なので、 {{ic|--use-random}} と {{ic|--use-urandom}} も等価です。&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;row&amp;quot; style=&amp;quot;text-align:right&amp;quot; | --verify-passphrase, -y&lt;br /&gt;
| Yes&lt;br /&gt;
| -&lt;br /&gt;
| {{ic|luksFormat}} と {{ic|luksAddKey}} で使われます。Arch Linux ではデフォルトで有効です。&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;row&amp;quot; style=&amp;quot;text-align:right&amp;quot; | --sector-size&lt;br /&gt;
| {{ic|512}} または {{ic|4096}} (デバイスに依存)&lt;br /&gt;
| {{ic|4096}}&lt;br /&gt;
| ディスク暗号化で使われるセクタサイズを設定します。デフォルトは、4Kn や 512e のブロックデバイスでは {{ic|4096}} で、512n のブロックデバイスでは {{ic|512}} です。最近のストレージデバイスのほとんどでは、セクタサイズを 512 バイトから 4096 バイトにすることでパフォーマンスが向上します。[[アドバンスドフォーマット#dm-crypt]]を見て下さい。&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
LUKS の機能やオプションについては [https://gitlab.com/cryptsetup/cryptsetup/-/raw/master/docs/on-disk-format.pdf LUKS1 の仕様]や [https://gitlab.com/cryptsetup/cryptsetup/-/raw/master/docs/on-disk-format-luks2.pdf LUKS2の仕様] で説明されています。&lt;br /&gt;
{{Tip|開発者によるプレゼンテーション [https://mbroz.fedorapeople.org/talks/DevConf2016/devconf2016-luks2.pdf devconfcz2016] (pdf) では、LUKS2 での主な仕様変更の背景がまとめられています。cryptsetup のメジャーバージョンである[https://mirrors.edge.kernel.org/pub/linux/utils/cryptsetup/v2.0/v2.0.0-ReleaseNotes バージョン 2 のリリースノート]も見て下さい。}}&lt;br /&gt;
&lt;br /&gt;
==== イテレーション時間 ====&lt;br /&gt;
[https://gitlab.com/cryptsetup/cryptsetup/-/wikis/FrequentlyAskedQuestions#2-setup cryptsetup FAQ§2.1] と [https://gitlab.com/cryptsetup/cryptsetup/-/wikis/FrequentlyAskedQuestions#3-common-problems §3.4]から:&lt;br /&gt;
&lt;br /&gt;
:鍵スロットを解除するのにかかる時間は[...]パスフレーズを設定した時に決まります。デフォルトでは1秒 (LUKS2 では2秒) です。[...]&lt;br /&gt;
:パスフレーズをイテレーションする回数は時間によって決まり、したがってセキュリティレベルは LUKS コンテナを作成したマシンの CPU の能力に依存します。[...]&lt;br /&gt;
:もし高速なマシンでパスフレーズを設定し、低速なマシンでそれを解除した場合、解除にかかる時間は長くなります。&lt;br /&gt;
&lt;br /&gt;
このことから、コンテナを作成する際は、そのコンテナに最も頻繁にアクセスすることになるマシンの上で作成するのが良いでしょう。&lt;br /&gt;
&lt;br /&gt;
イテレーション回数を正しく調節する方法については、上の記事の残りの部分を読んで下さい。&lt;br /&gt;
&lt;br /&gt;
=== plain モードの暗号化オプション ===&lt;br /&gt;
dm-crypt の &#039;&#039;plain&#039;&#039; モードでは、デバイスのマスター鍵が存在せず、セットアップも必要ありません。代わりに、直接暗号化オプションを使用して暗号ディスクと名前付きデバイスのマッピングを作成します。パーティションやデバイス全体に対してマッピングを作成できます。後者の場合、パーティションテーブルも不要です。&lt;br /&gt;
&lt;br /&gt;
cryptsetup のデフォルトパラメータを使って &#039;&#039;plain&#039;&#039; のマッピングを作成するには:&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup open --type plain &#039;&#039;options&#039;&#039; &#039;&#039;device&#039;&#039; &#039;&#039;dmname&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
実行するとパスワードを求められます。非常に高いエントロピーのパスワードが必要です。デフォルトではありませんが、 {{ic|--verify-passphrase}} オプションを使えます。&lt;br /&gt;
&lt;br /&gt;
一般論として、作成時の暗号化オプションは精確に記録しておくと良いでしょう。暗号化されたデバイス上にはオプションの内容は記録されていませんし、デフォルトのオプションは変わる可能性があります。&lt;br /&gt;
&lt;br /&gt;
以下は [[Dm-crypt/システム全体の暗号化#Plain dm-crypt]] の例とデフォルトパラメータの比較表です。&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! オプション !! Cryptsetup (2.7.0) のデフォルト !! 例 !! コメント !! &lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;--hash, -h&#039;&#039;&#039; || {{ic|sha256}} || - || パスフレーズからキーを作成するのに使用するハッシュ。キーファイルを用いる場合は使われない。&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;--cipher, -c&#039;&#039;&#039;|| {{ic|aes-xts-plain64}}|| {{ic|aes-xts-plain64}} || 暗号は3つの文字列からなります: cipher-chainmode-IV generator。[[ディスク暗号化#暗号と利用形態]]や [https://gitlab.com/cryptsetup/cryptsetup/wikis/DMCrypt DMCrypt のドキュメント] を見てください。&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;--key-size, -s&#039;&#039;&#039;||{{ic|256}}||{{ic|512}}||キーサイズ (ビット数)。サイズは使用する暗号や使用するチェインモードによって変わります。XTS モードは CBC モードの2倍のキーサイズを必要とします。&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;--size, -b&#039;&#039;&#039;||対象ディスクの実際のサイズ||- (デフォルト)||512バイト単位のセクタ数。デバイスの最大サイズ以下。&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;--offset, -o&#039;&#039;&#039;||{{ic|0}}||{{ic|0}}||マッピングを開始するディスクの先頭からのオフセット。512バイト単位のセクタ数。&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;--skip, -p&#039;&#039;&#039;||{{ic|0}}||{{ic|2048}} (512B×2048=1MiB をスキップ)||初期ベクトル (IV) を計算する際にスキップする暗号化されたデータのサイズ。512バイト単位のセクタ数。&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;--key-file, -d&#039;&#039;&#039;||デフォルトではパスフレーズを使用||{{ic|/dev/sd&#039;&#039;Z&#039;&#039;}} (もしくは {{ic|/boot/keyfile.enc}})||キーとして使用するデバイスまたはファイル。詳しくは [[#キーファイル]] を参照。&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;--keyfile-offset&#039;&#039;&#039;||{{ic|0}}||{{ic|0}}||キーファイルの先頭からのオフセット (バイト数)。&#039;&#039;cryptsetup&#039;&#039; 1.6.7 以上でサポートされているオプション。&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;--keyfile-size, -l&#039;&#039;&#039;||{{ic|8192kB}}||- (デフォルト)||キーファイルから読み込まれるバイト数を制限。&#039;&#039;cryptsetup&#039;&#039; 1.6.7 以上でサポートされているオプション。&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;--sector-size, -l&#039;&#039;&#039;||{{ic|512}}||{{ic|4096}}||ディスク暗号化の際に使われるセクタサイズ。4Kn のブロックデバイス以外では、 {{ic|512}} がデフォルト。最近のストレージデバイスのほとんどでは、セクタサイズを 512 バイトから 4096 バイトにすることでパフォーマンスが向上します。[[アドバンスドフォーマット#dm-crypt]]を見て下さい。&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
{{ic|/dev/sd&#039;&#039;X&#039;&#039;}} デバイスで、上記の例を使用する場合:&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup open --type plain --cipher=aes-xts-plain64 --offset=0 --skip=2048 --key-file=/dev/sd&#039;&#039;Z&#039;&#039; --key-size=512 --sector-size 4096 /dev/sdX enc&lt;br /&gt;
&lt;br /&gt;
{{Warning|LUKS による暗号化と違って、上記のコマンドはマッピングを再現するために毎回 &#039;&#039;&#039;全て&#039;&#039;&#039; 指定する必要があります。暗号やハッシュ、キーファイルについて覚えておく必要があります。cryptsetup のデフォルトはバージョンによって変わるので、デフォルトに頼らないで下さい。}}&lt;br /&gt;
&lt;br /&gt;
マッピングが作成されたことは以下のコマンドで確認できます:&lt;br /&gt;
 # fdisk -l&lt;br /&gt;
{{ic|/dev/mapper/enc}} のエントリが存在しているはずです。&lt;br /&gt;
&lt;br /&gt;
== cryptsetup でデバイスを暗号化 ==&lt;br /&gt;
以下のセクションでは新しい暗号化ブロックデバイスを作成するオプションと手動のアクセス方法を説明します。&lt;br /&gt;
&lt;br /&gt;
{{Warning|GRUB の LUKS2 サポートは限られています。[[GRUB#暗号化された /boot]] を見て下さい。GRUB が解錠する必要のあるパーティションには、LUKS2 を PBKDF2 で使用 ({{ic|cryptsetup luksFormat --pbkdf pbkdf2}})して下さい。}}&lt;br /&gt;
&lt;br /&gt;
=== LUKS モードでデバイスを暗号化 ===&lt;br /&gt;
==== LUKS パーティションのフォーマット ====&lt;br /&gt;
&lt;br /&gt;
暗号化 LUKS パーティションとして設定するには次を実行:&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup luksFormat &#039;&#039;device&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
上記のコマンドを実行するとパスワードの入力が要求されます。&lt;br /&gt;
&lt;br /&gt;
コマンドラインオプションは [[#LUKS モードの暗号化オプション]]を参照。&lt;br /&gt;
&lt;br /&gt;
結果は次のコマンドで確認できます:&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup luksDump &#039;&#039;device&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
ダンプでは暗号のヘッダー情報だけでなく、LUKS パーティションで使われるキースロットもわかります。&lt;br /&gt;
&lt;br /&gt;
以下の例はデフォルトの AES 暗号を使って256ビットの XTS モードで {{ic|/dev/sda1}} に暗号化したルートパーティションを作成します:&lt;br /&gt;
 # cryptsetup luksFormat -s 512 /dev/sda1&lt;br /&gt;
&lt;br /&gt;
=====LUKS を使ってキーファイルでパーティションをフォーマット=====&lt;br /&gt;
&lt;br /&gt;
LUKS による暗号化パーティションを新しく作成する場合、以下のように作成するときにパーティションにキーファイルを関連付けることができます:&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup luksFormat &#039;&#039;device&#039;&#039; &#039;&#039;/path/to/mykeyfile&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
キーファイルを作成・管理する方法は [[#キーファイル]] を見て下さい。&lt;br /&gt;
&lt;br /&gt;
====デバイスマッパーで LUKS パーティションのロックを解除・マップ====&lt;br /&gt;
&lt;br /&gt;
LUKS パーティションを作成したら、解錠することができます。&lt;br /&gt;
&lt;br /&gt;
解錠ではデバイスマッパーによってパーティションが新しいデバイス名にマッピングされます。カーネルは {{ic|&#039;&#039;device&#039;&#039;}} が暗号化デバイスであり暗号化データを上書きしないように {{ic|/dev/mapper/&#039;&#039;dm_name&#039;&#039;}} を使って LUKS 経由で解決する必要があることを知ります。間違って上書きしないために、設定が完了したら[[#バックアップとリストア|暗号化ヘッダーをバックアップ]]する方法を確認してください。&lt;br /&gt;
&lt;br /&gt;
暗号化された LUKS パーティションを開くには次のコマンドを実行:&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup open --type luks &#039;&#039;device&#039;&#039; &#039;&#039;dm_name&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
実行するとパーティションを解錠するパスワードの入力が求められます。通常、マッピングするデバイスの名前はパーティションの機能が分かりやすいものにします。例えば、以下のコマンドは luks パーティション {{ic|/dev/sda1}} を解錠して {{ic|cryptroot}} という名前のデバイスマッパーにマッピングします:&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup open --type luks /dev/sda1 cryptroot &lt;br /&gt;
&lt;br /&gt;
パーティションをオープンしたら、ルートパーティションのデバイスアドレスはパーティション本体 (例: {{ic|/dev/sda1}}) の代わりに {{ic|/dev/mapper/cryptroot}} が使われます。&lt;br /&gt;
&lt;br /&gt;
暗号化レイヤーの上に LVM をセットアップする場合、復号化したボリュームグループのデバイスファイルは {{ic|/dev/sda1}} ではなく {{ic|/dev/mapper/cryptroot}} などとなります。それから LVM は作成されている論理ボリューム全てに名前を割り当てます (例: {{ic|/dev/mapper/lvmpool-root}} や {{ic|/dev/mapper/lvmpool-swap}})。&lt;br /&gt;
&lt;br /&gt;
暗号化データをパーティションに書き込むときは、マッピングされたデバイス名を使ってアクセスする必要があります。例えば[[ファイルシステム#デバイスのフォーマット|ファイルシステムを作成]]するときは以下のようにします:&lt;br /&gt;
&lt;br /&gt;
 # mkfs -t ext4 /dev/mapper/cryptroot&lt;br /&gt;
&lt;br /&gt;
{{ic|/dev/mapper/cryptroot}} デバイスは他のパーティションと同じように[[マウント]]できます。&lt;br /&gt;
&lt;br /&gt;
luks コンテナを閉じるには、パーティションをアンマウントし、以下を実行して下さい:&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup close cryptroot&lt;br /&gt;
&lt;br /&gt;
==== 鍵を TPM に保管する ====&lt;br /&gt;
&lt;br /&gt;
[[Trusted Platform Module#LUKS による保存データの暗号化]]を見て下さい。&lt;br /&gt;
&lt;br /&gt;
=== plain モードでデバイスを暗号化 ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;dm-crypt&#039;&#039; プレーンモード暗号化の作成とその後のアクセスはどちらも、正しい [[#plain モードの暗号化オプション|オプション]] を指定して &#039;&#039;cryptsetup&#039;&#039; {{ic|open}} アクションを使用するだけで済みます。以下は、非ルートデバイスの2つの例で示していますが、両方をスタックする (2番目は最初のデバイス内に作成される) という変わった構成になっています。もちろん、暗号化をスタックするとオーバーヘッドが2倍になります。ここでの使用例は、暗号オプションの使用法のさらなる例を説明するためのものです。&lt;br /&gt;
&lt;br /&gt;
最初のマッパーは、上の表の左側の列で示されているような &#039;&#039;cryptsetup&#039;&#039; のプレーンモードのデフォルトで作成されます。&lt;br /&gt;
&lt;br /&gt;
{{hc|# cryptsetup --type plain -v open /dev/sdaX plain1|&lt;br /&gt;
WARNING: Using default options for cipher (aes-xts-plain64, key size 256 bits) that could be incompatible with older versions.&lt;br /&gt;
WARNING: Using default options for hash (sha256) that could be incompatible with older versions.&lt;br /&gt;
For plain mode, always use options --cipher, --key-size and if no keyfile is used, then also --hash.&lt;br /&gt;
Enter passphrase for /dev/sd&#039;&#039;xY&#039;&#039;:&lt;br /&gt;
Command successful.&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
次に、さまざまな暗号化パラメーターを使用し、(オプションの) オフセットを使用して、その中に2番目のブロックデバイスを追加し、ファイルシステムを作成してマウントします。&lt;br /&gt;
&lt;br /&gt;
{{hc|# lsblk -p|&lt;br /&gt;
 NAME                                                     &lt;br /&gt;
 /dev/sda                                     &lt;br /&gt;
 ├─/dev/sd&#039;&#039;xY&#039;&#039;          &lt;br /&gt;
 │ └─/dev/mapper/plain1     &lt;br /&gt;
 │   └─/dev/mapper/plain2              &lt;br /&gt;
 ...&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
 # mkfs -t ext2 /dev/mapper/plain2&lt;br /&gt;
 # mount -t ext2 /dev/mapper/plain2 /mnt&lt;br /&gt;
 # echo &amp;quot;This is stacked. one passphrase per foot to shoot.&amp;quot; &amp;gt; /mnt/stacked.txt&lt;br /&gt;
&lt;br /&gt;
正しくアクセスできるかどうか確認するため、一度スタックを閉じます。&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup close plain2&lt;br /&gt;
 # cryptsetup close plain1&lt;br /&gt;
&lt;br /&gt;
まず、ファイルシステムを直接開いてみます:&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup --type plain --cipher=serpent-xts-plain64 --hash=sha256 --key-size=256 --offset=10 open /dev/sd&#039;&#039;xY&#039;&#039; plain2&lt;br /&gt;
&lt;br /&gt;
{{hc|# mount -t ext2 /dev/mapper/plain2 /mnt|&lt;br /&gt;
mount: /mnt: wrong fs type, bad option, bad superblock on /dev/mapper/plain2, missing codepage or helper program, or other error.&lt;br /&gt;
       dmesg(1) may have more information after failed mount system call.&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
なぜこれが機能しないのでしょう? &amp;quot;plain2&amp;quot; の始まるブロック ({{ic|10}}) はまだ &amp;quot;plain1&amp;quot; によって暗号化されているからで、スタックされたマッパーを通してアクセスする必要があります。このエラーは曖昧で、誤ったパスワードや誤ったオプションでも同じエラーになります。 &#039;&#039;dm-crypt&#039;&#039; のプレーンモードでは、 {{ic|open}} アクション自体はエラーを出しません。&lt;br /&gt;
&lt;br /&gt;
上で開いたマッパーを閉じた上で、今度は正しい順序で:&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup close plain2&lt;br /&gt;
&lt;br /&gt;
{{hc|# cryptsetup --type plain open /dev/sd&#039;&#039;xY&#039;&#039; plain1|&lt;br /&gt;
WARNING: Using default options for cipher (aes-xts-plain64, key size 256 bits) that could be incompatible with older versions.&lt;br /&gt;
WARNING: Using default options for hash (sha256) that could be incompatible with older versions.&lt;br /&gt;
For plain mode, always use options --cipher, --key-size and if no keyfile is used, then also --hash.&lt;br /&gt;
Enter passphrase for /dev/sd&#039;&#039;xY&#039;&#039;:&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
{{hc|1=# cryptsetup --type plain --cipher=serpent-xts-plain64 --hash=sha256 --key-size=256 --offset=10 open /dev/mapper/plain1 plain2|2=&lt;br /&gt;
Enter passphrase for /dev/mapper/plain1:&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
{{hc|# mount /dev/mapper/plain2 /mnt &amp;amp;&amp;amp; cat /mnt/stacked.txt|&lt;br /&gt;
This is stacked. one passphrase per foot to shoot.&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;dm-crypt&#039;&#039; は異なるモードを混ぜてスタックされた暗号化も扱えます。たとえば、LUKS モードが &amp;quot;plain1&amp;quot; マッパーの上にスタックされることができます。LUKS のヘッダーが &amp;quot;plain1&amp;quot; 内で暗号化されることになります。&lt;br /&gt;
&lt;br /&gt;
プレーンモードのみで使用できるオプションに {{ic|--shared}} があります。これを用いると、重複しない範囲であれば、1つのデバイスから複数のマッパーを割り当てることができます。 &amp;quot;plain2&amp;quot; に &#039;&#039;loopaes&#039;&#039; 互換の暗号化モードを用いた例でその例を示します:&lt;br /&gt;
&lt;br /&gt;
{{hc|# cryptsetup --type plain --offset 0 --size 1000 open /dev/sd&#039;&#039;xY&#039;&#039; plain1|2=&lt;br /&gt;
WARNING: Using default options for cipher (aes-xts-plain64, key size 256 bits) that could be incompatible with older versions.&lt;br /&gt;
WARNING: Using default options for hash (sha256) that could be incompatible with older versions.&lt;br /&gt;
For plain mode, always use options --cipher, --key-size and if no keyfile is used, then also --hash.&lt;br /&gt;
Enter passphrase for /dev/sd&#039;&#039;xY&#039;&#039;:&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
{{hc|1=# cryptsetup --type plain --offset 1000 --size 1000 --shared --cipher=aes-cbc-lmk --hash=sha256 open /dev/sd&#039;&#039;xY&#039;&#039; plain2|2=&lt;br /&gt;
WARNING: Using default options for cipher (aes-cbc-lmk, key size 256 bits) that could be incompatible with older versions.&lt;br /&gt;
For plain mode, always use options --cipher, --key-size and if no keyfile is used, then also --hash.&lt;br /&gt;
Enter passphrase for /dev/sd&#039;&#039;xY&#039;&#039;:&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
{{hc|# lsblk -p|&lt;br /&gt;
NAME                    &lt;br /&gt;
dev/sd&#039;&#039;xY&#039;&#039;                    &lt;br /&gt;
├─/dev/sd&#039;&#039;xY&#039;&#039;               &lt;br /&gt;
│ ├─/dev/mapper/plain1     &lt;br /&gt;
│ └─/dev/mapper/plain2     &lt;br /&gt;
...&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
デバイスツリーは2つが同じレベルで横並びになっていることを示しており、 &amp;quot;plain2&amp;quot; がスタックされることなく別個に開かれていることを意味します。&lt;br /&gt;
&lt;br /&gt;
== LUKS 固有の Cryptsetup のアクション ==&lt;br /&gt;
=== キーの管理 ===&lt;br /&gt;
LUKS パーティションには異なる8つまでのキーを定義することができます。バックアップストレージのためのアクセスキーを作成することが可能です。いわゆるキーエスクローで、あるキーは日常的に使用するために使い、他のキーはエスクローに保存して日常用のパスフレーズを忘れたときやキーファイルを喪失・破損したときにパーティションにアクセスするために使います。また、別のキースロットを使って一時的なキーを発行してユーザーにパーティションへのアクセスを許可し、後でキーを無効化したりできます。&lt;br /&gt;
&lt;br /&gt;
暗号化パーティションを作成すると、初期キースロット 0 が作成されます (手動で別の数字を指定しなかった場合)。追加キースロットは 1 から 7 までの数字が割り振られます。どのキースロットを使用しているかは以下のコマンドで確認できます (&amp;lt;device&amp;gt; は LUKS ヘッダーが存在するボリュームに置き換えてください):&lt;br /&gt;
{{hc|# cryptsetup luksDump /dev/&amp;lt;device&amp;gt; {{!}} grep BLED|&lt;br /&gt;
Key Slot 0: ENABLED&lt;br /&gt;
Key Slot 1: ENABLED&lt;br /&gt;
Key Slot 2: ENABLED&lt;br /&gt;
Key Slot 3: DISABLED&lt;br /&gt;
Key Slot 4: DISABLED&lt;br /&gt;
Key Slot 5: DISABLED&lt;br /&gt;
Key Slot 6: DISABLED&lt;br /&gt;
Key Slot 7: DISABLED}}&lt;br /&gt;
&lt;br /&gt;
上記のコマンドや下で説明しているコマンドはヘッダーのバックアップファイルでも使えます。&lt;br /&gt;
&lt;br /&gt;
==== LUKS キーの追加 ====&lt;br /&gt;
&lt;br /&gt;
新しいキースロットの追加は cryptsetup の {{ic|luksAddKey}} アクションを使うことでできます。デバイスが解錠されていても安全のために、新しいキーを入力する前に、必ず既存のキーの入力が求められます (&amp;quot;any passphrase&amp;quot;):&lt;br /&gt;
&lt;br /&gt;
{{hc|# cryptsetup luksAddKey /dev/&amp;lt;device&amp;gt; (/path/to/&amp;lt;additionalkeyfile&amp;gt;)|&lt;br /&gt;
Enter any passphrase:&lt;br /&gt;
Enter new passphrase for key slot:&lt;br /&gt;
Verify passphrase: &lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
{{ic|/path/to/&amp;lt;additionalkeyfile&amp;gt;}} を指定した場合、cryptsetup は &amp;lt;additionalkeyfile&amp;gt; の新しいキースロットを追加します。指定しなかった場合は新しいパスフレーズの入力が2回要求されます。既存の&#039;&#039;キーファイル&#039;&#039;を使って認証するには、{{ic|--key-file}} または {{ic|-d}} オプションの後に&amp;quot;既存&amp;quot;の &amp;lt;keyfile&amp;gt; を指定することで全てのキーファイルのキースロットが解除されます:&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup luksAddKey /dev/&amp;lt;device&amp;gt; (/path/to/&amp;lt;additionalkeyfile&amp;gt;) -d /path/to/&amp;lt;keyfile&amp;gt;&lt;br /&gt;
&lt;br /&gt;
複数のキーを使っていてキーを変更・無効化する場合、{{ic|--key-slot}} または {{ic|-S}} オプションでスロットを指定できます:&lt;br /&gt;
{{hc|# cryptsetup luksAddKey /dev/&amp;lt;device&amp;gt; -S 6|&lt;br /&gt;
Enter any passphrase: &lt;br /&gt;
Enter new passphrase for key slot: &lt;br /&gt;
Verify passphrase:&lt;br /&gt;
}}&lt;br /&gt;
{{hc|# cryptsetup luksDump /dev/sda8 {{!}} grep &#039;Slot 6&#039;|&lt;br /&gt;
Key Slot 6: ENABLED&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
例えばキーを変更したい場合:&lt;br /&gt;
{{hc|# cryptsetup luksChangeKey /dev/&amp;lt;device&amp;gt; -S 6|&lt;br /&gt;
Enter LUKS passphrase to be changed: &lt;br /&gt;
Enter new LUKS passphrase: &lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==== LUKS キーの削除 ====&lt;br /&gt;
&lt;br /&gt;
ヘッダーからキーを削除するアクションは3つ存在します:&lt;br /&gt;
*{{ic|luksRemoveKey}} はパスフレーズやキーファイルを指定してキーを削除します。&lt;br /&gt;
*{{ic|luksKillSlot}} は (他のキーを使って) 指定したキースロットからキーを削除します。パスフレーズを忘れたり、キーファイルを無くしたり、誰もアクセスできないようにしたい場合に有用です。&lt;br /&gt;
*{{ic|luksErase}} は全ての有効なキーを即座に削除します。&lt;br /&gt;
{{warning|&lt;br /&gt;
*All above actions can be used to irrevocably delete the last active key for an encrypted device! &lt;br /&gt;
*The {{ic|luksErase}} command was added in version 1.6.4 to quickly nuke access to the device. This action &#039;&#039;&#039;will not&#039;&#039;&#039; prompt for a valid passphrase! It will not [[Dm-crypt/ドライブの準備#LUKS ヘッダーを消去|wipe the LUKS header]], but all keyslots at once and you will, therefore, not be able to regain access unless you have a valid backup of the LUKS header.}} &lt;br /&gt;
&lt;br /&gt;
For above warning it is good to know the key we want to &#039;&#039;&#039;keep&#039;&#039;&#039; is valid. An easy check is to unlock the device with the {{ic|-v}} option, which will specify which slot it occupies:  &lt;br /&gt;
{{hc|# cryptsetup -v open /dev/&amp;lt;device&amp;gt; testcrypt|&lt;br /&gt;
Enter passphrase for /dev/&amp;lt;device&amp;gt;: &lt;br /&gt;
Key slot 1 unlocked.&lt;br /&gt;
Command successful.}}&lt;br /&gt;
&lt;br /&gt;
Now we can remove the key added in the previous subsection using its passphrase: &lt;br /&gt;
{{hc|# cryptsetup luksRemoveKey /dev/&amp;lt;device&amp;gt;|&lt;br /&gt;
Enter LUKS passphrase to be deleted: &lt;br /&gt;
}}&lt;br /&gt;
If we had used the same passphrase for two keyslots, the first slot would be wiped now. Only executing it again would remove the second one. &lt;br /&gt;
&lt;br /&gt;
Alternatively, we can specify the key slot: &lt;br /&gt;
{{hc|# cryptsetup luksKillSlot /dev/&amp;lt;device&amp;gt; 6|&lt;br /&gt;
Enter any remaining LUKS passphrase:&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
Note that in both cases, no confirmation was required.&lt;br /&gt;
{{hc|# cryptsetup luksDump /dev/sda8 {{!}} grep &#039;Slot 6&#039;|&lt;br /&gt;
Key Slot 6: DISABLED&lt;br /&gt;
}}&lt;br /&gt;
To re-iterate the warning above: If the same passphrase had been used for key slots 1 and 6, both would be gone now.&lt;br /&gt;
&lt;br /&gt;
=== バックアップとリストア ===&lt;br /&gt;
If the header of a LUKS encrypted partition gets destroyed, you will not be able to decrypt your data. It is just as much of a dilemma as forgetting the passphrase or damaging a key-file used to unlock the partition. Damage may occur by your own fault while re-partitioning the disk later or by third-party programs misinterpreting the partition table. Therefore, having a backup of the header and storing it on another disk might be a good idea.&lt;br /&gt;
&lt;br /&gt;
{{Note|If the LUKS-encrypted  partitions&#039; master passphrase becomes compromised, you must revoke it on &#039;&#039;every&#039;&#039; copy of the cryptheader, even those you have backed up. Otherwise, a copy of the backed-up cryptheader that uses the compromised passphrase can be used to decrypt the associated partition. See [https://gitlab.com/cryptsetup/cryptsetup/wikis/FrequentlyAskedQuestions#6-backup-and-data-recovery LUKS FAQ] for further details.}}&lt;br /&gt;
&lt;br /&gt;
==== cryptsetup を使ってバックアップ ====&lt;br /&gt;
Cryptsetup&#039;s {{ic|luksHeaderBackup}} action stores a binary backup of the LUKS header and keyslot area:&lt;br /&gt;
 # cryptsetup luksHeaderBackup /dev/&amp;lt;device&amp;gt; --header-backup-file /mnt/&amp;lt;backup&amp;gt;/&amp;lt;file&amp;gt;.img&lt;br /&gt;
where &amp;lt;device&amp;gt; is the partition containing the LUKS volume.&lt;br /&gt;
&lt;br /&gt;
{{Tip|You can also back up the plaintext header into ramfs and encrypt it in example with gpg before writing to persistent backup storage by executing the following commands.}}&lt;br /&gt;
 # mkdir /root/&amp;lt;tmp&amp;gt;/&lt;br /&gt;
 # mount ramfs /root/&amp;lt;tmp&amp;gt;/ -t ramfs&lt;br /&gt;
 # cryptsetup luksHeaderBackup /dev/&amp;lt;device&amp;gt; --header-backup-file /root/&amp;lt;tmp&amp;gt;/&amp;lt;file&amp;gt;.img&lt;br /&gt;
 # gpg2 --recipient &amp;lt;User ID&amp;gt; --encrypt /root/&amp;lt;tmp&amp;gt;/&amp;lt;file&amp;gt;.img &lt;br /&gt;
 # cp /root/&amp;lt;tmp&amp;gt;/&amp;lt;file&amp;gt;.img.gpg /mnt/&amp;lt;backup&amp;gt;/&lt;br /&gt;
 # umount /root/&amp;lt;tmp&amp;gt;&lt;br /&gt;
{{Warning|Tmpfs can swap to harddisk if low on memory so it is not recommended here.}}&lt;br /&gt;
&lt;br /&gt;
==== cryptsetup を使ってリストア ====&lt;br /&gt;
&lt;br /&gt;
{{Warning|Restoring the wrong header or restoring to an unencrypted partition will cause data loss! The action can not perform a check whether the header is actually the &#039;&#039;correct&#039;&#039; one for that particular device.}} &lt;br /&gt;
&lt;br /&gt;
In order to evade restoring a wrong header, you can ensure it does work by using it as a remote {{ic|--header}} first: &lt;br /&gt;
&lt;br /&gt;
{{hc|# cryptsetup -v --header /mnt/&amp;lt;backup&amp;gt;/&amp;lt;file&amp;gt;.img open /dev/&amp;lt;device&amp;gt; test|&lt;br /&gt;
Key slot 0 unlocked.&lt;br /&gt;
Command successful.&lt;br /&gt;
}}&lt;br /&gt;
 # mount /dev/mapper/test /mnt/test &amp;amp;&amp;amp; ls /mnt/test &lt;br /&gt;
 # umount /mnt/test &lt;br /&gt;
 # cryptsetup close test &lt;br /&gt;
&lt;br /&gt;
Now that the check succeeded, the restore may be performed: &lt;br /&gt;
 # cryptsetup luksHeaderRestore /dev/&amp;lt;device&amp;gt; --header-backup-file ./mnt/&amp;lt;backup&amp;gt;/&amp;lt;file&amp;gt;.img&lt;br /&gt;
&lt;br /&gt;
Now that all the keyslot areas are overwritten; only active keyslots from the backup file are available after issuing the command.&lt;br /&gt;
&lt;br /&gt;
==== 手動バックアップとリストア ====&lt;br /&gt;
The header always resides at the beginning of the device and a backup can be performed without access to &#039;&#039;cryptsetup&#039;&#039; as well. First you have to find out the payload offset of the crypted partition:&lt;br /&gt;
{{hc|# cryptsetup luksDump /dev/&amp;lt;device&amp;gt; {{!}} grep &amp;quot;Payload offset&amp;quot;|&lt;br /&gt;
Payload offset:	4040}}&lt;br /&gt;
Second check the sector size of the drive&lt;br /&gt;
{{hc|# fdisk -l /dev/&amp;lt;device&amp;gt; {{!}} grep &amp;quot;Sector size&amp;quot;|Sector size (logical/physical): 512 bytes / 512 bytes}}&lt;br /&gt;
&lt;br /&gt;
Now that you know the values, you can backup the header with a simple dd command:&lt;br /&gt;
 # dd if=/dev/&amp;lt;device&amp;gt; of=/path/to/&amp;lt;file&amp;gt;.img bs=512 count=4040&lt;br /&gt;
and store it safely.&lt;br /&gt;
&lt;br /&gt;
A restore can then be performed using the same values as when backing up:&lt;br /&gt;
 # dd if=./&amp;lt;file&amp;gt;.img of=/dev/&amp;lt;device&amp;gt; bs=512 count=4040&lt;br /&gt;
&lt;br /&gt;
=== デバイスの再暗号化 ===&lt;br /&gt;
&lt;br /&gt;
The {{Pkg|cryptsetup}} package features the &#039;&#039;cryptsetup-reencrypt&#039;&#039; tool. It can be used to convert an existing unencrypted filesystem to a LUKS encrypted one (option {{ic|--new}}) and permanently remove LUKS encryption ({{ic|--decrypt}}) from a device. As its name suggests it can also be used to re-encrypt an existing LUKS encrypted device, though, re-encryption is not possible for a detached LUKS header or other encryption modes (e.g. plain-mode). For re-encryption it is possible to change the [[#LUKS モードの暗号化オプション]]. &#039;&#039;cryptsetup-reencrypt&#039;&#039; actions can be performed to unmounted devices only.  See {{man|8|cryptsetup-reencrypt}} for more information. &lt;br /&gt;
&lt;br /&gt;
One application of re-encryption may be to secure the data again after a passphrase or [[#キーファイル|keyfile]] has been compromised &#039;&#039;and&#039;&#039; one cannot be certain that no copy of the LUKS header has been obtained. For example, if only a passphrase has been shoulder-surfed but no physical/logical access to the device happened, it would be enough to change the respective passphrase/key only ([[#Key management]]). &lt;br /&gt;
&lt;br /&gt;
{{Warning|Always make sure a &#039;&#039;&#039;reliable backup&#039;&#039;&#039; is available and double-check options you specify before using the tool!}}&lt;br /&gt;
&lt;br /&gt;
The following shows an example to encrypt an unencrypted filesystem partition and a re-encryption of an existing LUKS device. &lt;br /&gt;
 &lt;br /&gt;
==== 暗号化されていないファイルシステムの暗号化 ====&lt;br /&gt;
&lt;br /&gt;
A LUKS encryption header is always stored at the beginning of the device. Since an existing filesystem will usually be allocated all partition sectors, the first step is to shrink it to make space for the LUKS header. &lt;br /&gt;
&lt;br /&gt;
The [[#LUKS モードの暗号化オプション|default]] LUKS header encryption cipher requires {{ic|4096}} 512-byte sectors. We already checked space and keep it simple by shrinking the existing {{ic|ext4}} filesystem on {{ic|/dev/sdaX}} to its current possible minimum: &lt;br /&gt;
&lt;br /&gt;
 # umount /mnt&lt;br /&gt;
{{hc|# e2fsck -f /dev/sdaX|&lt;br /&gt;
e2fsck 1.43-WIP (18-May-2015)&lt;br /&gt;
Pass 1: Checking inodes, blocks, and sizes&lt;br /&gt;
...&lt;br /&gt;
/dev/sda6: 12/166320 files (0.0% non-contiguous), 28783/665062 blocks&lt;br /&gt;
}}&lt;br /&gt;
{{hc|# resize2fs -M /dev/sdaX|&lt;br /&gt;
resize2fs 1.43-WIP (18-May-2015)&lt;br /&gt;
Resizing the filesystem on /dev/sdaX to 26347 (4k) blocks.&lt;br /&gt;
The filesystem on /dev/sdaX is now 26347 (4k) blocks long.}}&lt;br /&gt;
&lt;br /&gt;
Now we encrypt it, using the default cipher we do not have to specify it explicitly. Note there is no option (yet) to double-check the passphrase before encryption starts, be careful not to mistype: &lt;br /&gt;
&lt;br /&gt;
{{hc|# cryptsetup-reencrypt /dev/sdaX --new  --reduce-device-size 4096S|&lt;br /&gt;
WARNING: this is experimental code, it can completely break your data.&lt;br /&gt;
Enter new passphrase: &lt;br /&gt;
Progress: 100,0%, ETA 00:00, 2596 MiB written, speed  37,6 MiB/s}}&lt;br /&gt;
&lt;br /&gt;
After it finished, the encryption was performed to the full partition, i.e. not only the space the filesystem was shrunk to ({{ic|sdaX}} has {{ic|2.6GiB}} and the CPU used in the example has no hardware AES instructions). As a final step we extend the filesystem of the now encrypted device again to occupy available space: &lt;br /&gt;
&lt;br /&gt;
{{hc|# cryptsetup open /dev/sdaX recrypt|&lt;br /&gt;
Enter passphrase for /dev/sdaX: &lt;br /&gt;
...&lt;br /&gt;
}}&lt;br /&gt;
{{hc|# resize2fs /dev/mapper/recrypt|&lt;br /&gt;
resize2fs 1.43-WIP (18-May-2015)&lt;br /&gt;
Resizing the filesystem on /dev/mapper/recrypt to 664807 (4k) blocks.&lt;br /&gt;
The filesystem on /dev/mapper/recrypt is now 664807 (4k) blocks long.}}&lt;br /&gt;
 # mount /dev/mapper/recrypt /mnt&lt;br /&gt;
&lt;br /&gt;
and are done.&lt;br /&gt;
&lt;br /&gt;
==== 既存の LUKS パーティションの再暗号化 ====&lt;br /&gt;
&lt;br /&gt;
以下の例では既存の LUKS デバイスを最暗号化しています。&lt;br /&gt;
&lt;br /&gt;
{{Warning|Double-check you specify encryption options for &#039;&#039;cryptsetup-reencrypt&#039;&#039; correctly and &#039;&#039;never&#039;&#039; re-encrypt without a &#039;&#039;&#039;reliable backup&#039;&#039;&#039;! As of September 2015 the tool &#039;&#039;&#039;does&#039;&#039;&#039; accept invalid options and damage the LUKS header, if not used correctly!}}&lt;br /&gt;
&lt;br /&gt;
In order to re-encrypt a device with its existing encryption options, they do not need to be specified. A simple: &lt;br /&gt;
&lt;br /&gt;
{{hc|# cryptsetup-reencrypt /dev/sdaX| &lt;br /&gt;
WARNING: this is experimental code, it can completely break your data.&lt;br /&gt;
Enter passphrase for key slot 0: &lt;br /&gt;
Progress: 100,0%, ETA 00:00, 2596 MiB written, speed  36,5 MiB/s}}&lt;br /&gt;
&lt;br /&gt;
performs it. &lt;br /&gt;
&lt;br /&gt;
A possible usecase is to re-encrypt LUKS devices which have non-current encryption options. Apart from above warning on specifying options correctly, the ability to change the LUKS header may also be limited by its size. For example, if the device was initially encrypted using a CBC mode cipher and 128 bit key-size, the LUKS header will be half the size of above mentioned {{ic|4096}} sectors: &lt;br /&gt;
{{hc|# cryptsetup luksDump /dev/sdaX &amp;lt;nowiki&amp;gt;|&amp;lt;/nowiki&amp;gt;grep -e &amp;quot;mode&amp;quot; -e &amp;quot;Payload&amp;quot; -e &amp;quot;MK bits&amp;quot;|&lt;br /&gt;
Cipher mode:   	cbc-essiv:sha256&lt;br /&gt;
Payload offset:	&#039;&#039;&#039;2048&#039;&#039;&#039;&lt;br /&gt;
MK bits:       	128}}&lt;br /&gt;
While it is possible to upgrade the encryption of such a device, it is currently only feasible in two steps. First, re-encrypting with the same encryption options, but using the {{ic|--reduce-device-size}} option to make further space for the larger LUKS header. Second, re-encypt the whole device again with the desired cipher. For this reason and the fact that a backup should be created in any case, creating a new, fresh encrypted device to restore into is always the faster option.&lt;br /&gt;
&lt;br /&gt;
== 暗号化デバイスのリサイズ ==&lt;br /&gt;
&lt;br /&gt;
If a storage device encrypted with dm-crypt is being cloned (with a tool like dd) to another larger device, the underlying dm-crypt device must be resized to use the whole space. &lt;br /&gt;
&lt;br /&gt;
The destination device is /dev/sdX2 in this example, the whole available space adjacent to the partition will be used:&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup luksOpen /dev/sdX2 sdX2&lt;br /&gt;
 # cryptsetup resize sdX2&lt;br /&gt;
&lt;br /&gt;
Then the underlying filesystem must be resized.&lt;br /&gt;
&lt;br /&gt;
=== ループバックファイルシステム ===&lt;br /&gt;
&lt;br /&gt;
暗号化したループバックデバイスを {{ic|/mnt/secret}} にマウントしている場合、例えば [[Dm-crypt/root 以外のファイルシステムの暗号化#Loop デバイス]] に従っている場合、最初に暗号化コンテナをアンマウント:&lt;br /&gt;
 # umount /mnt/secret&lt;br /&gt;
 # cryptsetup close secret&lt;br /&gt;
 # losetup -d /dev/loop0&lt;br /&gt;
&lt;br /&gt;
次に、追加したいデータ容量を決めてコンテナファイルを拡張します:&lt;br /&gt;
&lt;br /&gt;
{{Warning|{{ic|&amp;gt;}} を2つ使っていることに注意してください。1つだけだと現在のコンテナを上書きしてしまいます。}}&lt;br /&gt;
 # dd if=/dev/urandom bs=1M count=1024 | cat - &amp;gt;&amp;gt; /bigsecret&lt;br /&gt;
&lt;br /&gt;
ループデバイスにコンテナをマップ:&lt;br /&gt;
 # losetup /dev/loop0 /bigsecret&lt;br /&gt;
 # cryptsetup --type luks open /dev/loop0 secret&lt;br /&gt;
&lt;br /&gt;
その後、コンテナの暗号化された部分をコンテナファイルの最大サイズまでリサイズ:&lt;br /&gt;
 # cryptsetup resize secret&lt;br /&gt;
&lt;br /&gt;
最後に、ファイルシステムのチェックを実行して、問題なければ、リサイズを行います (例: ext2/3/4):&lt;br /&gt;
 # e2fsck -f /dev/mapper/secret&lt;br /&gt;
 # resize2fs /dev/mapper/secret&lt;br /&gt;
&lt;br /&gt;
またコンテナをマウントすることができます:&lt;br /&gt;
 # mount /dev/mapper/secret /mnt/secret&lt;br /&gt;
&lt;br /&gt;
== キーファイル ==&lt;br /&gt;
&lt;br /&gt;
{{Note|このセクションでは平文のキーファイルを使う方法を説明しています。キーファイルを暗号化して二段階認証したい場合は[[Dm-crypt/特記事項#GPG や OpenSSL で暗号化されたキーファイルを使う|GPG や OpenSSL で暗号化されたキーファイルを使う]]を見て下さい。ただし、このセクションもあらかじめ読むようにしてください。}}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;キーファイルとは？&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
キーファイルは暗号化されたボリュームを解錠するパスフレーズとして使用するデータを含んだファイルです。ファイルを喪失した場合、ボリュームの復号化はできなくなります。&lt;br /&gt;
&lt;br /&gt;
{{Tip|Define a passphrase in addition to the keyfile for backup access to encrypted volumes in the event the defined keyfile is lost or changed.}}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;なぜキーファイルを使うのか？&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
キーファイルには様々なタイプがあります。以下で説明しているようにそれぞれメリットとデメリットが存在します:&lt;br /&gt;
&lt;br /&gt;
=== キーファイルのタイプ ===&lt;br /&gt;
==== passphrase ====&lt;br /&gt;
&lt;br /&gt;
単にパスフレーズが記述されたキーファイルです。メリットはファイルを無くしても暗号化ボリュームの所有者が簡単に中身を思い出せることです。デメリットはパスフレーズの入力と比べてセキュリティ上の優位点がないことです。&lt;br /&gt;
&lt;br /&gt;
例: {{ic|1234}}。&lt;br /&gt;
&lt;br /&gt;
{{Note|パスフレーズが記述されたキーファイルに改行を含めてはいけません。以下のようなコマンドでファイルを作成できます:&lt;br /&gt;
 # echo -n &#039;your_passphrase&#039; &amp;gt; /path/to/&amp;lt;keyfile&amp;gt;&lt;br /&gt;
 # chown root:root /path/to/&amp;lt;keyfile&amp;gt;; chmod 400 /path/to/&amp;lt;keyfile&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==== randomtext ====&lt;br /&gt;
&lt;br /&gt;
This is a keyfile containing a block of random characters. The benefit of this type of keyfile is that it is much more resistant to dictionary attacks than a simple passphrase. An additional strength of keyfiles can be utilized in this situation which is the length of data used. Since this is not a string meant to be memorized by a person for entry, it is trivial to create files containing thousands of random characters as the key. The disadvantage is that if this file is lost or changed, it will most likely not be possible to access the encrypted volume without a backup passphrase.&lt;br /&gt;
&lt;br /&gt;
例: {{ic|fjqweifj830149-57 819y4my1-38t1934yt8-91m 34co3;t8y;9p3y-}}。&lt;br /&gt;
&lt;br /&gt;
==== binary ====&lt;br /&gt;
&lt;br /&gt;
This is a binary file that has been defined as a keyfile. When identifying files as candidates for a keyfile, it is recommended to choose files that are relatively static such as photos, music, video clips. The benefit of these files is that they serve a dual function which can make them harder to identify as keyfiles. Instead of having a text file with a large amount of random text, the keyfile would look like a regular image file or music clip to the casual observer. The disadvantage is that if this file is lost or changed, it will most likely not be possible to access the encrypted volume without a backup passphrase. Additionally, there is a theoretical loss of randomness when compared to a randomly generated text file. This is due to the fact that images, videos and music have some intrinsic relationship between neighboring bits of data that does not exist for a text file. However this is controversial and has never been exploited publicly.&lt;br /&gt;
&lt;br /&gt;
例: 画像, テキスト, 動画。&lt;br /&gt;
&lt;br /&gt;
=== ランダムな文字列でキーファイルを作成 ===&lt;br /&gt;
&lt;br /&gt;
==== ファイルシステムにキーファイルを保存 ====&lt;br /&gt;
&lt;br /&gt;
キーファイルの中身とサイズは任意に決められます。&lt;br /&gt;
&lt;br /&gt;
以下では {{ic|dd}} を使って2048バイトのランダムなキーファイルを生成して、{{ic|/etc/mykeyfile}} ファイルに保存します:&lt;br /&gt;
&lt;br /&gt;
 # dd bs=512 count=4 if=/dev/urandom of=/etc/mykeyfile&lt;br /&gt;
&lt;br /&gt;
キーファイルを外部デバイスに保存する場合、出力先を適切なディレクトリに変更します:&lt;br /&gt;
&lt;br /&gt;
 # dd bs=512 count=4 if=/dev/urandom of=/media/usbstick/mykeyfile&lt;br /&gt;
&lt;br /&gt;
===== 保存されたキーファイルを完全に消去 =====&lt;br /&gt;
&lt;br /&gt;
物理的なストレージデバイスに一時的なキーファイルを保存する場合、削除するときは、以下のように完全に削除してください:&lt;br /&gt;
&lt;br /&gt;
 # shred --remove --zero mykeyfile&lt;br /&gt;
&lt;br /&gt;
FAT や ext2 であれば上記で十分ですが、ジャーナリングファイルシステムやフラッシュメモリハードウェアの場合、[[ディスクの完全消去]]あるいはキーファイルのあるパーティションを削除することを強く推奨します。&lt;br /&gt;
&lt;br /&gt;
==== ramfs にキーファイルを保存 ====&lt;br /&gt;
&lt;br /&gt;
もしくは、ramfs をマウントしてキーファイルを一時的に保存:&lt;br /&gt;
&lt;br /&gt;
 # mkdir /root/myramfs&lt;br /&gt;
 # mount ramfs /root/myramfs/ -t ramfs&lt;br /&gt;
 # cd /root/myramfs&lt;br /&gt;
&lt;br /&gt;
物理ディスクではなくメモリ上に配置することで、ramfs をアンマウントしたら復元できないという利点があります。キーファイルを他の安全なファイルシステムにコピーしたら、以下のコマンドで ramfs をアンマウントしてください:&lt;br /&gt;
 # umount /root/myramfs&lt;br /&gt;
&lt;br /&gt;
=== キーファイルを使用するように LUKS を設定 ===&lt;br /&gt;
&lt;br /&gt;
キーファイルのキースロットを LUKS ヘッダに追加:&lt;br /&gt;
&lt;br /&gt;
{{hc|# cryptsetup luksAddKey /dev/sda2 /etc/mykeyfile|&lt;br /&gt;
Enter any LUKS passphrase:&lt;br /&gt;
key slot 0 unlocked.&lt;br /&gt;
Command successful.}}&lt;br /&gt;
&lt;br /&gt;
=== キーファイルを使ってパーティションを手動でアンロック ===&lt;br /&gt;
&lt;br /&gt;
LUKS デバイスをオープンするときに {{ic|--key-file}} オプションを使ってください:&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup open /dev/sda2 &#039;&#039;dm_name&#039;&#039; --key-file /etc/mykeyfile&lt;br /&gt;
&lt;br /&gt;
=== 起動時にロックを解除 ===&lt;br /&gt;
&lt;br /&gt;
ファイルシステムのキーファイルを暗号化されたルートディレクトリの中に保存することで、システムの電源が切れているときに安全を確保することができ、[[Dm-crypt/システム設定#crypttab|crypttab]] によって起動時に自動的にマウントを解錠することができます。[[UUID]] を使用する例:&lt;br /&gt;
{{hc|/etc/crypttab|&amp;lt;nowiki&amp;gt;home    UUID=&amp;lt;UUID identifier&amp;gt;    /etc/mykeyfile&amp;lt;/nowiki&amp;gt;}}&lt;br /&gt;
is all needed for unlocking, and &lt;br /&gt;
{{hc|/etc/fstab|/dev/mapper/home        /home   ext4        defaults        0       2}} for mounting the LUKS blockdevice with the generated keyfile.&lt;br /&gt;
{{Tip|If you prefer to use a {{ic|--plain}} mode blockdevice, the encryption options necessary to unlock it are specified in {{ic|/etc/crypttab}}. Take care to apply the systemd workaround mentioned in [[Dm-crypt/システム設定#crypttab|crypttab]] in this case.}}&lt;br /&gt;
&lt;br /&gt;
=== キーファイルを使って起動時に root パーティションのロックを解除 ===&lt;br /&gt;
&lt;br /&gt;
[[mkinitcpio]] を設定して必要なモジュールやファイルを記述して [[Dm-crypt/システム設定#cryptkey|cryptkey]] [[カーネルパラメータ]]を設定してキーファイルの場所を指定します。&lt;br /&gt;
&lt;br /&gt;
2つの方法が存在します:&lt;br /&gt;
&lt;br /&gt;
# 外部メディア (USB スティック) にキーファイルを保存&lt;br /&gt;
# initramfs にキーファイルを埋め込む&lt;br /&gt;
&lt;br /&gt;
==== キーファイルを外部メディアに保存 ====&lt;br /&gt;
&lt;br /&gt;
===== mkinitcpio の設定 =====&lt;br /&gt;
&lt;br /&gt;
ドライブのファイルシステムを使うために {{ic|/etc/mkinitcpio.conf}} にモジュールを追加してください (以下の例では {{ic|vfat}} モジュール):&lt;br /&gt;
 MODULES=(vfat)&lt;br /&gt;
&lt;br /&gt;
In this example it is assumed that you use a FAT formatted USB drive ({{ic|vfat}} module). Replace those module names if you use another file system on your USB stick (e.g. {{ic|ext2}}) or another codepage. If it complains of bad superblock and bad codepage at boot, then you need an extra codepage module to be loaded. For instance, you may need {{ic|nls_iso8859-1}} module for {{ic|iso8859-1}} codepage.&lt;br /&gt;
&lt;br /&gt;
If you have a non-US keyboard, it might prove useful to load your keyboard layout before you are prompted to enter the password to unlock the root partition at boot. For this, you will need the {{ic|keymap}} hook before {{ic|encrypt}}.&lt;br /&gt;
&lt;br /&gt;
Generate a new initramfs image:&lt;br /&gt;
&lt;br /&gt;
 # mkinitcpio -p linux&lt;br /&gt;
&lt;br /&gt;
===== カーネルパラメータの設定 =====&lt;br /&gt;
&lt;br /&gt;
以下のオプションを[[カーネルパラメータ]]に追加してください ({{ic|encrypt}} フックではなく {{ic|sd-encrypt}} フックを使用する場合は [[Dm-crypt/システム設定#sd-encrypt フックを使う]]を参照):&lt;br /&gt;
&lt;br /&gt;
 cryptdevice=/dev/&#039;&#039;&amp;lt;partition1&amp;gt;&#039;&#039;:root cryptkey=/dev/&#039;&#039;&amp;lt;partition2&amp;gt;&#039;&#039;:&amp;lt;fstype&amp;gt;:&amp;lt;path&amp;gt;&lt;br /&gt;
&lt;br /&gt;
例:&lt;br /&gt;
&lt;br /&gt;
 cryptdevice=/dev/sda3:root cryptkey=/dev/sdb1:vfat:/keys/secretkey&lt;br /&gt;
&lt;br /&gt;
Choosing a plain filename for your key provides a bit of &#039;security through obscurity&#039;. The keyfile can not be a hidden file, that means the filename must not start with a dot, or the {{ic|encrypt}} hook will fail to find the keyfile during the boot process.&lt;br /&gt;
&lt;br /&gt;
{{ic|/dev/sdb1}} のようなデバイスノードの名前は再起動しても同じであるとは保証されていません。udev による[[永続的なブロックデバイスの命名]]を使ったほうが確実にデバイスにアクセスできます。外部ストレージデバイスからキーファイルを読み取るときに {{ic|encrypt}} フックが確実にキーファイルを見つけられるように、永続的なブロックデバイスの名前を絶対に使うべきです。[[永続的なブロックデバイスの命名]]を見て下さい。&lt;br /&gt;
&lt;br /&gt;
==== キーファイルを initramfs に埋め込む ====&lt;br /&gt;
&lt;br /&gt;
{{Warning|Use an embedded keyfile &#039;&#039;&#039;only&#039;&#039;&#039; if you have some form of authentication mechanism beforehand that protects the keyfile sufficiently. Otherwise auto-decryption will occur, defeating completely the purpose of block device encryption.}}&lt;br /&gt;
&lt;br /&gt;
This method allows to use a specially named keyfile that will be embedded in the [[initramfs]] and picked up by the {{ic|encrypt}} [[Mkinitcpio#HOOKS|hook]] to unlock the root filesystem ({{ic|cryptdevice}}) automatically. It may be useful to apply when using the [[GRUB#暗号化された /boot|GRUB early cryptodisk]] feature, in order to avoid entering two passphrases during boot.&lt;br /&gt;
&lt;br /&gt;
The {{ic|encrypt}} hook lets the user specify a keyfile with the {{ic|cryptkey}} kernel parameter: in the case of initramfs, the syntax is {{ic|rootfs:&#039;&#039;path&#039;&#039;}}, see [[Dm-crypt/システム設定#cryptkey]]. Besides, this kernel parameter defaults to use {{ic|/crypto_keyfile.bin}}, and if the initramfs contains a valid key with this name, decryption will occur automatically without the need to configure the {{ic|cryptkey}} parameter.&lt;br /&gt;
&lt;br /&gt;
{{ic|encrypt}} の代わりに {{ic|sd-encrypt}} を使用する場合、{{ic|luks.key}} カーネルパラメータでキーファイルの場所を指定してください。[[Dm-crypt/システム設定#luks.key]] を参照。&lt;br /&gt;
&lt;br /&gt;
[[#ランダムな文字列でキーファイルを作成|キーファイルを生成]]して適切な権限を与えて [[#LUKS キーの追加|LUKS キーとして追加]]:&lt;br /&gt;
&lt;br /&gt;
 # dd bs=512 count=4 if=/dev/urandom of=/crypto_keyfile.bin&lt;br /&gt;
 # chmod 000 /crypto_keyfile.bin&lt;br /&gt;
 # chmod 600 /boot/initramfs-linux*&lt;br /&gt;
 # cryptsetup luksAddKey /dev/sdX# /crypto_keyfile.bin&lt;br /&gt;
&lt;br /&gt;
{{Warning|When initramfs&#039; permissions are set to 644 (by default), then all users will be able to dump the keyfile. Make sure the permissions are still 600 if you install a new kernel.}}&lt;br /&gt;
&lt;br /&gt;
[[Mkinitcpio#BINARIES_と_FILES|mkinitcpio の FILES]] にキーを記述:&lt;br /&gt;
&lt;br /&gt;
{{hc|/etc/mkinitcpio.conf|2=FILES=(/crypto_keyfile.bin)}}&lt;br /&gt;
&lt;br /&gt;
最後に [[Mkinitcpio#イメージ作成とアクティベーション|initramfs を再生成]]してください。&lt;br /&gt;
&lt;br /&gt;
次の起動時からコンテナを復号化するパスフレーズを入力するのは一度だけですむようになります。&lt;br /&gt;
&lt;br /&gt;
詳しくは [http://www.pavelkogan.com/2014/05/23/luks-full-disk-encryption/#bonus-login-once こちら] を参照。&lt;/div&gt;</summary>
		<author><name>Takemaro</name></author>
	</entry>
	<entry>
		<id>https://wiki.archlinux.jp/index.php?title=Dm-crypt/%E3%83%87%E3%83%90%E3%82%A4%E3%82%B9%E3%81%AE%E6%9A%97%E5%8F%B7%E5%8C%96&amp;diff=39961</id>
		<title>Dm-crypt/デバイスの暗号化</title>
		<link rel="alternate" type="text/html" href="https://wiki.archlinux.jp/index.php?title=Dm-crypt/%E3%83%87%E3%83%90%E3%82%A4%E3%82%B9%E3%81%AE%E6%9A%97%E5%8F%B7%E5%8C%96&amp;diff=39961"/>
		<updated>2025-03-09T18:43:45Z</updated>

		<summary type="html">&lt;p&gt;Takemaro: /* cryptsetup でデバイスを暗号化 */ 翻訳済みの部分について最新の英語版を反映: GRUB の LUKS2 に関する警告、コマンド出力の更新、等&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Lowercase title}}&lt;br /&gt;
[[Category:保存データ暗号化]]&lt;br /&gt;
[[Category:ファイルシステム]]&lt;br /&gt;
[[en:Dm-crypt/Device encryption]]&lt;br /&gt;
このセクションではコマンドラインから &#039;&#039;dm-crypt&#039;&#039; を利用して手動でシステムを暗号化する方法を説明しています。&lt;br /&gt;
&lt;br /&gt;
== 準備 ==&lt;br /&gt;
{{pkg|cryptsetup}} を使用する前に、{{ic|dm_crypt}} [[カーネルモジュール]]がロードされていることを確認してください。&lt;br /&gt;
&lt;br /&gt;
== Cryptsetup の使用方法 ==&lt;br /&gt;
&#039;&#039;Cryptsetup&#039;&#039; は暗号化デバイスを作成・管理する &#039;&#039;dm-crypt&#039;&#039; を使うためのコマンドラインツールです。後に Linux カーネルの &#039;&#039;&#039;d&#039;&#039;&#039;evice-&#039;&#039;&#039;m&#039;&#039;&#039;apper と &#039;&#039;&#039;crypt&#039;&#039;&#039;ographic モジュールを使用する別の暗号化もサポートするように拡張されました。最も著しい拡張は Linux Unified Key Setup (LUKS) の拡張で、dm-crypt をセットアップするのに必要な情報を全てディスク自体に保存してパーティションとキーの管理を抽象化することで使いやすさを増しています。device-mapper によってアクセスされるデバイスはブロックデバイスと呼ばれます。詳しくは[[ディスク暗号化#ブロックデバイスの暗号化]]を見て下さい。&lt;br /&gt;
&lt;br /&gt;
ツールは以下のように使います:&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup &#039;&#039;action&#039;&#039; &#039;&#039;options&#039;&#039; &#039;&#039;device&#039;&#039; &#039;&#039;dmname&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
オプションや暗号化モードにはデフォルト値が存在し、コマンドラインで何も指定しなかった場合はデフォルト値が使われます。オプションや暗号化モードのデフォルトパラメータを確認するには以下のコマンドを実行:&lt;br /&gt;
&lt;br /&gt;
 $ cryptsetup --help &lt;br /&gt;
&lt;br /&gt;
オプションの完全なリストは man ページで確認できます。暗号化モードやアクションによって必要なパラメータは変わるため、以下のセクションでは違いについて説明しています。ブロックデバイス暗号化は高速ですが、速度は非常に重要な問題です。ブロックデバイスの暗号を設定後に変更することは難しいため、それぞれのパラメータについて &#039;&#039;dm-crypt&#039;&#039; のパフォーマンスをチェックすると良いでしょう:&lt;br /&gt;
&lt;br /&gt;
 $ cryptsetup benchmark &lt;br /&gt;
&lt;br /&gt;
上記のコマンドはインストールするときにアルゴリズムや鍵長を決める手がかりになります。特定の AES 暗号が非常に高速に処理される場合、おそらくその暗号は CPU によるハードウェア支援の恩恵に与かっています。&lt;br /&gt;
&lt;br /&gt;
{{Tip|テストしたいときは[[:カテゴリ:仮想化|仮想マシン]]の仮想ハードドライブで練習すると良いでしょう。}}&lt;br /&gt;
&lt;br /&gt;
=== Cryptsetup のパスフレーズとキー ===&lt;br /&gt;
暗号化されたブロックデバイスはキーによって保護されます。キーは以下のいずれかです:&lt;br /&gt;
&lt;br /&gt;
*パスフレーズ、[[セキュリティ#パスワード]]を見て下さい。&lt;br /&gt;
*キーファイル、[[#キーファイル]] を見て下さい。&lt;br /&gt;
&lt;br /&gt;
どちらのタイプのキーも最大サイズが決められています: パスフレーズは512文字まで、キーファイルは 8192kB までです。&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;LUKS&#039;&#039; の重要な特徴として、キーは LUKS によって暗号化されたデバイスのマスターキーを解除するのに使われ、root 権限で変えることができるということです。他の暗号化モードでは設定後にキーを変更することはできません。暗号化にマスターキーを使わないためです。詳しくは[[ディスク暗号化#ブロックデバイスの暗号化]]を見て下さい。&lt;br /&gt;
&lt;br /&gt;
== dm-crypt の暗号化オプション ==&lt;br /&gt;
&#039;&#039;Cryptsetup&#039;&#039; は &#039;&#039;dm-crypt&#039;&#039; で使用できる様々な暗号化モードをサポートしています。&lt;br /&gt;
* {{ic|--type luks}} - LUKS フォーマットのデフォルトバージョン ({{Pkg|cryptsetup}} &amp;lt; 2.1.0 ではLUKS1、{{Pkg|cryptsetup}} ≥ 2.1.0 では LUKS2) を使用。&lt;br /&gt;
* {{ic|--type luks1}} - LUKS の古いバージョンである LUKS1 を使用。&lt;br /&gt;
* {{ic|--type luks2}} - LUKS に追加の拡張が導入された現在のバージョンである LUKS2 を使用。&lt;br /&gt;
** {{ic|--hw-opal-only}} - TCG OPAL 標準をサポートするドライブでハードウェアベースの暗号化を使用。[[自己暗号化ドライブ]]や {{man|8|cryptsetup|SED (SELF ENCRYPTING DRIVE) OPAL EXTENSION}} も参照して下さい。&lt;br /&gt;
** {{ic|--hw-opal}} - OPAL によるハードウェア暗号化と dm-crypt によるソフトウェア暗号化の両方を使用。&lt;br /&gt;
*{{ic|--type plain}} - dm-crypt plain モードを使用。&lt;br /&gt;
*{{ic|--type loopaes}} - loopaes legacy モードを使用。&lt;br /&gt;
*{{ic|--type tcrypt}} - [[Truecrypt]] 互換モードを使用。&lt;br /&gt;
* {{ic|--type bitlk}} - [[Wikipedia:ja:BitLocker|BitLocker]] 互換モードを使用。 {{man|8|cryptsetup|BITLK (WINDOWS BITLOCKER COMPATIBLE) EXTENSION}} も参照して下さい。&lt;br /&gt;
&lt;br /&gt;
暗号やハッシュの基本的な暗号化オプションは全てのモードで使うことができ、カーネルの暗号化バックエンド機能を利用します。使用できるオプションは以下のコマンドで確認できます:&lt;br /&gt;
&lt;br /&gt;
 $ less /proc/crypto &lt;br /&gt;
&lt;br /&gt;
{{Tip|リストが短い場合、{{ic|cryptsetup benchmark}} を実行すればモジュールのロードが行われます。}}&lt;br /&gt;
&lt;br /&gt;
以下では LUKS モードと plain モードの暗号化オプションを紹介します。記事の中で例として使っているオプションを並べていますが、利用できるオプションの全てではないので注意してください。&lt;br /&gt;
&lt;br /&gt;
=== LUKS モードの暗号化オプション ===&lt;br /&gt;
LUKS 暗号化モードで新しい dm-crypt デバイスをセットアップする &#039;&#039;cryptsetup&#039;&#039; のアクションは &#039;&#039;luksFormat&#039;&#039; です。名前にはフォーマットとありますが、実際にはデバイスをフォーマットするのではなく、LUKS デバイスヘッダーを設定して指定された暗号オプションを使ってマスター鍵を暗号化します。&lt;br /&gt;
&lt;br /&gt;
デフォルトパラメータ ({{ic|cryptsetup --help}}で確認できます) で新しい LUKS デバイスを作成する場合、単に以下のコマンドを実行します:&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup luksFormat &#039;&#039;device&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
cryptsetup 2.4.0 の場合、これは以下と同等です:&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup luksFormat --type luks2 --cipher aes-xts-plain64 --hash sha256 --iter-time 2000 --key-size 256 --pbkdf argon2id --use-urandom --verify-passphrase &#039;&#039;device&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
以下の表でデフォルト値とサンプルにコメントを付けて説明しています:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; style=&amp;quot;text-align:left&amp;quot; | オプション&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; style=&amp;quot;text-align:left&amp;quot; | Cryptsetup 2.1.0 のデフォルト&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; style=&amp;quot;text-align:left&amp;quot; | 例&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; style=&amp;quot;text-align:left&amp;quot; | コメント&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;row&amp;quot; style=&amp;quot;text-align:right&amp;quot; | --cipher, -c&lt;br /&gt;
| {{ic|aes-xts-plain64}}&lt;br /&gt;
| {{ic|aes-xts-plain64}}&lt;br /&gt;
| [https://www.kernel.org/pub/linux/utils/cryptsetup/v1.6/v1.6.0-ReleaseNotes バージョン 1.6.0]からデフォルトが AES [[ディスク暗号化#暗号と利用形態|暗号]]と [[wikipedia:Disk encryption theory#XEX-based tweaked-codebook mode with ciphertext stealing (XTS)|XTS]] を使用するよう変わりました ([https://gitlab.com/cryptsetup/cryptsetup/wikis/FrequentlyAskedQuestions#5-security-aspects FAQ] の 5.16 も参照)。以前のデフォルトである  {{ic|--cipher aes-cbc-essiv}} は、[[wikipedia:Disk encryption theory#Cipher-block chaining (CBC)|既知の問題]]があり、[https://www.jakoblell.com/blog/2013/12/22/practical-malleability-attack-against-cbc-encrypted-luks-partitions/ 実際の攻撃]も存在するため、使用しないことをお勧めします。&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;row&amp;quot; style=&amp;quot;text-align:right&amp;quot; | --key-size, -s&lt;br /&gt;
| {{ic|256}} (XTSでは {{ic|512}})&lt;br /&gt;
| {{ic|512}}&lt;br /&gt;
| XTS モードの暗号では 512 ビットがデフォルトです。[[wikipedia:Disk encryption theory#XEX-based tweaked-codebook mode with ciphertext stealing (XTS)|XTS はキーを半分に割る]]ので、これは AES-256 が使われることを意味します。&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;row&amp;quot; style=&amp;quot;text-align:right&amp;quot; | --hash, -h&lt;br /&gt;
| {{ic|sha256}}&lt;br /&gt;
| {{ic|sha512}}&lt;br /&gt;
| [[ディスク暗号化#暗号メタデータ|鍵導出]] で使用されるハッシュアルゴリズム。リリース 1.7.0 でデフォルト設定が {{ic|sha1}} から {{ic|sha256}} に変更されました。セキュリティ上の理由ではなく SHA1 が使用できないシステムでも動作するようにするためです [https://www.kernel.org/pub/linux/utils/cryptsetup/v1.7/v1.7.0-ReleaseNotes]。{{ic|sha1}} でも十分セキュアであるため古いバージョンの &#039;&#039;cryptsetup&#039;&#039; と互換性を維持する目的で使用できます [https://gitlab.com/cryptsetup/cryptsetup/wikis/FrequentlyAskedQuestions#5-security-aspects]。&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;row&amp;quot; style=&amp;quot;text-align:right&amp;quot; | --iter-time, -i&lt;br /&gt;
| {{ic|2000}}&lt;br /&gt;
| {{ic|5000}}&lt;br /&gt;
| PBKDF2 パスフレーズを処理するのに費やす時間 (ミリ秒単位)。&amp;quot;&#039;&#039;PBKDF2 の反復回数を高く保持したままユーザーが許容できる数値にするため&#039;&#039;&amp;quot;、リリース 1.7.0 からデフォルト値が {{ic|1000}} から {{ic|2000}} に変更されています [https://www.kernel.org/pub/linux/utils/cryptsetup/v1.7/v1.7.0-ReleaseNotes]。&#039;&#039;luksFormat&#039;&#039; や &#039;&#039;luksAddKey&#039;&#039; などパスフレーズを設定・変更する LUKS 操作のみに関連するオプションです。パラメータとして 0 を指定するとコンパイル時のデフォルトが選択されます。&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;row&amp;quot; style=&amp;quot;text-align:right&amp;quot; | --use-{u,}random&lt;br /&gt;
| {{ic|--use-&#039;&#039;&#039;u&#039;&#039;&#039;random}}&lt;br /&gt;
| {{ic|--use-random}}&lt;br /&gt;
| [[乱数生成|乱数生成器]]の選択。現在は {{ic|/dev/random}} と {{ic|/dev/urandom}} は等価なので、 {{ic|--use-random}} と {{ic|--use-urandom}} も等価です。&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;row&amp;quot; style=&amp;quot;text-align:right&amp;quot; | --verify-passphrase, -y&lt;br /&gt;
| Yes&lt;br /&gt;
| -&lt;br /&gt;
| {{ic|luksFormat}} と {{ic|luksAddKey}} で使われます。Arch Linux ではデフォルトで有効です。&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;row&amp;quot; style=&amp;quot;text-align:right&amp;quot; | --sector-size&lt;br /&gt;
| {{ic|512}} または {{ic|4096}} (デバイスに依存)&lt;br /&gt;
| {{ic|4096}}&lt;br /&gt;
| ディスク暗号化で使われるセクタサイズを設定します。デフォルトは、4Kn や 512e のブロックデバイスでは {{ic|4096}} で、512n のブロックデバイスでは {{ic|512}} です。最近のストレージデバイスのほとんどでは、セクタサイズを 512 バイトから 4096 バイトにすることでパフォーマンスが向上します。[[アドバンスドフォーマット#dm-crypt]]を見て下さい。&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
LUKS の機能やオプションについては [https://gitlab.com/cryptsetup/cryptsetup/-/raw/master/docs/on-disk-format.pdf LUKS1 の仕様]や [https://gitlab.com/cryptsetup/cryptsetup/-/raw/master/docs/on-disk-format-luks2.pdf LUKS2の仕様] で説明されています。&lt;br /&gt;
{{Tip|開発者によるプレゼンテーション [https://mbroz.fedorapeople.org/talks/DevConf2016/devconf2016-luks2.pdf devconfcz2016] (pdf) では、LUKS2 での主な仕様変更の背景がまとめられています。cryptsetup のメジャーバージョンである[https://mirrors.edge.kernel.org/pub/linux/utils/cryptsetup/v2.0/v2.0.0-ReleaseNotes バージョン 2 のリリースノート]も見て下さい。}}&lt;br /&gt;
&lt;br /&gt;
==== イテレーション時間 ====&lt;br /&gt;
[https://gitlab.com/cryptsetup/cryptsetup/-/wikis/FrequentlyAskedQuestions#2-setup cryptsetup FAQ§2.1] と [https://gitlab.com/cryptsetup/cryptsetup/-/wikis/FrequentlyAskedQuestions#3-common-problems §3.4]から:&lt;br /&gt;
&lt;br /&gt;
:鍵スロットを解除するのにかかる時間は[...]パスフレーズを設定した時に決まります。デフォルトでは1秒 (LUKS2 では2秒) です。[...]&lt;br /&gt;
:パスフレーズをイテレーションする回数は時間によって決まり、したがってセキュリティレベルは LUKS コンテナを作成したマシンの CPU の能力に依存します。[...]&lt;br /&gt;
:もし高速なマシンでパスフレーズを設定し、低速なマシンでそれを解除した場合、解除にかかる時間は長くなります。&lt;br /&gt;
&lt;br /&gt;
このことから、コンテナを作成する際は、そのコンテナに最も頻繁にアクセスすることになるマシンの上で作成するのが良いでしょう。&lt;br /&gt;
&lt;br /&gt;
イテレーション回数を正しく調節する方法については、上の記事の残りの部分を読んで下さい。&lt;br /&gt;
&lt;br /&gt;
=== plain モードの暗号化オプション ===&lt;br /&gt;
dm-crypt の &#039;&#039;plain&#039;&#039; モードでは、デバイスのマスター鍵が存在せず、セットアップも必要ありません。代わりに、直接暗号化オプションを使用して暗号ディスクと名前付きデバイスのマッピングを作成します。パーティションやデバイス全体に対してマッピングを作成できます。後者の場合、パーティションテーブルも不要です。&lt;br /&gt;
&lt;br /&gt;
cryptsetup のデフォルトパラメータを使って &#039;&#039;plain&#039;&#039; のマッピングを作成するには:&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup open --type plain &#039;&#039;options&#039;&#039; &#039;&#039;device&#039;&#039; &#039;&#039;dmname&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
実行するとパスワードを求められます。非常に高いエントロピーのパスワードが必要です。デフォルトではありませんが、 {{ic|--verify-passphrase}} オプションを使えます。&lt;br /&gt;
&lt;br /&gt;
一般論として、作成時の暗号化オプションは精確に記録しておくと良いでしょう。暗号化されたデバイス上にはオプションの内容は記録されていませんし、デフォルトのオプションは変わる可能性があります。&lt;br /&gt;
&lt;br /&gt;
以下は [[Dm-crypt/システム全体の暗号化#Plain dm-crypt]] の例とデフォルトパラメータの比較表です。&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! オプション !! Cryptsetup (2.7.0) のデフォルト !! 例 !! コメント !! &lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;--hash, -h&#039;&#039;&#039; || {{ic|sha256}} || - || パスフレーズからキーを作成するのに使用するハッシュ。キーファイルを用いる場合は使われない。&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;--cipher, -c&#039;&#039;&#039;|| {{ic|aes-xts-plain64}}|| {{ic|aes-xts-plain64}} || 暗号は3つの文字列からなります: cipher-chainmode-IV generator。[[ディスク暗号化#暗号と利用形態]]や [https://gitlab.com/cryptsetup/cryptsetup/wikis/DMCrypt DMCrypt のドキュメント] を見てください。&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;--key-size, -s&#039;&#039;&#039;||{{ic|256}}||{{ic|512}}||キーサイズ (ビット数)。サイズは使用する暗号や使用するチェインモードによって変わります。XTS モードは CBC モードの2倍のキーサイズを必要とします。&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;--size, -b&#039;&#039;&#039;||対象ディスクの実際のサイズ||- (デフォルト)||512バイト単位のセクタ数。デバイスの最大サイズ以下。&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;--offset, -o&#039;&#039;&#039;||{{ic|0}}||{{ic|0}}||マッピングを開始するディスクの先頭からのオフセット。512バイト単位のセクタ数。&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;--skip, -p&#039;&#039;&#039;||{{ic|0}}||{{ic|2048}} (512B×2048=1MiB をスキップ)||初期ベクトル (IV) を計算する際にスキップする暗号化されたデータのサイズ。512バイト単位のセクタ数。&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;--key-file, -d&#039;&#039;&#039;||デフォルトではパスフレーズを使用||{{ic|/dev/sd&#039;&#039;Z&#039;&#039;}} (もしくは {{ic|/boot/keyfile.enc}})||キーとして使用するデバイスまたはファイル。詳しくは [[#キーファイル]] を参照。&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;--keyfile-offset&#039;&#039;&#039;||{{ic|0}}||{{ic|0}}||キーファイルの先頭からのオフセット (バイト数)。&#039;&#039;cryptsetup&#039;&#039; 1.6.7 以上でサポートされているオプション。&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;--keyfile-size, -l&#039;&#039;&#039;||{{ic|8192kB}}||- (デフォルト)||キーファイルから読み込まれるバイト数を制限。&#039;&#039;cryptsetup&#039;&#039; 1.6.7 以上でサポートされているオプション。&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;--sector-size, -l&#039;&#039;&#039;||{{ic|512}}||{{ic|4096}}||ディスク暗号化の際に使われるセクタサイズ。4Kn のブロックデバイス以外では、 {{ic|512}} がデフォルト。最近のストレージデバイスのほとんどでは、セクタサイズを 512 バイトから 4096 バイトにすることでパフォーマンスが向上します。[[アドバンスドフォーマット#dm-crypt]]を見て下さい。&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
{{ic|/dev/sd&#039;&#039;X&#039;&#039;}} デバイスで、上記の例を使用する場合:&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup open --type plain --cipher=aes-xts-plain64 --offset=0 --skip=2048 --key-file=/dev/sd&#039;&#039;Z&#039;&#039; --key-size=512 --sector-size 4096 /dev/sdX enc&lt;br /&gt;
&lt;br /&gt;
{{Warning|LUKS による暗号化と違って、上記のコマンドはマッピングを再現するために毎回 &#039;&#039;&#039;全て&#039;&#039;&#039; 指定する必要があります。暗号やハッシュ、キーファイルについて覚えておく必要があります。cryptsetup のデフォルトはバージョンによって変わるので、デフォルトに頼らないで下さい。}}&lt;br /&gt;
&lt;br /&gt;
マッピングが作成されたことは以下のコマンドで確認できます:&lt;br /&gt;
 # fdisk -l&lt;br /&gt;
{{ic|/dev/mapper/enc}} のエントリが存在しているはずです。&lt;br /&gt;
&lt;br /&gt;
== cryptsetup でデバイスを暗号化 ==&lt;br /&gt;
以下のセクションでは新しい暗号化ブロックデバイスを作成するオプションと手動のアクセス方法を説明します。&lt;br /&gt;
&lt;br /&gt;
{{Warning|GRUB の LUKS2 サポートは限られています。[[GRUB#暗号化された /boot]] を見て下さい。GRUB が解錠する必要のあるパーティションには、LUKS2 を PBKDF2 で使用 ({{ic|cryptsetup luksFormat --pbkdf pbkdf2}})して下さい。}}&lt;br /&gt;
&lt;br /&gt;
=== LUKS モードでデバイスを暗号化 ===&lt;br /&gt;
==== LUKS パーティションのフォーマット ====&lt;br /&gt;
&lt;br /&gt;
暗号化 LUKS パーティションとして設定するには次を実行:&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup luksFormat &#039;&#039;device&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
上記のコマンドを実行するとパスワードの入力が要求されます。&lt;br /&gt;
&lt;br /&gt;
コマンドラインオプションは [[#LUKS モードの暗号化オプション]]を参照。&lt;br /&gt;
&lt;br /&gt;
結果は次のコマンドで確認できます:&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup luksDump &#039;&#039;device&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
ダンプでは暗号のヘッダー情報だけでなく、LUKS パーティションで使われるキースロットもわかります。&lt;br /&gt;
&lt;br /&gt;
以下の例はデフォルトの AES 暗号を使って256ビットの XTS モードで {{ic|/dev/sda1}} に暗号化したルートパーティションを作成します:&lt;br /&gt;
 # cryptsetup luksFormat -s 512 /dev/sda1&lt;br /&gt;
&lt;br /&gt;
=====LUKS を使ってキーファイルでパーティションをフォーマット=====&lt;br /&gt;
&lt;br /&gt;
LUKS による暗号化パーティションを新しく作成する場合、以下のように作成するときにパーティションにキーファイルを関連付けることができます:&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup luksFormat &#039;&#039;device&#039;&#039; &#039;&#039;/path/to/mykeyfile&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
キーファイルを作成・管理する方法は [[#キーファイル]] を見て下さい。&lt;br /&gt;
&lt;br /&gt;
====デバイスマッパーで LUKS パーティションのロックを解除・マップ====&lt;br /&gt;
&lt;br /&gt;
LUKS パーティションを作成したら、解錠することができます。&lt;br /&gt;
&lt;br /&gt;
解錠ではデバイスマッパーによってパーティションが新しいデバイス名にマッピングされます。カーネルは {{ic|&#039;&#039;device&#039;&#039;}} が暗号化デバイスであり暗号化データを上書きしないように {{ic|/dev/mapper/&#039;&#039;dm_name&#039;&#039;}} を使って LUKS 経由で解決する必要があることを知ります。間違って上書きしないために、設定が完了したら[[#バックアップとリストア|暗号化ヘッダーをバックアップ]]する方法を確認してください。&lt;br /&gt;
&lt;br /&gt;
暗号化された LUKS パーティションを開くには次のコマンドを実行:&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup open --type luks &#039;&#039;device&#039;&#039; &#039;&#039;dm_name&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
実行するとパーティションを解錠するパスワードの入力が求められます。通常、マッピングするデバイスの名前はパーティションの機能が分かりやすいものにします。例えば、以下のコマンドは luks パーティション {{ic|/dev/sda1}} を解錠して {{ic|cryptroot}} という名前のデバイスマッパーにマッピングします:&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup open --type luks /dev/sda1 cryptroot &lt;br /&gt;
&lt;br /&gt;
パーティションをオープンしたら、ルートパーティションのデバイスアドレスはパーティション本体 (例: {{ic|/dev/sda1}}) の代わりに {{ic|/dev/mapper/cryptroot}} が使われます。&lt;br /&gt;
&lt;br /&gt;
暗号化レイヤーの上に LVM をセットアップする場合、復号化したボリュームグループのデバイスファイルは {{ic|/dev/sda1}} ではなく {{ic|/dev/mapper/cryptroot}} などとなります。それから LVM は作成されている論理ボリューム全てに名前を割り当てます (例: {{ic|/dev/mapper/lvmpool-root}} や {{ic|/dev/mapper/lvmpool-swap}})。&lt;br /&gt;
&lt;br /&gt;
暗号化データをパーティションに書き込むときは、マッピングされたデバイス名を使ってアクセスする必要があります。例えば[[ファイルシステム#デバイスのフォーマット|ファイルシステムを作成]]するときは以下のようにします:&lt;br /&gt;
&lt;br /&gt;
 # mkfs -t ext4 /dev/mapper/cryptroot&lt;br /&gt;
&lt;br /&gt;
{{ic|/dev/mapper/cryptroot}} デバイスは他のパーティションと同じように[[マウント]]できます。&lt;br /&gt;
&lt;br /&gt;
luks コンテナを閉じるには、パーティションをアンマウントし、以下を実行して下さい:&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup close cryptroot&lt;br /&gt;
&lt;br /&gt;
==== 鍵を TPM に保管する ====&lt;br /&gt;
&lt;br /&gt;
[[Trusted Platform Module#LUKS による保存データの暗号化]]を見て下さい。&lt;br /&gt;
&lt;br /&gt;
=== plain モードでデバイスを暗号化 ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;dm-crypt&#039;&#039; プレーンモード暗号化の作成とその後のアクセスはどちらも、正しい [[#plain モードの暗号化オプション|オプション]] を指定して &#039;&#039;cryptsetup&#039;&#039; {{ic|open}} アクションを使用するだけで済みます。以下は、非ルートデバイスの2つの例で示していますが、両方をスタックすることで癖が追加されています (つまり、2番目は最初のデバイス内に作成されます)  明らかに、暗号化をスタックするとオーバーヘッドが2倍になります。 ここでの使用例は、暗号オプションの使用法の別の例を説明するためのものです。&lt;br /&gt;
&lt;br /&gt;
上記の表の左側の列で説明されているように、最初のマッパーは &#039;&#039;cryptsetup&#039;&#039; のプレーンモードのデフォルトで作成されます。&lt;br /&gt;
{{hc|# cryptsetup --type plain -v open /dev/sdaX plain1|&lt;br /&gt;
WARNING: Using default options for cipher (aes-xts-plain64, key size 256 bits) that could be incompatible with older versions.&lt;br /&gt;
WARNING: Using default options for hash (sha256) that could be incompatible with older versions.&lt;br /&gt;
For plain mode, always use options --cipher, --key-size and if no keyfile is used, then also --hash.&lt;br /&gt;
Enter passphrase for /dev/sd&#039;&#039;xY&#039;&#039;:&lt;br /&gt;
Command successful.&lt;br /&gt;
}}&lt;br /&gt;
次に、さまざまな暗号化パラメーターを使用し、(オプションの) オフセットを使用して、その中に2番目のブロックデバイスを追加し、ファイルシステムを作成してマウントします。&lt;br /&gt;
{{hc|1=# cryptsetup --type plain --cipher=serpent-xts-plain64 --hash=sha256 --key-size=256 --offset=10  open /dev/mapper/plain1 plain2|2=&lt;br /&gt;
Enter passphrase for /dev/mapper/plain1:&lt;br /&gt;
}}&lt;br /&gt;
{{hc|# lsblk -p|&lt;br /&gt;
 NAME                                                     &lt;br /&gt;
 /dev/sda                                     &lt;br /&gt;
 ├─/dev/sdaX          &lt;br /&gt;
 │ └─/dev/mapper/plain1     &lt;br /&gt;
 │   └─/dev/mapper/plain2              &lt;br /&gt;
 ...&lt;br /&gt;
}}&lt;br /&gt;
 # mkfs -t ext2 /dev/mapper/plain2&lt;br /&gt;
 # mount -t ext2 /dev/mapper/plain2 /mnt&lt;br /&gt;
 # echo &amp;quot;This is stacked. one passphrase per foot to shoot.&amp;quot; &amp;gt; /mnt/stacked.txt&lt;br /&gt;
We close the stack to check access works&lt;br /&gt;
 # cryptsetup close plain2&lt;br /&gt;
 # cryptsetup close plain1&lt;br /&gt;
First, let&#039;s try to open the filesystem directly: &lt;br /&gt;
 # cryptsetup --type plain --cipher=serpent-xts-plain64 --hash=sha256 --key-size=256 --offset=10 open /dev/sdaX plain2&lt;br /&gt;
{{hc|# mount -t ext2 /dev/mapper/plain2 /mnt|&lt;br /&gt;
mount: wrong fs type, bad option, bad superblock on /dev/mapper/plain2,&lt;br /&gt;
      missing codepage or helper program, or other error&lt;br /&gt;
}}&lt;br /&gt;
Why that did not work? Because the &amp;quot;plain2&amp;quot; starting block ({{ic|10}}) is still encrypted with the cipher from &amp;quot;plain1&amp;quot;. It can only be accessed via the stacked mapper. The error is arbitrary though, trying a wrong passphrase or wrong options will yield the same. For &#039;&#039;dm-crypt&#039;&#039; plain mode, the {{ic|open}} action will not error out itself. &lt;br /&gt;
&lt;br /&gt;
Trying again in correct order: &lt;br /&gt;
 # cryptsetup close plain2    # dysfunctional mapper from previous try&lt;br /&gt;
{{hc|# cryptsetup --type plain open /dev/sdaX plain1|&lt;br /&gt;
Enter passphrase: &lt;br /&gt;
}}&lt;br /&gt;
{{hc|1=# cryptsetup --type plain --cipher=serpent-xts-plain64 --hash=sha256 --key-size=256 --offset=10 open /dev/mapper/plain1 plain2|2=&lt;br /&gt;
Enter passphrase: &lt;br /&gt;
}}&lt;br /&gt;
{{hc|# mount /dev/mapper/plain2 /mnt &amp;amp;&amp;amp; cat /mnt/stacked.txt|&lt;br /&gt;
This is stacked. one passphrase per foot to shoot.&lt;br /&gt;
}}&lt;br /&gt;
 # exit&lt;br /&gt;
&#039;&#039;dm-crypt&#039;&#039; will handle stacked encryption with some mixed modes too. For example LUKS mode could be stacked on the &amp;quot;plain1&amp;quot; mapper. Its header would then be encrypted inside &amp;quot;plain1&amp;quot; when that is closed.&lt;br /&gt;
&lt;br /&gt;
Available for plain mode only is the option {{ic|--shared}}. With it a single device can be segmented into different non-overlapping mappers. We do that in the next example, using a &#039;&#039;loopaes&#039;&#039; compatible cipher mode for &amp;quot;plain2&amp;quot; this time: &lt;br /&gt;
{{hc|# cryptsetup --type plain --offset 0 --size 1000 open /dev/sdaX plain1|2=&lt;br /&gt;
Enter passphrase: &lt;br /&gt;
}}&lt;br /&gt;
{{hc|1=# cryptsetup --type plain --offset 1000 --size 1000 --shared --cipher=aes-cbc-lmk --hash=sha256 open /dev/sdaX plain2|2=&lt;br /&gt;
Enter passphrase: &lt;br /&gt;
}}&lt;br /&gt;
{{hc|# lsblk -p|&lt;br /&gt;
NAME                    &lt;br /&gt;
dev/sdaX                    &lt;br /&gt;
├─/dev/sdaX               &lt;br /&gt;
│ ├─/dev/mapper/plain1     &lt;br /&gt;
│ └─/dev/mapper/plain2     &lt;br /&gt;
...&lt;br /&gt;
}}&lt;br /&gt;
As the devicetree shows both reside on the same level, i.e. are not stacked and &amp;quot;plain2&amp;quot; can be opened individually.&lt;br /&gt;
&lt;br /&gt;
== LUKS 固有の Cryptsetup のアクション ==&lt;br /&gt;
=== キーの管理 ===&lt;br /&gt;
LUKS パーティションには異なる8つまでのキーを定義することができます。バックアップストレージのためのアクセスキーを作成することが可能です。いわゆるキーエスクローで、あるキーは日常的に使用するために使い、他のキーはエスクローに保存して日常用のパスフレーズを忘れたときやキーファイルを喪失・破損したときにパーティションにアクセスするために使います。また、別のキースロットを使って一時的なキーを発行してユーザーにパーティションへのアクセスを許可し、後でキーを無効化したりできます。&lt;br /&gt;
&lt;br /&gt;
暗号化パーティションを作成すると、初期キースロット 0 が作成されます (手動で別の数字を指定しなかった場合)。追加キースロットは 1 から 7 までの数字が割り振られます。どのキースロットを使用しているかは以下のコマンドで確認できます (&amp;lt;device&amp;gt; は LUKS ヘッダーが存在するボリュームに置き換えてください):&lt;br /&gt;
{{hc|# cryptsetup luksDump /dev/&amp;lt;device&amp;gt; {{!}} grep BLED|&lt;br /&gt;
Key Slot 0: ENABLED&lt;br /&gt;
Key Slot 1: ENABLED&lt;br /&gt;
Key Slot 2: ENABLED&lt;br /&gt;
Key Slot 3: DISABLED&lt;br /&gt;
Key Slot 4: DISABLED&lt;br /&gt;
Key Slot 5: DISABLED&lt;br /&gt;
Key Slot 6: DISABLED&lt;br /&gt;
Key Slot 7: DISABLED}}&lt;br /&gt;
&lt;br /&gt;
上記のコマンドや下で説明しているコマンドはヘッダーのバックアップファイルでも使えます。&lt;br /&gt;
&lt;br /&gt;
==== LUKS キーの追加 ====&lt;br /&gt;
&lt;br /&gt;
新しいキースロットの追加は cryptsetup の {{ic|luksAddKey}} アクションを使うことでできます。デバイスが解錠されていても安全のために、新しいキーを入力する前に、必ず既存のキーの入力が求められます (&amp;quot;any passphrase&amp;quot;):&lt;br /&gt;
&lt;br /&gt;
{{hc|# cryptsetup luksAddKey /dev/&amp;lt;device&amp;gt; (/path/to/&amp;lt;additionalkeyfile&amp;gt;)|&lt;br /&gt;
Enter any passphrase:&lt;br /&gt;
Enter new passphrase for key slot:&lt;br /&gt;
Verify passphrase: &lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
{{ic|/path/to/&amp;lt;additionalkeyfile&amp;gt;}} を指定した場合、cryptsetup は &amp;lt;additionalkeyfile&amp;gt; の新しいキースロットを追加します。指定しなかった場合は新しいパスフレーズの入力が2回要求されます。既存の&#039;&#039;キーファイル&#039;&#039;を使って認証するには、{{ic|--key-file}} または {{ic|-d}} オプションの後に&amp;quot;既存&amp;quot;の &amp;lt;keyfile&amp;gt; を指定することで全てのキーファイルのキースロットが解除されます:&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup luksAddKey /dev/&amp;lt;device&amp;gt; (/path/to/&amp;lt;additionalkeyfile&amp;gt;) -d /path/to/&amp;lt;keyfile&amp;gt;&lt;br /&gt;
&lt;br /&gt;
複数のキーを使っていてキーを変更・無効化する場合、{{ic|--key-slot}} または {{ic|-S}} オプションでスロットを指定できます:&lt;br /&gt;
{{hc|# cryptsetup luksAddKey /dev/&amp;lt;device&amp;gt; -S 6|&lt;br /&gt;
Enter any passphrase: &lt;br /&gt;
Enter new passphrase for key slot: &lt;br /&gt;
Verify passphrase:&lt;br /&gt;
}}&lt;br /&gt;
{{hc|# cryptsetup luksDump /dev/sda8 {{!}} grep &#039;Slot 6&#039;|&lt;br /&gt;
Key Slot 6: ENABLED&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
例えばキーを変更したい場合:&lt;br /&gt;
{{hc|# cryptsetup luksChangeKey /dev/&amp;lt;device&amp;gt; -S 6|&lt;br /&gt;
Enter LUKS passphrase to be changed: &lt;br /&gt;
Enter new LUKS passphrase: &lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==== LUKS キーの削除 ====&lt;br /&gt;
&lt;br /&gt;
ヘッダーからキーを削除するアクションは3つ存在します:&lt;br /&gt;
*{{ic|luksRemoveKey}} はパスフレーズやキーファイルを指定してキーを削除します。&lt;br /&gt;
*{{ic|luksKillSlot}} は (他のキーを使って) 指定したキースロットからキーを削除します。パスフレーズを忘れたり、キーファイルを無くしたり、誰もアクセスできないようにしたい場合に有用です。&lt;br /&gt;
*{{ic|luksErase}} は全ての有効なキーを即座に削除します。&lt;br /&gt;
{{warning|&lt;br /&gt;
*All above actions can be used to irrevocably delete the last active key for an encrypted device! &lt;br /&gt;
*The {{ic|luksErase}} command was added in version 1.6.4 to quickly nuke access to the device. This action &#039;&#039;&#039;will not&#039;&#039;&#039; prompt for a valid passphrase! It will not [[Dm-crypt/ドライブの準備#LUKS ヘッダーを消去|wipe the LUKS header]], but all keyslots at once and you will, therefore, not be able to regain access unless you have a valid backup of the LUKS header.}} &lt;br /&gt;
&lt;br /&gt;
For above warning it is good to know the key we want to &#039;&#039;&#039;keep&#039;&#039;&#039; is valid. An easy check is to unlock the device with the {{ic|-v}} option, which will specify which slot it occupies:  &lt;br /&gt;
{{hc|# cryptsetup -v open /dev/&amp;lt;device&amp;gt; testcrypt|&lt;br /&gt;
Enter passphrase for /dev/&amp;lt;device&amp;gt;: &lt;br /&gt;
Key slot 1 unlocked.&lt;br /&gt;
Command successful.}}&lt;br /&gt;
&lt;br /&gt;
Now we can remove the key added in the previous subsection using its passphrase: &lt;br /&gt;
{{hc|# cryptsetup luksRemoveKey /dev/&amp;lt;device&amp;gt;|&lt;br /&gt;
Enter LUKS passphrase to be deleted: &lt;br /&gt;
}}&lt;br /&gt;
If we had used the same passphrase for two keyslots, the first slot would be wiped now. Only executing it again would remove the second one. &lt;br /&gt;
&lt;br /&gt;
Alternatively, we can specify the key slot: &lt;br /&gt;
{{hc|# cryptsetup luksKillSlot /dev/&amp;lt;device&amp;gt; 6|&lt;br /&gt;
Enter any remaining LUKS passphrase:&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
Note that in both cases, no confirmation was required.&lt;br /&gt;
{{hc|# cryptsetup luksDump /dev/sda8 {{!}} grep &#039;Slot 6&#039;|&lt;br /&gt;
Key Slot 6: DISABLED&lt;br /&gt;
}}&lt;br /&gt;
To re-iterate the warning above: If the same passphrase had been used for key slots 1 and 6, both would be gone now.&lt;br /&gt;
&lt;br /&gt;
=== バックアップとリストア ===&lt;br /&gt;
If the header of a LUKS encrypted partition gets destroyed, you will not be able to decrypt your data. It is just as much of a dilemma as forgetting the passphrase or damaging a key-file used to unlock the partition. Damage may occur by your own fault while re-partitioning the disk later or by third-party programs misinterpreting the partition table. Therefore, having a backup of the header and storing it on another disk might be a good idea.&lt;br /&gt;
&lt;br /&gt;
{{Note|If the LUKS-encrypted  partitions&#039; master passphrase becomes compromised, you must revoke it on &#039;&#039;every&#039;&#039; copy of the cryptheader, even those you have backed up. Otherwise, a copy of the backed-up cryptheader that uses the compromised passphrase can be used to decrypt the associated partition. See [https://gitlab.com/cryptsetup/cryptsetup/wikis/FrequentlyAskedQuestions#6-backup-and-data-recovery LUKS FAQ] for further details.}}&lt;br /&gt;
&lt;br /&gt;
==== cryptsetup を使ってバックアップ ====&lt;br /&gt;
Cryptsetup&#039;s {{ic|luksHeaderBackup}} action stores a binary backup of the LUKS header and keyslot area:&lt;br /&gt;
 # cryptsetup luksHeaderBackup /dev/&amp;lt;device&amp;gt; --header-backup-file /mnt/&amp;lt;backup&amp;gt;/&amp;lt;file&amp;gt;.img&lt;br /&gt;
where &amp;lt;device&amp;gt; is the partition containing the LUKS volume.&lt;br /&gt;
&lt;br /&gt;
{{Tip|You can also back up the plaintext header into ramfs and encrypt it in example with gpg before writing to persistent backup storage by executing the following commands.}}&lt;br /&gt;
 # mkdir /root/&amp;lt;tmp&amp;gt;/&lt;br /&gt;
 # mount ramfs /root/&amp;lt;tmp&amp;gt;/ -t ramfs&lt;br /&gt;
 # cryptsetup luksHeaderBackup /dev/&amp;lt;device&amp;gt; --header-backup-file /root/&amp;lt;tmp&amp;gt;/&amp;lt;file&amp;gt;.img&lt;br /&gt;
 # gpg2 --recipient &amp;lt;User ID&amp;gt; --encrypt /root/&amp;lt;tmp&amp;gt;/&amp;lt;file&amp;gt;.img &lt;br /&gt;
 # cp /root/&amp;lt;tmp&amp;gt;/&amp;lt;file&amp;gt;.img.gpg /mnt/&amp;lt;backup&amp;gt;/&lt;br /&gt;
 # umount /root/&amp;lt;tmp&amp;gt;&lt;br /&gt;
{{Warning|Tmpfs can swap to harddisk if low on memory so it is not recommended here.}}&lt;br /&gt;
&lt;br /&gt;
==== cryptsetup を使ってリストア ====&lt;br /&gt;
&lt;br /&gt;
{{Warning|Restoring the wrong header or restoring to an unencrypted partition will cause data loss! The action can not perform a check whether the header is actually the &#039;&#039;correct&#039;&#039; one for that particular device.}} &lt;br /&gt;
&lt;br /&gt;
In order to evade restoring a wrong header, you can ensure it does work by using it as a remote {{ic|--header}} first: &lt;br /&gt;
&lt;br /&gt;
{{hc|# cryptsetup -v --header /mnt/&amp;lt;backup&amp;gt;/&amp;lt;file&amp;gt;.img open /dev/&amp;lt;device&amp;gt; test|&lt;br /&gt;
Key slot 0 unlocked.&lt;br /&gt;
Command successful.&lt;br /&gt;
}}&lt;br /&gt;
 # mount /dev/mapper/test /mnt/test &amp;amp;&amp;amp; ls /mnt/test &lt;br /&gt;
 # umount /mnt/test &lt;br /&gt;
 # cryptsetup close test &lt;br /&gt;
&lt;br /&gt;
Now that the check succeeded, the restore may be performed: &lt;br /&gt;
 # cryptsetup luksHeaderRestore /dev/&amp;lt;device&amp;gt; --header-backup-file ./mnt/&amp;lt;backup&amp;gt;/&amp;lt;file&amp;gt;.img&lt;br /&gt;
&lt;br /&gt;
Now that all the keyslot areas are overwritten; only active keyslots from the backup file are available after issuing the command.&lt;br /&gt;
&lt;br /&gt;
==== 手動バックアップとリストア ====&lt;br /&gt;
The header always resides at the beginning of the device and a backup can be performed without access to &#039;&#039;cryptsetup&#039;&#039; as well. First you have to find out the payload offset of the crypted partition:&lt;br /&gt;
{{hc|# cryptsetup luksDump /dev/&amp;lt;device&amp;gt; {{!}} grep &amp;quot;Payload offset&amp;quot;|&lt;br /&gt;
Payload offset:	4040}}&lt;br /&gt;
Second check the sector size of the drive&lt;br /&gt;
{{hc|# fdisk -l /dev/&amp;lt;device&amp;gt; {{!}} grep &amp;quot;Sector size&amp;quot;|Sector size (logical/physical): 512 bytes / 512 bytes}}&lt;br /&gt;
&lt;br /&gt;
Now that you know the values, you can backup the header with a simple dd command:&lt;br /&gt;
 # dd if=/dev/&amp;lt;device&amp;gt; of=/path/to/&amp;lt;file&amp;gt;.img bs=512 count=4040&lt;br /&gt;
and store it safely.&lt;br /&gt;
&lt;br /&gt;
A restore can then be performed using the same values as when backing up:&lt;br /&gt;
 # dd if=./&amp;lt;file&amp;gt;.img of=/dev/&amp;lt;device&amp;gt; bs=512 count=4040&lt;br /&gt;
&lt;br /&gt;
=== デバイスの再暗号化 ===&lt;br /&gt;
&lt;br /&gt;
The {{Pkg|cryptsetup}} package features the &#039;&#039;cryptsetup-reencrypt&#039;&#039; tool. It can be used to convert an existing unencrypted filesystem to a LUKS encrypted one (option {{ic|--new}}) and permanently remove LUKS encryption ({{ic|--decrypt}}) from a device. As its name suggests it can also be used to re-encrypt an existing LUKS encrypted device, though, re-encryption is not possible for a detached LUKS header or other encryption modes (e.g. plain-mode). For re-encryption it is possible to change the [[#LUKS モードの暗号化オプション]]. &#039;&#039;cryptsetup-reencrypt&#039;&#039; actions can be performed to unmounted devices only.  See {{man|8|cryptsetup-reencrypt}} for more information. &lt;br /&gt;
&lt;br /&gt;
One application of re-encryption may be to secure the data again after a passphrase or [[#キーファイル|keyfile]] has been compromised &#039;&#039;and&#039;&#039; one cannot be certain that no copy of the LUKS header has been obtained. For example, if only a passphrase has been shoulder-surfed but no physical/logical access to the device happened, it would be enough to change the respective passphrase/key only ([[#Key management]]). &lt;br /&gt;
&lt;br /&gt;
{{Warning|Always make sure a &#039;&#039;&#039;reliable backup&#039;&#039;&#039; is available and double-check options you specify before using the tool!}}&lt;br /&gt;
&lt;br /&gt;
The following shows an example to encrypt an unencrypted filesystem partition and a re-encryption of an existing LUKS device. &lt;br /&gt;
 &lt;br /&gt;
==== 暗号化されていないファイルシステムの暗号化 ====&lt;br /&gt;
&lt;br /&gt;
A LUKS encryption header is always stored at the beginning of the device. Since an existing filesystem will usually be allocated all partition sectors, the first step is to shrink it to make space for the LUKS header. &lt;br /&gt;
&lt;br /&gt;
The [[#LUKS モードの暗号化オプション|default]] LUKS header encryption cipher requires {{ic|4096}} 512-byte sectors. We already checked space and keep it simple by shrinking the existing {{ic|ext4}} filesystem on {{ic|/dev/sdaX}} to its current possible minimum: &lt;br /&gt;
&lt;br /&gt;
 # umount /mnt&lt;br /&gt;
{{hc|# e2fsck -f /dev/sdaX|&lt;br /&gt;
e2fsck 1.43-WIP (18-May-2015)&lt;br /&gt;
Pass 1: Checking inodes, blocks, and sizes&lt;br /&gt;
...&lt;br /&gt;
/dev/sda6: 12/166320 files (0.0% non-contiguous), 28783/665062 blocks&lt;br /&gt;
}}&lt;br /&gt;
{{hc|# resize2fs -M /dev/sdaX|&lt;br /&gt;
resize2fs 1.43-WIP (18-May-2015)&lt;br /&gt;
Resizing the filesystem on /dev/sdaX to 26347 (4k) blocks.&lt;br /&gt;
The filesystem on /dev/sdaX is now 26347 (4k) blocks long.}}&lt;br /&gt;
&lt;br /&gt;
Now we encrypt it, using the default cipher we do not have to specify it explicitly. Note there is no option (yet) to double-check the passphrase before encryption starts, be careful not to mistype: &lt;br /&gt;
&lt;br /&gt;
{{hc|# cryptsetup-reencrypt /dev/sdaX --new  --reduce-device-size 4096S|&lt;br /&gt;
WARNING: this is experimental code, it can completely break your data.&lt;br /&gt;
Enter new passphrase: &lt;br /&gt;
Progress: 100,0%, ETA 00:00, 2596 MiB written, speed  37,6 MiB/s}}&lt;br /&gt;
&lt;br /&gt;
After it finished, the encryption was performed to the full partition, i.e. not only the space the filesystem was shrunk to ({{ic|sdaX}} has {{ic|2.6GiB}} and the CPU used in the example has no hardware AES instructions). As a final step we extend the filesystem of the now encrypted device again to occupy available space: &lt;br /&gt;
&lt;br /&gt;
{{hc|# cryptsetup open /dev/sdaX recrypt|&lt;br /&gt;
Enter passphrase for /dev/sdaX: &lt;br /&gt;
...&lt;br /&gt;
}}&lt;br /&gt;
{{hc|# resize2fs /dev/mapper/recrypt|&lt;br /&gt;
resize2fs 1.43-WIP (18-May-2015)&lt;br /&gt;
Resizing the filesystem on /dev/mapper/recrypt to 664807 (4k) blocks.&lt;br /&gt;
The filesystem on /dev/mapper/recrypt is now 664807 (4k) blocks long.}}&lt;br /&gt;
 # mount /dev/mapper/recrypt /mnt&lt;br /&gt;
&lt;br /&gt;
and are done.&lt;br /&gt;
&lt;br /&gt;
==== 既存の LUKS パーティションの再暗号化 ====&lt;br /&gt;
&lt;br /&gt;
以下の例では既存の LUKS デバイスを最暗号化しています。&lt;br /&gt;
&lt;br /&gt;
{{Warning|Double-check you specify encryption options for &#039;&#039;cryptsetup-reencrypt&#039;&#039; correctly and &#039;&#039;never&#039;&#039; re-encrypt without a &#039;&#039;&#039;reliable backup&#039;&#039;&#039;! As of September 2015 the tool &#039;&#039;&#039;does&#039;&#039;&#039; accept invalid options and damage the LUKS header, if not used correctly!}}&lt;br /&gt;
&lt;br /&gt;
In order to re-encrypt a device with its existing encryption options, they do not need to be specified. A simple: &lt;br /&gt;
&lt;br /&gt;
{{hc|# cryptsetup-reencrypt /dev/sdaX| &lt;br /&gt;
WARNING: this is experimental code, it can completely break your data.&lt;br /&gt;
Enter passphrase for key slot 0: &lt;br /&gt;
Progress: 100,0%, ETA 00:00, 2596 MiB written, speed  36,5 MiB/s}}&lt;br /&gt;
&lt;br /&gt;
performs it. &lt;br /&gt;
&lt;br /&gt;
A possible usecase is to re-encrypt LUKS devices which have non-current encryption options. Apart from above warning on specifying options correctly, the ability to change the LUKS header may also be limited by its size. For example, if the device was initially encrypted using a CBC mode cipher and 128 bit key-size, the LUKS header will be half the size of above mentioned {{ic|4096}} sectors: &lt;br /&gt;
{{hc|# cryptsetup luksDump /dev/sdaX &amp;lt;nowiki&amp;gt;|&amp;lt;/nowiki&amp;gt;grep -e &amp;quot;mode&amp;quot; -e &amp;quot;Payload&amp;quot; -e &amp;quot;MK bits&amp;quot;|&lt;br /&gt;
Cipher mode:   	cbc-essiv:sha256&lt;br /&gt;
Payload offset:	&#039;&#039;&#039;2048&#039;&#039;&#039;&lt;br /&gt;
MK bits:       	128}}&lt;br /&gt;
While it is possible to upgrade the encryption of such a device, it is currently only feasible in two steps. First, re-encrypting with the same encryption options, but using the {{ic|--reduce-device-size}} option to make further space for the larger LUKS header. Second, re-encypt the whole device again with the desired cipher. For this reason and the fact that a backup should be created in any case, creating a new, fresh encrypted device to restore into is always the faster option.&lt;br /&gt;
&lt;br /&gt;
== 暗号化デバイスのリサイズ ==&lt;br /&gt;
&lt;br /&gt;
If a storage device encrypted with dm-crypt is being cloned (with a tool like dd) to another larger device, the underlying dm-crypt device must be resized to use the whole space. &lt;br /&gt;
&lt;br /&gt;
The destination device is /dev/sdX2 in this example, the whole available space adjacent to the partition will be used:&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup luksOpen /dev/sdX2 sdX2&lt;br /&gt;
 # cryptsetup resize sdX2&lt;br /&gt;
&lt;br /&gt;
Then the underlying filesystem must be resized.&lt;br /&gt;
&lt;br /&gt;
=== ループバックファイルシステム ===&lt;br /&gt;
&lt;br /&gt;
暗号化したループバックデバイスを {{ic|/mnt/secret}} にマウントしている場合、例えば [[Dm-crypt/root 以外のファイルシステムの暗号化#Loop デバイス]] に従っている場合、最初に暗号化コンテナをアンマウント:&lt;br /&gt;
 # umount /mnt/secret&lt;br /&gt;
 # cryptsetup close secret&lt;br /&gt;
 # losetup -d /dev/loop0&lt;br /&gt;
&lt;br /&gt;
次に、追加したいデータ容量を決めてコンテナファイルを拡張します:&lt;br /&gt;
&lt;br /&gt;
{{Warning|{{ic|&amp;gt;}} を2つ使っていることに注意してください。1つだけだと現在のコンテナを上書きしてしまいます。}}&lt;br /&gt;
 # dd if=/dev/urandom bs=1M count=1024 | cat - &amp;gt;&amp;gt; /bigsecret&lt;br /&gt;
&lt;br /&gt;
ループデバイスにコンテナをマップ:&lt;br /&gt;
 # losetup /dev/loop0 /bigsecret&lt;br /&gt;
 # cryptsetup --type luks open /dev/loop0 secret&lt;br /&gt;
&lt;br /&gt;
その後、コンテナの暗号化された部分をコンテナファイルの最大サイズまでリサイズ:&lt;br /&gt;
 # cryptsetup resize secret&lt;br /&gt;
&lt;br /&gt;
最後に、ファイルシステムのチェックを実行して、問題なければ、リサイズを行います (例: ext2/3/4):&lt;br /&gt;
 # e2fsck -f /dev/mapper/secret&lt;br /&gt;
 # resize2fs /dev/mapper/secret&lt;br /&gt;
&lt;br /&gt;
またコンテナをマウントすることができます:&lt;br /&gt;
 # mount /dev/mapper/secret /mnt/secret&lt;br /&gt;
&lt;br /&gt;
== キーファイル ==&lt;br /&gt;
&lt;br /&gt;
{{Note|このセクションでは平文のキーファイルを使う方法を説明しています。キーファイルを暗号化して二段階認証したい場合は[[Dm-crypt/特記事項#GPG や OpenSSL で暗号化されたキーファイルを使う|GPG や OpenSSL で暗号化されたキーファイルを使う]]を見て下さい。ただし、このセクションもあらかじめ読むようにしてください。}}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;キーファイルとは？&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
キーファイルは暗号化されたボリュームを解錠するパスフレーズとして使用するデータを含んだファイルです。ファイルを喪失した場合、ボリュームの復号化はできなくなります。&lt;br /&gt;
&lt;br /&gt;
{{Tip|Define a passphrase in addition to the keyfile for backup access to encrypted volumes in the event the defined keyfile is lost or changed.}}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;なぜキーファイルを使うのか？&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
キーファイルには様々なタイプがあります。以下で説明しているようにそれぞれメリットとデメリットが存在します:&lt;br /&gt;
&lt;br /&gt;
=== キーファイルのタイプ ===&lt;br /&gt;
==== passphrase ====&lt;br /&gt;
&lt;br /&gt;
単にパスフレーズが記述されたキーファイルです。メリットはファイルを無くしても暗号化ボリュームの所有者が簡単に中身を思い出せることです。デメリットはパスフレーズの入力と比べてセキュリティ上の優位点がないことです。&lt;br /&gt;
&lt;br /&gt;
例: {{ic|1234}}。&lt;br /&gt;
&lt;br /&gt;
{{Note|パスフレーズが記述されたキーファイルに改行を含めてはいけません。以下のようなコマンドでファイルを作成できます:&lt;br /&gt;
 # echo -n &#039;your_passphrase&#039; &amp;gt; /path/to/&amp;lt;keyfile&amp;gt;&lt;br /&gt;
 # chown root:root /path/to/&amp;lt;keyfile&amp;gt;; chmod 400 /path/to/&amp;lt;keyfile&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==== randomtext ====&lt;br /&gt;
&lt;br /&gt;
This is a keyfile containing a block of random characters. The benefit of this type of keyfile is that it is much more resistant to dictionary attacks than a simple passphrase. An additional strength of keyfiles can be utilized in this situation which is the length of data used. Since this is not a string meant to be memorized by a person for entry, it is trivial to create files containing thousands of random characters as the key. The disadvantage is that if this file is lost or changed, it will most likely not be possible to access the encrypted volume without a backup passphrase.&lt;br /&gt;
&lt;br /&gt;
例: {{ic|fjqweifj830149-57 819y4my1-38t1934yt8-91m 34co3;t8y;9p3y-}}。&lt;br /&gt;
&lt;br /&gt;
==== binary ====&lt;br /&gt;
&lt;br /&gt;
This is a binary file that has been defined as a keyfile. When identifying files as candidates for a keyfile, it is recommended to choose files that are relatively static such as photos, music, video clips. The benefit of these files is that they serve a dual function which can make them harder to identify as keyfiles. Instead of having a text file with a large amount of random text, the keyfile would look like a regular image file or music clip to the casual observer. The disadvantage is that if this file is lost or changed, it will most likely not be possible to access the encrypted volume without a backup passphrase. Additionally, there is a theoretical loss of randomness when compared to a randomly generated text file. This is due to the fact that images, videos and music have some intrinsic relationship between neighboring bits of data that does not exist for a text file. However this is controversial and has never been exploited publicly.&lt;br /&gt;
&lt;br /&gt;
例: 画像, テキスト, 動画。&lt;br /&gt;
&lt;br /&gt;
=== ランダムな文字列でキーファイルを作成 ===&lt;br /&gt;
&lt;br /&gt;
==== ファイルシステムにキーファイルを保存 ====&lt;br /&gt;
&lt;br /&gt;
キーファイルの中身とサイズは任意に決められます。&lt;br /&gt;
&lt;br /&gt;
以下では {{ic|dd}} を使って2048バイトのランダムなキーファイルを生成して、{{ic|/etc/mykeyfile}} ファイルに保存します:&lt;br /&gt;
&lt;br /&gt;
 # dd bs=512 count=4 if=/dev/urandom of=/etc/mykeyfile&lt;br /&gt;
&lt;br /&gt;
キーファイルを外部デバイスに保存する場合、出力先を適切なディレクトリに変更します:&lt;br /&gt;
&lt;br /&gt;
 # dd bs=512 count=4 if=/dev/urandom of=/media/usbstick/mykeyfile&lt;br /&gt;
&lt;br /&gt;
===== 保存されたキーファイルを完全に消去 =====&lt;br /&gt;
&lt;br /&gt;
物理的なストレージデバイスに一時的なキーファイルを保存する場合、削除するときは、以下のように完全に削除してください:&lt;br /&gt;
&lt;br /&gt;
 # shred --remove --zero mykeyfile&lt;br /&gt;
&lt;br /&gt;
FAT や ext2 であれば上記で十分ですが、ジャーナリングファイルシステムやフラッシュメモリハードウェアの場合、[[ディスクの完全消去]]あるいはキーファイルのあるパーティションを削除することを強く推奨します。&lt;br /&gt;
&lt;br /&gt;
==== ramfs にキーファイルを保存 ====&lt;br /&gt;
&lt;br /&gt;
もしくは、ramfs をマウントしてキーファイルを一時的に保存:&lt;br /&gt;
&lt;br /&gt;
 # mkdir /root/myramfs&lt;br /&gt;
 # mount ramfs /root/myramfs/ -t ramfs&lt;br /&gt;
 # cd /root/myramfs&lt;br /&gt;
&lt;br /&gt;
物理ディスクではなくメモリ上に配置することで、ramfs をアンマウントしたら復元できないという利点があります。キーファイルを他の安全なファイルシステムにコピーしたら、以下のコマンドで ramfs をアンマウントしてください:&lt;br /&gt;
 # umount /root/myramfs&lt;br /&gt;
&lt;br /&gt;
=== キーファイルを使用するように LUKS を設定 ===&lt;br /&gt;
&lt;br /&gt;
キーファイルのキースロットを LUKS ヘッダに追加:&lt;br /&gt;
&lt;br /&gt;
{{hc|# cryptsetup luksAddKey /dev/sda2 /etc/mykeyfile|&lt;br /&gt;
Enter any LUKS passphrase:&lt;br /&gt;
key slot 0 unlocked.&lt;br /&gt;
Command successful.}}&lt;br /&gt;
&lt;br /&gt;
=== キーファイルを使ってパーティションを手動でアンロック ===&lt;br /&gt;
&lt;br /&gt;
LUKS デバイスをオープンするときに {{ic|--key-file}} オプションを使ってください:&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup open /dev/sda2 &#039;&#039;dm_name&#039;&#039; --key-file /etc/mykeyfile&lt;br /&gt;
&lt;br /&gt;
=== 起動時にロックを解除 ===&lt;br /&gt;
&lt;br /&gt;
ファイルシステムのキーファイルを暗号化されたルートディレクトリの中に保存することで、システムの電源が切れているときに安全を確保することができ、[[Dm-crypt/システム設定#crypttab|crypttab]] によって起動時に自動的にマウントを解錠することができます。[[UUID]] を使用する例:&lt;br /&gt;
{{hc|/etc/crypttab|&amp;lt;nowiki&amp;gt;home    UUID=&amp;lt;UUID identifier&amp;gt;    /etc/mykeyfile&amp;lt;/nowiki&amp;gt;}}&lt;br /&gt;
is all needed for unlocking, and &lt;br /&gt;
{{hc|/etc/fstab|/dev/mapper/home        /home   ext4        defaults        0       2}} for mounting the LUKS blockdevice with the generated keyfile.&lt;br /&gt;
{{Tip|If you prefer to use a {{ic|--plain}} mode blockdevice, the encryption options necessary to unlock it are specified in {{ic|/etc/crypttab}}. Take care to apply the systemd workaround mentioned in [[Dm-crypt/システム設定#crypttab|crypttab]] in this case.}}&lt;br /&gt;
&lt;br /&gt;
=== キーファイルを使って起動時に root パーティションのロックを解除 ===&lt;br /&gt;
&lt;br /&gt;
[[mkinitcpio]] を設定して必要なモジュールやファイルを記述して [[Dm-crypt/システム設定#cryptkey|cryptkey]] [[カーネルパラメータ]]を設定してキーファイルの場所を指定します。&lt;br /&gt;
&lt;br /&gt;
2つの方法が存在します:&lt;br /&gt;
&lt;br /&gt;
# 外部メディア (USB スティック) にキーファイルを保存&lt;br /&gt;
# initramfs にキーファイルを埋め込む&lt;br /&gt;
&lt;br /&gt;
==== キーファイルを外部メディアに保存 ====&lt;br /&gt;
&lt;br /&gt;
===== mkinitcpio の設定 =====&lt;br /&gt;
&lt;br /&gt;
ドライブのファイルシステムを使うために {{ic|/etc/mkinitcpio.conf}} にモジュールを追加してください (以下の例では {{ic|vfat}} モジュール):&lt;br /&gt;
 MODULES=(vfat)&lt;br /&gt;
&lt;br /&gt;
In this example it is assumed that you use a FAT formatted USB drive ({{ic|vfat}} module). Replace those module names if you use another file system on your USB stick (e.g. {{ic|ext2}}) or another codepage. If it complains of bad superblock and bad codepage at boot, then you need an extra codepage module to be loaded. For instance, you may need {{ic|nls_iso8859-1}} module for {{ic|iso8859-1}} codepage.&lt;br /&gt;
&lt;br /&gt;
If you have a non-US keyboard, it might prove useful to load your keyboard layout before you are prompted to enter the password to unlock the root partition at boot. For this, you will need the {{ic|keymap}} hook before {{ic|encrypt}}.&lt;br /&gt;
&lt;br /&gt;
Generate a new initramfs image:&lt;br /&gt;
&lt;br /&gt;
 # mkinitcpio -p linux&lt;br /&gt;
&lt;br /&gt;
===== カーネルパラメータの設定 =====&lt;br /&gt;
&lt;br /&gt;
以下のオプションを[[カーネルパラメータ]]に追加してください ({{ic|encrypt}} フックではなく {{ic|sd-encrypt}} フックを使用する場合は [[Dm-crypt/システム設定#sd-encrypt フックを使う]]を参照):&lt;br /&gt;
&lt;br /&gt;
 cryptdevice=/dev/&#039;&#039;&amp;lt;partition1&amp;gt;&#039;&#039;:root cryptkey=/dev/&#039;&#039;&amp;lt;partition2&amp;gt;&#039;&#039;:&amp;lt;fstype&amp;gt;:&amp;lt;path&amp;gt;&lt;br /&gt;
&lt;br /&gt;
例:&lt;br /&gt;
&lt;br /&gt;
 cryptdevice=/dev/sda3:root cryptkey=/dev/sdb1:vfat:/keys/secretkey&lt;br /&gt;
&lt;br /&gt;
Choosing a plain filename for your key provides a bit of &#039;security through obscurity&#039;. The keyfile can not be a hidden file, that means the filename must not start with a dot, or the {{ic|encrypt}} hook will fail to find the keyfile during the boot process.&lt;br /&gt;
&lt;br /&gt;
{{ic|/dev/sdb1}} のようなデバイスノードの名前は再起動しても同じであるとは保証されていません。udev による[[永続的なブロックデバイスの命名]]を使ったほうが確実にデバイスにアクセスできます。外部ストレージデバイスからキーファイルを読み取るときに {{ic|encrypt}} フックが確実にキーファイルを見つけられるように、永続的なブロックデバイスの名前を絶対に使うべきです。[[永続的なブロックデバイスの命名]]を見て下さい。&lt;br /&gt;
&lt;br /&gt;
==== キーファイルを initramfs に埋め込む ====&lt;br /&gt;
&lt;br /&gt;
{{Warning|Use an embedded keyfile &#039;&#039;&#039;only&#039;&#039;&#039; if you have some form of authentication mechanism beforehand that protects the keyfile sufficiently. Otherwise auto-decryption will occur, defeating completely the purpose of block device encryption.}}&lt;br /&gt;
&lt;br /&gt;
This method allows to use a specially named keyfile that will be embedded in the [[initramfs]] and picked up by the {{ic|encrypt}} [[Mkinitcpio#HOOKS|hook]] to unlock the root filesystem ({{ic|cryptdevice}}) automatically. It may be useful to apply when using the [[GRUB#暗号化された /boot|GRUB early cryptodisk]] feature, in order to avoid entering two passphrases during boot.&lt;br /&gt;
&lt;br /&gt;
The {{ic|encrypt}} hook lets the user specify a keyfile with the {{ic|cryptkey}} kernel parameter: in the case of initramfs, the syntax is {{ic|rootfs:&#039;&#039;path&#039;&#039;}}, see [[Dm-crypt/システム設定#cryptkey]]. Besides, this kernel parameter defaults to use {{ic|/crypto_keyfile.bin}}, and if the initramfs contains a valid key with this name, decryption will occur automatically without the need to configure the {{ic|cryptkey}} parameter.&lt;br /&gt;
&lt;br /&gt;
{{ic|encrypt}} の代わりに {{ic|sd-encrypt}} を使用する場合、{{ic|luks.key}} カーネルパラメータでキーファイルの場所を指定してください。[[Dm-crypt/システム設定#luks.key]] を参照。&lt;br /&gt;
&lt;br /&gt;
[[#ランダムな文字列でキーファイルを作成|キーファイルを生成]]して適切な権限を与えて [[#LUKS キーの追加|LUKS キーとして追加]]:&lt;br /&gt;
&lt;br /&gt;
 # dd bs=512 count=4 if=/dev/urandom of=/crypto_keyfile.bin&lt;br /&gt;
 # chmod 000 /crypto_keyfile.bin&lt;br /&gt;
 # chmod 600 /boot/initramfs-linux*&lt;br /&gt;
 # cryptsetup luksAddKey /dev/sdX# /crypto_keyfile.bin&lt;br /&gt;
&lt;br /&gt;
{{Warning|When initramfs&#039; permissions are set to 644 (by default), then all users will be able to dump the keyfile. Make sure the permissions are still 600 if you install a new kernel.}}&lt;br /&gt;
&lt;br /&gt;
[[Mkinitcpio#BINARIES_と_FILES|mkinitcpio の FILES]] にキーを記述:&lt;br /&gt;
&lt;br /&gt;
{{hc|/etc/mkinitcpio.conf|2=FILES=(/crypto_keyfile.bin)}}&lt;br /&gt;
&lt;br /&gt;
最後に [[Mkinitcpio#イメージ作成とアクティベーション|initramfs を再生成]]してください。&lt;br /&gt;
&lt;br /&gt;
次の起動時からコンテナを復号化するパスフレーズを入力するのは一度だけですむようになります。&lt;br /&gt;
&lt;br /&gt;
詳しくは [http://www.pavelkogan.com/2014/05/23/luks-full-disk-encryption/#bonus-login-once こちら] を参照。&lt;/div&gt;</summary>
		<author><name>Takemaro</name></author>
	</entry>
	<entry>
		<id>https://wiki.archlinux.jp/index.php?title=Dm-crypt/%E3%83%87%E3%83%90%E3%82%A4%E3%82%B9%E3%81%AE%E6%9A%97%E5%8F%B7%E5%8C%96&amp;diff=39960</id>
		<title>Dm-crypt/デバイスの暗号化</title>
		<link rel="alternate" type="text/html" href="https://wiki.archlinux.jp/index.php?title=Dm-crypt/%E3%83%87%E3%83%90%E3%82%A4%E3%82%B9%E3%81%AE%E6%9A%97%E5%8F%B7%E5%8C%96&amp;diff=39960"/>
		<updated>2025-03-09T18:23:21Z</updated>

		<summary type="html">&lt;p&gt;Takemaro: /* dm-crypt の暗号化オプション */ 英語版を反映: LUKS2の追加、オプションの追加、イテレーション時間の記述の追加、等&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Lowercase title}}&lt;br /&gt;
[[Category:保存データ暗号化]]&lt;br /&gt;
[[Category:ファイルシステム]]&lt;br /&gt;
[[en:Dm-crypt/Device encryption]]&lt;br /&gt;
このセクションではコマンドラインから &#039;&#039;dm-crypt&#039;&#039; を利用して手動でシステムを暗号化する方法を説明しています。&lt;br /&gt;
&lt;br /&gt;
== 準備 ==&lt;br /&gt;
{{pkg|cryptsetup}} を使用する前に、{{ic|dm_crypt}} [[カーネルモジュール]]がロードされていることを確認してください。&lt;br /&gt;
&lt;br /&gt;
== Cryptsetup の使用方法 ==&lt;br /&gt;
&#039;&#039;Cryptsetup&#039;&#039; は暗号化デバイスを作成・管理する &#039;&#039;dm-crypt&#039;&#039; を使うためのコマンドラインツールです。後に Linux カーネルの &#039;&#039;&#039;d&#039;&#039;&#039;evice-&#039;&#039;&#039;m&#039;&#039;&#039;apper と &#039;&#039;&#039;crypt&#039;&#039;&#039;ographic モジュールを使用する別の暗号化もサポートするように拡張されました。最も著しい拡張は Linux Unified Key Setup (LUKS) の拡張で、dm-crypt をセットアップするのに必要な情報を全てディスク自体に保存してパーティションとキーの管理を抽象化することで使いやすさを増しています。device-mapper によってアクセスされるデバイスはブロックデバイスと呼ばれます。詳しくは[[ディスク暗号化#ブロックデバイスの暗号化]]を見て下さい。&lt;br /&gt;
&lt;br /&gt;
ツールは以下のように使います:&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup &#039;&#039;action&#039;&#039; &#039;&#039;options&#039;&#039; &#039;&#039;device&#039;&#039; &#039;&#039;dmname&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
オプションや暗号化モードにはデフォルト値が存在し、コマンドラインで何も指定しなかった場合はデフォルト値が使われます。オプションや暗号化モードのデフォルトパラメータを確認するには以下のコマンドを実行:&lt;br /&gt;
&lt;br /&gt;
 $ cryptsetup --help &lt;br /&gt;
&lt;br /&gt;
オプションの完全なリストは man ページで確認できます。暗号化モードやアクションによって必要なパラメータは変わるため、以下のセクションでは違いについて説明しています。ブロックデバイス暗号化は高速ですが、速度は非常に重要な問題です。ブロックデバイスの暗号を設定後に変更することは難しいため、それぞれのパラメータについて &#039;&#039;dm-crypt&#039;&#039; のパフォーマンスをチェックすると良いでしょう:&lt;br /&gt;
&lt;br /&gt;
 $ cryptsetup benchmark &lt;br /&gt;
&lt;br /&gt;
上記のコマンドはインストールするときにアルゴリズムや鍵長を決める手がかりになります。特定の AES 暗号が非常に高速に処理される場合、おそらくその暗号は CPU によるハードウェア支援の恩恵に与かっています。&lt;br /&gt;
&lt;br /&gt;
{{Tip|テストしたいときは[[:カテゴリ:仮想化|仮想マシン]]の仮想ハードドライブで練習すると良いでしょう。}}&lt;br /&gt;
&lt;br /&gt;
=== Cryptsetup のパスフレーズとキー ===&lt;br /&gt;
暗号化されたブロックデバイスはキーによって保護されます。キーは以下のいずれかです:&lt;br /&gt;
&lt;br /&gt;
*パスフレーズ、[[セキュリティ#パスワード]]を見て下さい。&lt;br /&gt;
*キーファイル、[[#キーファイル]] を見て下さい。&lt;br /&gt;
&lt;br /&gt;
どちらのタイプのキーも最大サイズが決められています: パスフレーズは512文字まで、キーファイルは 8192kB までです。&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;LUKS&#039;&#039; の重要な特徴として、キーは LUKS によって暗号化されたデバイスのマスターキーを解除するのに使われ、root 権限で変えることができるということです。他の暗号化モードでは設定後にキーを変更することはできません。暗号化にマスターキーを使わないためです。詳しくは[[ディスク暗号化#ブロックデバイスの暗号化]]を見て下さい。&lt;br /&gt;
&lt;br /&gt;
== dm-crypt の暗号化オプション ==&lt;br /&gt;
&#039;&#039;Cryptsetup&#039;&#039; は &#039;&#039;dm-crypt&#039;&#039; で使用できる様々な暗号化モードをサポートしています。&lt;br /&gt;
* {{ic|--type luks}} - LUKS フォーマットのデフォルトバージョン ({{Pkg|cryptsetup}} &amp;lt; 2.1.0 ではLUKS1、{{Pkg|cryptsetup}} ≥ 2.1.0 では LUKS2) を使用。&lt;br /&gt;
* {{ic|--type luks1}} - LUKS の古いバージョンである LUKS1 を使用。&lt;br /&gt;
* {{ic|--type luks2}} - LUKS に追加の拡張が導入された現在のバージョンである LUKS2 を使用。&lt;br /&gt;
** {{ic|--hw-opal-only}} - TCG OPAL 標準をサポートするドライブでハードウェアベースの暗号化を使用。[[自己暗号化ドライブ]]や {{man|8|cryptsetup|SED (SELF ENCRYPTING DRIVE) OPAL EXTENSION}} も参照して下さい。&lt;br /&gt;
** {{ic|--hw-opal}} - OPAL によるハードウェア暗号化と dm-crypt によるソフトウェア暗号化の両方を使用。&lt;br /&gt;
*{{ic|--type plain}} - dm-crypt plain モードを使用。&lt;br /&gt;
*{{ic|--type loopaes}} - loopaes legacy モードを使用。&lt;br /&gt;
*{{ic|--type tcrypt}} - [[Truecrypt]] 互換モードを使用。&lt;br /&gt;
* {{ic|--type bitlk}} - [[Wikipedia:ja:BitLocker|BitLocker]] 互換モードを使用。 {{man|8|cryptsetup|BITLK (WINDOWS BITLOCKER COMPATIBLE) EXTENSION}} も参照して下さい。&lt;br /&gt;
&lt;br /&gt;
暗号やハッシュの基本的な暗号化オプションは全てのモードで使うことができ、カーネルの暗号化バックエンド機能を利用します。使用できるオプションは以下のコマンドで確認できます:&lt;br /&gt;
&lt;br /&gt;
 $ less /proc/crypto &lt;br /&gt;
&lt;br /&gt;
{{Tip|リストが短い場合、{{ic|cryptsetup benchmark}} を実行すればモジュールのロードが行われます。}}&lt;br /&gt;
&lt;br /&gt;
以下では LUKS モードと plain モードの暗号化オプションを紹介します。記事の中で例として使っているオプションを並べていますが、利用できるオプションの全てではないので注意してください。&lt;br /&gt;
&lt;br /&gt;
=== LUKS モードの暗号化オプション ===&lt;br /&gt;
LUKS 暗号化モードで新しい dm-crypt デバイスをセットアップする &#039;&#039;cryptsetup&#039;&#039; のアクションは &#039;&#039;luksFormat&#039;&#039; です。名前にはフォーマットとありますが、実際にはデバイスをフォーマットするのではなく、LUKS デバイスヘッダーを設定して指定された暗号オプションを使ってマスター鍵を暗号化します。&lt;br /&gt;
&lt;br /&gt;
デフォルトパラメータ ({{ic|cryptsetup --help}}で確認できます) で新しい LUKS デバイスを作成する場合、単に以下のコマンドを実行します:&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup luksFormat &#039;&#039;device&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
cryptsetup 2.4.0 の場合、これは以下と同等です:&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup luksFormat --type luks2 --cipher aes-xts-plain64 --hash sha256 --iter-time 2000 --key-size 256 --pbkdf argon2id --use-urandom --verify-passphrase &#039;&#039;device&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
以下の表でデフォルト値とサンプルにコメントを付けて説明しています:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; style=&amp;quot;text-align:left&amp;quot; | オプション&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; style=&amp;quot;text-align:left&amp;quot; | Cryptsetup 2.1.0 のデフォルト&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; style=&amp;quot;text-align:left&amp;quot; | 例&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; style=&amp;quot;text-align:left&amp;quot; | コメント&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;row&amp;quot; style=&amp;quot;text-align:right&amp;quot; | --cipher, -c&lt;br /&gt;
| {{ic|aes-xts-plain64}}&lt;br /&gt;
| {{ic|aes-xts-plain64}}&lt;br /&gt;
| [https://www.kernel.org/pub/linux/utils/cryptsetup/v1.6/v1.6.0-ReleaseNotes バージョン 1.6.0]からデフォルトが AES [[ディスク暗号化#暗号と利用形態|暗号]]と [[wikipedia:Disk encryption theory#XEX-based tweaked-codebook mode with ciphertext stealing (XTS)|XTS]] を使用するよう変わりました ([https://gitlab.com/cryptsetup/cryptsetup/wikis/FrequentlyAskedQuestions#5-security-aspects FAQ] の 5.16 も参照)。以前のデフォルトである  {{ic|--cipher aes-cbc-essiv}} は、[[wikipedia:Disk encryption theory#Cipher-block chaining (CBC)|既知の問題]]があり、[https://www.jakoblell.com/blog/2013/12/22/practical-malleability-attack-against-cbc-encrypted-luks-partitions/ 実際の攻撃]も存在するため、使用しないことをお勧めします。&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;row&amp;quot; style=&amp;quot;text-align:right&amp;quot; | --key-size, -s&lt;br /&gt;
| {{ic|256}} (XTSでは {{ic|512}})&lt;br /&gt;
| {{ic|512}}&lt;br /&gt;
| XTS モードの暗号では 512 ビットがデフォルトです。[[wikipedia:Disk encryption theory#XEX-based tweaked-codebook mode with ciphertext stealing (XTS)|XTS はキーを半分に割る]]ので、これは AES-256 が使われることを意味します。&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;row&amp;quot; style=&amp;quot;text-align:right&amp;quot; | --hash, -h&lt;br /&gt;
| {{ic|sha256}}&lt;br /&gt;
| {{ic|sha512}}&lt;br /&gt;
| [[ディスク暗号化#暗号メタデータ|鍵導出]] で使用されるハッシュアルゴリズム。リリース 1.7.0 でデフォルト設定が {{ic|sha1}} から {{ic|sha256}} に変更されました。セキュリティ上の理由ではなく SHA1 が使用できないシステムでも動作するようにするためです [https://www.kernel.org/pub/linux/utils/cryptsetup/v1.7/v1.7.0-ReleaseNotes]。{{ic|sha1}} でも十分セキュアであるため古いバージョンの &#039;&#039;cryptsetup&#039;&#039; と互換性を維持する目的で使用できます [https://gitlab.com/cryptsetup/cryptsetup/wikis/FrequentlyAskedQuestions#5-security-aspects]。&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;row&amp;quot; style=&amp;quot;text-align:right&amp;quot; | --iter-time, -i&lt;br /&gt;
| {{ic|2000}}&lt;br /&gt;
| {{ic|5000}}&lt;br /&gt;
| PBKDF2 パスフレーズを処理するのに費やす時間 (ミリ秒単位)。&amp;quot;&#039;&#039;PBKDF2 の反復回数を高く保持したままユーザーが許容できる数値にするため&#039;&#039;&amp;quot;、リリース 1.7.0 からデフォルト値が {{ic|1000}} から {{ic|2000}} に変更されています [https://www.kernel.org/pub/linux/utils/cryptsetup/v1.7/v1.7.0-ReleaseNotes]。&#039;&#039;luksFormat&#039;&#039; や &#039;&#039;luksAddKey&#039;&#039; などパスフレーズを設定・変更する LUKS 操作のみに関連するオプションです。パラメータとして 0 を指定するとコンパイル時のデフォルトが選択されます。&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;row&amp;quot; style=&amp;quot;text-align:right&amp;quot; | --use-{u,}random&lt;br /&gt;
| {{ic|--use-&#039;&#039;&#039;u&#039;&#039;&#039;random}}&lt;br /&gt;
| {{ic|--use-random}}&lt;br /&gt;
| [[乱数生成|乱数生成器]]の選択。現在は {{ic|/dev/random}} と {{ic|/dev/urandom}} は等価なので、 {{ic|--use-random}} と {{ic|--use-urandom}} も等価です。&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;row&amp;quot; style=&amp;quot;text-align:right&amp;quot; | --verify-passphrase, -y&lt;br /&gt;
| Yes&lt;br /&gt;
| -&lt;br /&gt;
| {{ic|luksFormat}} と {{ic|luksAddKey}} で使われます。Arch Linux ではデフォルトで有効です。&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;row&amp;quot; style=&amp;quot;text-align:right&amp;quot; | --sector-size&lt;br /&gt;
| {{ic|512}} または {{ic|4096}} (デバイスに依存)&lt;br /&gt;
| {{ic|4096}}&lt;br /&gt;
| ディスク暗号化で使われるセクタサイズを設定します。デフォルトは、4Kn や 512e のブロックデバイスでは {{ic|4096}} で、512n のブロックデバイスでは {{ic|512}} です。最近のストレージデバイスのほとんどでは、セクタサイズを 512 バイトから 4096 バイトにすることでパフォーマンスが向上します。[[アドバンスドフォーマット#dm-crypt]]を見て下さい。&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
LUKS の機能やオプションについては [https://gitlab.com/cryptsetup/cryptsetup/-/raw/master/docs/on-disk-format.pdf LUKS1 の仕様]や [https://gitlab.com/cryptsetup/cryptsetup/-/raw/master/docs/on-disk-format-luks2.pdf LUKS2の仕様] で説明されています。&lt;br /&gt;
{{Tip|開発者によるプレゼンテーション [https://mbroz.fedorapeople.org/talks/DevConf2016/devconf2016-luks2.pdf devconfcz2016] (pdf) では、LUKS2 での主な仕様変更の背景がまとめられています。cryptsetup のメジャーバージョンである[https://mirrors.edge.kernel.org/pub/linux/utils/cryptsetup/v2.0/v2.0.0-ReleaseNotes バージョン 2 のリリースノート]も見て下さい。}}&lt;br /&gt;
&lt;br /&gt;
==== イテレーション時間 ====&lt;br /&gt;
[https://gitlab.com/cryptsetup/cryptsetup/-/wikis/FrequentlyAskedQuestions#2-setup cryptsetup FAQ§2.1] と [https://gitlab.com/cryptsetup/cryptsetup/-/wikis/FrequentlyAskedQuestions#3-common-problems §3.4]から:&lt;br /&gt;
&lt;br /&gt;
:鍵スロットを解除するのにかかる時間は[...]パスフレーズを設定した時に決まります。デフォルトでは1秒 (LUKS2 では2秒) です。[...]&lt;br /&gt;
:パスフレーズをイテレーションする回数は時間によって決まり、したがってセキュリティレベルは LUKS コンテナを作成したマシンの CPU の能力に依存します。[...]&lt;br /&gt;
:もし高速なマシンでパスフレーズを設定し、低速なマシンでそれを解除した場合、解除にかかる時間は長くなります。&lt;br /&gt;
&lt;br /&gt;
このことから、コンテナを作成する際は、そのコンテナに最も頻繁にアクセスすることになるマシンの上で作成するのが良いでしょう。&lt;br /&gt;
&lt;br /&gt;
イテレーション回数を正しく調節する方法については、上の記事の残りの部分を読んで下さい。&lt;br /&gt;
&lt;br /&gt;
=== plain モードの暗号化オプション ===&lt;br /&gt;
dm-crypt の &#039;&#039;plain&#039;&#039; モードでは、デバイスのマスター鍵が存在せず、セットアップも必要ありません。代わりに、直接暗号化オプションを使用して暗号ディスクと名前付きデバイスのマッピングを作成します。パーティションやデバイス全体に対してマッピングを作成できます。後者の場合、パーティションテーブルも不要です。&lt;br /&gt;
&lt;br /&gt;
cryptsetup のデフォルトパラメータを使って &#039;&#039;plain&#039;&#039; のマッピングを作成するには:&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup open --type plain &#039;&#039;options&#039;&#039; &#039;&#039;device&#039;&#039; &#039;&#039;dmname&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
実行するとパスワードを求められます。非常に高いエントロピーのパスワードが必要です。デフォルトではありませんが、 {{ic|--verify-passphrase}} オプションを使えます。&lt;br /&gt;
&lt;br /&gt;
一般論として、作成時の暗号化オプションは精確に記録しておくと良いでしょう。暗号化されたデバイス上にはオプションの内容は記録されていませんし、デフォルトのオプションは変わる可能性があります。&lt;br /&gt;
&lt;br /&gt;
以下は [[Dm-crypt/システム全体の暗号化#Plain dm-crypt]] の例とデフォルトパラメータの比較表です。&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! オプション !! Cryptsetup (2.7.0) のデフォルト !! 例 !! コメント !! &lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;--hash, -h&#039;&#039;&#039; || {{ic|sha256}} || - || パスフレーズからキーを作成するのに使用するハッシュ。キーファイルを用いる場合は使われない。&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;--cipher, -c&#039;&#039;&#039;|| {{ic|aes-xts-plain64}}|| {{ic|aes-xts-plain64}} || 暗号は3つの文字列からなります: cipher-chainmode-IV generator。[[ディスク暗号化#暗号と利用形態]]や [https://gitlab.com/cryptsetup/cryptsetup/wikis/DMCrypt DMCrypt のドキュメント] を見てください。&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;--key-size, -s&#039;&#039;&#039;||{{ic|256}}||{{ic|512}}||キーサイズ (ビット数)。サイズは使用する暗号や使用するチェインモードによって変わります。XTS モードは CBC モードの2倍のキーサイズを必要とします。&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;--size, -b&#039;&#039;&#039;||対象ディスクの実際のサイズ||- (デフォルト)||512バイト単位のセクタ数。デバイスの最大サイズ以下。&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;--offset, -o&#039;&#039;&#039;||{{ic|0}}||{{ic|0}}||マッピングを開始するディスクの先頭からのオフセット。512バイト単位のセクタ数。&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;--skip, -p&#039;&#039;&#039;||{{ic|0}}||{{ic|2048}} (512B×2048=1MiB をスキップ)||初期ベクトル (IV) を計算する際にスキップする暗号化されたデータのサイズ。512バイト単位のセクタ数。&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;--key-file, -d&#039;&#039;&#039;||デフォルトではパスフレーズを使用||{{ic|/dev/sd&#039;&#039;Z&#039;&#039;}} (もしくは {{ic|/boot/keyfile.enc}})||キーとして使用するデバイスまたはファイル。詳しくは [[#キーファイル]] を参照。&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;--keyfile-offset&#039;&#039;&#039;||{{ic|0}}||{{ic|0}}||キーファイルの先頭からのオフセット (バイト数)。&#039;&#039;cryptsetup&#039;&#039; 1.6.7 以上でサポートされているオプション。&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;--keyfile-size, -l&#039;&#039;&#039;||{{ic|8192kB}}||- (デフォルト)||キーファイルから読み込まれるバイト数を制限。&#039;&#039;cryptsetup&#039;&#039; 1.6.7 以上でサポートされているオプション。&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;--sector-size, -l&#039;&#039;&#039;||{{ic|512}}||{{ic|4096}}||ディスク暗号化の際に使われるセクタサイズ。4Kn のブロックデバイス以外では、 {{ic|512}} がデフォルト。最近のストレージデバイスのほとんどでは、セクタサイズを 512 バイトから 4096 バイトにすることでパフォーマンスが向上します。[[アドバンスドフォーマット#dm-crypt]]を見て下さい。&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
{{ic|/dev/sd&#039;&#039;X&#039;&#039;}} デバイスで、上記の例を使用する場合:&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup open --type plain --cipher=aes-xts-plain64 --offset=0 --skip=2048 --key-file=/dev/sd&#039;&#039;Z&#039;&#039; --key-size=512 --sector-size 4096 /dev/sdX enc&lt;br /&gt;
&lt;br /&gt;
{{Warning|LUKS による暗号化と違って、上記のコマンドはマッピングを再現するために毎回 &#039;&#039;&#039;全て&#039;&#039;&#039; 指定する必要があります。暗号やハッシュ、キーファイルについて覚えておく必要があります。cryptsetup のデフォルトはバージョンによって変わるので、デフォルトに頼らないで下さい。}}&lt;br /&gt;
&lt;br /&gt;
マッピングが作成されたことは以下のコマンドで確認できます:&lt;br /&gt;
 # fdisk -l&lt;br /&gt;
{{ic|/dev/mapper/enc}} のエントリが存在しているはずです。&lt;br /&gt;
&lt;br /&gt;
== cryptsetup でデバイスを暗号化 ==&lt;br /&gt;
以下のセクションでは新しい暗号化ブロックデバイスを作成するオプションと手動のアクセス方法を説明します。&lt;br /&gt;
&lt;br /&gt;
=== LUKS モードでデバイスを暗号化 ===&lt;br /&gt;
==== LUKS パーティションのフォーマット ====&lt;br /&gt;
&lt;br /&gt;
暗号化 LUKS パーティションとして設定するには次を実行:&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup luksFormat &#039;&#039;device&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
上記のコマンドを実行するとパスワードの入力が要求されます。&lt;br /&gt;
&lt;br /&gt;
コマンドラインオプションは [[#LUKS モードの暗号化オプション]]を参照。&lt;br /&gt;
&lt;br /&gt;
結果は次のコマンドで確認できます:&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup luksDump &#039;&#039;device&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
ダンプでは暗号のヘッダー情報だけでなく、LUKS パーティションで使われるキースロットもわかります。&lt;br /&gt;
&lt;br /&gt;
以下の例はデフォルトの AES 暗号を使って256ビットの XTS モードで {{ic|/dev/sda1}} に暗号化したルートパーティションを作成します:&lt;br /&gt;
 # cryptsetup -s 512 luksFormat /dev/sda1&lt;br /&gt;
&lt;br /&gt;
=====LUKS を使ってキーファイルでパーティションをフォーマット=====&lt;br /&gt;
&lt;br /&gt;
LUKS による暗号化パーティションを新しく作成する場合、以下のように作成するときにパーティションにキーファイルを関連付けることができます:&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup luksFormat &#039;&#039;device&#039;&#039; &#039;&#039;/path/to/mykeyfile&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
キーファイルを作成・管理する方法は [[#キーファイル]] を見て下さい。&lt;br /&gt;
&lt;br /&gt;
====デバイスマッパーで LUKS パーティションのロックを解除・マップ====&lt;br /&gt;
&lt;br /&gt;
LUKS パーティションを作成したら、解錠することができます。&lt;br /&gt;
&lt;br /&gt;
解錠ではデバイスマッパーによってパーティションが新しいデバイス名にマッピングされます。カーネルは {{ic|&#039;&#039;device&#039;&#039;}} が暗号化デバイスであり暗号化データを上書きしないように {{ic|/dev/mapper/&#039;&#039;dm_name&#039;&#039;}} を使って LUKS 経由で解決する必要があることを知ります。間違って上書きしないために、設定が完了したら[[#バックアップとリストア|暗号化ヘッダーをバックアップ]]する方法を確認してください。&lt;br /&gt;
&lt;br /&gt;
暗号化された LUKS パーティションを開くには次のコマンドを実行:&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup open --type luks &#039;&#039;device&#039;&#039; &#039;&#039;dm_name&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
実行するとパーティションを解錠するパスワードの入力が求められます。通常、マッピングするデバイスの名前はパーティションの機能が分かりやすいものにします。例えば、以下のコマンドは luks パーティション {{ic|/dev/sda1}} を解錠して {{ic|cryptroot}} という名前のデバイスマッパーにマッピングします:&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup open --type luks /dev/sda1 cryptroot &lt;br /&gt;
&lt;br /&gt;
パーティションをオープンしたら、ルートパーティションのデバイスアドレスはパーティション本体 (例: {{ic|/dev/sda1}}) の代わりに {{ic|/dev/mapper/cryptroot}} が使われます。&lt;br /&gt;
&lt;br /&gt;
暗号化レイヤーの上に LVM をセットアップする場合、復号化したボリュームグループのデバイスファイルは {{ic|/dev/sda1}} ではなく {{ic|/dev/mapper/cryptroot}} などとなります。それから LVM は作成されている論理ボリューム全てに名前を割り当てます (例: {{ic|/dev/mapper/lvmpool-root}} や {{ic|/dev/mapper/lvmpool-swap}})。&lt;br /&gt;
&lt;br /&gt;
暗号化データをパーティションに書き込むときは、マッピングされたデバイス名を使ってアクセスする必要があります。例えば[[ファイルシステム#デバイスのフォーマット|ファイルシステムを作成]]するときは以下のようにします:&lt;br /&gt;
&lt;br /&gt;
 # mkfs -t ext4 /dev/mapper/cryptroot&lt;br /&gt;
&lt;br /&gt;
{{ic|/dev/mapper/cryptroot}} デバイスは他のパーティションと同じように[[マウント]]できます。&lt;br /&gt;
&lt;br /&gt;
luks コンテナを閉じるには、パーティションをアンマウントしてください:&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup close cryptroot&lt;br /&gt;
&lt;br /&gt;
=== plain モードでデバイスを暗号化 ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;dm-crypt&#039;&#039; プレーンモード暗号化の作成とその後のアクセスはどちらも、正しい [[#plain視の暗号化オプション| parametersで &#039;&#039;cryptsetup&#039;&#039; {{ic|open}} アクションを使用するだけで済みます。]] 以下は、非ルートデバイスの2つの例で示していますが、両方をスタックすることで癖が追加されています (つまり、2番目は最初のデバイス内に作成されます)  明らかに、暗号化をスタックするとオーバーヘッドが2倍になります。 ここでの使用例は、暗号オプションの使用法の別の例を説明するためのものです。&lt;br /&gt;
&lt;br /&gt;
上記の表の左側の列で説明されているように、最初のマッパーは &#039;&#039;cryptsetup&#039;&#039; のプレーンモードのデフォルトで作成されます。&lt;br /&gt;
{{hc|# cryptsetup --type plain -v open /dev/sdaX plain1|&lt;br /&gt;
Enter passphrase: &lt;br /&gt;
Command successful.&lt;br /&gt;
}}&lt;br /&gt;
次に、さまざまな暗号化パラメーターを使用し、(オプションの) オフセットを使用して、その中に2番目のブロックデバイスを追加し、ファイルシステムを作成してマウントします。&lt;br /&gt;
{{hc|1=# cryptsetup --type plain --cipher=serpent-xts-plain64 --hash=sha256 --key-size=256 --offset=10  open /dev/mapper/plain1 plain2|2=&lt;br /&gt;
Enter passphrase: &lt;br /&gt;
}}&lt;br /&gt;
{{hc|# lsblk -p|&lt;br /&gt;
 NAME                                                     &lt;br /&gt;
 /dev/sda                                     &lt;br /&gt;
 ├─/dev/sdaX          &lt;br /&gt;
 │ └─/dev/mapper/plain1     &lt;br /&gt;
 │   └─/dev/mapper/plain2              &lt;br /&gt;
 ...&lt;br /&gt;
}}&lt;br /&gt;
 # mkfs -t ext2 /dev/mapper/plain2&lt;br /&gt;
 # mount -t ext2 /dev/mapper/plain2 /mnt&lt;br /&gt;
 # echo &amp;quot;This is stacked. one passphrase per foot to shoot.&amp;quot; &amp;gt; /mnt/stacked.txt&lt;br /&gt;
We close the stack to check access works&lt;br /&gt;
 # cryptsetup close plain2&lt;br /&gt;
 # cryptsetup close plain1&lt;br /&gt;
First, let&#039;s try to open the filesystem directly: &lt;br /&gt;
 # cryptsetup --type plain --cipher=serpent-xts-plain64 --hash=sha256 --key-size=256 --offset=10 open /dev/sdaX plain2&lt;br /&gt;
{{hc|# mount -t ext2 /dev/mapper/plain2 /mnt|&lt;br /&gt;
mount: wrong fs type, bad option, bad superblock on /dev/mapper/plain2,&lt;br /&gt;
      missing codepage or helper program, or other error&lt;br /&gt;
}}&lt;br /&gt;
Why that did not work? Because the &amp;quot;plain2&amp;quot; starting block ({{ic|10}}) is still encrypted with the cipher from &amp;quot;plain1&amp;quot;. It can only be accessed via the stacked mapper. The error is arbitrary though, trying a wrong passphrase or wrong options will yield the same. For &#039;&#039;dm-crypt&#039;&#039; plain mode, the {{ic|open}} action will not error out itself. &lt;br /&gt;
&lt;br /&gt;
Trying again in correct order: &lt;br /&gt;
 # cryptsetup close plain2    # dysfunctional mapper from previous try&lt;br /&gt;
{{hc|# cryptsetup --type plain open /dev/sdaX plain1|&lt;br /&gt;
Enter passphrase: &lt;br /&gt;
}}&lt;br /&gt;
{{hc|1=# cryptsetup --type plain --cipher=serpent-xts-plain64 --hash=sha256 --key-size=256 --offset=10 open /dev/mapper/plain1 plain2|2=&lt;br /&gt;
Enter passphrase: &lt;br /&gt;
}}&lt;br /&gt;
{{hc|# mount /dev/mapper/plain2 /mnt &amp;amp;&amp;amp; cat /mnt/stacked.txt|&lt;br /&gt;
This is stacked. one passphrase per foot to shoot.&lt;br /&gt;
}}&lt;br /&gt;
 # exit&lt;br /&gt;
&#039;&#039;dm-crypt&#039;&#039; will handle stacked encryption with some mixed modes too. For example LUKS mode could be stacked on the &amp;quot;plain1&amp;quot; mapper. Its header would then be encrypted inside &amp;quot;plain1&amp;quot; when that is closed.&lt;br /&gt;
&lt;br /&gt;
Available for plain mode only is the option {{ic|--shared}}. With it a single device can be segmented into different non-overlapping mappers. We do that in the next example, using a &#039;&#039;loopaes&#039;&#039; compatible cipher mode for &amp;quot;plain2&amp;quot; this time: &lt;br /&gt;
{{hc|# cryptsetup --type plain --offset 0 --size 1000 open /dev/sdaX plain1|2=&lt;br /&gt;
Enter passphrase: &lt;br /&gt;
}}&lt;br /&gt;
{{hc|1=# cryptsetup --type plain --offset 1000 --size 1000 --shared --cipher=aes-cbc-lmk --hash=sha256 open /dev/sdaX plain2|2=&lt;br /&gt;
Enter passphrase: &lt;br /&gt;
}}&lt;br /&gt;
{{hc|# lsblk -p|&lt;br /&gt;
NAME                    &lt;br /&gt;
dev/sdaX                    &lt;br /&gt;
├─/dev/sdaX               &lt;br /&gt;
│ ├─/dev/mapper/plain1     &lt;br /&gt;
│ └─/dev/mapper/plain2     &lt;br /&gt;
...&lt;br /&gt;
}}&lt;br /&gt;
As the devicetree shows both reside on the same level, i.e. are not stacked and &amp;quot;plain2&amp;quot; can be opened individually.&lt;br /&gt;
&lt;br /&gt;
== LUKS 固有の Cryptsetup のアクション ==&lt;br /&gt;
=== キーの管理 ===&lt;br /&gt;
LUKS パーティションには異なる8つまでのキーを定義することができます。バックアップストレージのためのアクセスキーを作成することが可能です。いわゆるキーエスクローで、あるキーは日常的に使用するために使い、他のキーはエスクローに保存して日常用のパスフレーズを忘れたときやキーファイルを喪失・破損したときにパーティションにアクセスするために使います。また、別のキースロットを使って一時的なキーを発行してユーザーにパーティションへのアクセスを許可し、後でキーを無効化したりできます。&lt;br /&gt;
&lt;br /&gt;
暗号化パーティションを作成すると、初期キースロット 0 が作成されます (手動で別の数字を指定しなかった場合)。追加キースロットは 1 から 7 までの数字が割り振られます。どのキースロットを使用しているかは以下のコマンドで確認できます (&amp;lt;device&amp;gt; は LUKS ヘッダーが存在するボリュームに置き換えてください):&lt;br /&gt;
{{hc|# cryptsetup luksDump /dev/&amp;lt;device&amp;gt; {{!}} grep BLED|&lt;br /&gt;
Key Slot 0: ENABLED&lt;br /&gt;
Key Slot 1: ENABLED&lt;br /&gt;
Key Slot 2: ENABLED&lt;br /&gt;
Key Slot 3: DISABLED&lt;br /&gt;
Key Slot 4: DISABLED&lt;br /&gt;
Key Slot 5: DISABLED&lt;br /&gt;
Key Slot 6: DISABLED&lt;br /&gt;
Key Slot 7: DISABLED}}&lt;br /&gt;
&lt;br /&gt;
上記のコマンドや下で説明しているコマンドはヘッダーのバックアップファイルでも使えます。&lt;br /&gt;
&lt;br /&gt;
==== LUKS キーの追加 ====&lt;br /&gt;
&lt;br /&gt;
新しいキースロットの追加は cryptsetup の {{ic|luksAddKey}} アクションを使うことでできます。デバイスが解錠されていても安全のために、新しいキーを入力する前に、必ず既存のキーの入力が求められます (&amp;quot;any passphrase&amp;quot;):&lt;br /&gt;
&lt;br /&gt;
{{hc|# cryptsetup luksAddKey /dev/&amp;lt;device&amp;gt; (/path/to/&amp;lt;additionalkeyfile&amp;gt;)|&lt;br /&gt;
Enter any passphrase:&lt;br /&gt;
Enter new passphrase for key slot:&lt;br /&gt;
Verify passphrase: &lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
{{ic|/path/to/&amp;lt;additionalkeyfile&amp;gt;}} を指定した場合、cryptsetup は &amp;lt;additionalkeyfile&amp;gt; の新しいキースロットを追加します。指定しなかった場合は新しいパスフレーズの入力が2回要求されます。既存の&#039;&#039;キーファイル&#039;&#039;を使って認証するには、{{ic|--key-file}} または {{ic|-d}} オプションの後に&amp;quot;既存&amp;quot;の &amp;lt;keyfile&amp;gt; を指定することで全てのキーファイルのキースロットが解除されます:&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup luksAddKey /dev/&amp;lt;device&amp;gt; (/path/to/&amp;lt;additionalkeyfile&amp;gt;) -d /path/to/&amp;lt;keyfile&amp;gt;&lt;br /&gt;
&lt;br /&gt;
複数のキーを使っていてキーを変更・無効化する場合、{{ic|--key-slot}} または {{ic|-S}} オプションでスロットを指定できます:&lt;br /&gt;
{{hc|# cryptsetup luksAddKey /dev/&amp;lt;device&amp;gt; -S 6|&lt;br /&gt;
Enter any passphrase: &lt;br /&gt;
Enter new passphrase for key slot: &lt;br /&gt;
Verify passphrase:&lt;br /&gt;
}}&lt;br /&gt;
{{hc|# cryptsetup luksDump /dev/sda8 {{!}} grep &#039;Slot 6&#039;|&lt;br /&gt;
Key Slot 6: ENABLED&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
例えばキーを変更したい場合:&lt;br /&gt;
{{hc|# cryptsetup luksChangeKey /dev/&amp;lt;device&amp;gt; -S 6|&lt;br /&gt;
Enter LUKS passphrase to be changed: &lt;br /&gt;
Enter new LUKS passphrase: &lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==== LUKS キーの削除 ====&lt;br /&gt;
&lt;br /&gt;
ヘッダーからキーを削除するアクションは3つ存在します:&lt;br /&gt;
*{{ic|luksRemoveKey}} はパスフレーズやキーファイルを指定してキーを削除します。&lt;br /&gt;
*{{ic|luksKillSlot}} は (他のキーを使って) 指定したキースロットからキーを削除します。パスフレーズを忘れたり、キーファイルを無くしたり、誰もアクセスできないようにしたい場合に有用です。&lt;br /&gt;
*{{ic|luksErase}} は全ての有効なキーを即座に削除します。&lt;br /&gt;
{{warning|&lt;br /&gt;
*All above actions can be used to irrevocably delete the last active key for an encrypted device! &lt;br /&gt;
*The {{ic|luksErase}} command was added in version 1.6.4 to quickly nuke access to the device. This action &#039;&#039;&#039;will not&#039;&#039;&#039; prompt for a valid passphrase! It will not [[Dm-crypt/ドライブの準備#LUKS ヘッダーを消去|wipe the LUKS header]], but all keyslots at once and you will, therefore, not be able to regain access unless you have a valid backup of the LUKS header.}} &lt;br /&gt;
&lt;br /&gt;
For above warning it is good to know the key we want to &#039;&#039;&#039;keep&#039;&#039;&#039; is valid. An easy check is to unlock the device with the {{ic|-v}} option, which will specify which slot it occupies:  &lt;br /&gt;
{{hc|# cryptsetup -v open /dev/&amp;lt;device&amp;gt; testcrypt|&lt;br /&gt;
Enter passphrase for /dev/&amp;lt;device&amp;gt;: &lt;br /&gt;
Key slot 1 unlocked.&lt;br /&gt;
Command successful.}}&lt;br /&gt;
&lt;br /&gt;
Now we can remove the key added in the previous subsection using its passphrase: &lt;br /&gt;
{{hc|# cryptsetup luksRemoveKey /dev/&amp;lt;device&amp;gt;|&lt;br /&gt;
Enter LUKS passphrase to be deleted: &lt;br /&gt;
}}&lt;br /&gt;
If we had used the same passphrase for two keyslots, the first slot would be wiped now. Only executing it again would remove the second one. &lt;br /&gt;
&lt;br /&gt;
Alternatively, we can specify the key slot: &lt;br /&gt;
{{hc|# cryptsetup luksKillSlot /dev/&amp;lt;device&amp;gt; 6|&lt;br /&gt;
Enter any remaining LUKS passphrase:&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
Note that in both cases, no confirmation was required.&lt;br /&gt;
{{hc|# cryptsetup luksDump /dev/sda8 {{!}} grep &#039;Slot 6&#039;|&lt;br /&gt;
Key Slot 6: DISABLED&lt;br /&gt;
}}&lt;br /&gt;
To re-iterate the warning above: If the same passphrase had been used for key slots 1 and 6, both would be gone now.&lt;br /&gt;
&lt;br /&gt;
=== バックアップとリストア ===&lt;br /&gt;
If the header of a LUKS encrypted partition gets destroyed, you will not be able to decrypt your data. It is just as much of a dilemma as forgetting the passphrase or damaging a key-file used to unlock the partition. Damage may occur by your own fault while re-partitioning the disk later or by third-party programs misinterpreting the partition table. Therefore, having a backup of the header and storing it on another disk might be a good idea.&lt;br /&gt;
&lt;br /&gt;
{{Note|If the LUKS-encrypted  partitions&#039; master passphrase becomes compromised, you must revoke it on &#039;&#039;every&#039;&#039; copy of the cryptheader, even those you have backed up. Otherwise, a copy of the backed-up cryptheader that uses the compromised passphrase can be used to decrypt the associated partition. See [https://gitlab.com/cryptsetup/cryptsetup/wikis/FrequentlyAskedQuestions#6-backup-and-data-recovery LUKS FAQ] for further details.}}&lt;br /&gt;
&lt;br /&gt;
==== cryptsetup を使ってバックアップ ====&lt;br /&gt;
Cryptsetup&#039;s {{ic|luksHeaderBackup}} action stores a binary backup of the LUKS header and keyslot area:&lt;br /&gt;
 # cryptsetup luksHeaderBackup /dev/&amp;lt;device&amp;gt; --header-backup-file /mnt/&amp;lt;backup&amp;gt;/&amp;lt;file&amp;gt;.img&lt;br /&gt;
where &amp;lt;device&amp;gt; is the partition containing the LUKS volume.&lt;br /&gt;
&lt;br /&gt;
{{Tip|You can also back up the plaintext header into ramfs and encrypt it in example with gpg before writing to persistent backup storage by executing the following commands.}}&lt;br /&gt;
 # mkdir /root/&amp;lt;tmp&amp;gt;/&lt;br /&gt;
 # mount ramfs /root/&amp;lt;tmp&amp;gt;/ -t ramfs&lt;br /&gt;
 # cryptsetup luksHeaderBackup /dev/&amp;lt;device&amp;gt; --header-backup-file /root/&amp;lt;tmp&amp;gt;/&amp;lt;file&amp;gt;.img&lt;br /&gt;
 # gpg2 --recipient &amp;lt;User ID&amp;gt; --encrypt /root/&amp;lt;tmp&amp;gt;/&amp;lt;file&amp;gt;.img &lt;br /&gt;
 # cp /root/&amp;lt;tmp&amp;gt;/&amp;lt;file&amp;gt;.img.gpg /mnt/&amp;lt;backup&amp;gt;/&lt;br /&gt;
 # umount /root/&amp;lt;tmp&amp;gt;&lt;br /&gt;
{{Warning|Tmpfs can swap to harddisk if low on memory so it is not recommended here.}}&lt;br /&gt;
&lt;br /&gt;
==== cryptsetup を使ってリストア ====&lt;br /&gt;
&lt;br /&gt;
{{Warning|Restoring the wrong header or restoring to an unencrypted partition will cause data loss! The action can not perform a check whether the header is actually the &#039;&#039;correct&#039;&#039; one for that particular device.}} &lt;br /&gt;
&lt;br /&gt;
In order to evade restoring a wrong header, you can ensure it does work by using it as a remote {{ic|--header}} first: &lt;br /&gt;
&lt;br /&gt;
{{hc|# cryptsetup -v --header /mnt/&amp;lt;backup&amp;gt;/&amp;lt;file&amp;gt;.img open /dev/&amp;lt;device&amp;gt; test|&lt;br /&gt;
Key slot 0 unlocked.&lt;br /&gt;
Command successful.&lt;br /&gt;
}}&lt;br /&gt;
 # mount /dev/mapper/test /mnt/test &amp;amp;&amp;amp; ls /mnt/test &lt;br /&gt;
 # umount /mnt/test &lt;br /&gt;
 # cryptsetup close test &lt;br /&gt;
&lt;br /&gt;
Now that the check succeeded, the restore may be performed: &lt;br /&gt;
 # cryptsetup luksHeaderRestore /dev/&amp;lt;device&amp;gt; --header-backup-file ./mnt/&amp;lt;backup&amp;gt;/&amp;lt;file&amp;gt;.img&lt;br /&gt;
&lt;br /&gt;
Now that all the keyslot areas are overwritten; only active keyslots from the backup file are available after issuing the command.&lt;br /&gt;
&lt;br /&gt;
==== 手動バックアップとリストア ====&lt;br /&gt;
The header always resides at the beginning of the device and a backup can be performed without access to &#039;&#039;cryptsetup&#039;&#039; as well. First you have to find out the payload offset of the crypted partition:&lt;br /&gt;
{{hc|# cryptsetup luksDump /dev/&amp;lt;device&amp;gt; {{!}} grep &amp;quot;Payload offset&amp;quot;|&lt;br /&gt;
Payload offset:	4040}}&lt;br /&gt;
Second check the sector size of the drive&lt;br /&gt;
{{hc|# fdisk -l /dev/&amp;lt;device&amp;gt; {{!}} grep &amp;quot;Sector size&amp;quot;|Sector size (logical/physical): 512 bytes / 512 bytes}}&lt;br /&gt;
&lt;br /&gt;
Now that you know the values, you can backup the header with a simple dd command:&lt;br /&gt;
 # dd if=/dev/&amp;lt;device&amp;gt; of=/path/to/&amp;lt;file&amp;gt;.img bs=512 count=4040&lt;br /&gt;
and store it safely.&lt;br /&gt;
&lt;br /&gt;
A restore can then be performed using the same values as when backing up:&lt;br /&gt;
 # dd if=./&amp;lt;file&amp;gt;.img of=/dev/&amp;lt;device&amp;gt; bs=512 count=4040&lt;br /&gt;
&lt;br /&gt;
=== デバイスの再暗号化 ===&lt;br /&gt;
&lt;br /&gt;
The {{Pkg|cryptsetup}} package features the &#039;&#039;cryptsetup-reencrypt&#039;&#039; tool. It can be used to convert an existing unencrypted filesystem to a LUKS encrypted one (option {{ic|--new}}) and permanently remove LUKS encryption ({{ic|--decrypt}}) from a device. As its name suggests it can also be used to re-encrypt an existing LUKS encrypted device, though, re-encryption is not possible for a detached LUKS header or other encryption modes (e.g. plain-mode). For re-encryption it is possible to change the [[#LUKS モードの暗号化オプション]]. &#039;&#039;cryptsetup-reencrypt&#039;&#039; actions can be performed to unmounted devices only.  See {{man|8|cryptsetup-reencrypt}} for more information. &lt;br /&gt;
&lt;br /&gt;
One application of re-encryption may be to secure the data again after a passphrase or [[#キーファイル|keyfile]] has been compromised &#039;&#039;and&#039;&#039; one cannot be certain that no copy of the LUKS header has been obtained. For example, if only a passphrase has been shoulder-surfed but no physical/logical access to the device happened, it would be enough to change the respective passphrase/key only ([[#Key management]]). &lt;br /&gt;
&lt;br /&gt;
{{Warning|Always make sure a &#039;&#039;&#039;reliable backup&#039;&#039;&#039; is available and double-check options you specify before using the tool!}}&lt;br /&gt;
&lt;br /&gt;
The following shows an example to encrypt an unencrypted filesystem partition and a re-encryption of an existing LUKS device. &lt;br /&gt;
 &lt;br /&gt;
==== 暗号化されていないファイルシステムの暗号化 ====&lt;br /&gt;
&lt;br /&gt;
A LUKS encryption header is always stored at the beginning of the device. Since an existing filesystem will usually be allocated all partition sectors, the first step is to shrink it to make space for the LUKS header. &lt;br /&gt;
&lt;br /&gt;
The [[#LUKS モードの暗号化オプション|default]] LUKS header encryption cipher requires {{ic|4096}} 512-byte sectors. We already checked space and keep it simple by shrinking the existing {{ic|ext4}} filesystem on {{ic|/dev/sdaX}} to its current possible minimum: &lt;br /&gt;
&lt;br /&gt;
 # umount /mnt&lt;br /&gt;
{{hc|# e2fsck -f /dev/sdaX|&lt;br /&gt;
e2fsck 1.43-WIP (18-May-2015)&lt;br /&gt;
Pass 1: Checking inodes, blocks, and sizes&lt;br /&gt;
...&lt;br /&gt;
/dev/sda6: 12/166320 files (0.0% non-contiguous), 28783/665062 blocks&lt;br /&gt;
}}&lt;br /&gt;
{{hc|# resize2fs -M /dev/sdaX|&lt;br /&gt;
resize2fs 1.43-WIP (18-May-2015)&lt;br /&gt;
Resizing the filesystem on /dev/sdaX to 26347 (4k) blocks.&lt;br /&gt;
The filesystem on /dev/sdaX is now 26347 (4k) blocks long.}}&lt;br /&gt;
&lt;br /&gt;
Now we encrypt it, using the default cipher we do not have to specify it explicitly. Note there is no option (yet) to double-check the passphrase before encryption starts, be careful not to mistype: &lt;br /&gt;
&lt;br /&gt;
{{hc|# cryptsetup-reencrypt /dev/sdaX --new  --reduce-device-size 4096S|&lt;br /&gt;
WARNING: this is experimental code, it can completely break your data.&lt;br /&gt;
Enter new passphrase: &lt;br /&gt;
Progress: 100,0%, ETA 00:00, 2596 MiB written, speed  37,6 MiB/s}}&lt;br /&gt;
&lt;br /&gt;
After it finished, the encryption was performed to the full partition, i.e. not only the space the filesystem was shrunk to ({{ic|sdaX}} has {{ic|2.6GiB}} and the CPU used in the example has no hardware AES instructions). As a final step we extend the filesystem of the now encrypted device again to occupy available space: &lt;br /&gt;
&lt;br /&gt;
{{hc|# cryptsetup open /dev/sdaX recrypt|&lt;br /&gt;
Enter passphrase for /dev/sdaX: &lt;br /&gt;
...&lt;br /&gt;
}}&lt;br /&gt;
{{hc|# resize2fs /dev/mapper/recrypt|&lt;br /&gt;
resize2fs 1.43-WIP (18-May-2015)&lt;br /&gt;
Resizing the filesystem on /dev/mapper/recrypt to 664807 (4k) blocks.&lt;br /&gt;
The filesystem on /dev/mapper/recrypt is now 664807 (4k) blocks long.}}&lt;br /&gt;
 # mount /dev/mapper/recrypt /mnt&lt;br /&gt;
&lt;br /&gt;
and are done.&lt;br /&gt;
&lt;br /&gt;
==== 既存の LUKS パーティションの再暗号化 ====&lt;br /&gt;
&lt;br /&gt;
以下の例では既存の LUKS デバイスを最暗号化しています。&lt;br /&gt;
&lt;br /&gt;
{{Warning|Double-check you specify encryption options for &#039;&#039;cryptsetup-reencrypt&#039;&#039; correctly and &#039;&#039;never&#039;&#039; re-encrypt without a &#039;&#039;&#039;reliable backup&#039;&#039;&#039;! As of September 2015 the tool &#039;&#039;&#039;does&#039;&#039;&#039; accept invalid options and damage the LUKS header, if not used correctly!}}&lt;br /&gt;
&lt;br /&gt;
In order to re-encrypt a device with its existing encryption options, they do not need to be specified. A simple: &lt;br /&gt;
&lt;br /&gt;
{{hc|# cryptsetup-reencrypt /dev/sdaX| &lt;br /&gt;
WARNING: this is experimental code, it can completely break your data.&lt;br /&gt;
Enter passphrase for key slot 0: &lt;br /&gt;
Progress: 100,0%, ETA 00:00, 2596 MiB written, speed  36,5 MiB/s}}&lt;br /&gt;
&lt;br /&gt;
performs it. &lt;br /&gt;
&lt;br /&gt;
A possible usecase is to re-encrypt LUKS devices which have non-current encryption options. Apart from above warning on specifying options correctly, the ability to change the LUKS header may also be limited by its size. For example, if the device was initially encrypted using a CBC mode cipher and 128 bit key-size, the LUKS header will be half the size of above mentioned {{ic|4096}} sectors: &lt;br /&gt;
{{hc|# cryptsetup luksDump /dev/sdaX &amp;lt;nowiki&amp;gt;|&amp;lt;/nowiki&amp;gt;grep -e &amp;quot;mode&amp;quot; -e &amp;quot;Payload&amp;quot; -e &amp;quot;MK bits&amp;quot;|&lt;br /&gt;
Cipher mode:   	cbc-essiv:sha256&lt;br /&gt;
Payload offset:	&#039;&#039;&#039;2048&#039;&#039;&#039;&lt;br /&gt;
MK bits:       	128}}&lt;br /&gt;
While it is possible to upgrade the encryption of such a device, it is currently only feasible in two steps. First, re-encrypting with the same encryption options, but using the {{ic|--reduce-device-size}} option to make further space for the larger LUKS header. Second, re-encypt the whole device again with the desired cipher. For this reason and the fact that a backup should be created in any case, creating a new, fresh encrypted device to restore into is always the faster option.&lt;br /&gt;
&lt;br /&gt;
== 暗号化デバイスのリサイズ ==&lt;br /&gt;
&lt;br /&gt;
If a storage device encrypted with dm-crypt is being cloned (with a tool like dd) to another larger device, the underlying dm-crypt device must be resized to use the whole space. &lt;br /&gt;
&lt;br /&gt;
The destination device is /dev/sdX2 in this example, the whole available space adjacent to the partition will be used:&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup luksOpen /dev/sdX2 sdX2&lt;br /&gt;
 # cryptsetup resize sdX2&lt;br /&gt;
&lt;br /&gt;
Then the underlying filesystem must be resized.&lt;br /&gt;
&lt;br /&gt;
=== ループバックファイルシステム ===&lt;br /&gt;
&lt;br /&gt;
暗号化したループバックデバイスを {{ic|/mnt/secret}} にマウントしている場合、例えば [[Dm-crypt/root 以外のファイルシステムの暗号化#Loop デバイス]] に従っている場合、最初に暗号化コンテナをアンマウント:&lt;br /&gt;
 # umount /mnt/secret&lt;br /&gt;
 # cryptsetup close secret&lt;br /&gt;
 # losetup -d /dev/loop0&lt;br /&gt;
&lt;br /&gt;
次に、追加したいデータ容量を決めてコンテナファイルを拡張します:&lt;br /&gt;
&lt;br /&gt;
{{Warning|{{ic|&amp;gt;}} を2つ使っていることに注意してください。1つだけだと現在のコンテナを上書きしてしまいます。}}&lt;br /&gt;
 # dd if=/dev/urandom bs=1M count=1024 | cat - &amp;gt;&amp;gt; /bigsecret&lt;br /&gt;
&lt;br /&gt;
ループデバイスにコンテナをマップ:&lt;br /&gt;
 # losetup /dev/loop0 /bigsecret&lt;br /&gt;
 # cryptsetup --type luks open /dev/loop0 secret&lt;br /&gt;
&lt;br /&gt;
その後、コンテナの暗号化された部分をコンテナファイルの最大サイズまでリサイズ:&lt;br /&gt;
 # cryptsetup resize secret&lt;br /&gt;
&lt;br /&gt;
最後に、ファイルシステムのチェックを実行して、問題なければ、リサイズを行います (例: ext2/3/4):&lt;br /&gt;
 # e2fsck -f /dev/mapper/secret&lt;br /&gt;
 # resize2fs /dev/mapper/secret&lt;br /&gt;
&lt;br /&gt;
またコンテナをマウントすることができます:&lt;br /&gt;
 # mount /dev/mapper/secret /mnt/secret&lt;br /&gt;
&lt;br /&gt;
== キーファイル ==&lt;br /&gt;
&lt;br /&gt;
{{Note|このセクションでは平文のキーファイルを使う方法を説明しています。キーファイルを暗号化して二段階認証したい場合は[[Dm-crypt/特記事項#GPG や OpenSSL で暗号化されたキーファイルを使う|GPG や OpenSSL で暗号化されたキーファイルを使う]]を見て下さい。ただし、このセクションもあらかじめ読むようにしてください。}}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;キーファイルとは？&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
キーファイルは暗号化されたボリュームを解錠するパスフレーズとして使用するデータを含んだファイルです。ファイルを喪失した場合、ボリュームの復号化はできなくなります。&lt;br /&gt;
&lt;br /&gt;
{{Tip|Define a passphrase in addition to the keyfile for backup access to encrypted volumes in the event the defined keyfile is lost or changed.}}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;なぜキーファイルを使うのか？&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
キーファイルには様々なタイプがあります。以下で説明しているようにそれぞれメリットとデメリットが存在します:&lt;br /&gt;
&lt;br /&gt;
=== キーファイルのタイプ ===&lt;br /&gt;
==== passphrase ====&lt;br /&gt;
&lt;br /&gt;
単にパスフレーズが記述されたキーファイルです。メリットはファイルを無くしても暗号化ボリュームの所有者が簡単に中身を思い出せることです。デメリットはパスフレーズの入力と比べてセキュリティ上の優位点がないことです。&lt;br /&gt;
&lt;br /&gt;
例: {{ic|1234}}。&lt;br /&gt;
&lt;br /&gt;
{{Note|パスフレーズが記述されたキーファイルに改行を含めてはいけません。以下のようなコマンドでファイルを作成できます:&lt;br /&gt;
 # echo -n &#039;your_passphrase&#039; &amp;gt; /path/to/&amp;lt;keyfile&amp;gt;&lt;br /&gt;
 # chown root:root /path/to/&amp;lt;keyfile&amp;gt;; chmod 400 /path/to/&amp;lt;keyfile&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==== randomtext ====&lt;br /&gt;
&lt;br /&gt;
This is a keyfile containing a block of random characters. The benefit of this type of keyfile is that it is much more resistant to dictionary attacks than a simple passphrase. An additional strength of keyfiles can be utilized in this situation which is the length of data used. Since this is not a string meant to be memorized by a person for entry, it is trivial to create files containing thousands of random characters as the key. The disadvantage is that if this file is lost or changed, it will most likely not be possible to access the encrypted volume without a backup passphrase.&lt;br /&gt;
&lt;br /&gt;
例: {{ic|fjqweifj830149-57 819y4my1-38t1934yt8-91m 34co3;t8y;9p3y-}}。&lt;br /&gt;
&lt;br /&gt;
==== binary ====&lt;br /&gt;
&lt;br /&gt;
This is a binary file that has been defined as a keyfile. When identifying files as candidates for a keyfile, it is recommended to choose files that are relatively static such as photos, music, video clips. The benefit of these files is that they serve a dual function which can make them harder to identify as keyfiles. Instead of having a text file with a large amount of random text, the keyfile would look like a regular image file or music clip to the casual observer. The disadvantage is that if this file is lost or changed, it will most likely not be possible to access the encrypted volume without a backup passphrase. Additionally, there is a theoretical loss of randomness when compared to a randomly generated text file. This is due to the fact that images, videos and music have some intrinsic relationship between neighboring bits of data that does not exist for a text file. However this is controversial and has never been exploited publicly.&lt;br /&gt;
&lt;br /&gt;
例: 画像, テキスト, 動画。&lt;br /&gt;
&lt;br /&gt;
=== ランダムな文字列でキーファイルを作成 ===&lt;br /&gt;
&lt;br /&gt;
==== ファイルシステムにキーファイルを保存 ====&lt;br /&gt;
&lt;br /&gt;
キーファイルの中身とサイズは任意に決められます。&lt;br /&gt;
&lt;br /&gt;
以下では {{ic|dd}} を使って2048バイトのランダムなキーファイルを生成して、{{ic|/etc/mykeyfile}} ファイルに保存します:&lt;br /&gt;
&lt;br /&gt;
 # dd bs=512 count=4 if=/dev/urandom of=/etc/mykeyfile&lt;br /&gt;
&lt;br /&gt;
キーファイルを外部デバイスに保存する場合、出力先を適切なディレクトリに変更します:&lt;br /&gt;
&lt;br /&gt;
 # dd bs=512 count=4 if=/dev/urandom of=/media/usbstick/mykeyfile&lt;br /&gt;
&lt;br /&gt;
===== 保存されたキーファイルを完全に消去 =====&lt;br /&gt;
&lt;br /&gt;
物理的なストレージデバイスに一時的なキーファイルを保存する場合、削除するときは、以下のように完全に削除してください:&lt;br /&gt;
&lt;br /&gt;
 # shred --remove --zero mykeyfile&lt;br /&gt;
&lt;br /&gt;
FAT や ext2 であれば上記で十分ですが、ジャーナリングファイルシステムやフラッシュメモリハードウェアの場合、[[ディスクの完全消去]]あるいはキーファイルのあるパーティションを削除することを強く推奨します。&lt;br /&gt;
&lt;br /&gt;
==== ramfs にキーファイルを保存 ====&lt;br /&gt;
&lt;br /&gt;
もしくは、ramfs をマウントしてキーファイルを一時的に保存:&lt;br /&gt;
&lt;br /&gt;
 # mkdir /root/myramfs&lt;br /&gt;
 # mount ramfs /root/myramfs/ -t ramfs&lt;br /&gt;
 # cd /root/myramfs&lt;br /&gt;
&lt;br /&gt;
物理ディスクではなくメモリ上に配置することで、ramfs をアンマウントしたら復元できないという利点があります。キーファイルを他の安全なファイルシステムにコピーしたら、以下のコマンドで ramfs をアンマウントしてください:&lt;br /&gt;
 # umount /root/myramfs&lt;br /&gt;
&lt;br /&gt;
=== キーファイルを使用するように LUKS を設定 ===&lt;br /&gt;
&lt;br /&gt;
キーファイルのキースロットを LUKS ヘッダに追加:&lt;br /&gt;
&lt;br /&gt;
{{hc|# cryptsetup luksAddKey /dev/sda2 /etc/mykeyfile|&lt;br /&gt;
Enter any LUKS passphrase:&lt;br /&gt;
key slot 0 unlocked.&lt;br /&gt;
Command successful.}}&lt;br /&gt;
&lt;br /&gt;
=== キーファイルを使ってパーティションを手動でアンロック ===&lt;br /&gt;
&lt;br /&gt;
LUKS デバイスをオープンするときに {{ic|--key-file}} オプションを使ってください:&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup open /dev/sda2 &#039;&#039;dm_name&#039;&#039; --key-file /etc/mykeyfile&lt;br /&gt;
&lt;br /&gt;
=== 起動時にロックを解除 ===&lt;br /&gt;
&lt;br /&gt;
ファイルシステムのキーファイルを暗号化されたルートディレクトリの中に保存することで、システムの電源が切れているときに安全を確保することができ、[[Dm-crypt/システム設定#crypttab|crypttab]] によって起動時に自動的にマウントを解錠することができます。[[UUID]] を使用する例:&lt;br /&gt;
{{hc|/etc/crypttab|&amp;lt;nowiki&amp;gt;home    UUID=&amp;lt;UUID identifier&amp;gt;    /etc/mykeyfile&amp;lt;/nowiki&amp;gt;}}&lt;br /&gt;
is all needed for unlocking, and &lt;br /&gt;
{{hc|/etc/fstab|/dev/mapper/home        /home   ext4        defaults        0       2}} for mounting the LUKS blockdevice with the generated keyfile.&lt;br /&gt;
{{Tip|If you prefer to use a {{ic|--plain}} mode blockdevice, the encryption options necessary to unlock it are specified in {{ic|/etc/crypttab}}. Take care to apply the systemd workaround mentioned in [[Dm-crypt/システム設定#crypttab|crypttab]] in this case.}}&lt;br /&gt;
&lt;br /&gt;
=== キーファイルを使って起動時に root パーティションのロックを解除 ===&lt;br /&gt;
&lt;br /&gt;
[[mkinitcpio]] を設定して必要なモジュールやファイルを記述して [[Dm-crypt/システム設定#cryptkey|cryptkey]] [[カーネルパラメータ]]を設定してキーファイルの場所を指定します。&lt;br /&gt;
&lt;br /&gt;
2つの方法が存在します:&lt;br /&gt;
&lt;br /&gt;
# 外部メディア (USB スティック) にキーファイルを保存&lt;br /&gt;
# initramfs にキーファイルを埋め込む&lt;br /&gt;
&lt;br /&gt;
==== キーファイルを外部メディアに保存 ====&lt;br /&gt;
&lt;br /&gt;
===== mkinitcpio の設定 =====&lt;br /&gt;
&lt;br /&gt;
ドライブのファイルシステムを使うために {{ic|/etc/mkinitcpio.conf}} にモジュールを追加してください (以下の例では {{ic|vfat}} モジュール):&lt;br /&gt;
 MODULES=(vfat)&lt;br /&gt;
&lt;br /&gt;
In this example it is assumed that you use a FAT formatted USB drive ({{ic|vfat}} module). Replace those module names if you use another file system on your USB stick (e.g. {{ic|ext2}}) or another codepage. If it complains of bad superblock and bad codepage at boot, then you need an extra codepage module to be loaded. For instance, you may need {{ic|nls_iso8859-1}} module for {{ic|iso8859-1}} codepage.&lt;br /&gt;
&lt;br /&gt;
If you have a non-US keyboard, it might prove useful to load your keyboard layout before you are prompted to enter the password to unlock the root partition at boot. For this, you will need the {{ic|keymap}} hook before {{ic|encrypt}}.&lt;br /&gt;
&lt;br /&gt;
Generate a new initramfs image:&lt;br /&gt;
&lt;br /&gt;
 # mkinitcpio -p linux&lt;br /&gt;
&lt;br /&gt;
===== カーネルパラメータの設定 =====&lt;br /&gt;
&lt;br /&gt;
以下のオプションを[[カーネルパラメータ]]に追加してください ({{ic|encrypt}} フックではなく {{ic|sd-encrypt}} フックを使用する場合は [[Dm-crypt/システム設定#sd-encrypt フックを使う]]を参照):&lt;br /&gt;
&lt;br /&gt;
 cryptdevice=/dev/&#039;&#039;&amp;lt;partition1&amp;gt;&#039;&#039;:root cryptkey=/dev/&#039;&#039;&amp;lt;partition2&amp;gt;&#039;&#039;:&amp;lt;fstype&amp;gt;:&amp;lt;path&amp;gt;&lt;br /&gt;
&lt;br /&gt;
例:&lt;br /&gt;
&lt;br /&gt;
 cryptdevice=/dev/sda3:root cryptkey=/dev/sdb1:vfat:/keys/secretkey&lt;br /&gt;
&lt;br /&gt;
Choosing a plain filename for your key provides a bit of &#039;security through obscurity&#039;. The keyfile can not be a hidden file, that means the filename must not start with a dot, or the {{ic|encrypt}} hook will fail to find the keyfile during the boot process.&lt;br /&gt;
&lt;br /&gt;
{{ic|/dev/sdb1}} のようなデバイスノードの名前は再起動しても同じであるとは保証されていません。udev による[[永続的なブロックデバイスの命名]]を使ったほうが確実にデバイスにアクセスできます。外部ストレージデバイスからキーファイルを読み取るときに {{ic|encrypt}} フックが確実にキーファイルを見つけられるように、永続的なブロックデバイスの名前を絶対に使うべきです。[[永続的なブロックデバイスの命名]]を見て下さい。&lt;br /&gt;
&lt;br /&gt;
==== キーファイルを initramfs に埋め込む ====&lt;br /&gt;
&lt;br /&gt;
{{Warning|Use an embedded keyfile &#039;&#039;&#039;only&#039;&#039;&#039; if you have some form of authentication mechanism beforehand that protects the keyfile sufficiently. Otherwise auto-decryption will occur, defeating completely the purpose of block device encryption.}}&lt;br /&gt;
&lt;br /&gt;
This method allows to use a specially named keyfile that will be embedded in the [[initramfs]] and picked up by the {{ic|encrypt}} [[Mkinitcpio#HOOKS|hook]] to unlock the root filesystem ({{ic|cryptdevice}}) automatically. It may be useful to apply when using the [[GRUB#暗号化された /boot|GRUB early cryptodisk]] feature, in order to avoid entering two passphrases during boot.&lt;br /&gt;
&lt;br /&gt;
The {{ic|encrypt}} hook lets the user specify a keyfile with the {{ic|cryptkey}} kernel parameter: in the case of initramfs, the syntax is {{ic|rootfs:&#039;&#039;path&#039;&#039;}}, see [[Dm-crypt/システム設定#cryptkey]]. Besides, this kernel parameter defaults to use {{ic|/crypto_keyfile.bin}}, and if the initramfs contains a valid key with this name, decryption will occur automatically without the need to configure the {{ic|cryptkey}} parameter.&lt;br /&gt;
&lt;br /&gt;
{{ic|encrypt}} の代わりに {{ic|sd-encrypt}} を使用する場合、{{ic|luks.key}} カーネルパラメータでキーファイルの場所を指定してください。[[Dm-crypt/システム設定#luks.key]] を参照。&lt;br /&gt;
&lt;br /&gt;
[[#ランダムな文字列でキーファイルを作成|キーファイルを生成]]して適切な権限を与えて [[#LUKS キーの追加|LUKS キーとして追加]]:&lt;br /&gt;
&lt;br /&gt;
 # dd bs=512 count=4 if=/dev/urandom of=/crypto_keyfile.bin&lt;br /&gt;
 # chmod 000 /crypto_keyfile.bin&lt;br /&gt;
 # chmod 600 /boot/initramfs-linux*&lt;br /&gt;
 # cryptsetup luksAddKey /dev/sdX# /crypto_keyfile.bin&lt;br /&gt;
&lt;br /&gt;
{{Warning|When initramfs&#039; permissions are set to 644 (by default), then all users will be able to dump the keyfile. Make sure the permissions are still 600 if you install a new kernel.}}&lt;br /&gt;
&lt;br /&gt;
[[Mkinitcpio#BINARIES_と_FILES|mkinitcpio の FILES]] にキーを記述:&lt;br /&gt;
&lt;br /&gt;
{{hc|/etc/mkinitcpio.conf|2=FILES=(/crypto_keyfile.bin)}}&lt;br /&gt;
&lt;br /&gt;
最後に [[Mkinitcpio#イメージ作成とアクティベーション|initramfs を再生成]]してください。&lt;br /&gt;
&lt;br /&gt;
次の起動時からコンテナを復号化するパスフレーズを入力するのは一度だけですむようになります。&lt;br /&gt;
&lt;br /&gt;
詳しくは [http://www.pavelkogan.com/2014/05/23/luks-full-disk-encryption/#bonus-login-once こちら] を参照。&lt;/div&gt;</summary>
		<author><name>Takemaro</name></author>
	</entry>
	<entry>
		<id>https://wiki.archlinux.jp/index.php?title=Dm-crypt/%E3%83%87%E3%83%90%E3%82%A4%E3%82%B9%E3%81%AE%E6%9A%97%E5%8F%B7%E5%8C%96&amp;diff=39959</id>
		<title>Dm-crypt/デバイスの暗号化</title>
		<link rel="alternate" type="text/html" href="https://wiki.archlinux.jp/index.php?title=Dm-crypt/%E3%83%87%E3%83%90%E3%82%A4%E3%82%B9%E3%81%AE%E6%9A%97%E5%8F%B7%E5%8C%96&amp;diff=39959"/>
		<updated>2025-03-09T18:19:18Z</updated>

		<summary type="html">&lt;p&gt;Takemaro: /* Cryptsetup の使用方法 */ 英語版を反映&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Lowercase title}}&lt;br /&gt;
[[Category:保存データ暗号化]]&lt;br /&gt;
[[Category:ファイルシステム]]&lt;br /&gt;
[[en:Dm-crypt/Device encryption]]&lt;br /&gt;
このセクションではコマンドラインから &#039;&#039;dm-crypt&#039;&#039; を利用して手動でシステムを暗号化する方法を説明しています。&lt;br /&gt;
&lt;br /&gt;
== 準備 ==&lt;br /&gt;
{{pkg|cryptsetup}} を使用する前に、{{ic|dm_crypt}} [[カーネルモジュール]]がロードされていることを確認してください。&lt;br /&gt;
&lt;br /&gt;
== Cryptsetup の使用方法 ==&lt;br /&gt;
&#039;&#039;Cryptsetup&#039;&#039; は暗号化デバイスを作成・管理する &#039;&#039;dm-crypt&#039;&#039; を使うためのコマンドラインツールです。後に Linux カーネルの &#039;&#039;&#039;d&#039;&#039;&#039;evice-&#039;&#039;&#039;m&#039;&#039;&#039;apper と &#039;&#039;&#039;crypt&#039;&#039;&#039;ographic モジュールを使用する別の暗号化もサポートするように拡張されました。最も著しい拡張は Linux Unified Key Setup (LUKS) の拡張で、dm-crypt をセットアップするのに必要な情報を全てディスク自体に保存してパーティションとキーの管理を抽象化することで使いやすさを増しています。device-mapper によってアクセスされるデバイスはブロックデバイスと呼ばれます。詳しくは[[ディスク暗号化#ブロックデバイスの暗号化]]を見て下さい。&lt;br /&gt;
&lt;br /&gt;
ツールは以下のように使います:&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup &#039;&#039;action&#039;&#039; &#039;&#039;options&#039;&#039; &#039;&#039;device&#039;&#039; &#039;&#039;dmname&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
オプションや暗号化モードにはデフォルト値が存在し、コマンドラインで何も指定しなかった場合はデフォルト値が使われます。オプションや暗号化モードのデフォルトパラメータを確認するには以下のコマンドを実行:&lt;br /&gt;
&lt;br /&gt;
 $ cryptsetup --help &lt;br /&gt;
&lt;br /&gt;
オプションの完全なリストは man ページで確認できます。暗号化モードやアクションによって必要なパラメータは変わるため、以下のセクションでは違いについて説明しています。ブロックデバイス暗号化は高速ですが、速度は非常に重要な問題です。ブロックデバイスの暗号を設定後に変更することは難しいため、それぞれのパラメータについて &#039;&#039;dm-crypt&#039;&#039; のパフォーマンスをチェックすると良いでしょう:&lt;br /&gt;
&lt;br /&gt;
 $ cryptsetup benchmark &lt;br /&gt;
&lt;br /&gt;
上記のコマンドはインストールするときにアルゴリズムや鍵長を決める手がかりになります。特定の AES 暗号が非常に高速に処理される場合、おそらくその暗号は CPU によるハードウェア支援の恩恵に与かっています。&lt;br /&gt;
&lt;br /&gt;
{{Tip|テストしたいときは[[:カテゴリ:仮想化|仮想マシン]]の仮想ハードドライブで練習すると良いでしょう。}}&lt;br /&gt;
&lt;br /&gt;
=== Cryptsetup のパスフレーズとキー ===&lt;br /&gt;
暗号化されたブロックデバイスはキーによって保護されます。キーは以下のいずれかです:&lt;br /&gt;
&lt;br /&gt;
*パスフレーズ、[[セキュリティ#パスワード]]を見て下さい。&lt;br /&gt;
*キーファイル、[[#キーファイル]] を見て下さい。&lt;br /&gt;
&lt;br /&gt;
どちらのタイプのキーも最大サイズが決められています: パスフレーズは512文字まで、キーファイルは 8192kB までです。&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;LUKS&#039;&#039; の重要な特徴として、キーは LUKS によって暗号化されたデバイスのマスターキーを解除するのに使われ、root 権限で変えることができるということです。他の暗号化モードでは設定後にキーを変更することはできません。暗号化にマスターキーを使わないためです。詳しくは[[ディスク暗号化#ブロックデバイスの暗号化]]を見て下さい。&lt;br /&gt;
&lt;br /&gt;
== dm-crypt の暗号化オプション ==&lt;br /&gt;
&#039;&#039;Cryptsetup&#039;&#039; は &#039;&#039;dm-crypt&#039;&#039; で使用できる様々な暗号化モードをサポートしています。最も一般的な (デフォルトの) モードは:&lt;br /&gt;
*{{ic|--type LUKS}}&lt;br /&gt;
他に利用できるモードは:&lt;br /&gt;
*{{ic|--type plain}} - dm-crypt plain モードを使用。&lt;br /&gt;
*{{ic|--type loopaes}} - loopaes legacy モードを使用。&lt;br /&gt;
*{{ic|--type tcrypt}} - [[Truecrypt]] 互換モードを使用。&lt;br /&gt;
&lt;br /&gt;
暗号やハッシュの基本的な暗号化オプションは全てのモードで使うことができ、カーネルの暗号化バックエンド機能を利用します。使用できるオプションは以下のコマンドで確認できます:&lt;br /&gt;
 $ less /proc/crypto &lt;br /&gt;
リストが短い場合、{{ic|cryptsetup benchmark}} を実行すればモジュールのロードが行われます。&lt;br /&gt;
&lt;br /&gt;
以下では最初の2つのモードの暗号化オプションを紹介します。記事の中で例として使っているオプションを並べていますが、利用できるオプションの全てではないので注意してください。&lt;br /&gt;
&lt;br /&gt;
=== LUKS モードの暗号化オプション ===&lt;br /&gt;
LUKS 暗号化モードで新しい dm-crypt デバイスをセットアップする &#039;&#039;cryptsetup&#039;&#039; のアクションは &#039;&#039;luksFormat&#039;&#039; です。名前にはフォーマットとありますが、実際にはデバイスをフォーマットするのではなく、LUKS デバイスヘッダーを設定して指定された暗号オプションを使ってマスター鍵を暗号化します。&lt;br /&gt;
&lt;br /&gt;
LUKS はデフォルトの暗号化モードなので、デフォルトパラメータで新しい LUKS デバイスを作成する場合、以下のコマンドだけで作成できます ({{ic|-v}} は任意です):&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup -v luksFormat &#039;&#039;device&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
手動でデフォルトのオプションを全て指定すると以下のようになります:&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup -v --cipher aes-xts-plain64 --key-size 256 --hash sha256 --iter-time 2000 --use-urandom --verify-passphrase luksFormat &#039;&#039;device&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
以下の表ではデフォルト値とサンプルにコメントを付けて説明しています:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; style=&amp;quot;text-align:left&amp;quot; | オプション&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; style=&amp;quot;text-align:left&amp;quot; | Cryptsetup (1.7.0) のデフォルト&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; style=&amp;quot;text-align:left&amp;quot; | 例&lt;br /&gt;
! scope=&amp;quot;col&amp;quot; style=&amp;quot;text-align:left&amp;quot; | コメント&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;row&amp;quot; style=&amp;quot;text-align:right&amp;quot; | --cipher, -c&lt;br /&gt;
| {{ic|aes-xts-plain64}}&lt;br /&gt;
| {{ic|aes-xts-plain64}}&lt;br /&gt;
| 例ではデフォルトと同じ暗号を使っています: AES [[ディスク暗号化#暗号と利用形態|暗号]]と [[wikipedia:Disk encryption theory#XEX-based tweaked-codebook mode with ciphertext stealing (XTS)|XTS]]。&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;row&amp;quot; style=&amp;quot;text-align:right&amp;quot; | --key-size, -s&lt;br /&gt;
| {{ic|256}}&lt;br /&gt;
| {{ic|512}}&lt;br /&gt;
| デフォルトでは256ビットが使われます。ただし [[wikipedia:Disk encryption theory#XEX-based tweaked-codebook mode with ciphertext stealing (XTS)|XTS はキーを半分に割る]]ので、AES-128 ではなく AES-256 を使うには XTS のキーサイズを {{ic|512}} に設定する必要があります。&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;row&amp;quot; style=&amp;quot;text-align:right&amp;quot; | --hash, -h&lt;br /&gt;
| {{ic|sha256}}&lt;br /&gt;
| {{ic|sha512}}&lt;br /&gt;
| [[ディスク暗号化#キーとキーファイルとパスフレーズ|PBKDF2]] で使用されるハッシュアルゴリズム。リリース 1.7.0 でデフォルト設定が {{ic|sha1}} から {{ic|sha256}} に変更されました。セキュリティ上の理由ではなく SHA1 が使用できないシステムでも動作するようにするためです [https://www.kernel.org/pub/linux/utils/cryptsetup/v1.7/v1.7.0-ReleaseNotes]。{{ic|sha1}} でも十分セキュアであるため古いバージョンの &#039;&#039;cryptsetup&#039;&#039; と互換性を維持する目的で使用できます [https://gitlab.com/cryptsetup/cryptsetup/wikis/FrequentlyAskedQuestions#5-security-aspects]。&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;row&amp;quot; style=&amp;quot;text-align:right&amp;quot; | --iter-time, -i&lt;br /&gt;
| {{ic|2000}}&lt;br /&gt;
| {{ic|5000}}&lt;br /&gt;
| PBKDF2 パスフレーズを処理するのに費やす時間 (ミリ秒単位)。&amp;quot;&#039;&#039;PBKDF2 の反復回数を高く保持したままユーザーが許容できる数値にするため&#039;&#039;&amp;quot;、リリース 1.7.0 からデフォルト値が {{ic|1000}} から {{ic|2000}} に変更されています [https://www.kernel.org/pub/linux/utils/cryptsetup/v1.7/v1.7.0-ReleaseNotes]。&#039;&#039;luksFormat&#039;&#039; や &#039;&#039;luksAddKey&#039;&#039; などパスフレーズを設定・変更する LUKS 操作のみに関連するオプションです。パラメータとして 0 を指定するとコンパイル時のデフォルトが選択されます。&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;row&amp;quot; style=&amp;quot;text-align:right&amp;quot; | --use-{u,}random&lt;br /&gt;
| {{ic|--use-&#039;&#039;&#039;u&#039;&#039;&#039;random}}&lt;br /&gt;
| {{ic|--use-random}}&lt;br /&gt;
| [[乱数生成|乱数生成器]]の選択。cryptsetup のマニュアルページより: &amp;quot;エントロピーが少ない環境 (例: 組み込み環境)、どちらを選択しても問題になります。/dev/urandom を使うとキーが弱くなりかねません。/dev/random を使うと長い時間がかかり、カーネルから提供されるエントロピーが足りない場合、永遠に終わりません。&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! scope=&amp;quot;row&amp;quot; style=&amp;quot;text-align:right&amp;quot; | --verify-passphrase, -y&lt;br /&gt;
| Yes&lt;br /&gt;
| -&lt;br /&gt;
| luksFormat と luksAddKey のデフォルトのみ。Arch Linux で LUKS モードを使うのに入力する必要はありません。&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
LUKS の暗号機能について詳しく知りたい場合は [https://gitlab.com/cryptsetup/cryptsetup/wikis/Specification LUKS specification] (例: 付記) を読むと良いでしょう。&lt;br /&gt;
{{Tip|LUKS ヘッダーは、やがて別の大きな改訂を受けると予想されます。計画に興味がある場合は、開発者の [https://mbroz.fedorapeople.org/talks/DevConf2016/devconf2016-luks2.pdf devconfcz2016] (pdf) プレゼンテーションに要約されています。}}&lt;br /&gt;
&lt;br /&gt;
=== plain モードの暗号化オプション ===&lt;br /&gt;
dm-crypt の &#039;&#039;plain&#039;&#039; モードでは、デバイスのマスター鍵が存在せず、セットアップも必要ありません。代わりに、直接暗号化オプションを使用して暗号ディスクと名前付きデバイスのマッピングを作成します。パーティションやデバイス全体に対してマッピングを作成できます。後者の場合、パーティションテーブルも不要です。&lt;br /&gt;
&lt;br /&gt;
cryptsetup のデフォルトパラメータを使って &#039;&#039;plain&#039;&#039; のマッピングを作成するには:&lt;br /&gt;
 # cryptsetup &amp;lt;options&amp;gt; open --type plain &amp;lt;device&amp;gt; &amp;lt;dmname&amp;gt;&lt;br /&gt;
実行するとパスワードを求められます。以下は [[Dm-crypt/システム全体の暗号化#Plain dm-crypt]] の例とデフォルトパラメータの比較表です。&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! オプション !! Cryptsetup (1.7.0) のデフォルト !! 例 !! コメント !! &lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;--hash&#039;&#039;&#039; || {{ic|ripemd160}} || - || パスフレーズからキーを作成するのに使用するハッシュ。キーファイルでは使われない。&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;--cipher&#039;&#039;&#039;|| {{ic|aes-cbc-essiv:sha256}}|| {{ic|twofish-xts-plain64}} || 暗号は3つの文字列からなります: cipher-chainmode-IV generator。[[ディスク暗号化#暗号と利用形態]]や [https://gitlab.com/cryptsetup/cryptsetup/wikis/DMCrypt DMCrypt のドキュメント] を見てください。&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;--key-size&#039;&#039;&#039;||{{ic|256}}||{{ic|512}}||キーサイズ (ビット数)。サイズは使用する暗号や使用するチェインモードによって変わります。Xts モードは cbc モードの2倍のキーサイズを必要とします。&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;--offset&#039;&#039;&#039;||{{ic|0}}||{{ic|0}}||マッピングを開始するディスクの先頭からのオフセット。&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;--key-file&#039;&#039;&#039;||デフォルトではパスフレーズを使用||{{ic|/dev/sd&#039;&#039;Z&#039;&#039;}} (もしくは {{ic|/boot/keyfile.enc}})||キーとして使用するデバイスまたはファイル。詳しくは [[#キーファイル]] を参照。&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;--keyfile-offset&#039;&#039;&#039;||{{ic|0}}||{{ic|0}}||キーファイルの先頭からのオフセット (バイト数)。&#039;&#039;cryptsetup&#039;&#039; 1.6.7 以上でサポートされているオプション。&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;--keyfile-size&#039;&#039;&#039;||{{ic|8192kB}}||- (デフォルト)||キーファイルから読み込まれるバイト数を制限。&#039;&#039;cryptsetup&#039;&#039; 1.6.7 以上でサポートされているオプション。&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
{{ic|/dev/sd&#039;&#039;X&#039;&#039;}} デバイスで、上記の例を使用する場合:&lt;br /&gt;
 # cryptsetup --cipher=twofish-xts-plain64 --offset=0 --key-file=/dev/sd&#039;&#039;Z&#039;&#039; --key-size=512 open --type=plain /dev/sdX enc&lt;br /&gt;
&lt;br /&gt;
LUKS による暗号化と違って、上記のコマンドはマッピングを再現するために毎回全て指定する必要があります。暗号やハッシュ、キーファイルについて覚えておく必要があります。マッピングが作成されたことは以下のコマンドで確認できます:&lt;br /&gt;
 # fdisk -l&lt;br /&gt;
{{ic|/dev/mapper/enc}} のエントリが存在しているはずです。&lt;br /&gt;
&lt;br /&gt;
== cryptsetup でデバイスを暗号化 ==&lt;br /&gt;
以下のセクションでは新しい暗号化ブロックデバイスを作成するオプションと手動のアクセス方法を説明します。&lt;br /&gt;
&lt;br /&gt;
=== LUKS モードでデバイスを暗号化 ===&lt;br /&gt;
==== LUKS パーティションのフォーマット ====&lt;br /&gt;
&lt;br /&gt;
暗号化 LUKS パーティションとして設定するには次を実行:&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup luksFormat &#039;&#039;device&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
上記のコマンドを実行するとパスワードの入力が要求されます。&lt;br /&gt;
&lt;br /&gt;
コマンドラインオプションは [[#LUKS モードの暗号化オプション]]を参照。&lt;br /&gt;
&lt;br /&gt;
結果は次のコマンドで確認できます:&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup luksDump &#039;&#039;device&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
ダンプでは暗号のヘッダー情報だけでなく、LUKS パーティションで使われるキースロットもわかります。&lt;br /&gt;
&lt;br /&gt;
以下の例はデフォルトの AES 暗号を使って256ビットの XTS モードで {{ic|/dev/sda1}} に暗号化したルートパーティションを作成します:&lt;br /&gt;
 # cryptsetup -s 512 luksFormat /dev/sda1&lt;br /&gt;
&lt;br /&gt;
=====LUKS を使ってキーファイルでパーティションをフォーマット=====&lt;br /&gt;
&lt;br /&gt;
LUKS による暗号化パーティションを新しく作成する場合、以下のように作成するときにパーティションにキーファイルを関連付けることができます:&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup luksFormat &#039;&#039;device&#039;&#039; &#039;&#039;/path/to/mykeyfile&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
キーファイルを作成・管理する方法は [[#キーファイル]] を見て下さい。&lt;br /&gt;
&lt;br /&gt;
====デバイスマッパーで LUKS パーティションのロックを解除・マップ====&lt;br /&gt;
&lt;br /&gt;
LUKS パーティションを作成したら、解錠することができます。&lt;br /&gt;
&lt;br /&gt;
解錠ではデバイスマッパーによってパーティションが新しいデバイス名にマッピングされます。カーネルは {{ic|&#039;&#039;device&#039;&#039;}} が暗号化デバイスであり暗号化データを上書きしないように {{ic|/dev/mapper/&#039;&#039;dm_name&#039;&#039;}} を使って LUKS 経由で解決する必要があることを知ります。間違って上書きしないために、設定が完了したら[[#バックアップとリストア|暗号化ヘッダーをバックアップ]]する方法を確認してください。&lt;br /&gt;
&lt;br /&gt;
暗号化された LUKS パーティションを開くには次のコマンドを実行:&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup open --type luks &#039;&#039;device&#039;&#039; &#039;&#039;dm_name&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
実行するとパーティションを解錠するパスワードの入力が求められます。通常、マッピングするデバイスの名前はパーティションの機能が分かりやすいものにします。例えば、以下のコマンドは luks パーティション {{ic|/dev/sda1}} を解錠して {{ic|cryptroot}} という名前のデバイスマッパーにマッピングします:&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup open --type luks /dev/sda1 cryptroot &lt;br /&gt;
&lt;br /&gt;
パーティションをオープンしたら、ルートパーティションのデバイスアドレスはパーティション本体 (例: {{ic|/dev/sda1}}) の代わりに {{ic|/dev/mapper/cryptroot}} が使われます。&lt;br /&gt;
&lt;br /&gt;
暗号化レイヤーの上に LVM をセットアップする場合、復号化したボリュームグループのデバイスファイルは {{ic|/dev/sda1}} ではなく {{ic|/dev/mapper/cryptroot}} などとなります。それから LVM は作成されている論理ボリューム全てに名前を割り当てます (例: {{ic|/dev/mapper/lvmpool-root}} や {{ic|/dev/mapper/lvmpool-swap}})。&lt;br /&gt;
&lt;br /&gt;
暗号化データをパーティションに書き込むときは、マッピングされたデバイス名を使ってアクセスする必要があります。例えば[[ファイルシステム#デバイスのフォーマット|ファイルシステムを作成]]するときは以下のようにします:&lt;br /&gt;
&lt;br /&gt;
 # mkfs -t ext4 /dev/mapper/cryptroot&lt;br /&gt;
&lt;br /&gt;
{{ic|/dev/mapper/cryptroot}} デバイスは他のパーティションと同じように[[マウント]]できます。&lt;br /&gt;
&lt;br /&gt;
luks コンテナを閉じるには、パーティションをアンマウントしてください:&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup close cryptroot&lt;br /&gt;
&lt;br /&gt;
=== plain モードでデバイスを暗号化 ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;dm-crypt&#039;&#039; プレーンモード暗号化の作成とその後のアクセスはどちらも、正しい [[#plain視の暗号化オプション| parametersで &#039;&#039;cryptsetup&#039;&#039; {{ic|open}} アクションを使用するだけで済みます。]] 以下は、非ルートデバイスの2つの例で示していますが、両方をスタックすることで癖が追加されています (つまり、2番目は最初のデバイス内に作成されます)  明らかに、暗号化をスタックするとオーバーヘッドが2倍になります。 ここでの使用例は、暗号オプションの使用法の別の例を説明するためのものです。&lt;br /&gt;
&lt;br /&gt;
上記の表の左側の列で説明されているように、最初のマッパーは &#039;&#039;cryptsetup&#039;&#039; のプレーンモードのデフォルトで作成されます。&lt;br /&gt;
{{hc|# cryptsetup --type plain -v open /dev/sdaX plain1|&lt;br /&gt;
Enter passphrase: &lt;br /&gt;
Command successful.&lt;br /&gt;
}}&lt;br /&gt;
次に、さまざまな暗号化パラメーターを使用し、(オプションの) オフセットを使用して、その中に2番目のブロックデバイスを追加し、ファイルシステムを作成してマウントします。&lt;br /&gt;
{{hc|1=# cryptsetup --type plain --cipher=serpent-xts-plain64 --hash=sha256 --key-size=256 --offset=10  open /dev/mapper/plain1 plain2|2=&lt;br /&gt;
Enter passphrase: &lt;br /&gt;
}}&lt;br /&gt;
{{hc|# lsblk -p|&lt;br /&gt;
 NAME                                                     &lt;br /&gt;
 /dev/sda                                     &lt;br /&gt;
 ├─/dev/sdaX          &lt;br /&gt;
 │ └─/dev/mapper/plain1     &lt;br /&gt;
 │   └─/dev/mapper/plain2              &lt;br /&gt;
 ...&lt;br /&gt;
}}&lt;br /&gt;
 # mkfs -t ext2 /dev/mapper/plain2&lt;br /&gt;
 # mount -t ext2 /dev/mapper/plain2 /mnt&lt;br /&gt;
 # echo &amp;quot;This is stacked. one passphrase per foot to shoot.&amp;quot; &amp;gt; /mnt/stacked.txt&lt;br /&gt;
We close the stack to check access works&lt;br /&gt;
 # cryptsetup close plain2&lt;br /&gt;
 # cryptsetup close plain1&lt;br /&gt;
First, let&#039;s try to open the filesystem directly: &lt;br /&gt;
 # cryptsetup --type plain --cipher=serpent-xts-plain64 --hash=sha256 --key-size=256 --offset=10 open /dev/sdaX plain2&lt;br /&gt;
{{hc|# mount -t ext2 /dev/mapper/plain2 /mnt|&lt;br /&gt;
mount: wrong fs type, bad option, bad superblock on /dev/mapper/plain2,&lt;br /&gt;
      missing codepage or helper program, or other error&lt;br /&gt;
}}&lt;br /&gt;
Why that did not work? Because the &amp;quot;plain2&amp;quot; starting block ({{ic|10}}) is still encrypted with the cipher from &amp;quot;plain1&amp;quot;. It can only be accessed via the stacked mapper. The error is arbitrary though, trying a wrong passphrase or wrong options will yield the same. For &#039;&#039;dm-crypt&#039;&#039; plain mode, the {{ic|open}} action will not error out itself. &lt;br /&gt;
&lt;br /&gt;
Trying again in correct order: &lt;br /&gt;
 # cryptsetup close plain2    # dysfunctional mapper from previous try&lt;br /&gt;
{{hc|# cryptsetup --type plain open /dev/sdaX plain1|&lt;br /&gt;
Enter passphrase: &lt;br /&gt;
}}&lt;br /&gt;
{{hc|1=# cryptsetup --type plain --cipher=serpent-xts-plain64 --hash=sha256 --key-size=256 --offset=10 open /dev/mapper/plain1 plain2|2=&lt;br /&gt;
Enter passphrase: &lt;br /&gt;
}}&lt;br /&gt;
{{hc|# mount /dev/mapper/plain2 /mnt &amp;amp;&amp;amp; cat /mnt/stacked.txt|&lt;br /&gt;
This is stacked. one passphrase per foot to shoot.&lt;br /&gt;
}}&lt;br /&gt;
 # exit&lt;br /&gt;
&#039;&#039;dm-crypt&#039;&#039; will handle stacked encryption with some mixed modes too. For example LUKS mode could be stacked on the &amp;quot;plain1&amp;quot; mapper. Its header would then be encrypted inside &amp;quot;plain1&amp;quot; when that is closed.&lt;br /&gt;
&lt;br /&gt;
Available for plain mode only is the option {{ic|--shared}}. With it a single device can be segmented into different non-overlapping mappers. We do that in the next example, using a &#039;&#039;loopaes&#039;&#039; compatible cipher mode for &amp;quot;plain2&amp;quot; this time: &lt;br /&gt;
{{hc|# cryptsetup --type plain --offset 0 --size 1000 open /dev/sdaX plain1|2=&lt;br /&gt;
Enter passphrase: &lt;br /&gt;
}}&lt;br /&gt;
{{hc|1=# cryptsetup --type plain --offset 1000 --size 1000 --shared --cipher=aes-cbc-lmk --hash=sha256 open /dev/sdaX plain2|2=&lt;br /&gt;
Enter passphrase: &lt;br /&gt;
}}&lt;br /&gt;
{{hc|# lsblk -p|&lt;br /&gt;
NAME                    &lt;br /&gt;
dev/sdaX                    &lt;br /&gt;
├─/dev/sdaX               &lt;br /&gt;
│ ├─/dev/mapper/plain1     &lt;br /&gt;
│ └─/dev/mapper/plain2     &lt;br /&gt;
...&lt;br /&gt;
}}&lt;br /&gt;
As the devicetree shows both reside on the same level, i.e. are not stacked and &amp;quot;plain2&amp;quot; can be opened individually.&lt;br /&gt;
&lt;br /&gt;
== LUKS 固有の Cryptsetup のアクション ==&lt;br /&gt;
=== キーの管理 ===&lt;br /&gt;
LUKS パーティションには異なる8つまでのキーを定義することができます。バックアップストレージのためのアクセスキーを作成することが可能です。いわゆるキーエスクローで、あるキーは日常的に使用するために使い、他のキーはエスクローに保存して日常用のパスフレーズを忘れたときやキーファイルを喪失・破損したときにパーティションにアクセスするために使います。また、別のキースロットを使って一時的なキーを発行してユーザーにパーティションへのアクセスを許可し、後でキーを無効化したりできます。&lt;br /&gt;
&lt;br /&gt;
暗号化パーティションを作成すると、初期キースロット 0 が作成されます (手動で別の数字を指定しなかった場合)。追加キースロットは 1 から 7 までの数字が割り振られます。どのキースロットを使用しているかは以下のコマンドで確認できます (&amp;lt;device&amp;gt; は LUKS ヘッダーが存在するボリュームに置き換えてください):&lt;br /&gt;
{{hc|# cryptsetup luksDump /dev/&amp;lt;device&amp;gt; {{!}} grep BLED|&lt;br /&gt;
Key Slot 0: ENABLED&lt;br /&gt;
Key Slot 1: ENABLED&lt;br /&gt;
Key Slot 2: ENABLED&lt;br /&gt;
Key Slot 3: DISABLED&lt;br /&gt;
Key Slot 4: DISABLED&lt;br /&gt;
Key Slot 5: DISABLED&lt;br /&gt;
Key Slot 6: DISABLED&lt;br /&gt;
Key Slot 7: DISABLED}}&lt;br /&gt;
&lt;br /&gt;
上記のコマンドや下で説明しているコマンドはヘッダーのバックアップファイルでも使えます。&lt;br /&gt;
&lt;br /&gt;
==== LUKS キーの追加 ====&lt;br /&gt;
&lt;br /&gt;
新しいキースロットの追加は cryptsetup の {{ic|luksAddKey}} アクションを使うことでできます。デバイスが解錠されていても安全のために、新しいキーを入力する前に、必ず既存のキーの入力が求められます (&amp;quot;any passphrase&amp;quot;):&lt;br /&gt;
&lt;br /&gt;
{{hc|# cryptsetup luksAddKey /dev/&amp;lt;device&amp;gt; (/path/to/&amp;lt;additionalkeyfile&amp;gt;)|&lt;br /&gt;
Enter any passphrase:&lt;br /&gt;
Enter new passphrase for key slot:&lt;br /&gt;
Verify passphrase: &lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
{{ic|/path/to/&amp;lt;additionalkeyfile&amp;gt;}} を指定した場合、cryptsetup は &amp;lt;additionalkeyfile&amp;gt; の新しいキースロットを追加します。指定しなかった場合は新しいパスフレーズの入力が2回要求されます。既存の&#039;&#039;キーファイル&#039;&#039;を使って認証するには、{{ic|--key-file}} または {{ic|-d}} オプションの後に&amp;quot;既存&amp;quot;の &amp;lt;keyfile&amp;gt; を指定することで全てのキーファイルのキースロットが解除されます:&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup luksAddKey /dev/&amp;lt;device&amp;gt; (/path/to/&amp;lt;additionalkeyfile&amp;gt;) -d /path/to/&amp;lt;keyfile&amp;gt;&lt;br /&gt;
&lt;br /&gt;
複数のキーを使っていてキーを変更・無効化する場合、{{ic|--key-slot}} または {{ic|-S}} オプションでスロットを指定できます:&lt;br /&gt;
{{hc|# cryptsetup luksAddKey /dev/&amp;lt;device&amp;gt; -S 6|&lt;br /&gt;
Enter any passphrase: &lt;br /&gt;
Enter new passphrase for key slot: &lt;br /&gt;
Verify passphrase:&lt;br /&gt;
}}&lt;br /&gt;
{{hc|# cryptsetup luksDump /dev/sda8 {{!}} grep &#039;Slot 6&#039;|&lt;br /&gt;
Key Slot 6: ENABLED&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
例えばキーを変更したい場合:&lt;br /&gt;
{{hc|# cryptsetup luksChangeKey /dev/&amp;lt;device&amp;gt; -S 6|&lt;br /&gt;
Enter LUKS passphrase to be changed: &lt;br /&gt;
Enter new LUKS passphrase: &lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==== LUKS キーの削除 ====&lt;br /&gt;
&lt;br /&gt;
ヘッダーからキーを削除するアクションは3つ存在します:&lt;br /&gt;
*{{ic|luksRemoveKey}} はパスフレーズやキーファイルを指定してキーを削除します。&lt;br /&gt;
*{{ic|luksKillSlot}} は (他のキーを使って) 指定したキースロットからキーを削除します。パスフレーズを忘れたり、キーファイルを無くしたり、誰もアクセスできないようにしたい場合に有用です。&lt;br /&gt;
*{{ic|luksErase}} は全ての有効なキーを即座に削除します。&lt;br /&gt;
{{warning|&lt;br /&gt;
*All above actions can be used to irrevocably delete the last active key for an encrypted device! &lt;br /&gt;
*The {{ic|luksErase}} command was added in version 1.6.4 to quickly nuke access to the device. This action &#039;&#039;&#039;will not&#039;&#039;&#039; prompt for a valid passphrase! It will not [[Dm-crypt/ドライブの準備#LUKS ヘッダーを消去|wipe the LUKS header]], but all keyslots at once and you will, therefore, not be able to regain access unless you have a valid backup of the LUKS header.}} &lt;br /&gt;
&lt;br /&gt;
For above warning it is good to know the key we want to &#039;&#039;&#039;keep&#039;&#039;&#039; is valid. An easy check is to unlock the device with the {{ic|-v}} option, which will specify which slot it occupies:  &lt;br /&gt;
{{hc|# cryptsetup -v open /dev/&amp;lt;device&amp;gt; testcrypt|&lt;br /&gt;
Enter passphrase for /dev/&amp;lt;device&amp;gt;: &lt;br /&gt;
Key slot 1 unlocked.&lt;br /&gt;
Command successful.}}&lt;br /&gt;
&lt;br /&gt;
Now we can remove the key added in the previous subsection using its passphrase: &lt;br /&gt;
{{hc|# cryptsetup luksRemoveKey /dev/&amp;lt;device&amp;gt;|&lt;br /&gt;
Enter LUKS passphrase to be deleted: &lt;br /&gt;
}}&lt;br /&gt;
If we had used the same passphrase for two keyslots, the first slot would be wiped now. Only executing it again would remove the second one. &lt;br /&gt;
&lt;br /&gt;
Alternatively, we can specify the key slot: &lt;br /&gt;
{{hc|# cryptsetup luksKillSlot /dev/&amp;lt;device&amp;gt; 6|&lt;br /&gt;
Enter any remaining LUKS passphrase:&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
Note that in both cases, no confirmation was required.&lt;br /&gt;
{{hc|# cryptsetup luksDump /dev/sda8 {{!}} grep &#039;Slot 6&#039;|&lt;br /&gt;
Key Slot 6: DISABLED&lt;br /&gt;
}}&lt;br /&gt;
To re-iterate the warning above: If the same passphrase had been used for key slots 1 and 6, both would be gone now.&lt;br /&gt;
&lt;br /&gt;
=== バックアップとリストア ===&lt;br /&gt;
If the header of a LUKS encrypted partition gets destroyed, you will not be able to decrypt your data. It is just as much of a dilemma as forgetting the passphrase or damaging a key-file used to unlock the partition. Damage may occur by your own fault while re-partitioning the disk later or by third-party programs misinterpreting the partition table. Therefore, having a backup of the header and storing it on another disk might be a good idea.&lt;br /&gt;
&lt;br /&gt;
{{Note|If the LUKS-encrypted  partitions&#039; master passphrase becomes compromised, you must revoke it on &#039;&#039;every&#039;&#039; copy of the cryptheader, even those you have backed up. Otherwise, a copy of the backed-up cryptheader that uses the compromised passphrase can be used to decrypt the associated partition. See [https://gitlab.com/cryptsetup/cryptsetup/wikis/FrequentlyAskedQuestions#6-backup-and-data-recovery LUKS FAQ] for further details.}}&lt;br /&gt;
&lt;br /&gt;
==== cryptsetup を使ってバックアップ ====&lt;br /&gt;
Cryptsetup&#039;s {{ic|luksHeaderBackup}} action stores a binary backup of the LUKS header and keyslot area:&lt;br /&gt;
 # cryptsetup luksHeaderBackup /dev/&amp;lt;device&amp;gt; --header-backup-file /mnt/&amp;lt;backup&amp;gt;/&amp;lt;file&amp;gt;.img&lt;br /&gt;
where &amp;lt;device&amp;gt; is the partition containing the LUKS volume.&lt;br /&gt;
&lt;br /&gt;
{{Tip|You can also back up the plaintext header into ramfs and encrypt it in example with gpg before writing to persistent backup storage by executing the following commands.}}&lt;br /&gt;
 # mkdir /root/&amp;lt;tmp&amp;gt;/&lt;br /&gt;
 # mount ramfs /root/&amp;lt;tmp&amp;gt;/ -t ramfs&lt;br /&gt;
 # cryptsetup luksHeaderBackup /dev/&amp;lt;device&amp;gt; --header-backup-file /root/&amp;lt;tmp&amp;gt;/&amp;lt;file&amp;gt;.img&lt;br /&gt;
 # gpg2 --recipient &amp;lt;User ID&amp;gt; --encrypt /root/&amp;lt;tmp&amp;gt;/&amp;lt;file&amp;gt;.img &lt;br /&gt;
 # cp /root/&amp;lt;tmp&amp;gt;/&amp;lt;file&amp;gt;.img.gpg /mnt/&amp;lt;backup&amp;gt;/&lt;br /&gt;
 # umount /root/&amp;lt;tmp&amp;gt;&lt;br /&gt;
{{Warning|Tmpfs can swap to harddisk if low on memory so it is not recommended here.}}&lt;br /&gt;
&lt;br /&gt;
==== cryptsetup を使ってリストア ====&lt;br /&gt;
&lt;br /&gt;
{{Warning|Restoring the wrong header or restoring to an unencrypted partition will cause data loss! The action can not perform a check whether the header is actually the &#039;&#039;correct&#039;&#039; one for that particular device.}} &lt;br /&gt;
&lt;br /&gt;
In order to evade restoring a wrong header, you can ensure it does work by using it as a remote {{ic|--header}} first: &lt;br /&gt;
&lt;br /&gt;
{{hc|# cryptsetup -v --header /mnt/&amp;lt;backup&amp;gt;/&amp;lt;file&amp;gt;.img open /dev/&amp;lt;device&amp;gt; test|&lt;br /&gt;
Key slot 0 unlocked.&lt;br /&gt;
Command successful.&lt;br /&gt;
}}&lt;br /&gt;
 # mount /dev/mapper/test /mnt/test &amp;amp;&amp;amp; ls /mnt/test &lt;br /&gt;
 # umount /mnt/test &lt;br /&gt;
 # cryptsetup close test &lt;br /&gt;
&lt;br /&gt;
Now that the check succeeded, the restore may be performed: &lt;br /&gt;
 # cryptsetup luksHeaderRestore /dev/&amp;lt;device&amp;gt; --header-backup-file ./mnt/&amp;lt;backup&amp;gt;/&amp;lt;file&amp;gt;.img&lt;br /&gt;
&lt;br /&gt;
Now that all the keyslot areas are overwritten; only active keyslots from the backup file are available after issuing the command.&lt;br /&gt;
&lt;br /&gt;
==== 手動バックアップとリストア ====&lt;br /&gt;
The header always resides at the beginning of the device and a backup can be performed without access to &#039;&#039;cryptsetup&#039;&#039; as well. First you have to find out the payload offset of the crypted partition:&lt;br /&gt;
{{hc|# cryptsetup luksDump /dev/&amp;lt;device&amp;gt; {{!}} grep &amp;quot;Payload offset&amp;quot;|&lt;br /&gt;
Payload offset:	4040}}&lt;br /&gt;
Second check the sector size of the drive&lt;br /&gt;
{{hc|# fdisk -l /dev/&amp;lt;device&amp;gt; {{!}} grep &amp;quot;Sector size&amp;quot;|Sector size (logical/physical): 512 bytes / 512 bytes}}&lt;br /&gt;
&lt;br /&gt;
Now that you know the values, you can backup the header with a simple dd command:&lt;br /&gt;
 # dd if=/dev/&amp;lt;device&amp;gt; of=/path/to/&amp;lt;file&amp;gt;.img bs=512 count=4040&lt;br /&gt;
and store it safely.&lt;br /&gt;
&lt;br /&gt;
A restore can then be performed using the same values as when backing up:&lt;br /&gt;
 # dd if=./&amp;lt;file&amp;gt;.img of=/dev/&amp;lt;device&amp;gt; bs=512 count=4040&lt;br /&gt;
&lt;br /&gt;
=== デバイスの再暗号化 ===&lt;br /&gt;
&lt;br /&gt;
The {{Pkg|cryptsetup}} package features the &#039;&#039;cryptsetup-reencrypt&#039;&#039; tool. It can be used to convert an existing unencrypted filesystem to a LUKS encrypted one (option {{ic|--new}}) and permanently remove LUKS encryption ({{ic|--decrypt}}) from a device. As its name suggests it can also be used to re-encrypt an existing LUKS encrypted device, though, re-encryption is not possible for a detached LUKS header or other encryption modes (e.g. plain-mode). For re-encryption it is possible to change the [[#LUKS モードの暗号化オプション]]. &#039;&#039;cryptsetup-reencrypt&#039;&#039; actions can be performed to unmounted devices only.  See {{man|8|cryptsetup-reencrypt}} for more information. &lt;br /&gt;
&lt;br /&gt;
One application of re-encryption may be to secure the data again after a passphrase or [[#キーファイル|keyfile]] has been compromised &#039;&#039;and&#039;&#039; one cannot be certain that no copy of the LUKS header has been obtained. For example, if only a passphrase has been shoulder-surfed but no physical/logical access to the device happened, it would be enough to change the respective passphrase/key only ([[#Key management]]). &lt;br /&gt;
&lt;br /&gt;
{{Warning|Always make sure a &#039;&#039;&#039;reliable backup&#039;&#039;&#039; is available and double-check options you specify before using the tool!}}&lt;br /&gt;
&lt;br /&gt;
The following shows an example to encrypt an unencrypted filesystem partition and a re-encryption of an existing LUKS device. &lt;br /&gt;
 &lt;br /&gt;
==== 暗号化されていないファイルシステムの暗号化 ====&lt;br /&gt;
&lt;br /&gt;
A LUKS encryption header is always stored at the beginning of the device. Since an existing filesystem will usually be allocated all partition sectors, the first step is to shrink it to make space for the LUKS header. &lt;br /&gt;
&lt;br /&gt;
The [[#LUKS モードの暗号化オプション|default]] LUKS header encryption cipher requires {{ic|4096}} 512-byte sectors. We already checked space and keep it simple by shrinking the existing {{ic|ext4}} filesystem on {{ic|/dev/sdaX}} to its current possible minimum: &lt;br /&gt;
&lt;br /&gt;
 # umount /mnt&lt;br /&gt;
{{hc|# e2fsck -f /dev/sdaX|&lt;br /&gt;
e2fsck 1.43-WIP (18-May-2015)&lt;br /&gt;
Pass 1: Checking inodes, blocks, and sizes&lt;br /&gt;
...&lt;br /&gt;
/dev/sda6: 12/166320 files (0.0% non-contiguous), 28783/665062 blocks&lt;br /&gt;
}}&lt;br /&gt;
{{hc|# resize2fs -M /dev/sdaX|&lt;br /&gt;
resize2fs 1.43-WIP (18-May-2015)&lt;br /&gt;
Resizing the filesystem on /dev/sdaX to 26347 (4k) blocks.&lt;br /&gt;
The filesystem on /dev/sdaX is now 26347 (4k) blocks long.}}&lt;br /&gt;
&lt;br /&gt;
Now we encrypt it, using the default cipher we do not have to specify it explicitly. Note there is no option (yet) to double-check the passphrase before encryption starts, be careful not to mistype: &lt;br /&gt;
&lt;br /&gt;
{{hc|# cryptsetup-reencrypt /dev/sdaX --new  --reduce-device-size 4096S|&lt;br /&gt;
WARNING: this is experimental code, it can completely break your data.&lt;br /&gt;
Enter new passphrase: &lt;br /&gt;
Progress: 100,0%, ETA 00:00, 2596 MiB written, speed  37,6 MiB/s}}&lt;br /&gt;
&lt;br /&gt;
After it finished, the encryption was performed to the full partition, i.e. not only the space the filesystem was shrunk to ({{ic|sdaX}} has {{ic|2.6GiB}} and the CPU used in the example has no hardware AES instructions). As a final step we extend the filesystem of the now encrypted device again to occupy available space: &lt;br /&gt;
&lt;br /&gt;
{{hc|# cryptsetup open /dev/sdaX recrypt|&lt;br /&gt;
Enter passphrase for /dev/sdaX: &lt;br /&gt;
...&lt;br /&gt;
}}&lt;br /&gt;
{{hc|# resize2fs /dev/mapper/recrypt|&lt;br /&gt;
resize2fs 1.43-WIP (18-May-2015)&lt;br /&gt;
Resizing the filesystem on /dev/mapper/recrypt to 664807 (4k) blocks.&lt;br /&gt;
The filesystem on /dev/mapper/recrypt is now 664807 (4k) blocks long.}}&lt;br /&gt;
 # mount /dev/mapper/recrypt /mnt&lt;br /&gt;
&lt;br /&gt;
and are done.&lt;br /&gt;
&lt;br /&gt;
==== 既存の LUKS パーティションの再暗号化 ====&lt;br /&gt;
&lt;br /&gt;
以下の例では既存の LUKS デバイスを最暗号化しています。&lt;br /&gt;
&lt;br /&gt;
{{Warning|Double-check you specify encryption options for &#039;&#039;cryptsetup-reencrypt&#039;&#039; correctly and &#039;&#039;never&#039;&#039; re-encrypt without a &#039;&#039;&#039;reliable backup&#039;&#039;&#039;! As of September 2015 the tool &#039;&#039;&#039;does&#039;&#039;&#039; accept invalid options and damage the LUKS header, if not used correctly!}}&lt;br /&gt;
&lt;br /&gt;
In order to re-encrypt a device with its existing encryption options, they do not need to be specified. A simple: &lt;br /&gt;
&lt;br /&gt;
{{hc|# cryptsetup-reencrypt /dev/sdaX| &lt;br /&gt;
WARNING: this is experimental code, it can completely break your data.&lt;br /&gt;
Enter passphrase for key slot 0: &lt;br /&gt;
Progress: 100,0%, ETA 00:00, 2596 MiB written, speed  36,5 MiB/s}}&lt;br /&gt;
&lt;br /&gt;
performs it. &lt;br /&gt;
&lt;br /&gt;
A possible usecase is to re-encrypt LUKS devices which have non-current encryption options. Apart from above warning on specifying options correctly, the ability to change the LUKS header may also be limited by its size. For example, if the device was initially encrypted using a CBC mode cipher and 128 bit key-size, the LUKS header will be half the size of above mentioned {{ic|4096}} sectors: &lt;br /&gt;
{{hc|# cryptsetup luksDump /dev/sdaX &amp;lt;nowiki&amp;gt;|&amp;lt;/nowiki&amp;gt;grep -e &amp;quot;mode&amp;quot; -e &amp;quot;Payload&amp;quot; -e &amp;quot;MK bits&amp;quot;|&lt;br /&gt;
Cipher mode:   	cbc-essiv:sha256&lt;br /&gt;
Payload offset:	&#039;&#039;&#039;2048&#039;&#039;&#039;&lt;br /&gt;
MK bits:       	128}}&lt;br /&gt;
While it is possible to upgrade the encryption of such a device, it is currently only feasible in two steps. First, re-encrypting with the same encryption options, but using the {{ic|--reduce-device-size}} option to make further space for the larger LUKS header. Second, re-encypt the whole device again with the desired cipher. For this reason and the fact that a backup should be created in any case, creating a new, fresh encrypted device to restore into is always the faster option.&lt;br /&gt;
&lt;br /&gt;
== 暗号化デバイスのリサイズ ==&lt;br /&gt;
&lt;br /&gt;
If a storage device encrypted with dm-crypt is being cloned (with a tool like dd) to another larger device, the underlying dm-crypt device must be resized to use the whole space. &lt;br /&gt;
&lt;br /&gt;
The destination device is /dev/sdX2 in this example, the whole available space adjacent to the partition will be used:&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup luksOpen /dev/sdX2 sdX2&lt;br /&gt;
 # cryptsetup resize sdX2&lt;br /&gt;
&lt;br /&gt;
Then the underlying filesystem must be resized.&lt;br /&gt;
&lt;br /&gt;
=== ループバックファイルシステム ===&lt;br /&gt;
&lt;br /&gt;
暗号化したループバックデバイスを {{ic|/mnt/secret}} にマウントしている場合、例えば [[Dm-crypt/root 以外のファイルシステムの暗号化#Loop デバイス]] に従っている場合、最初に暗号化コンテナをアンマウント:&lt;br /&gt;
 # umount /mnt/secret&lt;br /&gt;
 # cryptsetup close secret&lt;br /&gt;
 # losetup -d /dev/loop0&lt;br /&gt;
&lt;br /&gt;
次に、追加したいデータ容量を決めてコンテナファイルを拡張します:&lt;br /&gt;
&lt;br /&gt;
{{Warning|{{ic|&amp;gt;}} を2つ使っていることに注意してください。1つだけだと現在のコンテナを上書きしてしまいます。}}&lt;br /&gt;
 # dd if=/dev/urandom bs=1M count=1024 | cat - &amp;gt;&amp;gt; /bigsecret&lt;br /&gt;
&lt;br /&gt;
ループデバイスにコンテナをマップ:&lt;br /&gt;
 # losetup /dev/loop0 /bigsecret&lt;br /&gt;
 # cryptsetup --type luks open /dev/loop0 secret&lt;br /&gt;
&lt;br /&gt;
その後、コンテナの暗号化された部分をコンテナファイルの最大サイズまでリサイズ:&lt;br /&gt;
 # cryptsetup resize secret&lt;br /&gt;
&lt;br /&gt;
最後に、ファイルシステムのチェックを実行して、問題なければ、リサイズを行います (例: ext2/3/4):&lt;br /&gt;
 # e2fsck -f /dev/mapper/secret&lt;br /&gt;
 # resize2fs /dev/mapper/secret&lt;br /&gt;
&lt;br /&gt;
またコンテナをマウントすることができます:&lt;br /&gt;
 # mount /dev/mapper/secret /mnt/secret&lt;br /&gt;
&lt;br /&gt;
== キーファイル ==&lt;br /&gt;
&lt;br /&gt;
{{Note|このセクションでは平文のキーファイルを使う方法を説明しています。キーファイルを暗号化して二段階認証したい場合は[[Dm-crypt/特記事項#GPG や OpenSSL で暗号化されたキーファイルを使う|GPG や OpenSSL で暗号化されたキーファイルを使う]]を見て下さい。ただし、このセクションもあらかじめ読むようにしてください。}}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;キーファイルとは？&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
キーファイルは暗号化されたボリュームを解錠するパスフレーズとして使用するデータを含んだファイルです。ファイルを喪失した場合、ボリュームの復号化はできなくなります。&lt;br /&gt;
&lt;br /&gt;
{{Tip|Define a passphrase in addition to the keyfile for backup access to encrypted volumes in the event the defined keyfile is lost or changed.}}&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;なぜキーファイルを使うのか？&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
キーファイルには様々なタイプがあります。以下で説明しているようにそれぞれメリットとデメリットが存在します:&lt;br /&gt;
&lt;br /&gt;
=== キーファイルのタイプ ===&lt;br /&gt;
==== passphrase ====&lt;br /&gt;
&lt;br /&gt;
単にパスフレーズが記述されたキーファイルです。メリットはファイルを無くしても暗号化ボリュームの所有者が簡単に中身を思い出せることです。デメリットはパスフレーズの入力と比べてセキュリティ上の優位点がないことです。&lt;br /&gt;
&lt;br /&gt;
例: {{ic|1234}}。&lt;br /&gt;
&lt;br /&gt;
{{Note|パスフレーズが記述されたキーファイルに改行を含めてはいけません。以下のようなコマンドでファイルを作成できます:&lt;br /&gt;
 # echo -n &#039;your_passphrase&#039; &amp;gt; /path/to/&amp;lt;keyfile&amp;gt;&lt;br /&gt;
 # chown root:root /path/to/&amp;lt;keyfile&amp;gt;; chmod 400 /path/to/&amp;lt;keyfile&amp;gt;&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==== randomtext ====&lt;br /&gt;
&lt;br /&gt;
This is a keyfile containing a block of random characters. The benefit of this type of keyfile is that it is much more resistant to dictionary attacks than a simple passphrase. An additional strength of keyfiles can be utilized in this situation which is the length of data used. Since this is not a string meant to be memorized by a person for entry, it is trivial to create files containing thousands of random characters as the key. The disadvantage is that if this file is lost or changed, it will most likely not be possible to access the encrypted volume without a backup passphrase.&lt;br /&gt;
&lt;br /&gt;
例: {{ic|fjqweifj830149-57 819y4my1-38t1934yt8-91m 34co3;t8y;9p3y-}}。&lt;br /&gt;
&lt;br /&gt;
==== binary ====&lt;br /&gt;
&lt;br /&gt;
This is a binary file that has been defined as a keyfile. When identifying files as candidates for a keyfile, it is recommended to choose files that are relatively static such as photos, music, video clips. The benefit of these files is that they serve a dual function which can make them harder to identify as keyfiles. Instead of having a text file with a large amount of random text, the keyfile would look like a regular image file or music clip to the casual observer. The disadvantage is that if this file is lost or changed, it will most likely not be possible to access the encrypted volume without a backup passphrase. Additionally, there is a theoretical loss of randomness when compared to a randomly generated text file. This is due to the fact that images, videos and music have some intrinsic relationship between neighboring bits of data that does not exist for a text file. However this is controversial and has never been exploited publicly.&lt;br /&gt;
&lt;br /&gt;
例: 画像, テキスト, 動画。&lt;br /&gt;
&lt;br /&gt;
=== ランダムな文字列でキーファイルを作成 ===&lt;br /&gt;
&lt;br /&gt;
==== ファイルシステムにキーファイルを保存 ====&lt;br /&gt;
&lt;br /&gt;
キーファイルの中身とサイズは任意に決められます。&lt;br /&gt;
&lt;br /&gt;
以下では {{ic|dd}} を使って2048バイトのランダムなキーファイルを生成して、{{ic|/etc/mykeyfile}} ファイルに保存します:&lt;br /&gt;
&lt;br /&gt;
 # dd bs=512 count=4 if=/dev/urandom of=/etc/mykeyfile&lt;br /&gt;
&lt;br /&gt;
キーファイルを外部デバイスに保存する場合、出力先を適切なディレクトリに変更します:&lt;br /&gt;
&lt;br /&gt;
 # dd bs=512 count=4 if=/dev/urandom of=/media/usbstick/mykeyfile&lt;br /&gt;
&lt;br /&gt;
===== 保存されたキーファイルを完全に消去 =====&lt;br /&gt;
&lt;br /&gt;
物理的なストレージデバイスに一時的なキーファイルを保存する場合、削除するときは、以下のように完全に削除してください:&lt;br /&gt;
&lt;br /&gt;
 # shred --remove --zero mykeyfile&lt;br /&gt;
&lt;br /&gt;
FAT や ext2 であれば上記で十分ですが、ジャーナリングファイルシステムやフラッシュメモリハードウェアの場合、[[ディスクの完全消去]]あるいはキーファイルのあるパーティションを削除することを強く推奨します。&lt;br /&gt;
&lt;br /&gt;
==== ramfs にキーファイルを保存 ====&lt;br /&gt;
&lt;br /&gt;
もしくは、ramfs をマウントしてキーファイルを一時的に保存:&lt;br /&gt;
&lt;br /&gt;
 # mkdir /root/myramfs&lt;br /&gt;
 # mount ramfs /root/myramfs/ -t ramfs&lt;br /&gt;
 # cd /root/myramfs&lt;br /&gt;
&lt;br /&gt;
物理ディスクではなくメモリ上に配置することで、ramfs をアンマウントしたら復元できないという利点があります。キーファイルを他の安全なファイルシステムにコピーしたら、以下のコマンドで ramfs をアンマウントしてください:&lt;br /&gt;
 # umount /root/myramfs&lt;br /&gt;
&lt;br /&gt;
=== キーファイルを使用するように LUKS を設定 ===&lt;br /&gt;
&lt;br /&gt;
キーファイルのキースロットを LUKS ヘッダに追加:&lt;br /&gt;
&lt;br /&gt;
{{hc|# cryptsetup luksAddKey /dev/sda2 /etc/mykeyfile|&lt;br /&gt;
Enter any LUKS passphrase:&lt;br /&gt;
key slot 0 unlocked.&lt;br /&gt;
Command successful.}}&lt;br /&gt;
&lt;br /&gt;
=== キーファイルを使ってパーティションを手動でアンロック ===&lt;br /&gt;
&lt;br /&gt;
LUKS デバイスをオープンするときに {{ic|--key-file}} オプションを使ってください:&lt;br /&gt;
&lt;br /&gt;
 # cryptsetup open /dev/sda2 &#039;&#039;dm_name&#039;&#039; --key-file /etc/mykeyfile&lt;br /&gt;
&lt;br /&gt;
=== 起動時にロックを解除 ===&lt;br /&gt;
&lt;br /&gt;
ファイルシステムのキーファイルを暗号化されたルートディレクトリの中に保存することで、システムの電源が切れているときに安全を確保することができ、[[Dm-crypt/システム設定#crypttab|crypttab]] によって起動時に自動的にマウントを解錠することができます。[[UUID]] を使用する例:&lt;br /&gt;
{{hc|/etc/crypttab|&amp;lt;nowiki&amp;gt;home    UUID=&amp;lt;UUID identifier&amp;gt;    /etc/mykeyfile&amp;lt;/nowiki&amp;gt;}}&lt;br /&gt;
is all needed for unlocking, and &lt;br /&gt;
{{hc|/etc/fstab|/dev/mapper/home        /home   ext4        defaults        0       2}} for mounting the LUKS blockdevice with the generated keyfile.&lt;br /&gt;
{{Tip|If you prefer to use a {{ic|--plain}} mode blockdevice, the encryption options necessary to unlock it are specified in {{ic|/etc/crypttab}}. Take care to apply the systemd workaround mentioned in [[Dm-crypt/システム設定#crypttab|crypttab]] in this case.}}&lt;br /&gt;
&lt;br /&gt;
=== キーファイルを使って起動時に root パーティションのロックを解除 ===&lt;br /&gt;
&lt;br /&gt;
[[mkinitcpio]] を設定して必要なモジュールやファイルを記述して [[Dm-crypt/システム設定#cryptkey|cryptkey]] [[カーネルパラメータ]]を設定してキーファイルの場所を指定します。&lt;br /&gt;
&lt;br /&gt;
2つの方法が存在します:&lt;br /&gt;
&lt;br /&gt;
# 外部メディア (USB スティック) にキーファイルを保存&lt;br /&gt;
# initramfs にキーファイルを埋め込む&lt;br /&gt;
&lt;br /&gt;
==== キーファイルを外部メディアに保存 ====&lt;br /&gt;
&lt;br /&gt;
===== mkinitcpio の設定 =====&lt;br /&gt;
&lt;br /&gt;
ドライブのファイルシステムを使うために {{ic|/etc/mkinitcpio.conf}} にモジュールを追加してください (以下の例では {{ic|vfat}} モジュール):&lt;br /&gt;
 MODULES=(vfat)&lt;br /&gt;
&lt;br /&gt;
In this example it is assumed that you use a FAT formatted USB drive ({{ic|vfat}} module). Replace those module names if you use another file system on your USB stick (e.g. {{ic|ext2}}) or another codepage. If it complains of bad superblock and bad codepage at boot, then you need an extra codepage module to be loaded. For instance, you may need {{ic|nls_iso8859-1}} module for {{ic|iso8859-1}} codepage.&lt;br /&gt;
&lt;br /&gt;
If you have a non-US keyboard, it might prove useful to load your keyboard layout before you are prompted to enter the password to unlock the root partition at boot. For this, you will need the {{ic|keymap}} hook before {{ic|encrypt}}.&lt;br /&gt;
&lt;br /&gt;
Generate a new initramfs image:&lt;br /&gt;
&lt;br /&gt;
 # mkinitcpio -p linux&lt;br /&gt;
&lt;br /&gt;
===== カーネルパラメータの設定 =====&lt;br /&gt;
&lt;br /&gt;
以下のオプションを[[カーネルパラメータ]]に追加してください ({{ic|encrypt}} フックではなく {{ic|sd-encrypt}} フックを使用する場合は [[Dm-crypt/システム設定#sd-encrypt フックを使う]]を参照):&lt;br /&gt;
&lt;br /&gt;
 cryptdevice=/dev/&#039;&#039;&amp;lt;partition1&amp;gt;&#039;&#039;:root cryptkey=/dev/&#039;&#039;&amp;lt;partition2&amp;gt;&#039;&#039;:&amp;lt;fstype&amp;gt;:&amp;lt;path&amp;gt;&lt;br /&gt;
&lt;br /&gt;
例:&lt;br /&gt;
&lt;br /&gt;
 cryptdevice=/dev/sda3:root cryptkey=/dev/sdb1:vfat:/keys/secretkey&lt;br /&gt;
&lt;br /&gt;
Choosing a plain filename for your key provides a bit of &#039;security through obscurity&#039;. The keyfile can not be a hidden file, that means the filename must not start with a dot, or the {{ic|encrypt}} hook will fail to find the keyfile during the boot process.&lt;br /&gt;
&lt;br /&gt;
{{ic|/dev/sdb1}} のようなデバイスノードの名前は再起動しても同じであるとは保証されていません。udev による[[永続的なブロックデバイスの命名]]を使ったほうが確実にデバイスにアクセスできます。外部ストレージデバイスからキーファイルを読み取るときに {{ic|encrypt}} フックが確実にキーファイルを見つけられるように、永続的なブロックデバイスの名前を絶対に使うべきです。[[永続的なブロックデバイスの命名]]を見て下さい。&lt;br /&gt;
&lt;br /&gt;
==== キーファイルを initramfs に埋め込む ====&lt;br /&gt;
&lt;br /&gt;
{{Warning|Use an embedded keyfile &#039;&#039;&#039;only&#039;&#039;&#039; if you have some form of authentication mechanism beforehand that protects the keyfile sufficiently. Otherwise auto-decryption will occur, defeating completely the purpose of block device encryption.}}&lt;br /&gt;
&lt;br /&gt;
This method allows to use a specially named keyfile that will be embedded in the [[initramfs]] and picked up by the {{ic|encrypt}} [[Mkinitcpio#HOOKS|hook]] to unlock the root filesystem ({{ic|cryptdevice}}) automatically. It may be useful to apply when using the [[GRUB#暗号化された /boot|GRUB early cryptodisk]] feature, in order to avoid entering two passphrases during boot.&lt;br /&gt;
&lt;br /&gt;
The {{ic|encrypt}} hook lets the user specify a keyfile with the {{ic|cryptkey}} kernel parameter: in the case of initramfs, the syntax is {{ic|rootfs:&#039;&#039;path&#039;&#039;}}, see [[Dm-crypt/システム設定#cryptkey]]. Besides, this kernel parameter defaults to use {{ic|/crypto_keyfile.bin}}, and if the initramfs contains a valid key with this name, decryption will occur automatically without the need to configure the {{ic|cryptkey}} parameter.&lt;br /&gt;
&lt;br /&gt;
{{ic|encrypt}} の代わりに {{ic|sd-encrypt}} を使用する場合、{{ic|luks.key}} カーネルパラメータでキーファイルの場所を指定してください。[[Dm-crypt/システム設定#luks.key]] を参照。&lt;br /&gt;
&lt;br /&gt;
[[#ランダムな文字列でキーファイルを作成|キーファイルを生成]]して適切な権限を与えて [[#LUKS キーの追加|LUKS キーとして追加]]:&lt;br /&gt;
&lt;br /&gt;
 # dd bs=512 count=4 if=/dev/urandom of=/crypto_keyfile.bin&lt;br /&gt;
 # chmod 000 /crypto_keyfile.bin&lt;br /&gt;
 # chmod 600 /boot/initramfs-linux*&lt;br /&gt;
 # cryptsetup luksAddKey /dev/sdX# /crypto_keyfile.bin&lt;br /&gt;
&lt;br /&gt;
{{Warning|When initramfs&#039; permissions are set to 644 (by default), then all users will be able to dump the keyfile. Make sure the permissions are still 600 if you install a new kernel.}}&lt;br /&gt;
&lt;br /&gt;
[[Mkinitcpio#BINARIES_と_FILES|mkinitcpio の FILES]] にキーを記述:&lt;br /&gt;
&lt;br /&gt;
{{hc|/etc/mkinitcpio.conf|2=FILES=(/crypto_keyfile.bin)}}&lt;br /&gt;
&lt;br /&gt;
最後に [[Mkinitcpio#イメージ作成とアクティベーション|initramfs を再生成]]してください。&lt;br /&gt;
&lt;br /&gt;
次の起動時からコンテナを復号化するパスフレーズを入力するのは一度だけですむようになります。&lt;br /&gt;
&lt;br /&gt;
詳しくは [http://www.pavelkogan.com/2014/05/23/luks-full-disk-encryption/#bonus-login-once こちら] を参照。&lt;/div&gt;</summary>
		<author><name>Takemaro</name></author>
	</entry>
</feed>