0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Vagrant上のCentOSにMacからsambaで接続

Last updated at Posted at 2016-12-23

この記事は、単純に設定してみる機会がありましたので、その作業メモ的内容になります。

実施する設定

  • ApacheのDocumentRoot (/var/www/html) をsambaで共有する
  • 検証目的ってことで試してます...
  • 直にsambaじゃなくて、シンボリックリンクを使うとか、git管理で出来上がったら構成管理ツールとかで適用とかのほうが望ましいです
  • VagrantでのCentOSインストールについては省略
  • Mac(Host側)は 192.168.331, CentOS側は 192.168.33.10

VirtualBox / CentOS側:

利用したのはこちら。(CentOS7でなくてすみません...)

$ uname -a
Linux localhost.localdomain 2.6.32-573.7.1.el6.x86_64 #1 SMP Tue Sep 22 22:00:00 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

$ cat /etc/redhat-release 
CentOS release 6.7 (Final)

Sambaのインストール

  • yum install httpd
  • yum install samba4

備考:

  • epelリポジトリを追加した状態ですが、"yum install samba" とすると、Samba 3.x系が入ります
  • Samba4系をインストールする際は、samba4 と明示が必要です
  • samba3を入れた上でsamba4を入れる場合は、依存関係で怒られる可能性があります
  • いったん yum erase samba してからsamba4を入れましょう
$ httpd -v
Server version: Apache/2.2.15 (Unix)
Server built:   Nov 18 2016 23:48:55

$ smbd -V
Version 4.2.10

Apacheの起動ユーザの確認

全てuser: apacheで起動しているのを確認

$ ps -elf | grep [a]pache
5 S apache    2448  2446  0  80   0 - 43828 inet_c 09:41 ?        00:00:00 /usr/sbin/httpd
5 S apache    2449  2446  0  80   0 - 43828 semtim 09:41 ?        00:00:00 /usr/sbin/httpd

-- [以下略] --

/var/www/html の権限設定

yum installの直後だと、Ownerがrootです。
Ownerとグループを変更し、setgidしておきます。

# chown -R apache:apache /var/www/html
# chmod g+s /var/www/html

# ls -la /var/www/

....
drwxrwsr-x   3 apache apache 4096 12月 23 12:50 2016 html # sが付きます

Sambaのプロセスの確認

sambaはrootdで上がるんですね....

# ps -elf | grep [s]mb
5 S root      3788     1  0  80   0 - 91918 poll_s 13:18 ?        00:00:00 smbd
1 S root      3791  3788  0  80   0 - 91918 poll_s 13:18 ?        00:00:00 smbd
5 S root      3793  3788  0  80   0 - 92496 poll_s 13:18 ?        00:00:00 smbd

smb.conf の編集

今回はsambaと言えどWindowsは登場しない / ADもいないので、シンプルに。

# vi /etc/samba/smb.conf


[global]
   unix charset = UTF-8
   dos charset = CP932
   max protocol = SMB2
   
   # 今回Vagrantのネットワーク 192.168.33 を許可
   hosts allow = 192.168.33. 127.   
   log level = 2
   workgroup = WORKGROUP
   server string = Samba Server Version %v
   security = user
   passdb backend = tdbsam
   load printers = yes
   cups options = raw
   
   # 接続元のIP毎にログが出ます、うまくいかなかったらここを見る
   log file = /var/log/samba/log.%m  
   max log size = 1000
   syslog = 0

[homes]
   comment = Home Directories
   path = %H/samba
   browseable = no
   writable = yes
   vfs objects = recycle
   recycle:repository = .recycle
   recycle:keeptree = no
   recycle:versions = yes
   recycle:touch = no
   recycle:maxsize = 0
   recycle:exclude = *.tmp ~$*

[apache_docroot]
   comment = Web
   path = /var/www/html
   browseable = no
   writable = yes
   vfs objects = recycle
   
   # sambaでのアクセスは強制的にuser: apacheに丸めちゃう
   # forceが嫌な場合はOS側でsambaでアクセスするユーザをapacheグループに所属させる
   force user = apache   
   force group = apache
   recycle:repository = .recycle
   recycle:keeptree = no
   recycle:versions = yes
   recycle:touch = no
   recycle:maxsize = 0
   recycle:exclude = *.tmp ~$*
   
   # /var/www/html 以下は指定グループなら読み書き変更可能にしたいので、umask設定
   create mask = 0664
   directory mask = 0775
   

設定ファイルの妥当性の確認をします。
testparmコマンドで、チェックしてくれます。

# testparm

Load smb config files from /etc/samba/smb.conf
rlimit_max: increasing rlimit_max (1024) to minimum Windows limit (16384)
Processing section "[homes]"
Processing section "[apache_docroot]"
Loaded services file OK.
Server role: ROLE_DOMAIN_MEMBER
Press enter to see a dump of your service definitions

リターンすると、確認のため設定内容が出力されます

-- [以下略] --

useradd で OS & samba双方で利用するユーザを作成

apacheグループに所属させたいので、まず確認。

# id apache
uid=48(apache) gid=48(apache) 所属グループ=48(apache)

グループを指定して、smbuserという名前のユーザを作成します。

# useradd -u 500 -G apache smbuser

作成されているか確認して、パスワードを設定します。

# id smbuser
uid=500(smbuser) gid=500(smbuser) 所属グループ=500(smbuser),48(apache)

# passwd smbuser
ユーザー smbuser のパスワードを変更。

-- [ 以下略 ] --

ユーザ:smbuser をsamba経由でアクセスできるようにするため、samba用の設定を実施します。
(pdbeditを利用)

基本はOS上のパスワードと揃えます。

# pdbedit -a smbuser
new password:
retype new password:
Unix username:        smbuser

[以下略]

# pdbedit -L
smbuser:500:

SSHでのsamba用ユーザのログイン確認

まずはSSHから接続できるか確認します。

$ ssh smbuser@192.168.33.10
smbuser@192.168.33.10's password: 
Last login: Fri Dec 23 10:36:33 2016 from 192.168.33.1
[smbuser@localhost ~]$ 

sambaユーザのsamba経由でのアクセス確認 (Macから)

接続はWindowsではなくMacを利用します。

やること:

  • apache用の/var/www/html を、Mac上のsmb_apacheというフォルダに割り当てる
  • 読み書き可能なことを確認する

ファインダから「サーバに接続」でもOKですが、コマンドラインからのほうが、ターミナルを利用して読み書きコピーしやすいので、mount_smbfs を使います。接続解除のときは、umount でOKです。
パスワードを入れると、うまく接続できても「success!」とかのメッセージは出ません。
ls とかでちゃんとマウントしているか確認しましょう。

# どこでもいいのでマウントポイント用の空のディレクトリ作成
$ mkdir smb_apache

# 接続実施
$ mount_smbfs //smbuser@192.168.33.10/apache_docroot smb_apache
Password for 192.168.33.10:   # パスワードを入れる

$ ls -la smb_apache

total 6208
drwx------   1 xxx  staff  16384 12 23 21:58 .
drwxr-xr-x  19 xxx  staff    646 12 23 21:29 ..
-rwx------@  1 xxx  staff   6148 12 23 20:07 .DS_Store
-rwx------   1 xxx  staff   4096 12 23 20:06 ._.DS_Store
drwx------   1 xxx  staff  16384 12 23 21:50 .recycle
-rwx------   1 xxx  staff      7 12 23 21:58 data.txt

$ umound smb_apache # 接続断

お好きなエディタでSamba経由で書き込み

smb.conf で force user = apache, force group = apache としているので、smbuser としてアクセスしていても、書き込んだファイルをCentOS側から見ると、Owner/Group ともにapache:apacheになります。

force ... が嫌な場合は、上記に書いた通り、sambaでアクセスさせるユーザをapacheグループに所属させておき、setgidもしておきましょう。

うまくいかない場合

サーバ側の /var/log/samba/log.%m (今回の例だと /var/log/samba/log.192.168.33.1 )を見て見ましょう。

こんな感じです。

# tail -2  /var/log/samba/log.192.168.33.1 

# 成功の場合
[2016/12/23 15:39:36.538770,  2] ../source3/auth/auth.c:305(auth_check_ntlm_password)
  check_ntlm_password:  authentication for user [smbuser] -> [smbuser] -> [smbuser] succeeded
  
# 失敗の場合  
[2016/12/23 11:34:33.483544,  2] ../source3/auth/auth.c:315(auth_check_ntlm_password)
  check_ntlm_password:  Authentication for user [smbuser] -> [smbuser] FAILED with error xxxxx 

以上、メモでした。

0
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?