新秀編##linuxにおけるCIFSファイルシステム##

76618 ワード

CIFS(Common Internet File System)の概要:
汎用インターネットファイルシステムがwindowsホスト間でネットワークファイル共有を行うのは、マイクロソフト社独自のCIFSサービスを利用することで実現される.CIFSは、プログラムがリモートインターネットコンピュータ上のファイルにアクセスし、このコンピュータのサービスを要求できるようにする新しいプロトコルです.CIFSは、クライアント/サーバモードを使用します.クライアント・プログラムは、サーバ上のサーバ・プログラムにサービスを提供するように要求します.サーバはリクエストを取得し、応答を返します.CIFSは、パブリックまたはオープンのSMBプロトコルバージョンであり、Microsoftによって使用されます.SMBプロトコルは現在、ローカルエリアネットワーク上でサーバファイルのアクセスと印刷に使用されるプロトコルです.SMBプロトコルのようにCIFSは上位層で動作し,TCP/IPプロトコルのように下位層で動作しない.CIFSは、ファイル転送プロトコルやハイパーテキスト転送プロトコルなどのアプリケーションプロトコルの実装と見なすことができる.
CIFS(Common Internet File System)機能:
    1.                
    2.              
    3.              
    4.         

一般的に、CIFSは、FTPよりもファイルの制御を容易にする.HTTPプロトコルを使用するブラウザよりも、より直接的なサーバ・プログラム・インタフェースを提供します.CIFSの最も典型的な応用はwindowsユーザーが「ネット上の隣人」からネット上の他のホストを見つけてその中の共有フォルダにアクセスできることである.
sambaのサービスインストールと構成:
1.sambaサービスのインストール:サービス側(server仮想マシン172.2 5.254.220):
[root@server ~]# yum install samba samba-client samba-common  -y             ##    (samba         samba-client          samba-common samba     )
Loaded plugins: langpacks
rhel_dvd                                                 | 4.1 kB     00:00     
(1/2): rhel_dvd/group_gz                                   | 134 kB   00:00     
(2/2): rhel_dvd/primary_db                                 | 3.4 MB   00:00     
Package samba-common-4.1.1-31.el7.x86_64 already installed and latest version
Resolving Dependencies
--> Running transaction check
---> Package samba.x86_64 0:4.1.1-31.el7 will be installed
---> Package samba-client.x86_64 0:4.1.1-31.el7 will be installed
--> Finished Dependency Resolution
[root@server ~]# systemctl start smb               ##    
[root@server ~]# systemctl enable smb.service          ##      
ln -s '/usr/lib/systemd/system/smb.service' '/etc/systemd/system/multi-user.target.wants/smb.service'
[root@server ~]# systemctl stop firewalld                 ##     
[root@server ~]# systemctl disable firewalld             ##         
rm '/etc/systemd/system/basic.target.wants/firewalld.service'
rm '/etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service'
[root@server ~]# netstat -antlupe | grep smb                      ##         445 139
tcp        0      0 0.0.0.0:445             0.0.0.0:*               LISTEN      0          59714      3257/smbd           
tcp        0      0 0.0.0.0:139             0.0.0.0:*               LISTEN      0          59715      3257/smbd           
tcp6       0      0 :::445                  :::*                    LISTEN      0          59712      3257/smbd           
tcp6       0      0 :::139                  :::*                    LISTEN      0          59713      3257/smbd     

クライアント(desktop仮想マシン127.225.254.120):
[root@client ~]# yum install samba-client -y               ##    
Loaded plugins: langpacks
rhel_dvd                                                 | 4.1 kB     00:00     
(1/2): rhel_dvd/group_gz                                   | 134 kB   00:00     
(2/2): rhel_dvd/primary_db                                 | 3.4 MB   00:00     
Resolving Dependencies
--> Running transaction check
---> Package samba-client.x86_64 0:4.1.1-31.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved
[root@client ~]# smbclient -L //172.25.254.220                 ##       smb
Enter root's password:                      ##        ,    
Anonymous login successful
Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]

    Sharename       Type      Comment
    ---------       ----      -------
    IPC$            IPC       IPC Service (Samba Server Version 4.1.1)
Anonymous login successful
Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]

    Server               Comment
    ---------            -------

    Workgroup            Master
    ---------            -------                             ##     ,     

2.ユーザー登録の確立:仮想マシンに実際に存在するユーザーでなければならない:サービス側(server仮想マシン):
[root@server ~]# id student       ##student     
uid=1000(student) gid=1000(student) groups=1000(student),10(wheel)
[root@server ~]# useradd westos     ##  westos  ,            
[root@server ~]# id westos           ##    
uid=1001(westos) gid=1001(westos) groups=1001(westos)
[root@server ~]# smbpasswd -a student    ##  smb  student
New SMB password:
Retype new SMB password:
Added user student.
[root@server ~]# smbpasswd -a westos       ##  smb  westos
New SMB password:
Retype new SMB password:
Added user westos.
[root@server ~]# pdbedit -L          ##  smb  
student:1000:Student User
westos:1001:                 ##      
[root@server ~]# getsebool -a | grep samba          ##  samba    
samba_create_home_dirs --> off
samba_domain_controller --> off
samba_enable_home_dirs --> off
samba_export_all_ro --> off
samba_export_all_rw --> off
samba_portmapper --> off
samba_run_unconfined --> off
samba_share_fusefs --> off
samba_share_nfs --> off
sanlock_use_samba --> off
use_samba_home_dirs --> off
virt_sandbox_use_samba --> off
virt_use_samba --> off
[root@server ~]# setsebool -P samba_enable_home_dirs  on         ##  samba     

クライアント(desktop仮想マシン):
[root@client ~]# smbclient //172.25.254.220/student -U student          ##  student        smb
Enter student's password:                     ##         
Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]
smb: \> ls                               ##    student                                                                
NT_STATUS_ACCESS_DENIED listing \*               ##      selinux      ,             ,            
smb: \> ls

  .                                   D        0  Thu Jul 10 19:06:52 2014
  ..                                  D        0  Fri Jun  1 21:46:23 2018
  .bash_logout                        H       18  Wed Jan 29 07:45:18 2014
  .bash_profile                       H      193  Wed Jan 29 07:45:18 2014
  .bashrc                             H      231  Wed Jan 29 07:45:18 2014
  .ssh                               DH        0  Thu Jul 10 18:19:10 2014
  .config                            DH        0  Thu Jul 10 19:06:53 2014

        40913 blocks of size 262144. 28597 blocks available                 

3.アップロードファイル実用sambaサービスを共有する:クライアント(desktop仮想マシン):
       :
[root@client ~]# vim /etc/fstab               ##        //172.25.254.220/student /mnt  cifs defaults,username=student,password=redhat 0 0
[root@client ~]# mount -a          ##  
Filesystem               1K-blocks    Used Available Use% Mounted on
/dev/vda1                 10473900 3182012   7291888  31% /
devtmpfs                    469344       0    469344   0% /dev
tmpfs                       484932      84    484848   1% /dev/shm
tmpfs                       484932   12788    472144   3% /run
tmpfs                       484932       0    484932   0% /sys/fs/cgroup
/dev/mapper/vg0-vo          483670    2339    451840   1% /home
//172.25.254.220/student  10473900 3157052   7316848  31% /mnt               ##    
[root@client ~]# cd /mnt       ##    
[root@client mnt]# touch file{1..8}                   ##    
[root@client mnt]# ls               ##  
file1  file2  file3  file4  file5  file6  file7  file8
       :
[root@client ~]# vim /etc/rc.d/rc.local          ##        
  :mount //172.25.254.220/student /mnt/ -o username=student,password=redhat
[root@client ~]# chmod -x /etc/rc.d/rc.local        ##        
[root@client ~]# reboot               ##     
Connection to 172.25.254.120 closed by remote host.
Connection to 172.25.254.120 closed.
[kiosk@foundation20 Desktop]$ ssh root@172.25.254.120       ##     
reroot@172.25.254.120's password: 
[root@client ~]# df                  ##    
Filesystem               1K-blocks    Used Available Use% Mounted on
/dev/vda1                 10473900 3180840   7293060  31% /
devtmpfs                    469344       0    469344   0% /dev
tmpfs                       484932      80    484852   1% /dev/shm
tmpfs                       484932   12764    472168   3% /run
tmpfs                       484932       0    484932   0% /sys/fs/cgroup
/dev/mapper/vg0-vo          483670    2339    451840   1% /home
//172.25.254.220/student  10473900 3157032   7316868  31% /mnt                   ##        
[root@client mnt]# touch file{1..8}                   ##    
[root@client mnt]# ls               ##  
file1  file2  file3  file4  file5  file6  file7  file8
   (server   ):
[root@server ~]# cd /home/student                ##    
[root@server student]# ls                ##          student     
file1  file2  file3  file4  file5  file6  file7  file8

4.ワークグループ名の変更:クライアント(desktop仮想マシン):
[root@client ~]# smbclient -L //172.25.254.220          ##      
Enter root's password: 
Anonymous login successful
Domain=[MYGROUP]  【   】 OS=[Unix] Server=[Samba 4.1.1]

    Sharename       Type      Comment
    ---------       ----      -------
    IPC$            IPC       IPC Service (Samba Server Version 4.1.1)
Anonymous login successful
Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]

    Server               Comment
    ---------            -------

    Workgroup            Master
    ---------            -------

サービス側(server仮想マシン):
[root@server ~]# rpm -qc samba-common           ##  samba     
/etc/logrotate.d/samba
/etc/samba/lmhosts
/etc/samba/smb.conf
/etc/sysconfig/samba
[root@server ~]# vim /etc/samba/smb.conf            ##    
89         workgroup = WESTOS              ##       
[root@server ~]# systemctl restart smb.service         ##    

クライアント(desktop仮想マシン):
[root@client ~]# smbclient -L //172.25.254.220          ##      
Enter root's password: 
Anonymous login successful
Domain=[WESTOS]  【   】 OS=[Unix] Server=[Samba 4.1.1]

    Sharename       Type      Comment
    ---------       ----      -------
    IPC$            IPC       IPC Service (Samba Server Version 4.1.1)
Anonymous login successful
Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]

    Server               Comment
    ---------            -------

    Workgroup            Master
    ---------            -------

5.白黒リストを作成する:【ホワイトリスト】:サービス側のプロファイルで、ホワイトリストのユーザーを追加してから、クライアントとホストでサービス側(server仮想マシン)をテストする:
[root@server ~]# vim /etc/samba/smb.conf    ##    ,     
 98 hosts allow = 172.25.254.120              ##  172.25.254.120    
[root@server ~]# systemctl restart smb.service    ##    

テスト:
   (desktop   ):
[root@client ~]# smbclient -L //172.25.254.220            ##    
Enter root's password: 
Anonymous login successful
Domain=[WESTOS] OS=[Unix] Server=[Samba 4.1.1]

    Sharename       Type      Comment
    ---------       ----      -------
    IPC$            IPC       IPC Service (Samba Server Version 4.1.1)
Anonymous login successful
Domain=[WESTOS] OS=[Unix] Server=[Samba 4.1.1]

    Server               Comment
    ---------            -------

    Workgroup            Master
    ---------            -------                        ##    
   (172.25.254.20):
[kiosk@foundation20 Desktop]$ smbclient -L //172.25.254.220           ##    
Enter kiosk's password: 
protocol negotiation failed: NT_STATUS_INVALID_NETWORK_RESPONSE        ##    

【ブラックリスト】:サービス側プロファイルで、ブラックリストユーザーを追加し、クライアントとホストでサービス側(server仮想マシン)をテストします.
[root@server ~]# vim /etc/samba/smb.conf    ##    ,     
 98 hosts deny = 172.25.254.120              ##  172.25.254.120     
[root@server ~]# systemctl restart smb.service    ##    

テスト:
   (desktop   ):
[root@client ~]# smbclient -L //172.25.254.220             ##    
Enter root's password: 
protocol negotiation failed: NT_STATUS_INVALID_NETWORK_RESPONSE ##    
   (172.25.254.20):
[kiosk@foundation20 Desktop]$ smbclient -L //172.25.254.220       ##    
Enter kiosk's password: 
Anonymous login successful
Domain=[WESTOS] OS=[Unix] Server=[Samba 4.1.1]

    Sharename       Type      Comment
    ---------       ----      -------
    IPC$            IPC       IPC Service (Samba Server Version 4.1.1)
Anonymous login successful
Domain=[WESTOS] OS=[Unix] Server=[Samba 4.1.1]

    Server               Comment
    ---------            -------

    Workgroup            Master
    ---------            -------                      ##    

6.自分で作成したディレクトリの共有:サービス側(server仮想マシン):
[root@server ~]# mkdir /westos                    ##         ,     
[root@server ~]# semanage fcontext -a -t samba_share_t '/westos(/.*)?'           ##            
[root@server ~]# restorecon -RvvF /westos                 ##       
restorecon reset /westos context unconfined_u:object_r:default_t:s0->system_u:object_r:samba_share_t:s0
[root@server ~]# vim /etc/samba/smb.conf             ##    
321         [DIR]
322         comment= westos dir
323         path = /westos
[root@server ~]# systemctl restart smb.service               ##  smb  

クライアント(desktop仮想マシン):
[root@client ~]# smbclient //172.25.254.220/DIR -U student    ##    
Enter student's password: 
Domain=[WESTOS] OS=[Unix] Server=[Samba 4.1.1]
smb: \> ls                                   ##         
  .                                   D        0  Sat Jun  2 01:46:21 2018
  ..                                  D        0  Sat Jun  2 01:46:21 2018

        40913 blocks of size 262144. 28578 blocks available

7.共有システムファイル:サービス側(server仮想マシン):
[root@server ~]# vim /etc/samba/smb.conf           ##    
325         [mnt]
326         comment= /mnt dir
327         path  =  /mnt
[root@server ~]# systemctl restart smb.service            ##  smb  
[root@server ~]# touch  /mnt/file{1..5}               ## /mnt   5   
[root@server ~]# ls /mnt                        ##      
file1  file2  file3  file4  file5
[root@server ~]# setenforce 0                   ##
[root@server ~]# setsebool -P samba_export_all_ro on          ##  samba      

クライアント(desktop仮想マシン):
[root@client ~]# smbclient //172.25.254.220/mnt -U student               ##  
Enter student's password: 
Domain=[WESTOS] OS=[Unix] Server=[Samba 4.1.1]
smb: \> ls
  .                                   D        0  Sat Jun  2 02:08:19 2018
  ..                                  D        0  Sat Jun  2 01:46:21 2018
  file1                               N        0  Sat Jun  2 02:08:19 2018
  file2                               N        0  Sat Jun  2 02:08:19 2018
  file3                               N        0  Sat Jun  2 02:08:19 2018
  file4                               N        0  Sat Jun  2 02:08:19 2018
  file5                               N        0  Sat Jun  2 02:08:19 2018                      ##    /mnt      

        40913 blocks of size 262144. 28578 blocks available
smb: \> quit

8.共有ファイルの非表示:サービス側(server仮想マシン):
[root@server ~]# vim /etc/samba/smb.conf       ##    
321         [DIR]
322         comment= westos dir
323         path = /westos
324         browseable = no                   ##     
[root@server ~]# systemctl restart smb.service            ##    

クライアント(desktop仮想マシン):
[root@client ~]# smbclient -L //172.25.254.220             ##  
Enter root's password: 
Anonymous login successful
Domain=[WESTOS] OS=[Unix] Server=[Samba 4.1.1]

    Sharename       Type      Comment
    ---------       ----      -------
    mnt             Disk      /mnt dir
    IPC$            IPC       IPC Service (Samba Server Version 4.1.1)              ##  DIR   
Anonymous login successful
Domain=[WESTOS] OS=[Unix] Server=[Samba 4.1.1]

    Server               Comment
    ---------            -------

    Workgroup            Master
    ---------            -------

9.共有ファイルに書き込み可能:サービス側(server仮想マシン):
[root@server ~]# vim /etc/samba/smb.conf       ##    
321         [DIR]
322         comment= westos dir
323         path = /westos
324         browseable = yes
325         writable = yes

[root@server ~]# systemctl restart smb.service             ##    
[root@server ~]# chmod 777 /westos/            ##       

クライアント(desktop仮想マシン):
[root@client ~]# mount //172.25.254.200/DIR /mnt -o username=student,password=redhat               ##    
mount: //172.25.254.200/DIR is write-protected, mounting read-only
mount: cannot mount //172.25.254.200/DIR read-only
[root@client ~]# cd /mnt
[root@client mnt]# touch filekk            ##    
[root@client mnt]# ll /mnt
total 0
-rw-r--r-- 1 student student 0 Jun  2 02:42 filekk                ##    

10.ユーザー共有ファイルの書き込み制限:サービス側(server仮想マシン):
[root@server ~]# vim /etc/samba/smb.conf     ##      
321         [DIR]
322         comment= westos dir
323         path = /westos
324         browseable = yes
325         #writable = yes
326         write list = student
[root@server ~]# systemctl restart smb.service             ##    
[root@server ~]# chmod 777 /westos/              ##   

クライアント(desktop仮想マシン):
[root@client ~]# mount //172.25.254.220/DIR /mnt -o username=westos,password=redhat          ##westos     
[root@client ~]# touch /mnt/file1
touch: cannot touch ‘/mnt/file1’: Permission denied
[root@client ~]# umount /mnt/
[root@client ~]# mount //172.25.254.220/DIR /mnt -o username=student,password=redhat          ##student    
[root@client ~]# touch /mnt/file1
[root@client ~]# umount /mnt

11.グループユーザー共有ファイルの書き込み制限:サービス側(server仮想マシン):
[root@server ~]# vim /etc/samba/smb.conf 
321         [DIR]
322         comment= westos dir
323         path = /westos
324         browseable = yes
325         #writable = yes
326         write list = @student
[root@server ~]# systemctl restart smb.service 
[root@server ~]# id westos
uid=1001(westos) gid=1001(westos) groups=1001(westos)
[root@server ~]# usermod -G student westos                 ## westos  student  
[root@server ~]# id westos
uid=1001(westos) gid=1001(westos) groups=1001(westos),1000(student)    ##      

クライアント(desktop仮想マシン):
[root@client ~]# mount //172.25.254.220/DIR /mnt -o username=westos,password=redhat     ##    
[root@client ~]# touch /mnt/file2                   ## student   westos      
[root@client ~]# umount /mnt

12.sambaによるユーザー権限の向上:サービス側(server仮想マシン):
[root@server ~]# chmod 755 /westos/           ##    /westos    
[root@server ~]# ls -ld /westos/
drwxr-xr-x. 2 root root 30 Jun  2 03:04 /westos/
[root@server ~]# vim /etc/samba/smb.conf               ##    
321         [DIR]
322         comment= westos dir
323         path = /westos
324         browseable = yes
325         writable = yes
326         #write list = @student
327         admin users = westos
[root@server ~]# systemctl restart smb.service         ##    

クライアント(desktop仮想マシン):
[root@client ~]# mount //172.25.254.220/DIR /mnt -o username=westos,password=redhat      ##    
[root@client ~]# cd /mnt
[root@client mnt]# touch file4
[root@client mnt]# ll
total 0
-rw-r--r-- 1 student student 0 Jun  2 02:58 file1
-rw-r--r-- 1    1001    1001 0 Jun  2 03:04 file2
-rw-r--r-- 1 root       1001 0 Jun  2 03:10 file4  

13.マルチユーザマウント:クライアント(desktop仮想マシン):
[root@client ~]# useradd linux             ##      linux  
[root@client ~]# su - linux              ##   linux
[linux@client ~]$ cd /mnt/
[linux@client mnt]$ ls                 ##  
file1  file2  file4                 ##linux         ,      westos       ,   
[root@client ~]# vim /root/smbpass                ##    
username=student
password=redhat                 ##  smb                                                                                                                                                      [root@client ~]# yum install cifs-utils -y               ##            cifs-utils
Loaded plugins: langpacks
Resolving Dependencies                                   
--> Running transaction check
---> Package cifs-utils.x86_64 0:6.2-6.el7 will be installed
--> Finished Dependency Resolution
[root@client ~]# mount -o credentials=/root/smbpass,sec=ntlmssp,multiuser //172.25.254.220/DIR /mnt            ##  
[root@client ~]# cd /mnt
[root@client mnt]# ls
file1  file2  file4
[root@client mnt]# su - linux                ##   linux  
Last login: Sat Jun  2 03:29:28 EDT 2018 on pts/0
[linux@client ~]$ cd /mnt
[linux@client mnt]$ ls          ##  
ls: reading directory .: Permission denied           ##    
[root@client mnt]# su - linux
Last login: Sat Jun  2 03:42:04 EDT 2018 on pts/0
[linux@client ~]$ cifscreds add -u westos 172.25.254.220            ##    
Password: 
[linux@client ~]$ ls /mnt 
file1  file2  file4                             ##    

14.匿名ユーザーのログインとマウント:クライアント(desktop仮想マシン):
[root@client ~]# smbclient //172.25.254.220/DIR   ##      
Enter root's password: 
Anonymous login successful
Domain=[WESTOS] OS=[Unix] Server=[Samba 4.1.1]
tree connect failed: NT_STATUS_ACCESS_DENIED             ##  

サービス側(server仮想マシン):
[root@server ~]# vim /etc/samba/smb.conf            ##    
125         map to guest = bad user
328         guest ok = yes              ##        
[root@server ~]# systemctl restart smb.service               ##    

クライアント(desktop仮想マシン):
[root@client ~]# smbclient //172.25.254.220/DIR          ##    
Enter root's password: 
Domain=[WESTOS] OS=[Unix] Server=[Samba 4.1.1]
smb: \> ls
  .                                   D        0  Sat Jun  2 03:10:13 2018
  ..                                  D        0  Sat Jun  2 01:46:21 2018
  file1                               N        0  Sat Jun  2 02:58:05 2018
  file2                               N        0  Sat Jun  2 03:04:15 2018
  file4                               N        0  Sat Jun  2 03:10:13 2018                ##    

        40913 blocks of size 262144. 28576 blocks available
smb: \> quit
[root@client ~]# mount //172.25.254.200/DIR /mnt -o username=guestr,password=""                   ##    
[root@client ~]# df
Filesystem           1K-blocks    Used Available Use% Mounted on
/dev/vda1             10473900 3182808   7291092  31% /
devtmpfs                469344       0    469344   0% /dev
tmpfs                   484932      80    484852   1% /dev/shm
tmpfs                   484932   12768    472164   3% /run
tmpfs                   484932       0    484932   0% /sys/fs/cgroup
/dev/mapper/vg0-vo      483670    2356    451823   1% /home
//172.25.254.220/DIR  10473900 3158180   7315720  31% /mnt                             ###    
[root@client ~]# umount /mnt/                                       ##