Accessing Network Resources - Ubuntu Linux Toolbox: 1000+ Commands for Power Users (2013)

Ubuntu Linux Toolbox: 1000+ Commands for Power Users (2013)

Chapter 12

Accessing Network Resources

IN THIS CHAPTER

· Web browsing with elinks

· Transferring files with wget, curl, lftp, and scp

· Sharing directories with NFS, Samba, and SSHFS

· Chatting with irssi IRC client

· Managing email with mutt and mail

In the time it takes to fire up a graphical FTP client, you could already have downloaded a few dozen files from a remote server using command line tools. Even when a GUI is available, commands for transferring files, web browsing, sharing directories, and reading mail can be quick and efficient to use. When no GUI is available, they can be lifesavers.

This chapter covers commands for accessing resources (files, e-mail, shared directories, and online chats) over the network.

Running Commands to Browse the Web

Text-mode web browsers provide a quick way to check that a web server is working or to get information from a web server when a usable GUI isn’t available. The once-popular lynx text-based browser was supplanted in most Linux systems by the links or elinks browsers.

To use a command line browser, you need to install one of these programs, with package names shown in parens: lynx (lynx-cur package), links (links package), and elinks (elinks package). In most cases, if you want a command line web browser, install the elinks package.

The elinks browser runs in a terminal window. Aside from not displaying images in the terminal, elinks can handle most basic HTML content and features: tables, frames, tabbed browsing, cookies, history, MIME types, and simple cascading style sheets. You can even use your mouse to follow links and select menu items.

Because elinks supports multiple colors, as long as the terminal you are using supports multiple colors, it’s easy to spot links and headings in the text. (Colors may not work within a screen session.) Here are some examples of elinks command lines:

$ elinks Prompts for file name or URL

$ elinks www.handsonhistory.com Opens file name or URL you request

If you have a mouse available, click near the top of the terminal window to see the menu. Select the menu name or item you want. Select a link to go to that link. The following list shows elinks keyboard navigation keys.

· Esc (or F9/F8)—Toggle the menu on and off (then use arrow keys or mouse to navigate menus).

· Down arrow— Go to the next link or editable field on the page.

· Up arrow— Go to the previous link or editable field on the page.

· Right arrow or Enter— Go forward to the highlighted link. Enter text in the highlighted form field.

· Left arrow— Go back to the previous page.

· /— Search forward.

· ?— Search backwards.

· n— Find next.

· N— Find previous.

· PageUp—Scroll one page up.

· PageDown— Scroll one page down.

· g— Go to a URL.

· q or Ctrl+c— Exit elinks.

· =— View page information.

· Ctrl+r— Reload page.

· a—Bookmark the current page.

· t—Open a new browser tab.

· >—Go to the next tab.

· <—Go to the previous tab.

· c—Close the current tab.

· d—Download the current link.

· D—View downloads.

· A—Add the current link to bookmarks.

· s—View bookmarks.

· v—View the current image.

· h—View the global history manager.

You can add global settings for elinks to /etc/elinks.conf. Per-user settings are stored in each user’s $HOME/.elinks directory. Type man elinkskeysto see available settings.

Transferring Files

Commands in Linux for downloading files from remote servers (HTTP, HTTPS, FTP, or SSH) are plentiful and powerful. You might choose one command over another because of the specific options you need. For example, you may want to perform a download over an encrypted connection, resume an aborted download, or do recursive downloads. This section describes how to use wget, ftp, lftp, scp, and sftp.

Downloading Files with wget

Sometimes you need to download a file from a remote server using the command line. For example, you find a link to an RPM software package, but the link goes through several HTTP redirects that prevent rpm from installing straight from HTTP. Or you may want to script the automated download of a file, such as a log file, every night.

The wget command can download files from web servers (HTTP and HTTPS) and FTP servers. With a server that doesn’t require authentication, a wget command can be as simple as the wget command and the location of the download file:

$ wget http://design.ubuntu.com/wp-content/uploads/ubuntu-logo14.png

If, for example, an FTP server requires a login and password, you can enter that information on the wget command line in the following forms:

$ wget ftp://user:password@ftp.example.com/path/to/file

$ wget --user=usr --password=passwd ftp://ftp.example.com/pathtofile

For example:

$ wget ftp://chris:mykuulpwd@ftp.linuxtoys.net/home/chris/image.jpg

$ wget --user=chris --password=mykuulpwd \

ftp://ftp.linuxtoys.net/home/chris/image.jpg

You can use wget to download a single web page as follows:

$ wget http://www.wiley.com Download only the Web page

If you open the resulting index.html file, you’ll see links to images that are gathered from the website but would have all sorts of broken links if you didn’t have an Internet connection. To download all the images and other elements required to render the page properly (if the site allows it, which it doesn’t in this case), use the -p option:

$ wget -p http://www.wiley.com Download Web page and other elements

But if you open the resulting index.html file in your browser, chances are you will still have all the broken links even though all the images were downloaded. That’s because the links need to be translated to point to your local files. So instead, do this:

$ wget -pk http://www.wiley.com Download pages, use local file names

And if you’d like wget to keep the original file and also do the translation, type this:

$ wget -pkK http://www.wiley.com Rename to local names, keep original

Sometimes an HTML file you download does not have a .html extension but ends in .asp or .cgi instead. That may result in your browser not knowing how to open your local copy of the file. You can have wget append .html to those files using the -E option:

$ wget -E http://cgi-app.org/index.cgi?Examples Add .html to files

With the wget command, you can recursively mirror an entire website. While copying files and directories for the entire depth of the server’s file structure, the -m option adds timestamping and keeps FTP directory listings (provided that the server allows it). (Use this with caution because it can take a lot of time and space.)

$ wget -m http://www.linuxtoys.net

Using some of the options just described, the following command line results in the most usable local copy of a website:

$ wget -mEkK http://www.linuxtoys.net

If you have ever had a large file download (such as a CD or DVD image file) disconnect before it completed, you may find the -c option to wget to be a lifesaver. Using -c, wget resumes where it left off, continuing an interrupted file download. For example:

$ wget http://example.com/DVD.iso Begin downloading large file

...

95%[========== ] 685,251,583 55K/s Download killed before completion

$ wget -c http://example.com/DVD.iso Resume download where stopped

...

HTTP request sent, awaiting response... 206 Partial Content

Length: 699,389,952 (667), 691,513 (66M) remaining [text/plain]

Because of the continue feature (-c), wget can be particularly useful for those with slow Internet connections who need to download large files. If you have ever had a several-hour download get killed just before it finished, you’ll know what I mean. (Note that if you don’t use the -c when you mean to resume a file download, the file will be saved to a different file: the original name with a .1 appended to it.)

Transferring Files with cURL

The client for URLs application (curl command) provides similar features to wget for transferring files using web and FTP protocols. However, the curl command can also transfer files using other popular protocols, including SSH protocols (SCP and SFTP), LDAP, DICT, Telnet, and File.

Instead of supporting large, recursive downloads (as wget does), curl is designed for single-shot file transfers. It does, however, support more protocols (as noted) and some neat advanced features. To use this command, you need to install the curl package. Here are a few interesting examples of file transfers with curl:

$ curl -O ftp://kernel.org/pub/linux/kernel/v1.0/patch[6-8].sign

$ curl -OO ftp://kernel.org/pub/linux/kernel/v2.6/ChangeLog-2.6.{1,4}

$ curl -O ftp://chris:MyPasswd@ftp.example.com/home/chris/fileA \

-Q ‘-DELE fileA’

$ curl -T install.log ftp://chris:MyPasswd@ftp.example.com/tmp/ \

-Q “-RNFR install.log” -Q “-RNTO Xinstall.log

$ curl ftp://ftp.kernel.org/pub// List /pub contents

The first two commands show how to use square brackets to indicate a range [6-8] and curly brackets for a list {1,4} of characters or numbers that are used to match files.

The third command line illustrates how to add a username and password (chris:MyPasswd), download a file (fileA) from the server, and then delete the file on the server once the download is done (-Q ‘-DELE fileA’).

The fourth example uploads (-T) the file install.log to an FTP server. Then it renames the remote file to Xinstall.log. The last example tells curl to list the contents of the /pub/ directory at ftp.kernel.org.

Transferring Files with FTP Commands

Ubuntu comes with the standard FTP client (ftp command), that works the same way it does on most UNIX and Windows systems. I recommend you use the full-featured, user-friendly lftp instead.

With these FTP clients, you open a session to the FTP server (as opposed to just grabbing a file, as you do with wget and curl). Then you navigate the server much as you would a local filesystem, getting and putting documents across the network connection.

To get the lftp command, type apt-get install lftp. Here are examples of how to connect to an FTP server withlftp:

$ lftp mirrors.kernel.org Anonymous connection

lftp mirrors.kernel.org:~>

$ lftp chris@example.com Authenticated connection

lftp example.com:~>

$ lftp -u chris example.com Authenticated connection

Password: ******

lftp example.com:~>

$ lftp -u chris,Mypwd example.com Authentication with password

lftp example.com:~>

$ lftp Start lftp with no connection

lftp :~> open mirrors.kernel.org Start connection in lftp session

lftp mirrors.kernel.org:~>

Warning The fourth example should be avoided in real life. Passwords that are entered in a command line end up stored in clear text in your ~/.bash_history. They may also be visible to other users in the output of ps auwx.

When a connection is established to an FTP server, you can use a set of commands during the FTP session. FTP commands are similar to shell commands. Just like in a bash shell, you can press Tab to autocomplete filenames. In a session, lftp also supports sending multiple jobs to the background (Ctrl+z) and returning them to the foreground (wait or fg). These are useful if you want to continue traversing the FTP site while files are downloading or uploading. Background jobs run in parallel. Type jobs to see a list of running background jobs. Type help to see a list of lftp commands.

The following sample lftp session illustrates useful commands when downloading:

$ lftp mirrors.kernel.org

lftp mirrors.kernel.org:~> pwd Check current directory

ftp://mirrors.kernel.org

lftp mirrors.kernel.org:~> ls List current directory

drwxr-xr-x 8 ftp ftp 4096 Mar 08 21:02 debian

...drwxr-xr-x 6 ftp ftp 4096 Mar 09 00:11 ubuntu

...

lftp mirrors.kernel.org:~> cd ubuntu/dists/quantal Change directory

lftp mirrors.kernel.org:...> get Contents-amd64.gz Download a file

24772197 bytes transferred in 37 seconds (646.1K/s)

lftp mirrors.kernel.org:...> <Ctrl+z> Background the download

lftp mirrors.kernel.org:...> mget main/i18n/* Get all in main/i18n/

lftp mirrors.kernel.org:...> !ls Run local ls

lftp mirrors.kernel.org:...> bookmark add quantalBookmark location

lftp mirrors.kernel.org:...> quit Close lftp

This session logs in as the anonymous user at mirrors.kernel.org. After changing to the directory containing the file I was looking for, I downloaded it using the get command. By typing Ctrl+z, the download could continue while I did other activities. Next, the mget command (which allows wildcards such as *) downloaded all files from the main/i18n/ directory.

Any command preceded by an exclamation mark (such as !ls) is executed by the local shell. The bookmark command saves the current location (in this case, ftp://mirrors.kernel.org/ubuntu/dists/) under the name quantal, so next time I can run lftp quantal to return to the same location. The quit command ends the session.

Here are some useful commands during an authenticated lftp upload session. This assumes you have the necessary file permissions on the server:

$ lftp chris@example.com

Password: *******

lftp example.com:~> lcd /home/chris/songs Change to a local directory

lftp example.com:~> cd pub/uploads Change to server directory

lftp example.com:~> mkdir songs Create directory on server

lftp example.com:~> chmod 700 songs Change remote directory perm

lftp example.com:~> cd songs Change to the new directory

lftp example.com:~> put song.ogg tune.ogg Upload files to server

3039267 bytes transferred

lftp example.com:~> mput /var/songs/* Upload matched files

lftp example.com:~> quit Close lftp

The lftp session illustrates how you can use shell command names to operate on remote directories (provided you have permission). The mkdir and chmod commands create a directory and leave permissions open only to your user account. The put command uploads one or more files to the remote server. The mput command can use wildcards to match multiple files for download. Other commands include mirror (to download a directory tree) and mirror -R (to upload a directory tree).

lftp also provides a shell script for non-interactive download sessions: lftpget. The syntax of lftpget is similar to that of the wget command:

$ lftpget ftp://mirrors.kernel.org/ubuntu/dists/quantal/Release

Keep in mind that standard FTP clients are insecure because they do all their work in clear text. So your alternative, especially when security is a major issue, is to use SSH tools to transfer files.

Using SSH Tools to Transfer Files

Because SSH utilities are among the most important tools in a system administrator’s arsenal of communications commands, some of the more complex uses of configuring and using SSH utilities are covered in Chapter 13. However, in their most basic form, SSH utilities are the tools you should use most often for basic file transfer.

In particular, the scp command will do most of what you need to get a file from one computer to another, while making that communication safe by encrypting both the password stage and data transfer stage of the process. The scpcommand replaces the rcp command as the most popular tool for host-to-host file copies.

Warning You do not get a warning before overwriting existing files with scp, so be sure that the target host doesn’t contain any files or directories you want that are in the path of your scp file copies.

Copying Remote Files with scp

To use scp to transfer files, the SSH service (usually the sshd server daemon) must be running on the remote system. Here are some examples of usefulscpcommands:

$ scp myfile chris@server1:/tmp/ Copy myfile to server1

Password: ******

$ scp server1:/tmp/myfile . Copy remote myfile to local dir

Password: ******

Use the -p option to preserve permissions and timestamps on the copied files:

$ scp -p myfile server1:/tmp/

If the SSH service is configured to listen on a port other than the default port 22, use -P to indicate that port on the scp command line:

$ scp -P 12345 myfile server1:/tmp/ Connect to a particular port

To do recursive copies, from a particular point in the remote filesystem, use the -r option:

$ scp -r mydir francois@server1:/tmp/ Copy all in mydir to remote /tmp

Although scp is most useful when you know the exact locations of the file(s) you need to copy, sometimes it’s more helpful to browse and transfer files interactively.

Copying Files Using rsync

Like scp, the rsync command lets you copy files between remote systems over the ssh facility. However, rsync has some features that make it particularly useful for doing backups and for syncing directories over a network.

In the rsync example ahead, replace localhost with the hostname or IP address of the remote system where you want to back up your files. Testing rsync by copying files to localhost makes it easier to see the results of rsync and see how it works.

The rsync command can recursively copy files from a local directory to another system (local or remote). The -a (archive) option requests a recursive copy and tries to maintain file timestamps and permission. The -v is for verbose:

$ cd /usr/share/doc

$ sudo rsync -av anacron/ chris@localhost:/tmp/anacron Recursive copy

chris@localhost’s password: ********

sending incremental file list

created directory /tmp/anacron

README.Debian

...

Type ls -l /usr/share/doc/anacron /tmp/anacron and notice that date and timestamps were maintained on the files copied by rsync. However, because the user chris was used to perform the rsync, all files copied to the target system are owned by chris.

One of the nice features of rsync is that it only copies files that have changed. So if you ran the same rsync command again after adding a few files, only the new files are copied.

$ cd /usr/share/doc

$ sudo touch anacron/testing Create a new file

$ sudo rsync -av anacron/ chris@localhost:/tmp/anacron/ Repeat rsync

chris@localhost’s password:

********

sending incremental file list

./

testing

Notice that only the new file is copied to the remote target. This makes it very efficient to use rsync as a backup tool. If instead of using rsync for backup, you wanted to just keep two directory structures in sync, you could add the --delete option to delete any files not in the original directory from the target directory. For example:

$ sudo rm anacron/testing

$ sudo rsync -av --delete anacron/ chris@localhost:/tmp/anacron/

chris@localhost’s password: ********

./

deleting testing

When copying files with rsync, you should also think about what to do with symbolically linked files. With the --links option, symbolic link files are copied; with --copy-links the file that the symbolic link ultimately points to is copied to the remote location. With --links, you risk not having a copy of the file the link points to at all. With --copy-links, you risk having too many copies of a file.

Copying Remote Files in sftp and lftp Sessions

The sftp command lets you use an FTP-like interface to find and copy files over SSH protocols. Here’s an example of how to start an sftp session:

$ sftp chris@server1

chris@server1’s password: *****

sftp>

Use sftp in the same manner as you use regular FTP clients. Type ? for a list of commands. You can change remote directories (cd), change local directories (lcd), check current remote and local directories (pwd and lpwd), and list remote and local contents (ls and lls). Depending on the permission of the user you logged in as, you may be able to create and remove directories (mkdir and rmdir), and change permissions (chmod) and ownership/group (chown and chgrp) of files and directories.

You can also use lftp (discussed earlier in this chapter) as an sftp client. Using lftp adds some user-friendly features such as path completion using the Tab key:

$ lftp sftp://chris@server1

Password: ********

lftp chris@server1:~>

Using Windows File Transfer Tools

In many cases, people need to get files from Linux servers using Windows clients. If your client operating system is Windows, you can use one of the following open source tools to get files from Linux servers:

· WinSCP (http://winscp.net)—Graphical scp, sftp, and FTP client for Windows over SSH1 and SSH2 protocols

· FileZilla (http://filezilla.sourceforge.net)—Provides graphical client FTP and SFTP services in Windows, as well as offering FTP server features

· PSCP (www.chiark.greenend.org.uk/~sgtatham/putty/)—Command line scp client that is part of the PuTTY suite

· PSFTP (www.chiark.greenend.org.uk/~sgtatham/putty/)—Command line sftp client that is part of the PuTTY suite

Sharing Remote Directories

Tools described to this point in the chapter provide atomic file access, where a connection is set up and files are transferred in one shot. In times where more persistent, ongoing access to a remote directory of files is needed, services for sharing and mounting remote filesystems can be most useful. Such services include Network File System (NFS), Samba, and SSHFS.

Sharing Remote Directories with NFS

Assuming a server is already running the NFS service (part of the nfs-kernel-server package), you can use exportfs and showmount commands to see available and mounted shared directories. Mounting a shared directory is done with special options to the standard mount command. If you install the nfs-kernel-server package, Ubuntu will start the NFS service.

Viewing and Exporting NFS Shares

Run from the NFS server, the exportfs command shows all shared directories available from that server:

$ sudo /usr/sbin/exportfs -v

/export/mysh client.example.com(ro,root_squash,no_subtree_check)

/mnt/public *(rw,wdelay,root_squash,no_subtree_check)

The two directories being shared are /export/mysh and /mnt/public. The first is only available to host computer client.example.com, whereas the second is available to everyone. Options for each share are shown in parentheses. The first share is available read-only (ro), and requests from the root user on the client are mapped into the anonymous UID (root_squash). Also, a less thorough check of filesystem permission is done (no_subtree_check). The second share allows read-write mounting (rw) and writes to the share are delayed to improve performance when more writes are expected (wdelay).

You can add and modify shared NFS directories by making changes to the /etc/exports file. To get changes to take effect, type any of the following as root:

$ sudo /etc/init.d/nfs-kernel-server reload Reload exported directories

$ sudo exportfs -r Reload exported directories

$ sudo exportfs -rv Verbose reload of shares

exporting client.example.com:/export/myshare

exporting *:/mnt/public

From the Linux server system, you can use the showmount command to see what shared directories are available from the local system. For example:

$ sudo /usr/sbin/showmount -e

Export list for server.example.com

/export/myshare client.example.com

/mnt/public *

From a client Linux system, you can use the showmount command to see what shared directories are available from a selected computer. For example:

$ sudo /usr/sbin/showmount -e server.example.com

/export/myshare client.example.com

/mnt/public *

Note If you are unable to see the NFS shared directories from another system, it is possible that the firewall (iptables) is blocking access to the NFS service on the server side.

Mounting NFS Shares

Use the mount command to mount a remote NFS share on the local computer. Here is an example:

$ sudo mkdir /mnt/server-share

$ sudo mount server.example.com:/export/myshare /mnt/server-share

This example notes the NFS server (server.example.com) and the shared directory from that server (/export/myshare). The local mount point, which must exist before mounting the share, appears at the end of the command (/mnt/server-share).

Pass NFS-specific options to the mount command by adding them after the -o option:

$ sudo mount -o rw,hard,intr server.example.com:/export/myshare /mnt/server-share

The rw option mounts the remote directory with read-write permissions, assuming that permission is available. With hard set, someone using the share will see a server not responding message when a read or write operation times out. If that happens, having set the intr option lets you interrupt a hung request to a remote server (press Ctrl+c to send an interrupt).

On older systems, NFS version 3 (nfs3) protocol is used to connect to the share. To use NFS version 4, which is designed to work over the Internet and through firewalls, indicate that protocol as the filesystem type on the command line as follows:

$ sudo mount -t nfs4 server.example.com:/ /mnt/server-share

Note Depending on which version of Ubuntu you are using, the implementation of NFS v4 may not be robust enough for production. It may be safer and/or more reliable to tunnel earlier versions of NFS over SSH. You can find more information on this topic with an Internet search for “nfs ssh”, and especially look at www.howtoforge.com/nfs_ssh_tunneling. In addition, look at http://tldp.org/HOWTO/NFS-HOWTO/security.html for more on NFS security.

Sharing Remote Directories with Samba

Samba is the open source implementation of the Windows file and print sharing protocol originally known as Server Message Block (SMB) and now called Common Internet File System(CIFS). There is an implementation of Samba in Linux, as well as in many other operating systems. To use Samba, install the packages samba and samba-doc.

Graphical tools for sharing, querying, and mounting shared SMB directories from Windows include the Samba SWAT web-based administration tool. To use the SWAT tool in Linux, install the swat package. Next, read the instructions at https://help.ubuntu.com/community/Swat for details on how you can start SWAT.

Commands for working with Samba shares can be used to query SMB servers, mount directories, and share directories.

Viewing and Accessing Samba Shares

To scan your network for SMB hosts, type the following:

$ findsmb

*=DMB

+=LMB

IP ADDR NETBIOS NAME WORKGROUP/OS/VERSION

--------------------------------------------------------------------

192.168.1.1 SERVER1 +[MYWORKGROUP] [Unix] [Samba 3.6.3]

To view a text representation of your network neighborhood (shared directories and printers), use smbtree:

$ sudo smbtree

Password: ******

MYGROUP

\\THOMPSON thompson server (Samba, Ubuntu)

\\THOMPSON\hp2100 HP LaserJet 2100M Printer

\\THOMPSON\IPC$ IPC Service (thompson server (Samba, Ubuntu))

\\EINSTEIN Samba Server

\\EINSTEIN\hp5550 HP DeskJet 5550 Printer

\\EINSTEIN\IPC$ IPC Service (Samba Server)

To add an existing Linux user as a Samba user, use the smbpasswd command:

$ sudo smbpasswd -a chris

New SMB password: ******

Retype new SMB password: ******

Added user chris

Note You need to set up a Samba password for yourself to perform any of the commands that ask for a password.

To list services offered by a server to an anonymous user, type the following (press Enter to skip the password):

$ smbclient -L server

Enter chris’s password:

Anonymous login successful

Domain=[MYGROUP] OS=[Unix] Server=[Samba 3.6.3]

Server Comment

----------- -------

EINSTEIN

THOMPSON thompson server (Samba, Ubuntu)

Here’s the output from smbclient for a specific user named chris:

$ smbclient -L server -U chris

Enter chris’s password:

Domain=[WORKGROUP] OS=[Unix] Server=[Samba 3.6.3]

Sharename Type Comment

--------- ---- -------

homes Disk Home Directories

print$ Disk Printer Drivers

IPC$ IPC IPC Service (thompson server (Samba, Ubuntu))

chris Disk Home Directories

Domain=[WORKGROUP] OS=[Unix] Server=[Samba 3.6.3]

Server Comment

--------- -------

EINSTEIN

THOMPSON thompson server (Samba, Ubuntu)

Workgroup Master

--------- -------

DATAGROUP MYSERVER

WORKGROUP THOMPSON

To connect to a Samba share FTP-style, type the following:

$ smbclient //thompson/homes -U chris

Password: ********

Domain=[WORKGROUP] OS=[Unix] Server=[Samba 3.6.3]

smb: \>

As with most FTP clients, type help or ? to see a list of available commands. Likewise, you can use common shell-type commands, such as cd, ls, get, put, and quit, to get around on the SMB host.

Mounting Samba Shares

You can mount remote Samba shares on your local filesystem much as you would mount a local hard disk partition or remote NFS filesystem. To mount the share:

$ sudo mount -t cifs -o username=chris,password=MySecret \

//192.168.1.1/homes /mnt/mymount/

You can see the current connections and file locks on a server using the smbstatus command. This will tell you if someone has mounted your shared directories or is currently using an smbclient connection to your server:

$ sudo smbstatus

Samba version 3.6.3

PID Username Group Machine

------------------------------------------

5466 chris chris 192.168.1.1 (192.168.1.1)

Service pid machine Connected at

-------------------------------------------

IPC$ 30180 192.168.0.145 Sat Mar 9 12:19:28 2013

chris 30180 192.168.0.145 Sat Mar 9 12:19:28 2013

Looking Up Samba Hosts

NetBIOS names are used to identify hosts in Samba. You can determine the IP address of a computer using the nmblookup command to broadcast for a particular NetBIOS name on the local subnet as follows:

$ nmblookup thompson

querying thompson on 192.168.1.255

192.168.1.1 thompson<00>

To find the IP address for a server on a specific subnet, use the -U option:

$ nmblookup -U 192.168.1.255 einstein

querying einstein on 192.168.1.255

192.168.1.1 einstein<00>

Checking Samba Configuration

If you are unable to use a Samba share or if you have other problems communicating with your Samba server, you can test the Samba configuration on the server. The testparm command can be used to check your main Samba configuration file (smb.conf):

$ testparm

Load smb config files from /etc/samba/smb.conf

Processing section “[homes]”

Processing section “[printers]”

Processing section “[myshare]”

Loaded services file OK.

Server role: ROLE_STANDALONE

Press Enter to see a dump of your service definitions

After pressing Enter as instructed, you can see the settings from your smb.conf file. Here’s how an entry for the myshare shared directory, used earlier in an example, might appear in the smb.conf file:

[myshare]

path = /home/chris

username = chris

valid users = chris

hosts allow = einstein

available = yes

This entry allows the Samba user chris to access the /home/chris directory (represented by the myshare share name) from the host computer named einstein. The share is shown as being currently available.

The previous example of testparm showed the entries you set in the smb.conf file. However, it doesn’t show all the default entries you didn’t set. You can view those using the -v option. Pipe it to the less command to page through the settings:

$ testparm -v | less

If you want to test a configuration file before it goes live, you can tell testparm to use a file other than /etc/samba/smb.conf:

$ testparm /etc/samba/test-smb.conf

Sharing Remote Directories with SSHFS

Another magical trick you can do over the SSH protocol is to mount remote filesystems. Using the SSH filesystem (sshfs), you can mount any directory from an SSH server that your user account can access from your local Linux system. sshfs provides encryption of the mount operation as well as of all the data being transferred. Another cool aspect of sshfs is that it requires no setup on the server side (other than having SSH service running).

Here is a quick procedure for mounting a directory of documents from a remote server to a local directory. Doing this only requires that the remote server is running SSH and that the directory you want is accessible to your user account on the server. Here, you mount a directory named /var/docs from the host at 10.0.0.50 to a mount point called /mnt/docs on the local system:

$ sudo apt-get install sshfs Install sshfs software

$ mkdir /var/tmp/chris Create mount point

$ sshfs chris@10.0.0.5:/home/chris /var/tmp/chris Mount remote directory

When you are done using the remote directory, you can unmount it with the fusermount command (part of the fuse-utils package):

$ fusermount -u /var/tmp/chris Unmount remote directory

Chatting with Friends in IRC

Despite the emergence of instant messaging, Internet Relay Chat (IRC) is still used by a lot of people today. With global companies and more employees working remotely, IRC has become a popular technology for helping groups of people to work together from different locations.

Freenode.net has tons of chat rooms dedicated to supporting major open source software projects. In fact, many people stay logged into them all day and just watch the discussions of their favorite Linux projects scroll by. This is known as lurking.

The xchat utility is a good graphical, multi-operating system IRC client. You can install just the xchat package or the GNOME bindings in the xchat-gnome package. To run either version of xchat from Ubuntu, type xchat from the dashboard and select the XChat IRC or XChat GNOME IRC icon.

The elite way to do IRC, however, is to run a text-mode client in screen on an always-on machine, such as an old server. Another similar option is to use an IRC proxy client, also known as a bouncer, such as dircproxy (part of the dircproxy package).

The original IRC client was ircII. It allowed the addition of scripts—in some ways similar to macros found in productivity suites—that automated some of the commands and increased usability. The most popular was PhoEniX by Vassago. Then came BitchX, which started as an ircII script and then became a full-blown client. Today, many people use irssi. To install and launch irssi from Ubuntu, type:

$ sudo apt-get install irssi

$ irssi -n JayJoe199x Open an irssi chat session

In this example, the username (referred to as the person’s nick) is set to JayJoe199x (you should choose your own). You should see a blue status bar at the bottom of the screen indicating that you are in Window 1, the status window. If this is the first time you’ve run irssi, the program displays help messages pointing you to the documentation. IRC commands are preceded with a / character. For example, to connect to the freenode server (after accepting policies at http://freenode.net) type the following:

/connect chat.freenode.net

If you didn’t add your username on the command line, you are connected to chat.freenode.net with the username you are logged in under. On IRC, a chat room is called a channel and has a pound sign (#) in front of the name. Next, try joining the #ubuntu IRC channel:

/join #ubuntu

You are now in the channel in Window 2, as indicated in the status bar. Switch among the irssi windows by pressing Ctrl+n and Ctrl+p. Alternatively, you could select Alt+1 or Alt+2, but those keys won’t work inside a gnome-terminal window, because the gnome-terminal eats those keystrokes.

To get help at any time, type /help command, where command is the name of the command you want more information on. Help text will output in the status window, not necessarily the current window.

To add to the IRC chat, simply type a message and press Enter to send the message to those in the channel. To direct a message to a specific user in the chat, type the first few characters of that user’s nick, and press the Tab key to autocomplete the nick. Then type the message on the rest of the line and press Enter. The message appears in the chat window and appears highlighted to that user.

You can change your nick at any time using the /nick command. For example, to change your nick to joe, type /nick joe. Type /part to leave a channel. Type /quit to exit the program.

There is a lot more to irssi. You can customize it and improve your experience significantly. Refer to the irssi documentation (www.irssi.org/documentation) for more information about how to use irssi.

Using Text-Based E-mail Clients

Most Mail User Agents (MUAs) are GUI-based these days. So if you began using e-mail in the past decade or so, you probably think of browser-based e-mail clients (such as Gmail) or stand-alone graphical applications such as Evolution, Kmail, Thunderbird, or (on Windows systems) Outlook when it comes to e-mail clients. On the first UNIX and Linux systems, however, reading e-mail was handled by text-based applications.

If you find yourself needing to check e-mail on a remote server or other text-based environment, venerable text-based mail clients are available and still quite useful. In fact, some hard core geeks still use text-based mail clients exclusively, touting their efficiency and scoffing at HTML-based messages.

The mail clients described in this chapter expect your messages to be stored in standard MBOX format on the local system. That means that you are either logged into the mail server or you have already downloaded the messages locally (for example, by using POP3 or similar).

As an alternative, there are some text-based e-mail readers, such as the mutt command, that let you connect to POP and IMAP mail servers (including encrypted protocols) to read your e-mail.

Note Text-based mail clients can be used to read mail already downloaded by other mail clients. For example, you could open your Evolution mail Inbox file by typing mail -f $HOME/.evolution/mail/loc/Inbox.

Managing E-mail with mail

The oldest command, and easiest to use when you just want a quick check for messages in the root user’s mailbox on a remote server, is the mail command (/bin/mail), part of the mailutils package.

Before you can use the command line mail program, you must configure the package. There are many possible issues with mail servers that depend on your Internet service provider, or ISP. The configuration process is started as part of the installation when you run the following command:

$ sudo apt-get install mailutils

Although mail can be used interactively, it is often used for sending script-based e-mails. Here are some examples:

$ mail -s ‘My Linux version’ chris@localhost < /etc/lsb-release

$ ps auwx | mail -s ‘My Process List’ root@localhost

The two mail examples just shown provide quick ways to mail off some text without having to open a GUI mail application. The first example sends the contents of the /etc/lsb-release file to the user chris@localhost. The subject (-s) is set to ‘My Linux Version’. In the second example, a list of currently running processes (ps auwx) is sent to the root user with a subject of ‘My Process List’.

Used interactively, by default the mail command opens the mailbox set by your current shell’s $MAIL value. For example:

$ echo $MAIL

/var/mail/chris

Note You may need to set this environment variable. The value should be /var/mail/username (where chris or some other name replaces username). On Ubuntu, the MAIL variable is not set by default, as the mailcommand is not installed by default.

To read the mail for the root user, run the following command:

$ sudo mail

Mail version 8.1 6/6/93. Type ? for help.

“/var/mail/root”: 25 messages 25 new

>U 1 logwatch@a.l Sat Jun 15 20:03 44/1667 “Logwatch for a (Linux)”

U 2 logwatch@a.l Sun Jun 16 04:32 87/2526 “Logwatch for a (Linux)”

3 logwatch@a.l Mon Jun 17 04:32 92/2693 “Logwatch for a (Linux)”

N 4 logwatch@a.l Sat Jun 22 09:28 44/1667 “Logwatch for a (Linux)”

N 5 MAILER-DAEMON@a Sat Jun 22 09:28 93/3348 “Warning: not sent “

&

The current message has a greater-than sign (>) next to it. New messages have an N at the beginning, unread (but not new) messages have a U, and if there is no letter, the message has been read. The prompt at the bottom (&) is ready to accept commands.

At this point, you are in command mode. You can use simple commands to move around and perform basic mail functions in mail. Type the following to enact these functions:

· ?—To see a list of commands

· The number of the message—To see that message

· v3—Opens the third message in the vi editor

· h18—To see a list of message headers that begins with message 18.

· r7—To reply to message 7 (type your message, and then put a dot on a line by itself to send the message)

· d4— To delete the fourth message (or d4-9 to delete messages four through nine)

· !bash—To escape to the shell (then exit to return to mail)

Before you exit mail, know that any messages you view will be copied from your mailbox file to your $HOME/mbox file when you exit, unless you preserve them (pre*). To have all messages stay in your mailbox, exit by typing x. To save any messages you have already viewed to the mailbox, type q to exit.

You can open any file that is in MBOX format when you use mail. For example, if you are logged in as one user, but want to open the mailbox for the user chris, type this:

$ sudo mail -f /var/mail/chris

Managing E-mail with mutt

If you want to use a command line mail client on an ongoing basis, I recommend you use mutt instead of mail. The mail command has many limitations, such as not being able to send attachments without encoding them in advance (such as with the uuencode command), while mutt has many features for handling modern e-mail needs. The mutt command is part of the mutt package, which you need to install to use this command. Configure mutt by editing /etc/Muttrc. You also need to configure sendmail to allow for sending e-mail.

Like mail, mutt can also be used to send a message from a script. mutt also adds the capability to send attachments. For example:

$ mutt -s “My Linux Version” -a /etc/lsb-release \

chris@example.com < email-body.txt

$ mutt -s “My Linux Version” -a /etc/lsb-release \

chris@example.com < /dev/null

The first example just shown includes the file email-body.txt as the body of the message and attaches the file /etc/lsb-release as an attachment. The second example sends the attachment, but has a blank message body (< /dev/null).

You can begin your mutt mail session (assuming your default mailbox is $MAIL) by simply typing mutt:

$ mutt

/home/chris/Mail does not exist. Create it? ([yes]/no): y

q:Quit d:Del u:Undel s:Save m:Mail r:Reply g:Group ?:Help

1 O Jun 16 logwatch@a ( 69) Logwatch for a (Linux)

2 O Jun 18 logwatch@a ( 171) Logwatch for a (Linux)

3 O Jun 18 Mail Delivery S ( 219) Warning: could not send message

4 O Jun 19 logwatch@a ( 33) Logwatch for a (Linux)

--Mutt: /var/mail/root [Msgs:22 New:2 Old:20 63K]--(date/date)--(all)--

Because mutt is screen-oriented, it is easier to use than mail. As with mail, you use key commands to move around in mutt. Type the following to navigate around your mailbox:

· ?—As usual, you types this to get help. Hints appear across the top bar to help you with your mail.

· Up and down arrow keys—To highlight the messages you want to read.

· Enter—To view the highlighted message.

· PageUp and PageDown—To page through each message.

· i—To return to the message headers.

· slash (/)—Search forward for text.

· Esc-/—Search backwards.

· n—To search again.

· Tab—To jump to the next new or unread message.

· Esc+Tab—To go to the previous message.

· s—To save the current message to a file.

· d—To delete a message.

· u—To undelete a message.

You can type the following to write and send e-mails:

· m—To send a new mail message. After adding the recipient and subject, a blank message opens in joe (or whatever you have your $EDITOR set to).

· a—After exiting the message body, type a to add an attachment, if you like.

· ?—To see other ways of manipulating your message, headers, or attachments.

· y—To send the message or q to abort the send.

· x—To exit without changing your mailbox when you are done.

· q—To exit and incorporate the changes you made (messages read, deleted, and so on).

Although, by default, mutt reads mail from your local mail spool file, it can also attach to mail servers using POP, POPS, IMAP, and IMAPS protocols. If you know the address of your mail server, here are examples of how to connect to a POPS or IMAPS e-mail server from mutt:

$ mutt -f imaps://chris@imapsserver.example.com

$ mutt -f pops://chris@popsserver.example.com

For mail protocols (SMTP) that require certificates (such as secure POP and IMAP protocols) you are presented with a certificate to accept or not after you connect to the server. Once you accept the certificate and provide the correct username and password, you can read, write, and reply to mail as you would with mail from a local mail spool file.

Summary

Network access commands provide quick and efficient ways to get content you need over a network. The elinks web browser is a popular screen-oriented command for browsing the web or taking a quick look at any HTML file. Dozens of commands are available to download files over FTP, HTTP, SSH, or other protocols, including wget, curl, lftp, and scp.

For more ongoing access to remote directories of files, this chapter covers how to use NFS, Samba, and SSHFS command tools. You can do IRC chats, which are popular among open source projects, using the irssi command. For text-based e-mail clients, you have choices such as the mail and mutt commands.