Handling filesystems
This may seem like plug and pray but it’s not too complicated.
The filesystem layout of a Linux system is as per the Filesystem Hierarchy Standard. The table ( see bottomright) lists some of the important directories though there may be slight differences depending on your distro. Unlike Windows, Linux maintains a single filesystem tree and even external devices attach to it at various points.
The first step in managing a storage device is attaching it to the filesystem tree. This process is called mounting. A file named /etc/fstab lists the devices that will be mounted at boot. Read through fstab’s man page ( man fstab ) for an explanation of each of the fields in the file.
The mount command is used to mount file systems. Entering the command without arguments will display a list of the filesystem’s currently mounted. There are two main components to mounting a device. The first is the actual name of the device file associated with the physical device. The Linux kernel treats all disk-like devices, such as SATA hard disks and USB drives as SCSI disks with names such as /dev/sdaX. The X is replaced with a number that represents the partition number. However, many distros associate a device with a text label that can be either a simple text or a randomly generated UUID (Universally Unique Identifier). The second component is the mount point, which is the directory where the device is attached to the filesystem tree.
Virtually all distros will automatically mount a removable disk and you can find its mount point with the mount command. However, when you’re done writing to a USB disk, make sure you first unmount it before yanking it from the USB port, e.g. umount /dev/sda1 unmounts the device associated with /dev/sda1. The unmounting process makes sure all data destined for the device has been transferred.
Data as we encounter it every day is in the form of files organised into structures that we can comprehend. However, in reality data exists in the form of blocks and there are times when you’ll need to move it around in its raw form. For this purpose the Linux shell includes the dd utility (short for data duplicator) that helps copy blocks of data from one place to another. While dd can be used for a variety of purposes, you’ll mostly encounter this command when dealing with ISO images. Virtually all distros put out new releases in the form of ISO images that you can either burn onto an optical media or transfer to USB disk with the use of dd, such as: $ sudo umount /dev/sdd $ sudo dd if=/path/to/fedora.iso of=/dev/sdd bs=4M $ sync
In these set of commands, we’ve first unmounted the USB drive connected to /dev/sdd before asking dd to copy the ISO file to the USB disk in chunks of four megabytes. The operation will take some time to complete but before yanking the disk issue the sync command to make sure all data has been transferred to the disk.
You can also use the mount command to mount an ISO image while it’s still on the hard disk: $ sudo mkdir /mnt/iso_image $ sudo mount -t iso9660 -o loop image.iso /mnt/iso_image
Here we’ve first created a mount point named /mnt/iso_ image and mounted the image file at that mount point which is now a loop device. A loop device is a pseudo-device that makes a file accessible as a block device. After the image is mounted, it can be treated just as though it were a real CD-ROM or DVD. Remember to unmount the image when it’s no longer needed.
Remote operations
Linux is well-known for its networking dexterity. Its popular graphical tools and applications derive their power from feature-rich command line utilities that you can use directly for more control over network operations. Two of the most popular commands used for debugging network-related issues are ping and traceroute . For instance, the command ping linuxformat.com will send a packet called an ICMP ECHO_REQUEST to the specified host. Most web servers are configured to reply to this packet, which enables you to verify
the connection. The command prints performance statistics after it is interrupted by pressing Ctrl+C. A properly functioning network will have no packet loss. You can also use the traceroute program which displays a listing of all the hops the network traffic takes to get from your local system to a specified host, e.g. type traceroute techradar.com to see the route taken by packets flowing from your computer to techradar.com’s web server.
The Linux CLI offers several options for sending files over the network. A popular command-line program for file downloading is wget. It is useful for downloading content from both web and FTP sites. The utility can download everything from single files and multiple files to even entire sites. You’ll most often use it to fetch ISO images: $ wget -c http://releases.ubuntu.com/16.04.1/ubuntu-16.04.1desktop-amd64.iso
The -c option asks wget to grab a partially-downloaded file and is useful for resuming interrupted downloads.
It’s often useful to verify the integrity of an ISO image that you have downloaded. In most cases, a distributor of an image will also supply a checksum file which contains a string of alphanumeric characters that have been calculated from image. If the contents of the image file changes by even one bit, the resulting checksum will be very different. Checksums are most commonly generated by the md5sum program. After downloading an image, you should run md5sum against the image and compare the results with the md5sum value supplied by the publisher: $ md5sum downloaded-image.iso 85f43dc4c4ceb007661a3044845g243c downloaded-image. iso
Secure transfers
The first generation of tools that were designed to access remote computers conducted their business in cleartext. Later, a protocol called SSH was written to securely communicate with the remote machine. Virtually all Linux distros ship with an implementation of SSH which is called OpenSSH. You can use this to run a secure terminal session over the network. All traffic passing over the session is encrypted Including passwords. A SSH setup includes an SSH server that runs on the remote host and listens for incoming connections on port 22 and an SSH client that’s used on the local system to communicate with the remote server. You’ll find various tutorials about SSH in previous issues of LinuxFormat issues.
The most common use for SSH these days is to remotely access a Raspberry Pi. Assuming the IP address of the Pi is 192.168.3.101, you can access it via SSH with: $ sudo ssh pi@192.168.3.101
This SSH client will connect to the SSH server running on the Raspberry Pi and prompt you for the password of the pi user. The first time a connection is attempted, you’ll have to accept the credentials of the remote host. Once authenticated, you’ll get the shell prompt of the remote Raspberry Pi host. Any commands you enter now will be executed on the Raspberry Pi. When you’re done, type exit to terminate the remote session and return to your local shell.
The OpenSSH package also includes scp (secure copy) that makes use of an SSH encrypted tunnel to copy files across the network. Unlike the tradition cp command, when using the scp command you’ll have to ensure that the source or destination pathnames also include the name of a remote host. For example, to copy a file named document.txt from our home directory to the remote Pi, we could do this: $ scp ~/document.txt pi@192.168.3.101:/home/pi/ Documents/
To copy files from a remote system down to a local machine, the order of the scp command just needs to be reversed so that the first, or source, argument is the remote system and the second, or destination, argument is a directory on the local system. To illustrate, the following we’ll copy an ISO image in the Downloads directory on the remote Pi into the current directory on the local machine: $ scp pi@192.168.3.101:/home/pi/Downloads/distro.iso .