LoginSignup
5
3

More than 5 years have passed since last update.

itamae|ldap認証のCentos7でwebサーバーを構築するレシピ

Last updated at Posted at 2016-08-15

概要

CentOS7のサーバにldap認証を実装して、webサーバーを構築するレシピ

構築ネットワーク

▪️ 192.168.56.101
centos7-admin(管理サーバー)
ldapサーバーとitamae実行サーバーを兼ねる構築済みが前提(本稿では取り扱わない)

▪️ 192.168.56.102
centos7-web(webサーバー)
本稿で構築するwebサーバー

▪️ 192.168.56.103
centos7-db(dbサーバー)
DBサーバー(本稿では取り扱わない)

▪️ 192.168.56.104
centos7-adm2(管理サーバーのバックアップサーバー)
管理サーバーのバックアップサーバー(本稿では取り扱わない)

▪️ 192.168.56.201 centos7−ldap(ldap用VIP)
管理サーバーの冗長運用時のVIP

前準備

  • 構築するサーバーにCentOS7をMinimalのisoからインストールする
  • (構築するwebサーバーで)インストール後に192.168.56.102のローカルIPを振る
  • (構築するwebサーバーで)root以外のマスターアカウントを作成し、そのアカウントで公開鍵認証ができるようにauthorized_keysに公開鍵を追加する
  • (構築するwebサーバーで)マスターアカウントでパスワードなしsudoができるように設定する
  • (構築するwebサーバーで)yum upgrade を実行してOSを最新状態にする

本編

本稿で使用したレシピはgithubに上げてある

管理サーバーからitamaeを実行して構築する

(管理サーバーで)

mkdir itamae_cookbooks
cd itamae_cookbooks
mkdir repos os_package os_package/files nginx
cd os_package

レシピを作成する

  • sshdの設定変更する

rootのログインをforced-commands-onlyに変更、パスワード認証をoffにして、公開鍵認証のみに変更する

sshd.rb
# recipe
template "/etc/ssh/sshd_config" do
    action :create
    source "files/sshd_config.template"
end

service "sshd" do
    action :restart
end

(管理サーバーでitamaeを実行する)
itamae ssh -u xxx -h 192.168.56.102 -i id_rsa_xxx sshd.rb

  • selinxをDisabledにする

Disabledにしたconfigのテンプレートを準備して入れ替える

selinux.rb
# recipe
template "/etc/selinux/config" do
  action :create
  source "files/selinux.config.template"
end

(管理サーバーでitamaeを実行する)
itamae ssh -u xxx -h 192.168.56.102 -i id_rsa_xxx selinux.rb

  • hostnameとhostsファイルの設定

host名のjsonを変数で渡して動的に変更できるようにする

hostname.rb
execute "hostname #{node[:name]}"

template "/etc/hostname" do
    source "files/hostname.erb"
end

template "/etc/hosts" do
    source "files/hosts.erb"
end

file "/etc/hosts" do
    action :edit
    block do | content |
        content.gsub!(/^([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3} #{node[:name]})$/, '#\1')
    end
end
centos7-web.json
{"name" : "centos7-web" }
hostname.erb
<%= node[:name] %>
hosts.erb
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4 <%= node[:name] %>
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.56.101 centos7-admin
192.168.56.102 centos7-web
192.168.56.103 centos7-db
192.168.56.104 centos7-adm2
192.168.56.201 centos7-ldap

(管理サーバーでitamaeを実行する)
itamae ssh -u xxx -h 192.168.56.102 -i id_rsa_xxx --node-json files/centos7-web.json hostname.rb

* os標準パッケージの追加

bzip2 xz bind-utils lsof を追加し、ネットワークマネージャー等不要なものをoffに、logrotateに圧縮を設定する

centos7_default.rb
[
  "bzip2", 
  "xz",
  "bind-utils",
  "lsof"
].each {| pkg |
    package pkg do
        action :install
    end
}


[
    "tuned.service",
    "NetworkManager-dispatcher.service",
    "NetworkManager.service"
].each{| srv |
    service srv do
        action [:stop, :disable]
    end
}

template "/etc/logrotate.conf" do
    action :create
    owner  "root"
    group  "root"
    source "files/logrotate.conf.template"
end
  • ldap認証の設定を追加する

ldapのclientを追加インストール、ldap.confの設置、mkhomedirでログイン時にホームディレクトリがない場合に自動作成する設定、sssdauthで認証にsssを使う設定、sssd.confの設置、system-auth-acの設置、nsswitch.confの設置、/etc/ssh/ldap.confでldap公開鍵認証用の設定を行う

ldap_client.rb
[
    "openldap-clients",
    "sssd",
    "nscd",
    "openssh-ldap",
].each {| pkg |
    package pkg do
        action :install
    end
}

template "/etc/openldap/ldap.conf" do
    action :create
    owner  "root"
    group  "root"
    source "files/ldap.conf.template"

end

execute "authconfig mkhomedir" do
    action  :run
    command "authconfig --enablemkhomedir --update"
end

execute "authconfig sssdauth" do
    action  :run
    command "authconfig --enablesssd --enablesssdauth --update"
end

template "/etc/sssd/sssd.conf" do
    action :create
    owner  "root"
    group  "root"
    source "files/sssd.conf.template"
end

template "/etc/pam.d/system-auth-ac" do
    action :create
    owner  "root"
    group  "root"
    source "files/system-auth-ac.template"
end

template "/etc/nsswitch.conf" do
    action :create
    owner  "root"
    group  "root"
    mode   "644"
    source "files/nsswitch.conf.template"
end

template "/etc/ssh/ldap.conf" do
    action :create
    owner  "root"
    group  "root"
    source "files/ssh_ldap.conf.template"
end

[
    "sssd",
    "sshd",
].each {| srv |
    service srv do
        action :restart
    end
}

nsswitch.confのパーミッションに気をつける必要がある。
ログインしたユーザーがnsswitch.confを読めないと

id: cannot find name for user ID xxxx
id: グループ ID xxxx のグループ名がみつかりません
id: cannot find name for user ID xxxx
[私は名前がありません!@centos7-web ~]$

という状態になるので、レシピでかならず
mode "644"
を設定すること

(管理サーバーでitamaeを実行する)
itamae ssh -u xxx -h 192.168.56.102 -i id_rsa_xxx ldap_client.rb


  • ここまで構築したら、一度OSを再起動させる

(構築するwebサーバーで)
sync;sync; init 6


  • 再起動後に、epelリポジトリを追加する
epel.rb
package "epel-release" do
    action :install
end

(管理サーバーでitamaeを実行する)
itamae ssh -u xxx -h 192.168.56.102 -i id_rsa_xxx epel.rb

確認作業

  • LDAP認証の確認

sshでOSに登録していない、ldapサーバーに登録してあるユーザーでログイン、sudoまで 可能か確認する
スクリーンショット 2016-08-15 18.37.26.jpg

Webサーバーの構築に関しては、別投稿で継続する

5
3
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
5
3