Setting up Samba

Note: The following is part of a series of steps to setup an email server using Exim 4.x, with imap and webmail access. It will use winbind to get user information from an NT server. If you found this page via a search engine it may not cover what you need or you may need to start at the beginning to understand everything I have done.

(Note: These instructions reference software that is now possibly much newer with many new or different configuration options. This page is being left up for reference.)

First samba and winbind must be installed. See http://www.samba.org for more information.

Quick steps:

  1. Edit smb.conf.
  2. Edit /etc/pam.d/samba.
  3. Edit /etc/nsswitch.conf.
  4. Setup firewall ports.
  5. Edit /etc/hosts so that your PDC & BDC are defined.
  6. Join the domain.
  7. Start the samba daemons.
  8. Start winbind.
  9. Test.
  10. Starting on boot.

Here is everything the way I did it.

Note: I am running Red Hat Linux 8.0 and samba 2.2.7, via rpms, in an all NT/Win2k workstation and NT server domain. The primary purpose of this box is to be an email server, so there are a few things that will not apply if you are only interested in file storage. Also, I do not cover printers here. Tip: If you have Win9x clients, you may need to read up on encrypted vs non-encrypted connections.

1) Edit smb.conf

For me, this is located in /etc/samba. Here is mine. Note that every option available is not shown and this should be considered an example. Things in bold are items I have added to the default file. Use 'man smb.conf' for much more information.

Remember to replace:
WORKGROUP = workgroup or domain group name
MACHINE_NAME = name this machine will be browseable by
PDC & BDC = primary domain controller and backup domain controller name
any ip number = your ip scheme or related machine's ip

# This is the main Samba configuration file. You should read the
# smb.conf(5) manual page in order to understand the options listed
# here. Samba has a huge number of configurable options (perhaps too
# many!) most of which are not shown in this example
#
# Any line which starts with a ; (semi-colon) or a # (hash) 
# is a comment and is ignored. In this example we will use a #
# for commentry and a ; for parts of the config file that you
# may wish to enable
#
# NOTE: Whenever you modify this file you should run the command "testparm"
# to check that you have not made any basic syntactic errors. 
#
#======================= Global Settings =====================================
[global]
 
# workgroup = NT-Domain-Name or Workgroup-Name
   workgroup = WORKGROUP
 
# Netbios name
   netbios name = MACHINE_NAME
 
# server string is the equivalent of the NT Description field
   server string = Samba Server
 
# This option is important for security. It allows you to restrict
# connections to machines which are on your local network. The
# following example restricts access to two C class networks and
# the "loopback" interface. For more examples of the syntax see
# the smb.conf man page
   hosts allow = 192.168.1. 192.168.2. 127.
 
# Uncomment this if you want a guest account, you must add this to /etc/passwd
# otherwise the user "nobody" is used
;  guest account = pcguest
 
# this tells Samba to use a separate log file for each machine
# that connects
   log file = /var/log/samba/log.%m
 
# Put a capping on the size of the log files (in Kb).
   max log size = 10
 
# logging level 0 thru 3, none to most
   log level = 1
 
# Security mode. Most people will want user level security. See
# security_level.txt for details.
   security = domain
 
# Use password server option only with security = server
# The argument list may include:
#   password server = My_PDC_Name [My_BDC_Name] [My_Next_BDC_Name]
# or to auto-locate the domain controller/s
#   password server = *
   password server = PDC BDC
 
# Winbind config. My additions.
   winbind separator = +
   winbind uid = 10000-20000
   winbind gid = 10000-20000
   winbind cache time = 15
   winbind enum users = yes
   winbind enum groups = yes
   template homedir = /home/%U
   template shell = /bin/bash
   # this is the key, otherwise Exim sees Domain+Username and fails
   winbind use default domain = yes
 
# Password Level allows matching of _n_ characters of the password for
# all combinations of upper and lower case.
;  password level = 8
;  username level = 8
 
# You may wish to use password encryption. Please read
# ENCRYPTION.txt, Win95.txt and WinNT.txt in the Samba documentation.
# Do not enable this option unless you have read those documents
   encrypt passwords = yes
   smb passwd file = /etc/samba/smbpasswd
 
# The following is needed to keep smbclient from spouting spurious errors
# when Samba is built with support for SSL.
;   ssl CA certFile = /usr/share/ssl/certs/ca-bundle.crt
 
# The following are needed to allow password changing from Windows to
# update the Linux system password also.
# NOTE: Use these with 'encrypt passwords' and 'smb passwd file' above.
# NOTE2: You do NOT need these to allow workstations to change only
#        the encrypted SMB passwords. They allow the Unix password
#        to be kept in sync with the SMB password.
   unix password sync = Yes
   passwd program = /usr/bin/passwd %u
   passwd chat = *New*password* %n\n *Retype*new*password* %n\n *passwd:*all*authentication*tokens*updated*successfully*
 
# You can use PAM's password change control flag for Samba. If
# enabled, then PAM will be used for password changes when requested
# by an SMB client instead of the program listed in passwd program.
# It should be possible to enable this without changing your passwd
# chat parameter for most setups.
 
   pam password change = yes
 
# Unix users can map to different SMB User names
;  username map = /etc/samba/smbusers
 
# Using the following line enables you to customize your configuration
# on a per machine basis. The %m gets replaced with the netbios name
# of the machine that is connecting
;   include = /etc/samba/smb.conf.%m
 
# This parameter will control whether or not Samba should obey PAM's
# account and session management directives. The default behavior is
# to use PAM for clear text authentication only and to ignore any
# account or session management. Note that Samba always ignores PAM
# for authentication in the case of encrypt passwords = yes
 
  obey pam restrictions = yes
 
# Most people will find that this option gives better performance.
# See speed.txt and the manual pages for details
   socket options = TCP_NODELAY SO_RCVBUF=8192 SO_SNDBUF=8192
 
# Configure Samba to use multiple interfaces
# If you have multiple network interfaces then you must list them
# here. See the man page for details.
;   interfaces = 192.168.12.2/24 192.168.13.2/24 
 
# Configure remote browse list synchronization here
#  request announcement to, or browse list sync from:
#	a specific host or from / to a whole subnet (see below)
   remote browse sync = 192.168.1.1 192.168.1.2
# Cause this host to announce itself to local subnets here
   remote announce = 192.168.1.255
 
# Browser Control Options:
# set local master to no if you don't want Samba to become a master
# browser on your network. Otherwise the normal election rules apply
   local master = no
 
# OS Level determines the precedence of this server in master browser
# elections. The default value should be reasonable
;   os level = 33
 
# Domain Master specifies Samba to be the Domain Master Browser. This
# allows Samba to collate browse lists between subnets. Don't use this
# if you already have a Windows NT domain controller doing this job
;   domain master = yes 
 
# Preferred Master causes Samba to force a local browser election on startup
# and gives it a slightly higher chance of winning the election
;   preferred master = yes
 
# Windows Internet Name Serving Support Section:
# WINS Support - Tells the NMBD component of Samba to enable it's WINS Server
;   wins support = yes
 
# WINS Server - Tells the NMBD components of Samba to be a WINS Client
#	Note: Samba can be either a WINS Server, or a WINS Client, but NOT both
   wins server = 192.168.1.1
 
# WINS Proxy - Tells Samba to answer name resolution queries on
# behalf of a non WINS capable client, for this to work there must be
# at least one	WINS Server on the network. The default is NO.
;   wins proxy = yes
 
# All NetBIOS names must be resolved to IP Addresses
# 'Name Resolve Order' allows the named resolution mechanism to be specified
# the default order is "host lmhosts wins bcast". "host" means use the unix
# system gethostbyname() function call that will use either /etc/hosts OR
# DNS or NIS depending on the settings of /etc/host.config, /etc/nsswitch.conf
# and the /etc/resolv.conf file. "host" therefore is system configuration
# dependant. This parameter is most often of use to prevent DNS lookups
# in order to resolve NetBIOS names to IP Addresses. Use with care!
# The example below excludes use of name resolution for machines that are NOT
# on the local network segment
# - OR - are not deliberately to be known via lmhosts or via WINS.
 name resolve order = wins lmhosts host bcast
 
# DNS Proxy - tells Samba whether or not to try to resolve NetBIOS names
# via DNS nslookups. The built-in default for versions 1.9.17 is yes,
# this has been changed in version 1.9.18 to no.
   dns proxy = no 
 
# Case Preservation can be handy - system default is _no_
# NOTE: These can be set on a per share basis
  preserve case = yes
  short preserve case = yes
# Default case is normally upper case for all DOS files
  default case = lower
# Be very careful with case sensitivity - it can break things!
  case sensitive = no
 
#============================ Share Definitions ==============================
# Note: Even though I am setting up this box for email only we want home
# directories so power users can use .procmailrc and .forward files
[homes]
   comment = Home Directories
   browseable = no
   writable = yes
# Note: This causes problems when using winbind and security=domain
#   valid users = %S
# Use domain+winbind separator+user instead
   valid users = %D+%S
   create mode = 0664
   directory mode = 0775
# If you want users samba doesn't recognize to be mapped to a guest user
#    map to guest = Guest
 
 
# This one is useful for people to share files
# Note: We will keep it for testing purposes at the very least.
[tmp]
   comment = Temporary file space
   path = /tmp
   read only = no
   public = yes
#======== End smb.conf ====

Don't forget to run testparm smb.conf after editing this file to check for errors.

2) Edit /etc/pam.d/samba

This is where you define the login security for clients browsing over the network. If you want to also use the PDC to authenticate console logins, you'll need to also add to the login file. For help on this, look on the internet for examples. Also read up on PAM before messing with it, as you can cause your system not to let you login.

Example pam.d/samba file:

#%PAM-1.0
auth       required     pam_nologin.so
auth       required     pam_stack.so service=system-auth
auth       required     /lib/security/pam_winbind.so
account    required     /lib/security/pam_winbind.so
account    required     pam_stack.so service=system-auth
session    required     /lib/security/pam_mkhomedir.so skel=/etc/samba/skel umask=0022
session    required     pam_stack.so service=system-auth
password   required     pam_stack.so service=system-auth

Note the use of pam_mkhomedir.so. Adding this will automatically create a /home/username diretory if none exists the first time a person tries to access this machine. Of course they must first have a valid account. Using the skel option specifies a directory of files you want copied to the user's directory as it is created.

For imap authentication via winbind I use this for my pam.d/imap file:

#%PAM-1.0
auth       required     /lib/security/pam_winbind.so
auth       required     /lib/security/pam_stack.so service=system-auth
account    required     /lib/security/pam_winbind.so
account    required     /lib/security/pam_stack.so service=system-auth
session    required     /lib/security/pam_mkhomedir.so umask=0022

Note the use of pam_mkhomedir.so here too. Because this is an email server and I am using IMAP, instead of POP3, the users must have a home directory for their IMAP folders. Since we can't determine if they'll connect via samba or imap the first time, I've added it both places. This also requires a special build of imap. See the "Setting up IMAP" section.

I also [had] winbind in my pam.d/system-auth file:

#%PAM-1.0
# This file is auto-generated.
# User changes will be destroyed the next time authconfig is run.
auth        required      /lib/security/pam_env.so
auth        sufficient    /lib/security/pam_winbind.so
auth        sufficient    /lib/security/pam_unix.so likeauth nullok use_first_pass
auth        required      /lib/security/pam_deny.so
...

However, I can't remember exactly why. Tip: Always take good notes as you install. Update: Now that I've gone back, r&r'ed the machine, and not used this things are still working fine. Also I've read more on PAM and basically if you put winbind in system-auth then anything that calls it can use winbind. See the lines in the pam.d/ files that say:
... /lib/security/pam_stack.so service=system-auth
So basically you can just put the winbind calls in system-auth and most things will be set to use it. However, I only want to give access to a few things with winbind, so I'm not using it.

If you want to allow users to login physically at the box, you need a pam.d/login file like this:

#%PAM-1.0
auth       required     /lib/security/pam_securetty.so
auth       sufficient   /lib/security/pam_winbind.so
auth       sufficient   /lib/security/pam_unix.so use_first_pass
auth       required     /lib/security/pam_stack.so service=system-auth
auth       required     /lib/security/pam_nologin.so
account    required     /lib/security/pam_stack.so service=system-auth
account    sufficient   /lib/security/pam_winbind.so
password   required     /lib/security/pam_stack.so service=system-auth
session    required     /lib/security/pam_stack.so service=system-auth
session    optional     /lib/security/pam_console.so

Note the use of use_first_pass. This will eliminate a double prompt for the password. Also, I used sufficient instead of required. See the man page on PAM for more information.

3) Edit /etc/nsswitch.conf

This tells the system where to lookup passwords and groups, among other things. Basically it will first look at the local files and then query the PDC/BDC.

Example partial nsswitch.conf file:

# /etc/nsswitch.conf
#
# An example Name Service Switch config file. This file should be
# sorted with the most-used services at the beginning.
#
# The entry '[NOTFOUND=return]' means that the search for an
# entry should stop if the search in the previous entry turned
# up nothing. Note that if the search failed due to some other reason
# (like no NIS server responding) then the search continues with the
# next entry.
 
passwd:     files winbind
shadow:     files winbind
group:      files winbind

4) Setup firewall ports

Allow access to ports 137, 138, 139, and 443 on tcp and udp.

When you are first setting this up and testing it, ie before the real users get to it, I recommend disabling firewalling all together. No use trying to figure out what appears to be a samba problem when it is your firewall.

5) Edit /etc/hosts so that your PDC & BDC are defined

If WINS can resolve the names you should not have to do this. However... I use it. Example hosts file:

# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1               localhost.localdomain localhost
192.168.1.1             pdc.domain.com        pdc
192.168.1.2             bdc.domain.com        bdc
#==== end hosts ====

Remember to replace "pdc.domain.com pdc" with the primary domain controller information and correct ip. Same for the BDC.

6) Join the domain

Add the machine on the NT PDC using Server Manager and the "netbios name" supplied in smb.conf.

Do this by typing:
smbpasswd -j DOMAIN -r PDC -U user_name

Where:
DOMAIN = the domain name
PDC = the primary controller name
user_name = a user on the PDC that has the rights to add machines, such as Administrator

You will be prompted for a password.

7) Start the samba daemons

At the command line type:
service smb start

Now you can use ps to see if they are running:
ps -A | grep smbd

8) Start winbind

At the command line type:
service winbind start

9) Test

Basic tests are noted here. The O'Rielly book, Using Samba, has a good troubleshooting section.

  1. To see shares, name, ip, and more at the command line type:
    smbclient -L localhost -U%
  2. To test connection to a share type as a user defined on the NT PDC:
    smbclient '\\machine_name\share_name' -U user_name
    Where:
    machine_name = the netbios name of the machine
    share_name = a share name on the machine
    user_name = a user on the NT PDC

    You will be prompted for a password, enter the one associated with the user name.
  3. To see if winbind is seeing users from the NT PDC type:
    wbinfo -u
    Tip: if 'wbinfo -m', 'wbinfo -t', and 'wbinfo -u username' work, but this does not try:
    wbinfo -a user_name%password
    If that is successful and a subsequent 'wbinfo -u' works, then store that name and password with:
    wbinfo -A user_name%password
    Tip on password: if you have a special character, like &, in it then you need to use a backslash (\) before the character. Example:
    nate&me should be input as nate\&me
    Note on storing password: Obviously this should be a user name who's password does not change or you will need to restore the new password each time it is run. Luckily this user does not have to be an administrator on the NT box. Interestingly I didn't encounter this situation until v2.2.7.
  4. Try getent as well. Example:
    getent passwd
    getent group
  5. Try accessing the machine from a Windows computer using Network Neighborhood.

If you can do everything at the Linux machine but cannot access it from a Windows machine, double-check your firewall rules.

10) Starting on boot

chkconfig smb on
chkconfig winbind on