Chapter 23

Sharing Files and Printers

In the early days of computing, file and printer sharing was pretty much impossible because of the lack of good networking standards and interoperability. If you wanted to use a printer connected to another computer, you had to save the file to a floppy disk and walk to the other computer. Sometimes people do practically the same thing today, using a USB thumb drive or emailing the file. However, there are better ways.

Both file and printer sharing are important because it is not unusual for someone to own more than one computer. Whether you want to share photographs among various computers or have a central repository available for collaboration, file sharing is an important part of the information age. Alongside this is the need to be able to share printers; after all, people do not want to have to plug and unplug a computer to a printer just so they can print out a quick letter.

Whatever your reasons for needing to share files and printers across a network, you find out how to do both in this chapter. This chapter shows you how you can share files using the popular UNIX NFS protocol and the more Windows-friendly Samba system. The chapter covers both graphical and command-line tools, so you should find something to suit the way you work.

Caution

By default, Ubuntu ships with all its network ports blocked. That is, it does not listen to any requests on any network ports when it is first installed. To configure the firewall, use Uncomplicated Firewall (UFW) as described in Chapter 20, “Securing Your Machines.”

Using Network File System

Network File System (NFS) is a protocol developed by Sun Microsystems that enables computers to use a remote file system as if it were a real part of the local machine. A common use of NFS is to allow users’ /home directories to appear on every local machine they use, thus eliminating the need to have physical home directories. This opens up hot desking and other flexible working arrangements, especially because no matter where the users are, their /home directories follow them around.

Another popular use for NFS is to share binary files between similar computers. If you have a new version of a package that you want all machines to have, you have to upgrade only on the NFS server, and all hosts running the same version of Ubuntu have the same upgraded package.

Installing and Starting or Stopping NFS

NFS is not installed by default on Ubuntu, so you need to install the nfs-kernel-server package. NFS itself consists of several programs that work together. One is portmap, which maps NFS requests to the correct daemon. Two others are nfsd, which is the NFS daemon, and mountd, which controls the mounting and unmounting of file systems.

Ubuntu automatically adds NFS to the system startup scripts, so it will always be available after you have configured it. To check this, use the command sudo /etc/init.d/nfs-kernel-server status, and you see that the service is running. If you need to manually start the NFS server, use the following command:

matthew@seymour:~$ sudo systemctl start nfs

In this example, NFS has been started. Use stop to stop the service or restart to restart the service. This approach to controlling NFS is handy, especially after configuration changes have been made. See the next section on how to configure NFS support on your Ubuntu system.

NFS Server Configuration

You can configure the NFS server by editing the /etc/exports file. This file is similar to the /etc/fstab file in that it is used to set the permissions for the file systems being exported. The entries look like this:

/file/system yourhost(options) *.yourdomain.com(options) 192.168.0.0/24(options)

This shows three common clients to which to share /file/system. The first, yourhost, shares /file/system to just one host. The second, .yourdomain.com, uses the asterisk (*) as a wildcard to enable all hosts in yourdomain.com to access /file/system. The third share enables all hosts of the Class C network 192.168.0.0 to access /file/share. For security, it is best not to use shares such as the last two across the Internet because all data will be readable by any network the data passes by.

Table 23.1 shows some common options.

Table 23.1 /etc/fstab Options

Option

Purpose

Rw

Gives read and write access

Ro

Gives read-only access

async

Writes data when the server, not the client, feels the need

Sync

Writes data as it is received

The following is an example of an /etc/exports file:

# /etc/exports: the access control list for filesystems which may be exported
#               to NFS clients.  See exports(5).
/home/matthew 192.168.0.0/24(rw,no_root_squash)

This file exports (makes available) /home/matthew to any host in 192.168.0.* and allows users to read from and write to /home/matthew.

After you have finished with the /etc/exports file, the following command exports all the file systems in the /etc/exports file to a list named xtab under the /var/lib/nfs directory, which is used as a guide for mounting when a remote computer asks for a directory to be exported:

matthew@seymour:~$ sudo exportfs -a

The -r option, which stands for re-export, tells the command to reread the entire /etc/exports file and (re)mount all the entries. You can also use the exportfs command to export specific files temporarily. Here’s an example using exportfs to export a file system:

matthew@seymour:~$ /usr/sbin/exportfs -o async yourhost:/usr/tmp

This command exports /usr/tmp to yourhost with the async option.

Be sure to restart the NFS server after making any changes to /etc/exports. If you prefer, you can use Ubuntu’s shares-admin graphical client to set up NFS from the GUI. Search for “personal file sharing” in the Dash to start. Fill in the required information, and off you go. You still need to install some packages on Ubuntu for this to work (the same ones mentioned earlier in this chapter, in the “Installing and Starting or Stopping NFS” section and in the next section, “NFS Client Configuration”).

NFS Client Configuration

To configure your host as an NFS client (to acquire remote files or directories), you need to ensure that you have the nfs-common package installed to be able to access NFS shares. After you’ve installed this, edit the /etc/fstab file as you would to mount any local file system. However, instead of using a device name to be mounted (such as /dev/sda1), enter the remote hostname and the desired file system to be imported. For example, an entry might look like this:

# Device             Mount Point  Type  Options       Freq Pass
yourhost:/home/share  /export/share   none   nfs  0    0

You can also use the mount command, as root, to quickly attach a remote directory to a local file system by using a remote host’s name and exported directory, as in this example:

matthew@seymour:~$ sudo mount -t nfs 192.168.2.67:/music /music

After you press Enter, the entire remote directory appears on your file system. You can verify the imported file system by using the df command, as follows:

matthew@seymour:~$ df
Filesystem          1k-blocks     Used Available Use% Mounted on
/dev/hda2            18714368  9642600   8121124  55% /
/dev/hda1               46636    13247     30981  30% /boot
none                   120016        0    120016   0% /dev/shm
192.168.2.67:/music  36875376 20895920  14106280  60% /music

Make sure the desired mount point exists before using the mount command. When you are finished using the directory (perhaps for copying backups), you can use the umount command to remove the remote file system. Note that if you specify the root directory (/) as a mount point, you cannot unmount the NFS directory until you reboot (because Linux complains that the file system is in use).

Putting Samba to Work

Samba uses the Server Message Block (SMB) protocol to enable the Windows operating system (or any other operating system) to access Linux files. Using Samba, you can make your Ubuntu machine look just like a Windows computer to other Windows computers on your network—without needing to install Windows on your PC.

Although Samba is a complex program, setting it up and using it do not have to be difficult. There are many options, which accounts for some of Samba’s complexity. Depending on what you want, Samba’s use can be as easy or as difficult as you would like it to be.

Fortunately, Ubuntu includes a very easy way to access files on a Windows network share by default. To start, find the Network entry in the menu at the left side of the Settings application. If there are other computers on the network with shared files using Windows or Samba, you will see a Windows Network icon that, when double-clicked, shows you all Windows domains or workgroups on your network. Just double-click any computer icon there to access shares and files.

To share from your Ubuntu desktop, right-click a folder you own while using the file browser (most likely one in your /home/username directory) and select Local Network Share. You are given a chance to confirm your desire to share the folder and give it a name. Sharing gives others the permission to view—but not necessarily to create or delete—files, although those permissions are also available from this menu. If you do not have the Windows sharing service already installed on your computer, Ubuntu prompts you for permission to install it.

Note

Most Ubuntu users do not need the information contained in the rest of this section because installing the sharing service also takes care of the configuration and, as a result, everything should just work.

For greater configurability and control, follow these instructions:

  1. Install Samba and the GUI configuration application by installing these packages: samba and samba-common.

  2. From the applications menu, search for samba and open it.

  3. In the Samba configuration application, open Preferences and then Server Settings. Here you will configure basic settings such as setting Workgroup to the name of your Windows workgroup name on your network and giving a description of your computer to be seen by others on the network. You can change the computer’s network security options for Samba here as well, but the default settings are good.

  4. Also in the Samba configuration application, open Preferences and then Samba Users. Click Add User. You need to enter the details for the following:

    The user on this Ubuntu machine who will be given privileges to use Samba to view files on other network computers

    A Windows username that will be used when accessing this Ubuntu machine’s files from a Windows machine on the network

    A password that will be required with the Windows username when accessing files on this Ubuntu computer from a Windows machine

  5. Restart Samba from the terminal to finish by entering sudo service samba restart.

You can share a folder from the Samba configuration application; set directory, name, and read/write permissions; and access users.

For even greater configurability, read through the editable configuration file at /etc/samba/smb.conf. It is well commented and clear and explained in greater detail later in this chapter.

To learn more about Samba, see www.samba.org. This section delves into the basics of configuring Samba, and you should first read how to manually configure Samba to get an understanding of how the software works.

When you install Samba, also installing the samba-doc and samba-doc-pdf packages is a good idea because they contain extensive documentation in text, PDF, and HTML formats. After you install Samba, you can find this documentation in /usr/share/doc/samba*/doc. If you install Samba using your Ubuntu disc, you can find a large amount of documentation in the directory tree starting at /usr/share/doc/samba-doc or /usr/share/doc/samba-doc-pdf. Altogether, almost 3MB of documentation is included with the source code, in several formats, including PDF, HTML, and text.

After installing Samba, you can either create the file /etc/samba/smb.conf or use the smb.conf file supplied with Samba, which is located by default under the /etc/samba directory with Ubuntu. You can find nearly a dozen sample configuration files in the /usr/share/doc/samba*/examples directory.

Note

Depending on your needs, smb.conf can be a simple file of fewer than 20 lines or a huge file spanning many pages of text. If your needs are complex, I suggest that you browse through the Samba website at www.samba.org.

Manually Configuring Samba with /etc/samba/smb.conf

The /etc/samba/smb.conf file is broken into sections. Each section provides a description of the resource that is to be shared (called a Samba share or just a share) and should be titled appropriately. The three special sections are as follows:

[global]—Establishes the global configuration settings (defined in detail in the smb.conf man page and Samba documentation, found in the /usr/share/doc/samba/docs directory)

[homes]—Shares users’ /home directories and specifies directory paths and permissions

[printers]—Handles printing by defining shared printers and printer access

Each section in your /etc/samba/smb.conf configuration file should be named for the resource being shared. For example, if the resource /usr/local/programs is being shared, you could call the section [programs]. When Windows sees the share, it is called by whatever you name the section (programs in this example). The easiest and fastest way to set up this share is with the following example from smb.conf:

[programs]
path = /usr/local/programs
writeable = true

This bit shares the /usr/local/programs directory with any valid user who asks for it and makes that directory writable. It is the most basic share because it sets no limits on the directory.

Here are some parameters you can set in the sections:

Requiring a user to enter a password before accessing a shared directory

Limiting the hosts allowed to access the shared directory

Altering permissions users are allowed to have on the directory

Limiting the time of day during which the directory is accessible

The possibilities are almost endless. Any parameters set in the individual sections override the parameters set in the [global] section. The following section adds a few restrictions to the [programs] section:

[programs]
path = /usr/local/programs
writeable = true
valid users = mhelmke
browseable = yes
create mode = 0700

Note

You can spell it as writeable or writable; either variant will work, and both spellings are used in this chapter.

The valid users entry in this example limits userid to just mhelmke. All other users can browse the directory because of the browseable = yes entry, but only mhelmke can write to the directory. Any files created by ahudson in the directory give ahudson full permissions, but no one else will have access to these files. This is the same as setting permissions with the chmod command. Again, there are numerous options, so you can be as creative as you want when developing sections.

Setting Global Samba Behavior with the [global] Section

The [global] section establishes configuration settings for all of Samba. If a given parameter is not specifically set in another section, Samba uses the default setting in the [global] section. The [global] section also sets the general security configuration for Samba. The [global] section is the only section that does not require the name in brackets.

Samba assumes that anything before the first bracketed section not labeled [global] is part of the global configuration. (Using bracketed headings in /etc/samba/smb.conf makes your configuration file more readable.) The following sections discuss common Samba settings to share directories and printers. You will then see how to test your Samba configuration.

Sharing Home Directories Using the [homes] Section

The [homes] section shares Ubuntu /home directories for the users. The /home directory is shared automatically when a user’s Windows computer connects to the Linux server holding the /home directory. The one problem with using the default configuration is that the users see all the configuration files (such as .profile and other files with a leading period in the filename) that they normally wouldn’t see when logging on through Linux. One quick way to avoid this is to include a path option in the [homes] section. To use this solution, any user who requires a Samba share of his or her /home directory needs a separate “home directory” to act as the Windows /home directory.

This setting specifies that the directory named share under each user’s directory is the shared Samba directory. The corresponding manual smb.conf setting to provide a separate “home directory” looks like this:

[homes]
        comment = Home Directories
        path = /home/%u/share
        valid users = %S
        read only = No
        create mask = 0664
        directory mask = 0775
        browseable = No

If you have a default [homes] section, the share shows up in the user’s Network Neighborhood as the user’s name. When the user connects, Samba scans the existing sections in smb.conf for a specific instance of the user’s /home directory. If there is not one, Samba looks up the username in /etc/passwd. If the correct username and password have been given, the home directory listed in /etc/passwd is shared out at the user’s /home directory. Typically, the [homes] section looks like this:

[homes]
browseable = no
writable = yes

The browseable = no entry in this example prevents other users from being able to browse the /home directory and is a good security practice.

This example shares out the /home directory and makes it writable to the user. Here’s how you specify a separate Windows /home directory for each user:

[homes]
browseable = no
writable = yes
path = /path/to/windows/directories
Sharing Printers by Editing the [printers] Section

The [printers] section works much like the [homes] section but defines shared printers for use on your network. If the section exists, users have access to any printer listed in your Ubuntu /etc/printcap file. Note that /etc/printcap is not used if CUPS is installed and printing=CUPS is listed in the [global] section.

As with the [homes] section, when a print request is received, all the sections are scanned for the printer. If no share is found (with careful naming, there should not be a share found unless you create a section for a specific printer), the /etc/printcap file is scanned for the printer name that is then used to send the print request.

For printing to work properly, you must correctly set up printing services on your Ubuntu computer. A typical [printers] section looks like this:

[printers]
comment = Ubuntu Printers
browseable = no
printable = yes
path = /var/spool/samba

/var/spool/samba is a spool path set just for Samba printing.

Testing Samba with the testparm Command

After you have created your /etc/samba/smb.conf file, you can check it for correctness by using the testparm command. This command parses through your /etc/samba/smb.conf file and checks for any syntax errors. If none are found, your configuration file will probably work correctly. It does not, however, guarantee that the services specified in the file will work; it is merely making sure that the file is correctly written.

As with all configuration files, if you are modifying an existing, working file, it is always prudent to copy the working file to a different location and modify that file. Then you can check the file with the testparm utility. The command syntax is as follows:

matthew@seymour:~$ sudo testparm /path/to/smb.conf.back-up
Load smb config files from smb.conf.back-up
Processing section "[homes]"
Processing section "[printers]"
Loaded services file OK.

This output shows that the Samba configuration file is correct, and as long as all the services are running correctly on your Ubuntu machine, Samba should be working correctly. Now copy your old smb.conf file to a new location, put the new one in its place, and restart Samba with the command sudo smbd restart. Your new or modified Samba configuration should now be in place.

Starting, Stopping, and Restarting the smbd Daemon

After your smb.conf file is correctly configured, you might want to start, stop, or restart your Samba server daemon. You can do this with the /usr/sbin/smbdsystemctl samba start command, which starts the Samba server with all the defaults. The most common option you will change in this command is the location of the smb.conf file; you change this option if you don’t want to use the default location /etc/samba/smb.conf. The -s option enables you to change the smb.conf file Samba uses; this option is also useful for testing whether a new smb.conf file actually works. Another useful option is the -l option, which specifies the log file Samba uses to store information.

To start, stop, or restart Samba from the command line, use the following, replacing start with either stop or restart as appropriate:

matthew@seymour:~$ sudo systemctl samba start
Using the smbstatus Command

The smbstatus command reports on the current status of your Samba connections. The syntax is as follows:

/usr/bin/smbstatus [options]

Table 23.2 shows some of the available options.

Table 23.2 smbstatus Options

Option

Result

-b

Brief output

-d

Verbose output

-s /path/to/config

Used if the configuration file used at startup is not the standard one

-u username

Shows the status of a specific user’s connection

-p

Lists current smb processes, which can be useful in scripts

Connecting with the smbclient Command

The smbclient command allows users on other Linux hosts to access your smb shares. You cannot mount a share on your host, but you can use it in a way that is similar to an FTP client. Several options can be used with the smbclient command. The most frequently used is -I followed by the IP address of the computer to which you are connecting. The smbclient command does not require root access to run:

matthew@seymour:~$ smbclient -I 10.10.10.20 -Uusername%password

This gives you the following prompt:

smb: <current directory on share>

From here, the commands are almost identical to the standard UNIX/Linux FTP commands. Note that you can omit a password on the smbclient command line. You are then prompted to enter the Samba share password.

Mounting Samba Shares

There are two ways to mount Samba shares to your Linux host. Mounting a share is the same as mounting an available media partition or remote NFS directory except that the Samba share is accessed using SMB. The first method is to use the standard Linux mount command:

matthew@seymour:~$ sudo mount -t smbfs //10.10.10.20/homes /mount/point -o
username=sandra,dmask=777, fmask=777

Note

You can substitute the IP address for the hostname if your name service is running or the host is in your /etc/hosts file.

The preceding command mounts sandra’s /home directory on your host and gives all users full permissions to the mount. The permissions are equal to the permissions on the chmod command.

The second method produces the same results using the smbmount command, as follows:

matthew@seymour:~$ sudo smbmount //10.10.10.20/homes /mount/point -o
username=sandra,dmask-777, fmask=777

To unmount the share, use the following standard command:

matthew@seymour:~$ sudo umount /mount/point

You can also use these mount commands to mount true Windows client shares to your Ubuntu host. Using Samba, you can configure your server to provide any service Windows can serve, and no one but you will ever know.

Network and Remote Printing with Ubuntu

Chapter 1, “Installing Ubuntu and Post-Installation Configuration,” discusses how to set up and configure local printers and the associated print services. This section covers configuring printers for sharing and access across a network.

Offices all around the world benefit from using print servers and shared printers. It is a simple thing to do and can bring real productivity benefits, even in small settings.

Creating Network Printers

Setting up remote printing service involves configuring a print server and then creating a remote printer entry on one or more computers on your network. This section introduces a quick method of enabling printing from one Linux workstation to another Linux computer on a LAN. You also learn about SMB printing using Samba and its utilities. Finally, this section discusses how to configure network-attached printers and use them to print single or multiple documents.

Enabling Network Printing on a LAN

If a computer with an attached printer is using Ubuntu and you want to set up the system for print serving, use the system-config-printer client to create a new printer, which is available in the menu at System, Administration, Printing.

You need to install any printers you have to the server as discussed in Chapter 1. (Click Add and wait a moment, and it is likely that the printer will be detected automatically; it is probably easy enough that you don’t have to consult the instructions in Chapter 1.)

Next, open Server, Settings and enable Publish Shared Printers Connected to This System. Click OK. Right-click any printer’s icon and select Share. That’s it. Most users will not need the information in the rest of this section, even to enable access to Common UNIX Printing System (CUPS) via the web interface.

To enable sharing manually, edit your /etc/cups/cupsd.conf file. Look for the section that begins with <Location /> and modify it so that it reads as follows:

<Location />
Order Deny,Allow
Deny From All
Allow From 127.0.0.1
Allow From 192.168.0.*
</Location>

This tells CUPS to share your printers across the network 192.168.0.*, for example. Make sure to change this to match your network settings.

Next, you need to look in the same file for the section that starts like this:

Listen localhost:631

Modify it to show this:

Listen 631

This tells CUPS to listen on port 631 for any printer requests.

Server Message Block Printing

Printing to an SMB printer requires Samba, along with its utilities such as the smbclient and associated smbprint printing filter. You can use the Samba software included with Ubuntu to print to a shared printer on a Windows network or set up a printer attached to your system as an SMB printer. This section describes how to create a local printer entry to print to a remote shared printer using SMB.

You usually set up an SMB or shared printer under Windows operating systems through configuration settings using the Control Panel’s Network device. After enabling print sharing, reboot the computer. In the My Computer, Printers folder, right-click the name or icon of the printer you want to share and select Sharing from the pop-up menu. Set the Shared As item and then enter a descriptive shared name, such as HP2100, and a password.

You must enter a shared name and password to configure the printer when running Linux. You also need to know the printer’s workgroup name, IP address, and printer name, and you must have the username and password on hand. To find this information, select Start, Settings, Printers, and then right-click the shared printer’s listing in the Printers window. Select Properties from the pop-up window.

You can use CUPS to configure Samba to use your printers by editing the smb.conf file.

In the [global] section enter the following lines if they are not already there:

...
load printers = yes
printing = cups
printcap name = cups

This tells Samba to use CUPS to provide printing services. Next, you need to create a new section in the smb.conf file at the end of the file, as follows:

[printers]
comment = Use this for All Printers
path = /var/spool/samba
browseable = no
public = yes
guest ok = yes
writable = no
printable = yes
printer admin = root, andrew

This publishes your printers to the network and allows others to connect to them via Windows clients.

Make sure you restart the Samba service using the command shown earlier to cause Samba to pick up the changes to the configuration file.

Using the CUPS GUI

You can use CUPS to create printer queues, get print server information, and manage queues by launching a browser (such as Firefox) and browsing to http://localhost:631. CUPS provides a web-based administration interface, as shown in Figure 23.1.

Images

FIGURE 23-1 Use the web-based CUPS administrative interface to configure and manage printing.

If you click the Administration tab in the browser page, you can start configuring your printers, as shown in Figure 23.2.

Images

FIGURE 23-2 Select the Administration tab to perform printer administration with CUPS.

Creating a CUPS Printer Entry

This section provides a short example of creating a Linux printer entry using the CUPS web-based interface. You can use the CUPS interface to create a printer and device queue type (such as local, remote, serial port, or Internet); then you can enter a device uniform resource identifier (URI), such as lpd://192.168.2.35/lp, which represents the IP address of a remote UNIX print server and the name of the remote print queue on the server. You also need to specify the model or make of printer and its driver. A Printers page link enables you to print a test page, stop the printing service, manage the local print queue, modify the printer entry, or add another printer.

In the Admin page, click the Add Printer button and then enter the username and password of someone on this computer who has sudo privileges. You can then select from detected local printers, discovered network printers, or the type of printer for which you may want to enter details manually.

CUPS offers many additional features and provides transparent traditional UNIX printing support for Ubuntu.

Note

To learn more about CUPS and to get a basic overview of the system, visit www.cups.org.

Avoiding Printer Support Problems

Troubleshooting printer problems can be frustrating, especially if you find that your new printer is not working properly with Linux. Keep in mind, however, that nearly all printers on the market today work with Linux. However, some vendors have higher batting averages than others in the game of supporting Linux.

All-in-One (Print/Fax/Scan) Devices

Problematic printers, or printing devices that might or might not work with Ubuntu, include multifunction (or all-in-one) printers that combine scanning, faxing, and printing services. You should research any planned purchase and avoid any vendor that is unwilling to support Linux with drivers or development information.

Using USB and Legacy Printers

Problems can arise because of a lack of a printer’s Universal Serial Bus (USB) vendor and device ID information—a problem shared by some USB scanners under Linux.

RELATED UBUNTU AND LINUX COMMANDS

The following commands can help you manage printing services:

cupsaccept—Allows print job access to the CUPS server

cupsreject—Prevents print job access to the CUPS server

cupscancel—Cancels a print job

cupsdisable—Disables printing

cupsenable—Enables printing

lp—Sends a specified file to the printer and allows control of the print service

lpc—Displays the status of printers and print service at the console

lpq—Views print queues (pending print jobs) at the console

lprm—Removes print jobs from the print queue via the command line

lpstat—Displays printer and server status

References

https://help.ubuntu.com/community/SettingUpNFSHowToUbuntu community documentation for setting up NFS

https://help.ubuntu.com/community/SambaUbuntu community documentation for setting up Samba

www.samba.orgBase entry point for getting more information about Samba and using the SMB protocol with Linux, UNIX, macOS, and other operating systems

www.cups.orgA comprehensive repository of CUPS software and information

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.223.10