个人工具

UbuntuHelp:ComprehensiveSambaGuide

来自Ubuntu中文

跳转至: 导航, 搜索
Please note. This guide is out of date. Please see "man mount.cifs", find another guide, or get someone to update this guide. Please provide a link to a better guide here if you find one. Thankyou.

This tutorial will help you setup reliable and secure sharing between Linux and Windows. It covers the steps to take for both operating systems. This guide should be compatible with the following versions of Ubuntu.

  • Dapper Drake (6.06)
  • Edgy Eft (6.10)
  • Feisty Fawn (7.04)

It should also work on these versions of Microsoft Windows (with a Workgroup configuration, not Domain).

  • Windows 2000 Professional
  • Windows XP Home
  • Windows XP Professional
  • Windows XP 64-bit Edition

It has however only been tested on "Feisty Fawn" and "Windows XP 64-bit Edition" so far. In this guide we will set up a share on Windows and access it from Linux. This appears to be the most reliable and natural configuration. (!) Samba is Linux's implementation of Windows' SMB (Server Message Block) protocol.

Configuring SMB on Windows

Installing the SMB Protocol

Open a command prompt by going to the start menu and pressing run. Type cmd and click OK. In the command prompt, type this command (the > prefix should be ignored):
> control netconnections 

Right click on the icon of the network adapter in question and select Properties. Ensure File and Printer Sharing... is listed. If it is not you can use the Install button to install that service. [1] <!> If you have the XP firewall enabled, ensure that File and Printer Sharing is selected under the Exceptions tab for the firewall configuration. Likewise, make sure any other firewall you are using does not restrict SMB (sharing) access.

Configuring Workgroup and Computer Name

Right click on My Computer on the desktop and go to Properties. Choose the Computer Name tab at the top. Click Change... and use Workgroup. In this example we will use MSHOME for the workgroup name as that is also the default in Ubuntu's Samba. Also enter a computer name (in this example, DEVMACHINE). Workgroup and computer names are not case-sensitive. It is important to reboot now if it wants you to. Please reboot, don't simply log out. [2]

Setting Up Access Control

We will need to add a group and user to effectively control access to our Windows shares.

Go back to your command line, and type these lines. Change S'hareAccessPassword' to a password of your choice.
> net localgroup UbuntuSMB /add
> net user ubuntu ShareAccessPassword /add /passwordchg:no /expires:never
> net localgroup UbuntuSMB ubuntu /add
> net localgroup Users ubuntu /delete 
We're also going to have to make sure the password never expires. Type:
> compmgmt.msc 

Find the Local Users and Groups section under System Tools. Right click on the ubuntu user and select Properties. Enable Password Never Expires and click OK.

Disable Simple Sharing

This feature does not make sharing any easier; it's basically a roadblock. Open an Explorer window (just double click My Computer) and go to View->Folder Options. Click the View tab and disable Use simple file sharing. Click OK to exit the dialog.

Setting Up Your Shares

In Explorer, find a drive or folder that you would like to share. In my example I will be sharing a folder at c:\shares. Right click the folder or drive and select Properties. Go to the Sharing tab and enable Share this folder. Enter a proper share name (shares in this example). Click Permissions and remove the Everyone group from the list. Now add the UbuntuSMB group (Add... and type in UbuntuSMB and click OK). Set the desired permissions for how Ubuntu should be able to access this share (in my example, I enabled full read/write access). This should mean that nobody is allowed to access the share, except for users in the UbuntuSMB group. If you explicitly press Deny on Everyone, that would also Deny UbuntuSMB since Deny has precedence over Allow and we don't want that. [3]

<!> If you connect to the share by using a user that is not in the UbuntuSMB group, the connection will succeed but the mount point will not at all be accessible, even by root. The listing will appear as such:
?---------  ? ?    ?          ?                ? winshares 

(!) You can change the Access Control List (ACL), the permissions, while the share is mounted without having to restart any service on either operating system. The changes will immediately take effect. [As of: Feisty Fawn; Windows XP 64-bit Edition]

Configuring Samba on Ubuntu

Installing Samba

Open a terminal by going to Applications->Accessories->Terminal and type the following.
$ sudo apt-get install samba smbfs
$ sudo mkdir -p /media/winshares
$ sudo addgroup smb
$ sudo adduser $USER smb 

You must now log out to ensure your user is now in the smb group. You do not have to reboot.

Testing Your Shares

Now let's try and mount our Windows share! Remember to replace S'hareAccessPassword' with the ubuntu user's password. The user name for the login is not case-sensitive, although the password is. Also, use the proper workgroup and share names. "//DEVMACHINE/shares" is called our share path. This is a type of UNC path where the format is //COMPUTER_NAME/SHARE_NAME. In Windows, backslashes are used instead of forward slashes to access UNC paths. Recall that we used "DEVMACHINE" for our computer name and "shares" for our share name.
$ sudo mount -t smbfs -o username=ubuntu,password=ShareAccessPassword,workgroup=MSHOME,gid=smb,uid=$USER,fmask=770,dmask=770,rw //DEVMACHINE/shares /media/winshares 

<!> By using the 770 permission, we are giving full access to the owner (the current user), to everybody in the smb group, and no access at all to users not in the smb group. <!> If your share name contains a space, you must enclose the share name in double quotes, e.g. for a share named "Share with Spaces":

 
$ sudo mount -t smbfs -o username=ubuntu,password=ShareAccessPassword,workgroup=MSHOME,gid=smb,uid=$USER,fmask=770,dmask=770,rw "//DEVMACHINE/Share with Spaces" /media/winshares 

You must do something else in the fstab entry which will be mentioned later. To test another share you must specify another share path (e.g. //DEVMACHINE/shares) and target (e.g. /media/winshares).

Break Down

  • sudo mount -- this is the command to mount a drive to a target.
  • -t smbfs -- this specifes that we would like to use the "smbfs" (Samba) file system.
  • -o username=... -- these are the parameters we pass to "smbfs" to make a proper connection to a share.
  • gid=smb -- this specifies that we would like the "smb" group to have ownership of the mount point.
  • fmask=777,dmask=777 -- these are the permissions we are giving to the owner, all users of the "smb" group.
  • rw -- this specifies that we would like read/write access to the share. ro will do read-only.
If your terminal printed nothing, it probably mounted just fine. Verify that the permissions are what you expect.
$ stat -c %a /media/winshares 

If they are different than 770 as specified, verify that you typed the mount command properly.

<!> Vital Note: you can not mount a whole computer to one location. For example, you can not mount the whole //DEVMACHINE/ computer to one folder on your Linux system. You also need to be careful about how you specify your share path. //DEVMACHINE/shares will work, but //DEVMACHINE/shares/ will return the following!
25034: tree connect failed: ERRDOS - ERRnosuchshare (You specified an invalid share name)
SMB connection failed 

Automatically Mounting the Shares

Securing Our Password

Let's unmount every share that we may have test-mounted from the last step.
$ sudo umount -a -t smbfs 
We don't want our username and password to be in plaintext and visible to everybody in /etc/fstab. Thus, we can use a credentials file that only root can see since only root should be mounting this share. Type the following.
$ sudo touch /etc/smbcredentials
$ sudo chmod 600 /etc/smbcredentials 

Open /etc/smbcredentials using your favorite text editor.

Ubuntu/GNOME:
 
$ gksu gedit /etc/smbcredentials 
Kubuntu/KDE:
$ kdesu kate /etc/smbcredentials 

Type the following into the /etc/smbcredentials file:

username = ubuntu
password = ShareAccessPassword  
Save it and close your text editor. Some text editors make a back-up of the file by appending a ~ to the filename. Let's ensure this file is removed so nobody can get our login and password. Be sure to type the ~ at the end in the following.
 $ sudo rm /etc/smbcredentials~

Registering Our Shares

Open /etc/fstab the same way you opened /etc/smbcredentials. For each share you want mounted at boot-up, add a line like the following.
//DEVMACHINE/shares     /media/winshares        smbfs   auto,credentials=/etc/smbcredentials,workgroup=MSHOME,gid=smb,uid=1000,fmask=770,dmask=770,rw       0       0

uid=1000 is assumed to be the user you are using now. Change it to your current user's ID or name as necessary. This must all be on one line in fstab. It may appear word-wrapped in your text editor, but as long as there are no actual line feeds, you're fine. Save and exit your text editor.

<!> If your share name contains a space, you must replace all spaces with \040, e.g. for a share named "Share with Spaces":
 
//DEVMACHINE/Share\040with\040Spaces     /media/winshares        smbfs   auto,credentials=/etc/smbcredentials,workgroup=MSHOME,gid=smb,uid=1000,fmask=770,dmask=770,rw       0       0

Testing fstab

Type the following to test our automatic Samba mounts.
$ sudo mount -a -t smbfs
$ mount | grep smbfs 

Your Samba share should be listed. It should also be accessible and have the correct permissions. If it does not, I suggest you review the previous steps.

Congratulations

Your shares should be working perfectly now, secure and reliable. The shares should appear in your filesystem at boot-up automatically.

Troubleshooting

If you get an error like this:
mount: wrong fs type, bad option, bad superblock on //DEVMACHINE/SHARE,
       missing codepage or helper program, or other error
       In some cases useful info is found in syslog - try
       dmesg | tail  or so 
You probably did not install the package smbfs.
 
$ sudo apt-get install smbfs 

Please add any errors and solutions to them that you may find. -- This guide uses smbfs rather than cifsfs for mounting. smbfs has been unmaintained for years and is slated for removal from the kernel. -- JelmerVernooij