netctl

提供: ArchWiki
2015年12月18日 (金) 20:28時点におけるKusakata (トーク | 投稿記録)による版
ナビゲーションに移動 検索に移動

関連記事

netctl はプロファイルを使ってネットワーク接続を管理・設定する CLI ベースのツールです。netctl は古い netcfg ユーティリティを置き換える新しい Arch Linux の独自プロジェクトです。

インストール

netctl パッケージが公式リポジトリから利用できます。netctl をインストールすると netcfgAUR が置き換えられます。

netctlnetcfgAUR は衝突するパッケージです。プロファイルが正しく設定されていないと、netctl をインストールした後に接続を失う可能性があります。

ノート: systemctl --type=service を使ってネットワークを設定するサービスが動作していないことを確認するとよいでしょう。ネットワークサービスが複数あると衝突します。

必読

netctl を使う前に以下の man ページを読むのを推奨します:

設定

netctl はプロファイルを使ってネットワーク接続を管理します。様々な動作方法があり、プロファイルを自動的に起動したり、必要に応じて手動で起動することができます。

プロファイルの設定

netctl のプロファイルは /etc/netctl/ に保存されています。ユーザーがネットワーク接続を設定するのを助けるために、設定のサンプルファイルが提供されています。これらのサンプルプロファイルが置かれている場所は /etc/netctl/examples/ です。一般的な設定として以下が用意されています:

  • ethernet-dhcp
  • ethernet-static
  • wireless-wpa
  • wireless-wpa-static

サンプルプロファイルを使うには、プロファイルのどれかを /etc/netctl/examples/ から /etc/netctl/ にコピーしてあなたの設定にあわせてプロファイルを修正して下さい:

# cp /etc/netctl/examples/wireless-wpa /etc/netctl/profile
ノート: ほとんどの場合プロファイルの中のインターフェイス名を編集する必要があるはずです。v197 から、udev は wlanX や ethX という命名形式を使ってネットワークのインターフェイス名を割り当てなくなりました。無線インターフェイスが wlan0 で、有線インターフェイスが eth0 ということはなくなっています。インターフェイスの名前を調べるには ip link コマンドを使って下さい。
ヒント:
  • ワイヤレス設定のために、wifi-menu -o を使ってプロファイルを /etc/netctl/ に生成することができます。
  • ケーブルが接続されているかどうかにかかわらず有線インターフェイスで固定 IP プロファイルを有効にしたい場合は、プロファイルに SkipNoCarrier=yes を使ってください。

プロファイルを作成したら、新しく作ったプロファイルを使って接続を確立するために次を実行して下さい:

# netctl start profile
ノート: profile はファイル名です、フルパスを含みません。フルパスを指定すると netctl はエラーコードを返します。

上のコマンドが失敗する場合、journalctl -xnnetctl status profile を使って失敗の詳細情報を取得して下さい。そして設定を修正してから再テストして下さい。

自動操作

プロファイルを(一つのインターフェイスに)一つしか使わなかったり、手動でプロファイルを切り替える場合、基本的な方法を使って下さい。一般的な例として、サーバー・ワークステーション・ルーターなどがあります。

頻繁に複数のプロファイルを切り替える必要がある時は、プロファイルの自動切り替えを使って下さい。一般的な例として、ラップトップがあります。

基本的な方法

この方法では、インターフェイス毎に一つのプロファイルを固定して起動することができます。最初にプロファイルが問題なく起動することを手動で確認します:

# netctl start profile

それから次のコマンドで enabled にしてください:

# netctl enable profile

このコマンドによって systemd サービスが作成・有効にされ、コンピュータが立ち上がる時にサービスが起動するようになります。ただし、netctl はネットワークインターフェイスデバイスが既に起動している場合 (例えば dhcpcd を使っている場合) はプロファイルを実行しません。

プロファイルへの変更はサービスファイルに自動的には適用されません。変更後、プロファイルを再度有効にする必要があります:

# netctl reenable profile

ブート時 (もしくはサービスの起動時) にプロファイルの起動が成功した時に初めて接続は確立されます。具体的に言うと、有線接続の場合はケーブルが挿さっている必要があり、無線接続の場合、ネットワークが圏内である必要があります。

プロファイルの自動切り替え

netctl は自動でプロファイルを切り替えるために2つの特別な systemd サービスを用意しています:

  • 有線インターフェイス: netctl-ifplugd@interface.service。これを使うと、あなたがケーブルを抜き差しするたびに netctl プロファイルが変更されます。
  • 無線インターフェイス: netctl-auto@interface.service。これを使うと、あなたが特定のネットワークの圏内から他のネットワークの圏内に移動したときに netctl のプロファイルが変更されます。

まず必要なパッケージをインストールしてください:

  • netctl-auto@interface.service を使うには wpa_actiond パッケージが必要です。
  • netctl-ifplugd@interface.service を使うには ifplugd パッケージが必要です。

そして netctl-auto@interface.servicenetctl-ifplugd@interface.service が起動できるプロファイルを全て設定してください。

netctl-auto@interface.service によって無線プロファイルが自動的に起動しないようにしたい場合は、そのプロファイルに ExcludeAuto=yes を加える必要があります。また、Priority= を使うことで複数のプロファイルが利用可能な場合に、どのプロファイルを優先的に使うか設定できます。netctl-ifplugd@interface.serviceDHCP を使うプロファイルを優先します。固定 IP のプロファイルを優先させるために、AutoWired=yes を使うことができます。詳しくは netctl.profile(5) を見て下さい。

警告: netctl-auto による WPA のプロファイルの自動選択は Security=wpa-config オプションでは出来ません、代わりに Security=wpa-configsection を使って下さい。

プロファイルを設定して、動作するのを確認したら、systemctl を使ってサービスを有効にしてください:

# systemctl enable netctl-auto@interface.service 
# systemctl enable netctl-ifplugd@interface.service  
警告:
  • Key= の値が空だったり、プロファイルのどれかにエラーがある場合、このユニットは起動時に "Failed to read or parse configuration '/run/network/wpa_supplicant_wlan0.conf' というメッセージを残して (そのプロファイルが使われていなかったとしても) 失敗します。
  • この方法は基本的な方法と衝突します。以前 netctl でプロファイルを有効にしていたのならば、# netctl disable profile を実行してプロファイルを二重に起動しないようにしてください。

netctl 1.3 から、netctl-auto サービスを止めないで netctl-auto によって管理されているインターフェイスを手動で操作できるようになりました。netctl-auto コマンドを使ってこれを行うことが可能です。利用可能な操作のリストを表示するには次を実行してください:

# netctl-auto --help

パスワードの暗号化 (256-bit PSK)

ノート: "暗号化"されていたとしても、プロファイルに記述したキーだけで WPA-PSK ネットワークに接続することは可能です。従って、この手順は人間が読めるパスフレーズを隠すことにしか訳に立ちません。ファイルを読める人間がネットワークに接続するのを止めることはできません。どこでも同じパスフレーズを使うというのはセキュリティ的に問題があります、同じパスワードをどこかで使っていないか自問自答するべきでしょう。

パスワードを平文で保存したくない場合、256-bit Encrypted PSK を生成するという方法があります。これはパスフレーズと SSID から標準アルゴリズムを使って計算します。

  • 方法 1: wifi-menu -o を実行して /etc/netctl に設定ファイルを生成する
  • 方法 2: 手順に従って手動設定する

どちらの方法でも、chmod 600 /etc/netctl/<config_file> をしてパスワードにユーザーがアクセスできないようにするのを提案します。

wpa_passphrase を使ってあなたの 256-bit Encrypted PSK を生成してください:

$ wpa_passphrase your_essid passphrase
network={
  ssid="your_essid"
  #psk="passphrase"
  psk=64cf3ced850ecef39197bb7b7b301fc39437a6aa6c6a599d0534b16af578e04a
}
ノート: 表示された情報はプロファイルで使うのでターミナルを閉じないで下さい。

新しいターミナルを開き、サンプルファイル wireless-wpa/etc/netctl/examples から /etc/netctl にコピーしてください。

# cp /etc/netctl/examples/wireless-wpa /etc/netctl/wireless-wpa

それからお好きなテキストエディタを使って /etc/netctl/wireless-wpa を編集する必要があります。先に wpa_passphrase を使って生成された Encrypted Pre-shared Key (psk) をプロファイルの Key 変数に追加してください。

256-bit Encrypted PSK を含むネットワークプロファイル wireless-wpa は以下のようになるはずです:

/etc/netctl/wireless-wpa
Description='A simple WPA encrypted wireless connection using 256-bit Encrypted PSK'
Interface=wlp2s2
Connection=wireless
Security=wpa
IP=dhcp
ESSID=your_essid
Key=\"64cf3ced850ecef39197bb7b7b301fc39437a6aa6c6a599d0534b16af578e04a
ノート:
  • Key 変数には netctl.profile(5) の最後で説明されている special non-quoting rules を使うようにして下さい。
  • パスフレーズが失敗する場合、Key の中の \" を削除してみてください。

Tips and Tricks

GUI を使う

netctl や接続を管理するのにグラフィカルユーザーインターフェースが欲しい場合、netguiAUR をインストールすることができます。ただし、netgui はまだベータなので問題が起こった時のために基本的な netctl の構文には慣れておいて下さい。

'netcfg current' の代用

以前 netcfg current を使っていた場合は、netctl-auto で起動した接続の代わりとして # netctl-auto current を使うことができます (netctl-1.3 以降の機能)。

接続を手動でパースするには、次を使って下さい:

# netctl list | awk '/*/ {print $2}'

Eduroam

WPA2 Enterprise#netctl を見て下さい。

ボンディング

kernel documentation より:

Linux bonding ドライバは複数のネットワークインターフェースを単一の論理的な「結合された」インターフェースに統合する手段を提供します。結合されたインターフェースの動作はモードに依存します。一般に言われる事ですが、モードはホットスタンバイまたは負荷分散サービスを提供します。加えて、リンクの保全監視が実現されます。

ロードバランシング

netctl とボンディングを使うには、公式リポジトリから追加のパッケージをインストールする必要があります: ifenslave.

/etc/netctl/examples/bonding/etc/netctl/bonding にコピーして、以下のように編集してください:

/etc/netctl/bonding
Description='Bond Interface'
Interface='bond0'
Connection=bond
BindsToInterfaces=('eth0' 'eth1')
IP=dhcp
IP6=stateless

古い設定を無効にして、bonding を自動的に起動するよう設定してください。新しいプロファイルに切り替えるには:

# netctl switch-to bonding
ノート: これは bonding ドライバーのデフォルトであるラウンドロビン方式を使います。詳しくは公式ドキュメントを見て下さい。
ヒント: 状態とボンディングモードを確認するには:
$ cat /proc/net/bonding/bond0

有線から無線へのフェイルオーバー

ここでは有線のイーサネットが切断されたときに無線にフォールバックするボンディングを使用する方法を説明します。有線と無線のインターフェイスがどちらも同じネットワークに接続されるような場合に有用です。無線ルーター/アクセスポイントが bridge モードに設定されている必要があります。

公式リポジトリから以下のパッケージをインストールしてください: ifenslavewpa_supplicant

まずは起動時に bonding モジュールがロードされるように設定します (カーネルモジュール#ロードを参照):

/etc/modules-load.d/bonding.conf
bonding

Then, configure the options of the bonding driver to use active-backup and configure the primary parameter to the device you want to be the active one (normally the wired interface). Also, be sure to use the same device name as returned when running ip link:

/etc/modprobe.d/bonding.conf
options bonding mode=active-backup miimon=100 primary=eth0 max_bonds=0

The miimon option is needed, for the link failure detection. The max_bonds option avoids the Interface bond0 already exists error. More information can be obtained on the kernel documentation.

Next, configure a netctl profile to enslave the two hardware interfaces. Use the name of all the devices you want to enslave. If you have more than two wired or wireless interfaces, you can enslave all of them on a bond interface. But, for most cases you will have only two devices, a wired and a wireless one:

/etc/netctl/failover
Description='A wired connection with failover to wireless'
Interface='bond0'
Connection=bond
BindsToInterfaces=('eth0' 'wlan0')
IP='dhcp'

Disable any other profiles (specially a wired or wireless) you had enabled before and then enable the failover profile on startup:

# netctl enable failover

Now you need to configure wpa_supplicant to connect to any know network you wish. You should create a file for each interface and enable it on systemd. Create the following file with this content:

/etc/wpa_supplicant/wpa_supplicant-wlan0.conf
ctrl_interface=/run/wpa_supplicant
update_config=1

And append to the end of this file any networks you want to connect:

network={
    ssid="SSID"
    psk=PSK
}

難読化された PSK を生成するには WPA supplicant#wpa_passphrase で接続する のページに書かれているように wpa_passphrase を実行します。

そして、wpa_supplicant@ テンプレートサービスにネットワークインターフェイスを指定して有効化してください。例: wpa_supplicant@wlan0

マシンを再起動して設定が上手く機能するかどうか確認できます。

ノート: ボンディングの起動時に以下のエラーが表示される場合:
wlan0 is up - this may be due to an out of date ifenslave

このエラーは failover netctl プロファイルよりも前に wpa_supplicant が実行されてしまっているのが原因です。特に指定がないかぎり systemd は何もかもを同時に実行してしまうからです。bond0 インターフェイスにボンディングする前に、ifenslave は全てのインターフェイスが立ち上がっていないを要求します。ネットワークをスキャンするためにはインターフェイスが立ち上がっている必要があるため、有線インターフェイスだけがボンディングされることにもなります。

このような場合、wpa_supplicant@wlan0 サービスと netctl@failover の依存関係をカスタマイズする必要があります。特に、netctl プロファイルの後に wpa_supplicant が起動されるようにしなくてはなりません。systemd#依存関係を解決する に書かれているようにして、カスタム依存関係ファイルを作成してください:

/etc/systemd/system/wpa_supplicant@wlan0.service.d/customdependency.conf
[Unit]
After=netctl@failover.service

その後、システムをもう一度再起動して問題ないか確認してください。netctl@failover.service ユニットの journal を確認することでボンディングの状態をチェックできます。

And by checking:

# ip link

You should see something like this:

1: eth0: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master bond0 state UP mode DEFAULT group default qlen 1000
    link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff
2: wlan0: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc mq master bond0 state UP mode DORMANT group default qlen 1000
    link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff
3: bond0: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default 
    link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff

巨大なダウンロードをすることで、ファイルオーバーの設定をテストすることができます。ダウンロードの途中に有線インターフェイスを抜いて見て下さい。無線インターフェイスに切り替わってダウンロードが継続するはずです。そして、もう一度有線インターフェイスを接続すると有線でダウンロードが続行されるはずです。netctl@failover.servicewpa_supplicant@wlan0.service ユニットの journal を確認することでデバッグできます。

全てのインターフェイスを使う

In some cases it may be desirable to allow a profile to use any interface on the system. A common example use case is using a common disk image across many machines with differing hardware (this is especially useful if they are headless). If you use the kernel's naming scheme, and your machine has only one ethernet interface, you can probably guess that eth0 is the right interface. If you use udev's Predictable Network Interface Names, however, names will be assigned based on the specific hardware itself (e.g. enp1s0), rather than simply the order that the hardware was detected (e.g. eth0, eth1). This means that a netctl profile may work on one machine and not another, because they each have different interface names.

A quick and dirty solution is to make use of the /etc/netctl/interfaces/ directory. Choose a name for your interface alias (en-any in this example), and write the following to a file with that name (making sure it is executable).

/etc/netctl/interfaces/en-any
#!/bin/bash
for interface in /sys/class/net/en*; do
        break;
done
Interface=$(basename $interface)
echo "en-any: using interface $Interface";

Then create a profile that uses the interface. Pay special attention to the Interface directive. The rest are only provided as examples.

/etc/netctl/wired
Description='Wired'
Interface=en-any
Connection=ethernet
IP=static
Address=('192.168.1.15/24')
Gateway='192.168.1.1'
DNS=('192.168.1.1')

When the wired profile is started, any machine using the two files above will automatically bring up and configure the first ethernet interface found on the system, regardless of what name udev assigned to it. Note that this is not the most robust way to go about configuring interfaces. If you use multiple interfaces, netctl may try to assign the same interface to them, and will likely cause a disruption in connectivity. If you do not mind a more complicated solution, netctl-auto is likely to be more reliable.

フックを使う

netctl supports hooks in /etc/netctl/hooks/ and per interface hooks in /etc/netctl/interfaces/. You can set any option in a hook/interface that you can in a profile. They are read the same way! Most importantly this includes ExecUpPost and ExecDownPre.

When a profile is read, netctl sources all executable scripts in hooks, then it reads the profile file for the connection and finally it sources an executable script with the name of the interface used in the profile from the interfaces directory. Therefore, declarations in an interface script override declarations in the profile, which override declarations in hooks.

The variables $INTERFACE, $SSID, $ACTION and $Profile are available in hooks/interfaces only when using netctl-auto

サンプル

接続の確立時にコマンドを実行
/etc/netctl/hooks/myservices
#!/bin/sh
ExecUpPost="systemctl start crashplan.service; systemctl start dropbox@<username>.service"
ExecDownPre="systemctl stop crashplan.service; systemctl stop dropbox@<username>.service"
network-online.target の有効化
/etc/netctl/hooks/status
#!/bin/sh
ExecUpPost="systemctl start network-online.target"
ExecDownPre="systemctl stop network-online.target"

このフックを使うことで、ネットワーク接続が必要な systemd サービスを network-online.target の後に起動するように順番を変えることができ、接続が切断される前にサービスが停止されます。

デフォルトの DHCP クライアントの設定

全てのプロファイルで使用する DHCP クライアントを設定・変更するには:

/etc/netctl/hooks/dhcp
#!/bin/sh
DHCPClient='dhclient'

もしくは、以下の行を含んだ実行可能ファイル /etc/netctl/interfaces/<interface> を作成することで、特定のネットワークインターフェイスでクライアントを指定することもできます:

DHCPClient='dhclient'

トラブルシューティング

Job for netctl@wlan(...).service failed

netctl でネットワークに接続するときに、以下のような問題が発生することがあります:

# netctl start wlan0-ssid
Job for netctl@wlan0\x2ssid.service failed. See 'systemctl status netctl@wlan0\x2ssid.service' and 'journalctl -xn' for details.

journalctl -xn を確認して、以下のどちらかを行なって下さい:

1. デバイスが立ち上がっている場合 (上記の場合 wlan0):

network[2322]: The interface of network profile 'wlan0-ssid' is already up

インターフェイスを落とすように設定すれば問題は解決します:

# ip link set wlan0 down

一度落としてから再試行してください:

# netctl start wlan0-ssid

2. インターフェイスが落ちている場合:

dhcpcd[261]: wlan0: ipv4_sendrawpacket: Network is down

dhclient など、別の DHCP クライアントを使えば解決します。クライアントのパッケージをインストールしてから netctl を設定してください:

/etc/netctl/wlan0-ssid
...
DHCPClient='dhclient'

ForceConnect オプションを追加することで解決することもあります:

/etc/netctl/wlan0-ssid

...

ForceConnect=yes

プロファイルを保存したら再度接続を試行してください:

# netctl start wlan0-ssid

dhcpcd: ipv4_addroute: File exists

On some systems dhcpcd in combination with netctl causes timeout issues on resume, particularly when having switched networks in the meantime. netctl will report that you are successfully connected but you still receive timeout issues. In this case, the old default route still exists and is not being renewed. A workaround to avoid this misbehaviour is to switch to dhclient as the default dhcp client. More information on the issue can be found here.

DHCP がタイムアウトする問題

If you are having timeout issues when requesting leases via DHCP you can set the timeout value higher than netctl's 30 seconds by default. Create a file in /etc/netctl/hooks/ or /etc/netctl/interfaces/, add TimeoutDHCP=40 to it for a timeout of 40 seconds and make the file executable.

接続がタイムアウトする問題

If you are having timeout issues that are unrelated to DHCP (on a static ethernet connection for example), and are experiencing errors similar to the following when starting your profile:

# journalctl _SYSTEMD_UNIT=netctl@profile.service
Starting network profile 'profile'...
No connection found on interface 'eth0' (timeout)
Failed to bring the network up for profile 'profile'

Then you should increase carrier and up timeouts by adding TimeoutUp= and TimeoutCarrier= to your profile file:

/etc/netctl/profile
...
TimeoutUp=300
TimeoutCarrier=300

Do not forget to reenable your profile:

# netctl reenable profile

復帰時の netctl-auto の問題

サスペンドからシステムが復帰したときに netctl-auto がときどき再接続に失敗することがあります。一番簡単な解決法は netctl-auto のサービスを再起動することです。以下のようなサービスを作ることで自動で再起動させることができます:

/etc/systemd/system/netctl-auto-resume@.service
[Unit]
Description=restart netctl-auto on resume.
Requisite=netctl-auto@%i.service
After=suspend.target

[Service]
Type=oneshot
ExecStart=/usr/bin/systemctl restart netctl-auto@%i.service

[Install]
WantedBy=suspend.target

ワイヤレスカードでサービスを有効化するには、root で netctl-auto-resume@wlan0.service などを有効化してください。wlan0 は適当なネットワークインターフェイスに置き換えてください。

netctl-auto で突然 WiFi アダプタが動作しなくなる

この問題は最近の wpa_supplicant のアップデートが関連しています (FS#44731 を参照)。ただし回避方法はとても簡単です。/etc/netctl/interfaces にあなたの使っているインターフェイスの名前のファイル (例: wlp3s0) を以下の内容で作成して実行可能属性を付与してください:

/etc/netctl/interfaces/wlp3s0
WPAOptions="-m ''"

その後、netctl-auto サービスを再起動すれば WiFi の自動検出がまた動作するようになります。

参照