0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Linux DHCP PAM LDAP

Last updated at Posted at 2025-09-12

DHCP

Dynamic Host Configuration Protocol

IP アドレスやネットワーク設定をクライアントに自動配布するためのプロトコル。

主に以下を配布(リース)することにより、クライアント側では手動設定が不要になる。

  • IP アドレス
  • サブネットマスク
  • デフォルトゲートウェイ
  • DNS サーバ
  • ドメイン名
  • リース時間(利用期限)

Linux では、ISC DHCP Server(dhcpd デーモン)がよく利用される。

クライアント側の dhclientdhcpcd は、通常ディストリビューションに含まれているので、特別インストールする必要はない(NetworkManager が内部で DHCP クライアントを利用することも多い)。

$ dhclient コマンドで手動操作で IP アドレスをリクエストすることもできる。

/etc/dhcp/dhcpd.conf

サーバ側で稼働する dhcpd デーモンの挙動を制御するための設定ファイル。

フォーマット
ディレクティブ 値;

設定ファイル内の各ディレクティブは、大きく 3 つの設定スコープに分類することができる。

グローバルに書いた設定は全体に適用される。subnet ブロックや host ブロックに書いた場合は、そのスコープ内でのみ有効になる。

/etc/dhcp/dhcpd.conf
/etc/dhcp/dhcpd.conf
# dhcpd.conf
#
# Sample configuration file for ISC dhcpd
#
# Attention: If /etc/ltsp/dhcpd.conf exists, that will be used as
# configuration file instead of this file.
#

# option definitions common to all supported networks...
option domain-name "example.org";
option domain-name-servers ns1.example.org, ns2.example.org;

default-lease-time 600;
max-lease-time 7200;

# The ddns-updates-style parameter controls whether or not the server will
# attempt to do a DNS update when a lease is confirmed. We default to the
# behavior of the version 2 packages ('none', since DHCP v2 didn't
# have support for DDNS.)
ddns-update-style none;

# If this DHCP server is the official DHCP server for the local
# network, the authoritative directive should be uncommented.
#authoritative;

# Use this to send dhcp log messages to a different log file (you also
# have to hack syslog.conf to complete the redirection).
#log-facility local7;

# No service will be given on this subnet, but declaring it helps the 
# DHCP server to understand the network topology.

#subnet 10.152.187.0 netmask 255.255.255.0 {
#}

# This is a very basic subnet declaration.

#subnet 10.254.239.0 netmask 255.255.255.224 {
#  range 10.254.239.10 10.254.239.20;
#  option routers rtr-239-0-1.example.org, rtr-239-0-2.example.org;
#}

# This declaration allows BOOTP clients to get dynamic addresses,
# which we don't really recommend.

#subnet 10.254.239.32 netmask 255.255.255.224 {
#  range dynamic-bootp 10.254.239.40 10.254.239.60;
#  option broadcast-address 10.254.239.31;
#  option routers rtr-239-32-1.example.org;
#}

# A slightly different configuration for an internal subnet.
#subnet 10.5.5.0 netmask 255.255.255.224 {
#  range 10.5.5.26 10.5.5.30;
#  option domain-name-servers ns1.internal.example.org;
#  option domain-name "internal.example.org";
#  option subnet-mask 255.255.255.224;
#  option routers 10.5.5.1;
#  option broadcast-address 10.5.5.31;
#  default-lease-time 600;
#  max-lease-time 7200;
#}

# Hosts which require special configuration options can be listed in
# host statements.   If no address is specified, the address will be
# allocated dynamically (if possible), but the host-specific information
# will still come from the host declaration.

#host passacaglia {
#  hardware ethernet 0:0:c0:5d:bd:95;
#  filename "vmunix.passacaglia";
#  server-name "toccata.example.com";
#}

# Fixed IP addresses can also be specified for hosts.   These addresses
# should not also be listed as being available for dynamic assignment.
# Hosts for which fixed IP addresses have been specified can boot using
# BOOTP or DHCP.   Hosts for which no fixed address is specified can only
# be booted with DHCP, unless there is an address range on the subnet
# to which a BOOTP client is connected which has the dynamic-bootp flag
# set.
#host fantasia {
#  hardware ethernet 08:00:07:26:c0:a5;
#  fixed-address fantasia.example.com;
#}

# You can declare a class of clients and then do address allocation
# based on that.   The example below shows a case where all clients
# in a certain class get addresses on the 10.17.224/24 subnet, and all
# other clients get addresses on the 10.0.29/24 subnet.

#class "foo" {
#  match if substring (option vendor-class-identifier, 0, 4) = "SUNW";
#}

#shared-network 224-29 {
#  subnet 10.17.224.0 netmask 255.255.255.0 {
#    option routers rtr-224.example.org;
#  }
#  subnet 10.0.29.0 netmask 255.255.255.0 {
#    option routers rtr-29.example.org;
#  }
#  pool {
#    allow members of "foo";
#    range 10.17.224.10 10.17.224.250;
#  }
#  pool {
#    deny members of "foo";
#    range 10.0.29.10 10.0.29.230;
#  }
#}

グローバルな設定

全体に共通して適用される設定。

項目 説明
option domain-name ドメイン名
【例】option domain-name "example.org";
option domain-name-servers DNS サーバ
【例】option domain-name-servers ns1.example.org, ns2.example.org;
option routers クライアントに配布するデフォルトゲートウェイの IP アドレス
【例】option routers 192.168.1.1;
option subnet-mask クライアントに通知するサブネットマスク
【例】option subnet-mask 255.255.255.0;
option broadcast-address ネットワークのブロードキャストアドレス
【例】option broadcast-address 192.168.1.255;
option ntp-servers NTP サーバ の IP アドレス
【例】option ntp-servers 192.168.1.10;
option nis-servers NIS サーバの IP アドレス
【例】option nis-servers 192.168.1.20;
option nis-domain NIS ドメイン名
【例】option nis-domain "example-nis";
option netbios-name-servers WINS サーバ の IP アドレス(クライアントが Windows の場合に有効)
【例】option netbios-name-servers 192.168.1.30;
defult-lease-time クライアントが期限を指定しなかった場合のデフォルトのリース期限(秒)
【例】default-lease-time 600;
max-lease-time クライアントが希望するリース時間の上限(秒)
【例】max-lease-time 7200;

サブネットの設定

subnet ブロックを使用して特定のネットワークにスコープを限定して適用する設定を記述する。

フォーマット
subnet ネットワークアドレス netmask サブネットマスク値 {
  ディレクティブ 値;
}
subnet 10.254.239.0 netmask 255.255.255.224 {
  range 10.254.239.10 10.254.239.20;
  option routers rtr-239-0-1.example.org, rtr-239-0-2.example.org;
}
項目 説明
range クライアントに割り当てる IP アドレスの範囲
【例】range 192.168.1.100 192.168.1.200;

ホストの設定

host ブロックを使用して特定のホストにスコープを限定して適用する設定を記述する。

特に、特定のホストに固定の IP アドレスを割り当てる場合に利用される。

フォーマット
host ホスト名 {
  ディレクティブ 値;
}
host host123 {
  hardware ethernet 00:11:22:33:44:55;
  fixed-address 192.168.1.50;
}
項目 説明
hardware ethernet クライアントの MAC アドレス
ethernet は本来はアドレスの種類の指定だが、実用上はほとんど ethernet しか指定しない)
【例】hardware ethernet 00:11:22:33:44:55;
fixed-address 固定の IP アドレスを割り当てる
【例】fixed-address 192.168.1.50;

/var/lib/dhcp/dhcpd.leases

dhcpd デーモンが現在リースしている IP アドレスが記録されるファイル。

$ dhclient

ISC DHCP クライアントを起動、制御するためのコマンド。

DHCP サーバから IP アドレスやネットワーク設定を取得するために利用される。

指定したインターフェースに対して IP アドレスのリースを要求する
$ dhclient インターフェース名
$ dhclient eth0
指定したインターフェースのリースを解放する(release)
$ dhclient -r インターフェース名

PAM

Pluggable Authentication Modules

Linux の基幹の認証フレームワーク。

Linux 上で動作するシステムアプリケーション(/bin/sudo/bin/ssh/bin/login)などからライブラリとして利用される

PAM が登場するまでの Linux では、サービスごと(sshsudo...)に個別の認証機能の実装が必要だったが、PAM が登場したことで、これらの認証方法が PAM に委譲された。これによって、認証処理が共通化・一元管理化され、またサービスが自身で認証処理を実装する必要がなくなった。

モジュールタイプ

PAM は モジュール から構成される。

設定ファイル からモジュールを追加したり、削除、変更することによって認証方法を柔軟に変更することができるのが PAM の特徴でもある。

設定ファイルでは 1 行ごとに 1 つのモジュールが指定される。

指定されたモジュールは設定ファイルの上から順 1 つずつに評価され、実行される。

このように PAM では複数のモジュールを順番に呼び出すことで認証が行われる。

LPIC (36).png

/etc/pam.d/

各サービスごとの PAM の設定ファイルが配置されるディレクトリ。

/etc/pam.d/ 配下の設定ファイル
$ ls /etc/pam.d/
chfn      common-account   common-session                 login     passwd     samba  su-l    vmtoolsd
chpasswd  common-auth      common-session-noninteractive  newusers  runuser    sshd   sudo
chsh      common-password  cron 

設定ファイル

設定ファイルを手動編集は、誤った設定をしてしまった際に、以降、ログインできなくなるなどの影響があるため基本的には推奨されない。

そのため安全に編集したい場合には、各サービスから提供されるコマンドを利用する。

ただし、コマンドでは編集できない項目もあるため、手動編集は絶対に避けるべき方法というわけではない。

もし手動編集する際は、編集しようとしているセッションとは別で、root ユーザとしてログインしたセッションを用意しておくと良い。仮に編集に失敗した場合でも、root セッションで復旧することができるためである。

また、事前に設定ファイルのバックアップをとっておくことも必要。

設定ファイルを手動で編集する際は、編集用セッションとは別で復旧用の root セッションを用意し、ファイルのバックアップをとっておく

フォーマット
モジュールタイプ 制御フラグ モジュール名 モジュール引数
モジュールタイプ 説明
auth パスワードなどの認証を行う
account ユーザーがログイン可能か確認する(有効期限、ログイン制限など)
password パスワード変更を管理する
session ログイン ~ ログアアウトまでの挙動を制御する
include / @inculde 指定した設定ファイルを読み込む。

制御フラグ ではモジュールの実行成否が認証にどのように影響するかを指定する。

制御フラグ 説明
required 成功が必須。
失敗すると最終的には認証自体も失敗するが、認証手続き自体は最後まで継続される
認証を受けるものはどの段階で自分が認証に失敗したかがわからない(セキュリティ向上)。
requisite 成功が必須。
手続きに失敗した時点で 認証が即終了する
sufficient 成功した場合、それ以降の同じモジュールタイプの手続きを不要とする(他が失敗しても認証は成功)。
optional 結果が無視される。
ログなどに利用される。

モジュール名 にはモジュールのファイルパスを指定する。モジュールは通常、.so ファイルとして存在する。

モジュール 説明
pam_env.so ログイン時の環境変数の設定を行う。
pam_unix.so 通常のログインで使用される。
/etc/passwd/etc/shadow を使った従来型の認証
全モジュールタイプ(authaccountpasswordsession)に対応する万能モジュール。
pam_rootok.so $ su による root ユーザへの切替時に使用される。
pam_wheel.so wheel などの、特定のグループに所属するユーザだけが $ suroot になれるよう制御する。
pam_securetty.so root ログインを許可する端末を /etc/securetty で制御する。
コンソールからのみ root ログインを許可するなど。
pam_nologin.so /etc/nologin ファイルが存在する場合に root 以外のログインを拒否する。
メンテナンス時などに利用する。

モジュール引数 には、各モジュールに渡すパラメータを指定する。

モジュール引数 説明
nullok パスワードが設定されていないアカウントでのログインを許可する。
try_first_pass すでに入力済みのパスワードを使う。
例:/etc/pam.d/sshd
/etc/pam.d/sshd
# PAM configuration for the Secure Shell service

# Standard Un*x authentication.
@include common-auth

# Disallow non-root logins when /etc/nologin exists.
account    required     pam_nologin.so

# Uncomment and edit /etc/security/access.conf if you need to set complex
# access limits that are hard to express in sshd_config.
# account  required     pam_access.so

# Standard Un*x authorization.
@include common-account

# SELinux needs to be the first session rule.  This ensures that any
# lingering context has been cleared.  Without this it is possible that a
# module could execute code in the wrong domain.
session [success=ok ignore=ignore module_unknown=ignore default=bad]        pam_selinux.so close

# Set the loginuid process attribute.
session    required     pam_loginuid.so

# Create a new session keyring.
session    optional     pam_keyinit.so force revoke

# Standard Un*x session setup and teardown.
@include common-session

# Print the message of the day upon successful login.
# This includes a dynamically generated part from /run/motd.dynamic
# and a static (admin-editable) part from /etc/motd.
session    optional     pam_motd.so  motd=/run/motd.dynamic
session    optional     pam_motd.so noupdate

# Print the status of the user's mailbox upon successful login.
session    optional     pam_mail.so standard noenv # [1]

# Set up user limits from /etc/security/limits.conf.
session    required     pam_limits.so

# Read environment variables from /etc/environment and
# /etc/security/pam_env.conf.
session    required     pam_env.so # [1]
# In Debian 4.0 (etch), locale-related environment variables were moved to
# /etc/default/locale, so read that as well.
session    required     pam_env.so user_readenv=1 envfile=/etc/default/locale

# SELinux needs to intervene at login time to ensure that the process starts
# in the proper default security context.  Only sessions which are intended
# to run in the user's context should be run after this.
session [success=ok ignore=ignore module_unknown=ignore default=bad]        pam_selinux.so open

# Standard Un*x password updating.
@include common-password

LDAP

Lightweight Directory Access Protocol

ディレクトリサービスにアクセスするためのプロトコル。

ディレクトリサービスとは、主に 組織内のアカウント、所属グループ、共有フォルダなどのリソース管理 を目的とした データベース のようなサービスを指す。

データベースは一般に「業務データの保存・検索・分析」を目的とした汎用サービスであり、ディレクトリサービスは 用途特化型のデータベース と考えることができる。

LDAP を実装したソフトウェアには OpenLDAP や Active Directory などがある。

LDAP はプロトコル。
OpenLDAP、Active Directory は LDAP が実装されたソフトウェア。

また LDAP はディレクトリサービスへアクセスするための単なる「ネットワークプロトコル」ではなく、「データの持ち方(データモデル)」までを含む広い仕様である。

  • データモデル
    • ディレクトリはツリー構造(DIT)で表現される
    • 各ノードは エントリ と呼ばれる
    • エントリには必ず一意な DN が存在する
    • DN は RDN の連結によって構成される

LDAP ができること

何らかのサービスを提供するために複数のサーバ(メールサーバ、Web サーバなど)が連携しているとする。

従来は、サーバ群が各々でユーザ情報を /etc/passwd/etc/shadow ファイルで管理していた。

ユーザが追加された場合には、それぞれのサーバのパスワードファイルにエントリを追加する必要がある。

しかし、各サーバの /etc/nsswitch.confpasswdshadow の情報源として LDAP を指定することによって、各々のサーバが保持していたユーザ情報が LDAP サーバに集約される。

これにより一元管理が可能となり、新たなユーザが追加された際などは、LDAP サーバがエントリ追加処理をするだけでよい。

LDAP x PAM

認証については PAM と連携することによって柔軟な運用が可能になる。

LDAP が PAM と連携する時、LDAP は「認証の実施者」ではなく「ユーザ情報を提供する単なるデータベース」として PAM から利用される。

例えばユーザが Linux サーバに SSH ログインを試みる場合、まず sshd は認証を自身で処理せず PAM に委譲する。

PAM の 設定ファイル で LDAP モジュールが指定されていれば、そのモジュールが LDAP サーバに問い合わせを行い、正しいユーザ・パスワードかを照合する。

DIT

Directory Information Tree

LDAP のデータモデル。

ディレクトリ内の全エントリをツリー構造で表す。

LPIC (42).png

エントリ

LDAP におけるデータの基本単位。

各エントリはユーザ、グループ、組織、デバイスなどを表す。

属性(attribute)と値(value)によって構成される。属性と値は = で連結して表現する

エントリは操作の対象となるため オブジェクト とも呼ばれる。

LPIC (39).png

LPIC (41).png

属性 説明
dc domain component
ドメイン名 を分解した各要素
ou organizational unit
組織、部署、グループ
o organization name
組織名
cn common name
一般名
uid use ID
ユーザ ID

エントリがどのような属性を持つかは スキーマ によって定義される。

DN

Desinguished Name

DIT 内でエントリを一意に識別するための識別子。

RDN(Relative Distinguished Name:相対識別名)によって構成される。RDN は , で連結して表現する

DN
uid=bob,ou=people,dc=example,dc=jp

LPIC (45).png

LPIC (44).png

LPIC (38).png

スキーマ

Schema

エントリがどのような属性を持つかの定義。

実体はテキストファイル(xxx.schema)。

LDAP には汎用的なスキーマがあらかじめ用意されているが、独自のスキーマを定義することもできる。

スキーマによって各エントリは一貫性、整合性を持ったデータにすることができる。また、別のスキーマを継承したスキーマを定義することもできる。

スキーマファイル 説明
core.schema OpenLDAP の必須のスキーマ
nis.schema Linux のユーザ、グループを扱うためのスキーマ
cosine.schema ディレクトリサービス X.500 規格で決められているスキーマ
inetorgpeason.shema アドレス帳などの個人情報を扱うためのスキーマ

LDIF

LDAP Data Interchange Format、エルディフ

スキーマに対して、実際に各属性の値を投入するための実データ(エントリを初期化するためのファイル、フォーマットファイル)

実体はテキストファイルで、エクスポートしたり、インポートすることができる。

LDAP サーバに エントリ を登録したり、変更する際に $ ldapadd$ ldapmodify コマンドから利用される。

1 つのエントリに対して下記のような 1 つの LDIF が存在する。

先頭に DN を書き、以降に属性と値を : 区切りで列挙する。

LDIF
dn: uid=bob,ou=people,dc=example,dc=jp
objectClass: inetOrgPerson
uid: bob
cn: Robert
sn: Bob Dylan
mail: bob@example.com

/etc/openldap/slapd.conf

Linux では一般的に LDAP を実装した OpenLDAP がサーバソフトウェアとして利用される。

OpenLAP のサーバでは slapd デーモンが稼働していて 設定ファイル /etc/opsnldap/slapd.conf によって挙動を変更することができる。

ただし、設定ファイルを手動編集する必要があるのは古いバージョンで、現代の OpenLDAP ではコマンドによって編集する。

LDAP クライアント側のコマンド

LDAP クライアント側から LDAP サーバに対して実行するコマンド。

$ ldapsearch

エントリを検索する
$ ldapsearch -x -b "起点となるDN" "(検索対象)"
  • -x : simple external authentication(シンプル認証)
  • -b : base DN(検索の起点になる DN)
$ ldapsearch -x -b "dc=example,dc=com" "(uid=bob)"

$ ldapadd

LDIF ファイルを使ってエントリを追加する。

$ ldapadd -x -D "エントリを管理するDN" -W -f LDIFファイル
  • -D : DN(追加するエントリを管理する DN を指定する)
  • W : パスワード入力を求める
  • f : file(入力元ファイルを指定)
$ ldapadd -x -D "cn=admin,dc=example,dc=com" -W -f data.ldif

$ ldapdelete

エントリを削除する。

$ ldapdelete -x -D "エントリを管理するDN" -W "削除するエントリのDN"
  • -D : DN(バインドする管理者 DN を指定)
  • W : Word(パスワード入力を求める)
  • f : file(入力元ファイルを指定)
$ ldapdelete -x -D "cn=admin,dc=example,dc=com" -W "uid=bob,ou=people,dc=example,dc=com"

$ ldapmodify

既存のエントリを変更する(LDIFchangetype: modify などを書いて実行する)。

$ ldapmodify -x -D "変更するエントリ管理するDN" -W -f LDIFファイル
  • -D : DN(変更するエントリを管理する DN を指定する)
$ ldapmodify -x -D "cn=admin,dc=example,dc=com" -W -f data.ldif

$ ldappasswd

エントリのパスワードを変更する。

LDAP ディレクトリに登録されている ユーザのパスワードを変更 する。
$ ldappasswd -x -D "エントリを管理するDN" -W "パスワードを変更するエントリのDN"
  • -D : DN(変更するエントリを管理する DN を指定する)
$ ldappasswd -x -D "cn=admin,dc=example,dc=com" -W "uid=bob,ou=people,dc=example,dc=com"

LADP サーバ側のコマンド

LDAP サーバが自身のデータベースを管理するためのコマンド。

LDAP サーバでは slapd デーモンが稼働している。

$ slapadd

LDIF ファイルを元にデータベースへエントリを追加する。

slapd を停止させて実行する必要がある($ systemctl)。

$ slapadd -l LDIFファイル
$ slapadd -l data.ldif

$ slapcat

データベースを LDIF ファイルとしてエクスポートする。

LDIF ファイルを出力する
$ slapcat -l 出力LDIFファイル名
LDIF ファイルを出力する
$ slapcat -l data.ldif

$ slapindex

データベースの インデックス を再構築する。

slapd を停止させて実行する必要がある($ systemctl)。

データベースのインデックスを再構築する
$ slapindex

$ slappasswd

パスワードをハッシュ化して出力する(slapd.conf に直接書くときなどに使う)。

パスワードをハッシュ化して出力する
$ slappasswd

SSSD

System Security Service Daemon

LDAP サーバがダウンしたり、負荷によってパフォーマンスが低下した場合に LDAP サーバの可用性を高めるためのキャッシュを提供する。

sssd デーモンに対して /etc/sssd/sssd.conf ファイルで設定を行う。

/etc/nsswitch.confsss を指定することで有効化される。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?