検索

3.4.3. サービス間でファイルを共有する

download PDF
Type Enforcement を使用すると、 プロセスが別のプロセス用のファイルにアクセスしてしまうのを防ぐのに役立ちます。たとえば、デフォルトでは Samba は httpd_sys_content_t タイプのラベルが付いたファイルの読み込みはできません。このタイプは Apache HTTP Server での使用を目的としています。目的のファイルに public_content_t または public_content_rw_t タイプのラベルを付けると、Apache HTTP Server、FTP、rsync、Samba 間でファイルを共有することができるようになります。
以下の例では、 ディレクトリとファイルを作成し、 Apache HTTP Server、 FTP、 rsync、 Samba 経由でそのディレクトリファイルを共有 (読み取り専用) できるようにしています。
  1. root ユーザーで mkdir /shares を実行して、 複数のサービス間でファイルを共有できるよう最上位に新規のディレクトリを作成します。
  2. ファイルコンテキスト設定のパターンに合致しないファイルやディレクトリには default_t タイプのラベルが付けられる場合があります。 制限のあるサービスからはこのタイプのファイルやディレクトリにはアクセスできません。
    $ ls -dZ /shares
    drwxr-xr-x  root root unconfined_u:object_r:default_t:s0 /shares
    
  3. root ユーザーで /shares/index.html ファイルを作成します。 次のコンテンツをコピーして /shares/index.html に貼り付けます。
    <html>
    <body>
    <p>Hello</p>
    </body>
    </html>
    
  4. /shares/public_content_t タイプのラベルを付けることで、 Apache HTTP Server、 FTP、 rsync、Samba による読み取り専用アクセスを許可します。 root ユーザーで次のコマンドを実行し、 ラベルの変更をファイルコンテキスト設定に追加します。
    semanage fcontext -a -t public_content_t "/shares(/.*)?"
    
  5. root ユーザーで restorecon -R -v /shares/ を実行しラベルの変更を適用します。
    # restorecon -R -v /shares/
    restorecon reset /shares context unconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
    restorecon reset /shares/index.html context unconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
    
Samba で /shares/ を共有する場合
  1. rpm -q samba samba-common samba-client を実行して、 sambasamba-commonsamba-client の各パッケージがインストールされているか確認します (バージョン番号は使用しているバージョンによって異なります)。
    $ rpm -q samba samba-common samba-client
    samba-3.4.0-0.41.el6.3.i686
    samba-common-3.4.0-0.41.el6.3.i686
    samba-client-3.4.0-0.41.el6.3.i686
    
    上記いずれのパッケージもインストールされていない場合は、 root ユーザーで yum install package-name を実行してインストールを行ないます。
  2. root ユーザーで /etc/samba/smb.conf を編集します。 Samba で /shares/ ディレクトリを共有するため、 次のエントリをこのファイルの末尾に追加します。
    [shares]
    comment = Documents for Apache HTTP Server, FTP, rsync, and Samba
    path = /shares
    public = yes
    writeable = no
    
  3. Samba ファイルシステムをマウントするには Samba アカウントが必要になります。 root ユーザーで smbpasswd -a username を実行し、 Samba アカウントを作成します。 username の部分は既存の Linux ユーザーにします。 たとえば、 smbpasswd -a testuser を実行すると、 Linux の testuser ユーザー用の Samba アカウントが作成されます。
    # smbpasswd -a testuser
    New SMB password: Enter a password
    Retype new SMB password: Enter the same password again
    Added user testuser.
    
    smbpasswd -a username を実行する際、 システムに存在しない Linux アカウントのユーザー名をusername に使用すると、 「Cannot locate Unix account for 'username'!」というエラーが発生する原因になります。
  4. root ユーザーで service smb start を実行し Samba サービスを起動します。
    service smb start
    Starting SMB services:                                     [  OK  ]
    
  5. smbclient -U username -L localhost を実行し、 使用できる共有を表示させます。 username はステップ 3 で追加した Samba アカウントにします。 パスワード入力を求められたら、 ステップ 3 で Samba アカウントに割り当てたパスワードを入力します (バージョン番号は使用しているバージョンによって異なります)。
    $ smbclient -U username -L localhost
    Enter username's password:
    Domain=[HOSTNAME] OS=[Unix] Server=[Samba 3.4.0-0.41.el6]
    
    Sharename       Type      Comment
    ---------       ----      -------
    shares          Disk      Documents for Apache HTTP Server, FTP, rsync, and Samba
    IPC$            IPC       IPC Service (Samba Server Version 3.4.0-0.41.el6)
    username        Disk      Home Directories
    Domain=[HOSTNAME] OS=[Unix] Server=[Samba 3.4.0-0.41.el6]
    
    Server               Comment
    ---------            -------
    
    Workgroup            Master
    ---------            -------
    
  6. root ユーザーで mkdir /test/ を実行し新規ディレクトリを作成します。 このディレクトリは Samba 共有の sharesをマウントする際に使用します。
  7. root で次のコマンドを実行して、Samba 共有の shares/test/ にマウントします。username はステップ 3 のユーザー名にしてください。
    mount //localhost/shares /test/ -o user=username
    ステップ 3 で設定した username のパスワードを入力します。
  8. cat /test/index.html を実行して Samba で共有しているファイルを表示させます。
    $ cat /test/index.html
    <html>
    <body>
    <p>Hello</p>
    </body>
    </html>
    
Apache HTTP Server で /shares/ を共有する場合
  1. rpm -q httpd を実行して httpd パッケージがインストールされているか確認します (バージョン番号は使用しているバージョンによって異なります)。
    $ rpm -q httpd
    httpd-2.2.11-6.i386
    
    このパッケージがインストールされていない場合は、 root ユーザーで yum install httpd を実行してインストールします。
  2. /var/www/html/ ディレクトリに移動します。 root ユーザーで次のコマンドを実行して /shares/ ディレクトリへのリンクを作成します。
    ln -s /shares/ shares
    
  3. root ユーザーで service httpd start を実行して Apache HTTP Server を起動します。
    service httpd start
    Starting httpd:                                            [  OK  ]
    
  4. web ブラウザを使って http://localhost/shares に行きます。 /shares/index.html が表示されます。
デフォルトでは、index.html ファイルが存在していればそのファイルが Apache HTTP Server によって読み込まれます。/shares/file1file2file3 しかなく index.html がない場合、http://localhost/shares にアクセスするとディレクトリ一覧が表示されます。
  1. root ユーザーで rm -i /shares/index.html を実行して index.html ファイルを削除します。
  2. root ユーザーで touch /shares/file{1,2,3} を実行し、 /shares/ 内に 3 つのファイルを作成します。
    # touch /shares/file{1,2,3}
    # ls -Z /shares/
    -rw-r--r--  root root system_u:object_r:public_content_t:s0 file1
    -rw-r--r--  root root unconfined_u:object_r:public_content_t:s0 file2
    -rw-r--r--  root root unconfined_u:object_r:public_content_t:s0 file3
    
  3. root ユーザーで service httpd status を実行して Apache HTTP Server の状態を確認します。 サーバーが停止している場合は、 root ユーザーで service httpd start を実行し再起動を行ないます。
  4. web ブラウザで http://localhost/shares に行きます。 ディレクトリ一覧が表示されます。
Red Hat logoGithubRedditYoutubeTwitter

詳細情報

試用、購入および販売

コミュニティー

Red Hat ドキュメントについて

Red Hat をお使いのお客様が、信頼できるコンテンツが含まれている製品やサービスを活用することで、イノベーションを行い、目標を達成できるようにします。

多様性を受け入れるオープンソースの強化

Red Hat では、コード、ドキュメント、Web プロパティーにおける配慮に欠ける用語の置き換えに取り組んでいます。このような変更は、段階的に実施される予定です。詳細情報: Red Hat ブログ.

会社概要

Red Hat は、企業がコアとなるデータセンターからネットワークエッジに至るまで、各種プラットフォームや環境全体で作業を簡素化できるように、強化されたソリューションを提供しています。

© 2024 Red Hat, Inc.