Sharing Files


When you collaborate on a project, you need some way to pass files between group members. The direct approach is to send the file as an e-mail attachment. Although this does give them a copy, it does not provide updates-people may pass around very old copies of files. This approach also does not incorporate changes. In most cases, there will need to be an owner whose responsibility is to collect changes and incorporate them into a single document. This may work well with a few people, but it does not scale well to a dozen people working concurrently.

Note 

Other downsides to using e-mail for sharing files concerns disk space and convenience. Most corporations and service providers limit the mail queue size. If the queue fills, then no more e-mail can be received. Also, transferring large attachments can be time consuming and searching for a specific attachment is usually inconvenient.

Another option is to place files on an FTP or web server. This gives a central source for distribution, allowing people to view recent changes to files, but web servers don't readily allow feedback and active collaboration, and FTP servers are not known for security.

The best option is to share a file system's directory among computers. This way, everyone can see all files in one common location. Everyone can also see all changes and everyone can make changes as needed. For Unix and Linux systems, there is NFS for file sharing, but for compatibility with Windows users, you'll probably want SAMBA.

Enabling NFS

Under Linux and most Unix operating systems, the network file system (NFS) is the common way to share directories. With other Unix and Linux operating systems, NFS is part of the core installation. But with Ubuntu, you need to install it as a package. There are three main components required by NFS:

  • portmap-This package provides support for remote procedure calls (RPC) and is used by NFS. You don't need to install portmap by itself-the apt-get commands for the other two components will install portmap as a requirement.

  • nfs-common-Although portmap provides support for RPC function, this package actually provides the RPC functions for NFS. This package is required for NFS clients and servers. It provides basic RPC functions like file locking and status. If you only need to install an NFS client (meaning you will mount a directory exported by some other server), then you can use: sudo apt-get install nfs-common.

    Note 

    Installing nfs-common will generate an error message, "Not starting NFS kernel daemon: No exports." This is expected since it is not configured. To configure it, see the section titled "Acting as an NFS Server."

  • nfs-kernel-server-This package adds kernel modules so you can actually export a directory for use by a remote host; with this package, you get a server. You can install it using: sudo apt-get install nfs-kernel-server. This brings in portmap and nfs-common as required packages.

NFS is a great collaboration tool because entire file systems can be shared transparently. Everyone sees the same files and file changes are immediately accessible by everyone. The main limitation is operating system support. Although NFS exists for Linux, BSD, HP-UX, AIX, Solaris, BeOS, Mac OS X, and even OS/2, Windows does not natively include it. If you need to share files with Windows users, skip to the next section on SAMBA.

Tip 

If you want to use NFS with Windows, consider installing the Windows Services for UNIX (http://www.microsoft.com/technet/interopmigration/unix/sfu/). This free product from Microsoft includes NFS server and client support.

Acting as an NFS Client

Mounting a remote file system with NFS is really easy. Just as the mount command can be used to access a hard drive, CD-ROM, or other block device, it can be used to mount a remote file system. You just need three items: the server's name, the directory name on the server that is being exported, and the mount point on your local system (a directory) for the connection. For example, to mount the directory /home/project from the server sysprj1 and place it at /mnt/project on your local computer, you would use:

 sudo mkdir /mnt/project # to make sure it exists sudo mount -t nfs sysprj1:/home/project /mnt/project 

Now, all the files under /home/project on the host sysprj1 are accessible from the local directory /mnt/project. The access is completely transparent-anything you can do on your local file system can be done over this NFS mount.

Tip 

Access restrictions are set by the NFS server and follow the Unix permissions. If you find that you cannot access the directory after mounting it, check the permissions with ls -l. If you do not have permission, then talk to the administrator for the NFS server.

If you don't know the name of the exported directory, NFS enables you to browse the list of exported partitions using the showmount -e command. This lists the directories and list of clients that can access it. The client list returned from the server can be an entire domain (for example, *.local.net) or a list of clients.

 $ showmount -e sysprj1 /home/projects *.local.net /media/cdrom *.local.net 

When you are done with the mounted partition, you can remove it using sudo umount /mnt/project.

For short-term access, you will probably want to use mount and umount to access the directory as needed. For long-term collaboration, you can add the entry in /etc/fstab. For example:

 sysprj1:/home/project   /mnt/project   nfs    defaults    0   0 

Having the entry in /etc/fstab will make sure the directory is mounted every time you reboot. You can also use sudo mount /mnt/project (specifying only the mount point) as a shortcut since mount consults /etc/fstab when determining devices.

Warning 

NFS has one huge limitation. If the server goes down then all file accesses to the network partition will hang-up to hours-before failing. The hang-up is due to network timeouts and retries. If your connection to the server is unstable, then don't use NFS.

Acting as an NFS Server

NFS servers export directories for use by NFS clients. This is a two-step process. First, you need to create a file called /etc/exports. This file contains a list of directories to export and clients that are permitted to access the directories. Special access permissions can also be specified such as ro for read-only, rw for read-write, and sync for synchronous writes. An example /etc/exports file is given in Listing 6-1.

Tip 

There are many more options besides ro, rw, and sync. See the man page for exports (man 5 exports) for the full list of options.

Listing 6-1: Example of a /etc/exports File

image from book
     /home/project            *.local.net(rw,sync)     /home/solo_project       host.local.net(rw,sync)     /media/cdrom             *.local.net(ro,async) 
image from book

Note 

The NFS server will not start if /etc/exports is missing or contains no exported directories. The default file contains only a few comments, so the server will not start. After you create your first entries, you will need to start the server. The easy way to start it is with the command sudo /etc/init.d/nfs-kernel-server start.

After modifying the /etc/exports file, you need to tell the NFS server to actually export the entries.

 sudo exportfs -r # re-export all entries in /etc/exports 

The exportfs command can also be used for other tasks:

  • List the current export table-Run exportfs without any parameters.

  • Export a specific directory once-This is useful if the export is not intended to be permanent (/etc/exports is really for permanent mounts). You will need to specify options, and the list of clients is specified before the directory. For example:

     sudo exportfs -o ro,async '*.local.net:/media/cdrom' 
  • Un-export directory-If the entry is still listed in /etc/exports, then the removal is temporary; the mount will be re-exported the next time you reboot or restart the NFS server.

     sudo exportfs -u '*.local.net:/media/cdrom' 
Tip 

Add -v to any of the exportfs commands (for example, exportfs -v -r) to verbosely list additional information.

You can export anything that is mounted. This includes CD-ROM drives, USB thumb drives, and even mounted NFS partitions from other servers! Although you cannot export single files or block devices, you can export the entire /dev directory (not that you would want to).

Warning 

NFS offers no security, encryption, or authentication. Furthermore, established NFS connections can be easily hijacked. NFS is fine for most internal, corporate networks and for use within your home, but don't use it to share files across the Internet.

Exchanging Files with SAMBA

Although NFS is useful for collaborating with Unix and Linux systems, it is not ideal for sharing directories with Windows users. As mentioned in Chapter 3, SAMBA allows Linux to use the SMB protocol and communicate with Windows systems. Chapter 3 showed how to share printers, but SAMBA can also be used to share directories. First, if you have not done it already, install the SAMBA server: sudo apt-get install samba. You will need to edit /etc/samba/smb.conf and configure your workgroup. This configuration file contains many other options that you will probably want to review. For example, you can bind the SAMBA server to a specific network interface, control client logging, and configure alternate login credentials-these are documented with comments found in the file. After configuring the server, you should restart it: sudo /etc/init.d/samba restart.

There are two ways to use SAMBA for collaboration. It can be a server that shares directories with Windows users, or a client that receives directories exported from Windows servers.

Sharing a Directory with Windows

The /etc/samba/smb.conf file comes with an entry that allows you to share every user's home directory. Search the configuration file for the "[homes]" section and uncomment it (remove the ; before each line). The section should look like:

 [homes]    comment = Home Directories    browseable = no 

This defines a Windows service (called a share) that can be accessed using \\server\username, where server is the name of your Ubuntu system and username is an account found under /home/username. There are other options that can be uncommented in order to restrict access (valid users = %S and writable = no) and set file permissions.

If you want to export a specific directory then you will need to create your own section in /etc/samba/smb.conf. Listing 6-2 gives an example for exporting the CD-ROM and a projects directory.

Listing 6-2: Sample Export Directories for /etc/samba/smb.conf

image from book
 # Export the CD-ROM. # The Windows system will use \\server\cdrom\ to access it. [cdrom]   comment = CD-ROM drive   path = /media/cdrom # Export a group project directory. # The Windows system will use \\server\groupproject\ to access it. [groupproject]   comment = Group Project directory   path = /home/project   read only = no   valid users = nealk, @team # nealk and group "team" have access 
image from book

Tip 

Although the default Windows installation cannot access NFS partitions, SAMBA can export a mounted NFS partition to Windows users.

image from book
Learning to SAMBA

Although SAMBA is very powerful, it is not very easy to manage if you are new to it. If SAMBA does not immediately share partitions, then be prepared to devote an hour or more to debugging. Common problems that I usually check (in order) before going into "Search the web for solutions" mode:

  • Is the smb.conf file correct? Use the testparms program to check for problems. Not all problems are critical, but big problems will be identified. (Some warnings come from default settings in the configuration file.)

  • Is the share name spelled correctly? I have spent hours chasing down problems only to find typos in the smb.conf share name or on the Windows side.

  • If you are using a "valid user" option for the share, you may need to use smbpasswd to create a user account. SAMBA does not consult /etc/passwd. Instead, it uses its own password database found in /var/lib/samba. The command sudo smbpasswd -a username adds a new user to the database, and smbpasswd (as the user) changes the password.

  • Older versions of Windows (for example, Windows 95, 98, and NT) use plain- text passwords. Later versions use encrypted passwords. Check the "encrypted password" value in smb.conf and make sure it matches the system you are supporting. Unfortunately, SAMBA cannot support old and new systems at the same time unless they all use the same encrypted (or unencrypted) password system.

  • If the Windows system can read the partition but not write, then check the permissions on the directory. The SAMBA account may not have write-access.

If all else fails, refer to the FAQ list HOWTO guides at http://www.samba.org and the Ubuntu Guide at http://ubuntuguide.org/.

image from book

Accessing a Windows Directory

There are many different ways for SAMBA to access a Windows directory. The main things you need are the Windows system name and the share. The command smbclient -L can be used to list the public shares on a system:

 $ smbclient -L wserver Password: [hit enter with no password] Domain=[WSERVER] OS=[Windows 5.1] Server=[Windows 2000 LAN Manager]    Sharename       Type      Comment    ---------       ----      -------    IPC$            IPC       Remote IPC    print$          Disk      Printer Drivers    Big             Disk    Printer         Printer   Brother HL-1850/70N BR-Script3    ADMIN$          Disk      Remote Admin 

The smbclient command can be used as an FTP-like client for accessing the Windows share. In this example, smblcient //wserver/big will open an FTP client for accessing the Big share.

For remote backups, I prefer to use smbtar. This command enables you to use remotely archive (or restore) files in a share. The backup is saved to a TAR file. For example, to back up the Big share from host Wserver, you can use either:

 smbtar -t archive.tar -s wserver -x big                   # regular archive smbtar -t - -s wserver -x big | gzip -9 > archive.tgz  # compressed archive 

For restores, add in the -r parameter:

smbtar -r -t archive.tar -s wserver -x big # regular archive zcat archive.tgz | smbtar -r -t - -s wserver -x big # compressed archive

Warning 

This is good for backing up user files, but it is not necessarily a full system backup. For example, Windows XP and Me will not allow SMB access to the system directory or registry.

Although smbclient and smbtar enable you to access files, they do not allow you to actually mount the share. For this, the smbfs package is needed.

 sudo apt-get install smbfs 

The smbfs package provides an SMB file system driver and the smbmount command. This allows you to transparently mount a share and use files concurrently with other people.

 sudo mkdir /mnt/smb sudo smbmount //wserver/big /mnt/smb 
Tip 

The smbmount command is actually a wrapper around the mount command. You can also use sudo mount -t smb.

As with other mounted directories, sudo umount /mnt/smb will remove the mount.



Hacking Ubuntu
Hacking Ubuntu: Serious Hacks Mods and Customizations (ExtremeTech)
ISBN: 047010872X
EAN: 2147483647
Year: 2004
Pages: 124
Authors: Neal Krawetz

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net