Managing the System - Ubuntu Linux Toolbox: 1000+ Commands for Power Users (2013)

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

Chapter 10

Managing the System


· Checking memory use with free, top, vmstat, and slabtop

· Viewing CPU use with iostat, dstat, and top

· Monitoring storage devices with iostat, vmstat, and lsof

· Working with dates/time using date, hwclock, cal, and NTP

· Changing GRUB boot loader behavior

· Rebuilding the initial ramdisk

· Dealing with run levels with runlevel and init

· Adding, removing, and listing services with chkconfig and service

· Shutting down the system with reboot, halt, and shutdown

· Checking and changing kernel driver settings with lsmod, modinfo, and modprobe

· Watching hardware settings with lspci, dmidecode, and hdparm

Without careful management, the demands on your Linux system can sometimes exceed the resources you have available. Being able to monitor your system’s activities (memory, CPU, and device usage) over time can help you make sure that your machine has enough resources to do what you need it to. Likewise, managing other aspects of your system, such as the device drivers it uses and how the boot process works, can help avoid performance problems and system failures.

This chapter is divided into several sections that relate to ways of managing your Ubuntu or other Linux system. The first section can help you monitor the resources (processing power, devices, and memory) on your Linux system. The next section describes how to check and set your system clock. Descriptions of the boot process and subsequent runlevels follow. The last sections describe how to work with the kernel and related device drivers, as well as how to view information about your computer’s hardware components.

Monitoring Resources

Ubuntu, Debian, and other Linux systems do a wonderful job of keeping track of what they do. If you care to look, you can find lots of information about how your CPU, hard disks, virtual memory, and other computer resources are being used.

You can go to where the Linux kernel stores realtime information about your system by directly viewing the contents of files in the /proc filesystem (see Appendix C). An alternative, however, is to use commands to view information about how your computer’s virtual memory, processor, storage devices, and network interfaces are being used on your system.

There are commands that can monitor several different aspects of your system’s resources. Because this book is not just a man page, however, I have divided the following sections by topic (monitoring memory, CPU, and storage devices) rather than by the commands that do them (top, vmstat, and iostat).

Note Some applications described in this section are installed by default in Ubuntu, in packages such as the procps package. To use iostat or sar, however, you need to install the sysstat package. Install the sysstat package with the following command:

$ sudo apt-get install sysstat

Monitoring Memory Use

Few things will kill system performance faster than running out of memory. Commands such as free and top enable you to see basic information about how your RAM and swap are used. The vmstat command gives detailed information about memory use and can run continuously. The slabtop command can show how much memory the kernel (slab cache) is consuming.

The free command provides the quickest way to see how much memory is being used on your system. It shows the total amount of RAM (Mem:) and swap space (Swap:), along with the amount currently being used. The following are examples of the free command:

$ free List memory usage in kilobytes (-k default)

$ free -m List memory usage in megabytes

total used free shared buffers cached

Mem: 3920 2105 1815 0 550 942

-/+ buffers/cache: 612 3307

Swap: 4059 0 4059

$ free -b List memory usage in blocks

total used free shared buffers cached

Mem: 4110852096 2207576064 1903276032 0 576901120 987951104

-/+ buffers/cache: 642723840 3468128256

Swap: 4257214464 0 4257214464

$ free -mt List memory usage with totals (Swap + Mem)

total used free shared buffers cached

Mem: 3920 2075 1845 0 550 942

-/+ buffers/cache: 582 3337

Swap: 4059 0 4059

Total: 7980 2075 5905

$ free -g List memory usage in gigabytes (rounding down)

total used free shared buffers cached

Mem: 3 2 1 0 0 0

-/+ buffers/cache: 0 3

Swap: 3 0 3

$ free -s 5 Continuously display memory usage every 5 seconds

To avoid wasting RAM and speed up applications, Linux uses as much otherwise unused RAM as possible for the disc cache. For that reason, the first line of output from free that often shows little free RAM can be misleading. I recommend you pay closer attention to the second line of output, which shows the amount of RAM actually available for applications. That amount is 3307MB in this example:

-/+ buffers/cache: 612 3307

One way to guess how much memory you need on a system is to go to another computer running Ubuntu, and then open every application you think you may be running at once. Run free with the total option (free -t) to see how much memory is being used. Then make sure that your new system has at least that much total memory (with most or all of it preferably being available in RAM).

The top command provides a means of watching the currently running processes, with those processes sorted by CPU usage or memory (see Chapter 9 for a description of top for watching running processes). However, you can also use top to watch your memory usage in a screen-oriented way. Here is an example:

$ top

top - 8:43:04 up 3 days, 5:00, 3 users,

load average: 0.21, 0.18, 0.11

Tasks: 188 total, 1 running, 187 sleeping 0 stopped, 0 zombie

Cpu(s): 17.4%us, 3.3%sy, 0.0%ni, 79.3%id,

0.0%wa, 0.0%hi, 0.0%si, 0.0%st

Mem: 4014504k total, 1950936k used, 2063568k free, 248148k buffers

Swap: 4157436k total, 0k used, 4157436k free, 1016924k cached


11062 chris 20 0 1636m 47m 23m S 26 1.2 0:20.19 totem

2646 chris 20 0 1198m 88m 28m S 7 2.3 6:36.98 compiz

1750 root 20 0 209m 29m 12m S 4 0.8 4:33.56 Xorg

To exit top, press q. Like the output for free, top shows the total of memory usage for RAM (Mem:) and swap space (Swap:). However, because top is screen-oriented and provides ongoing monitoring, you can watch memory usage change every three seconds (by default). With top running, press Shift+m and the running processes will be displayed in memory-use order (so you can watch which processes are consuming the most memory). The most useful column to analyze a process’s memory usage is RES, which shows the process’s actual physical RAM usage, also known as resident size. The %MEM column is based on this resident size.

Because most new computers today have multiple CPUs, the Cpu(s): line shows the average CPU usage. To see CPU usage for each individual CPU, press the number 1 key. The Cpu lines will look something like the following:

Cpu0 : 17.7%us, 5.3%sy, 0.0%ni, 44.7%id,

31.7%wa, 0.0%hi, 0.7%si, 0.0%st

Cpu1 : 16.4%us, 4.4%sy, 0.0%ni, 46.4%id,

32.8%wa, 0.0%hi, 0.0%si, 0.0%st

For a more detailed view of your virtual memory statistics, use the vmstat command. With vmstat, you can view memory use over a given time period, such as since the previous reboot or using a sample period. The following example shows vmstat redisplaying statistics every three seconds:

$ vmstat 3

procs -----------memory------- --swap-- ----io---- --system-- --cpu--

r b swpd free buff cache si so bi bo in cs us sy id wa st

1 0 7740 32488 3196 148360 0 0 0 1 26 3876 85 15 0 0 0

1 1 8388 7428 3204 151472 0 216 0 333 30 3200 82 18 0 0 0

1 0 13316 8148 2980 146968 0 4980 4 5121 79 3846 77 23 0 0 0

2 0 32648 7472 2904 148488 0 6455 3 6455 90 3644 83 17 0 0 0

2 0 47892 8088 2732 144208 0 5085 9 5220 79 3468 84 16 0 0 0

1 0 57948 7680 2308 134812 0 3272 12 3296 69 3174 77 23 0 0 0

3 0 58348 7944 1100 123888 21 144 25 275 26 3178 86 14 0 1 0

2 0 66116 7320 568 10 11 2401 20 2403 51 3175 84 16 0 0 0

3 0 81048 7708 648 119452 53 4852 796 4984 123 1783 86 13 0 1 0

To exit vmstat, press Ctrl+c. The vmstat example shows a 30-second time period where more than 100 applications are started. Notice that when the free space goes from 32488 kilobytes to 7428 kilobytes (RAM is filling up), data begins moving to the swap area (see the 216 under the so column). Because the swap area resides on the hard disk, you can see that the blocks written to a disk device (bo) increases as the swap out increases. You can see the amount of swap space being used increasing under the swpd column.

The CPU is also straining in the example, with no idle time showing (id 0). Notice also that when some of the applications need to be swapped back in (see the last three lines of output), the processor has to wait on two occasions for input/output to complete (wa 1).

Here are some other options for using vmstat:

$ vmstat -S m Display output in 1000k megabytes

$ vmstat -S M Display output in 1024k megabytes

$ vmstat -S k Display output in 1000-byte kilobytes

$ vmstat -S K Display output in 1024-byte kilobytes

$ vmstat -n 2 10 Output every two seconds, repeat 10 times

$ vmstat -s | less Display event counters and memory stats

$ vmstat -S M -s | less Display statistics in megabytes

3920 M total memory

1953 M used memory

972 M active memory

726 M inactive memory

1966 M free memory

261 M buffer memory

993 M swap cache

4059 M total swap

0 M used swap

4059 M free swap

137803 non-nice user cpu ticks

9412 nice user cpu ticks

52746 system cpu ticks

62828527 idle cpu ticks


The previous example shows various memory statistics (-s) output in megabytes (-S M), which you will find more convenient to get a general view of memory usage. The other examples show how to display vmstat output in megabytes and kilobytes (in both marketing and technical terms). After that, the -n 2 10 option tells vmstat to repeat every set number of seconds (2) for a limited number of times (10).

With commands, such as ps and top, you can see how much memory each application is consuming on your system. The kernel itself, however, has its own memory cache to keep track of its resources, called the kernel slab. You can use the vmstat command to display kernel slab memory cache statistics (from /proc/slabinfo) as follows:

# vmstat -m | less Page through kernel slab memory cache

Cache Num Total Size Pages

udf_inode_cache 120 120 664 24

dm_crypt_io 0 0 152 26

nf_conntrack_expect 0 0 240 17

nfsd4_openowners 0 0 392 20

nfs_read_data 0 0 768 21

nfs_inode_cache 0 0 1008 16

fscache_cookie_jar 51 51 80 51

rpc_inode_cache 19 19 832 19

ext2_inode_cache 105 105 752 21

ip6_dst_cache 50 50 320 25

UDPLITEv6 0 0 1024 16


The slab memory cache information shows each cache name, the number of objects active for that cache type, the total number of objects available for that cache type, the size of the cache (in bytes), and the number of pages for each cache. You can display kernel slab memory cache information in a screen-oriented view (similar to the top command) using slabtop:

# slabtop

Active / Total Objects (% used) : 562520 / 566736 (99.3%)

Active / Total Slabs (% used) : 20103 / 20103 (100.0%)

Active / Total Caches (% used) : 74 / 110 (67.3%)

Active / Total Size (% used) : 152005.08K / 153123.88K (99.3%)

Minimum / Average / Maximum Object: 0.01K / 0.27K / 8.00K


144768 144400 99% 0.10K 3712 39 14848K buffer_head

142170 141903 99% 0.19K 6770 21 7080K dentry

57168 57168 100% 0.86K 3176 18 50816K ext4_inode_cache

40898 40726 99% 0.61K 1573 26 25168K proc_inode_cache

23713 23611 99% 0.17K 1031 23 4124K vm_area_struct

19824 19824 100% 0.14K 708 28 2832K sysfs_dir_cache

The slabtop output updates every three seconds. By default, slab caches are sorted by the number of objects (first column) in each cache. By pressing c you can sort by cache size instead (as shown in the previous example).

Monitoring CPU Usage

An overburdened CPU is another obvious place to look for performance problems on your system. The vmstat command, shown earlier, can produce basic statistics relating to CPU usage (user activity, system activity, idle time, I/O wait time, and time stolen from a virtual machine). The iostat command (from the sysstat package), however, can generate more detailed reports of CPU utilization.

Here are two examples of using iostat to display a CPU utilization report:

$ iostat -c 3 CPU stats every 3 seconds (starting apps)

Linux 3.2.0-38-generic (ubuntutb) 05/20/2013 _x86_64_ (2 CPU)

avg-cpu: %user %nice %system %iowait %steal %idle

0.26 0.01 0.09 0.05 0.00 99.58

avg-cpu: %user %nice %system %iowait %steal %idle

22.64 0.00 8.18 2.36 0.00 66.82

avg-cpu: %user %nice %system %iowait %steal %idle

32.01 0.00 4.14 19.90 0.00 43.95

$ iostat -c 3 CPU stats every 3 seconds (copying files)

Linux 3.2.0-38-generic (ubuntutb) 05/20/2013 _x86_64_ (2 CPU)

avg-cpu: %user %nice %system %iowait %steal %idle

0.37 0.01 0.11 0.15 0.00 99.35

avg-cpu: %user %nice %system %iowait %steal %idle

21.69 0.00 6.10 65.42 0.00 6.78

avg-cpu: %user %nice %system %iowait %steal %idle

21.48 0.00 5.87 63.76 0.00 8.89

avg-cpu: %user %nice %system %iowait %steal %idle

19.35 0.00 7.30 68.76 0.00 4.58

The first iostat example just shown starts with a quiet system, and then several applications start up. You can see that, after the first line which shows average use since the system was last booted, most of the processing to start the applications is being done in user space.

The second iostat example shows a case where several large files are copied from one hard disk to another. The result is a higher percentage of time being spent waiting for I/O requests to complete (%iowait).

The following examples use iostat to print CPU utilization reports with timestamps:

$ iostat -c -t Print time stamp with CPU report

Linux 3.2.0-38-generic (ubuntutb) 05/20/2013 _x86_64_ (2 CPU)

05/20/2013 07:08:20 PM

avg-cpu: %user %nice %system %iowait %steal %idle

0.40 0.01 0.12 0.21 0.00 99.26

$ iostat -c -t 2 10 Repeat every 2 seconds for 10 times

The dstat command (dstat package) is available as an alternative to iostat for viewing information about your CPU usage (as well as other performance-related items). One advantage of dstat over other tools is that it more precisely shows the units of measurement it is displaying (such as kilobytes or megabytes) and also uses colors to differentiate the data. To install the dstat package, type the following:

$ sudo apt-get install dstat

Here is an example of dstat for displaying CPU information:

$ dstat -t -c 3 View CPU usage continuously with time stamps

----system---- ----total-cpu-usage----

time |usr sys idl wai hiq siq

20-05 19:14:38| 27 5 59 9 0 1

20-05 19:14:41| 34 7 55 3 0 1

20-05 19:14:44| 35 6 54 4 0 0

20-05 19:14:47| 21 4 74 1 0 0

20-05 19:14:50| 19 3 76 1 0 0

20-05 19:14:53| 19 4 77 0 0 0

20-05 19:14:56| 18 3 79 0 0 0

In this example, the output includes date/time values based on the start of the epoch (-t) for the CPU report (-c) that is produced every three seconds (3). This report runs continuously until you stop it (Ctrl+c).

If you want to find out specifically which processes are consuming the most processing time, you can use the top command. Type top, and then press Shift+p to sort by CPU usage (this is the default sorting order):

$ top Display running processes and sort by CPU usage

top - 9:18:29 up 3 days, 16:36, 3 users, load avg: 0.29, 1.05, 1.73

Tasks: 192 total, 1 running, 191 sleeping, 0 stopped, 0 zombie

Cpu(s): 14.6%us,2.2%sy,0.0%ni,82.7%id,0.3%wa,0.0%hi,0.2%si,0.0%st

Mem: 4014504k total, 3305876k used, 708628k free, 360896k buffers

Swap: 4157436k total, 264k used 4157172k free,2229972k cached


11338 chris 20 0 1637m 48m 24m S 22 1.2 10:48.97 totem

2646 chris 20 0 1214m 88m 28m S 6 2.3 17:55.98 compiz

1750 root 20 0 206m 32m 13m S 4 0.8 8:53.63 Xorg

The full output would show many more processes, all sorted by current CPU usage (%CPU column). In this example, Totem Movie Player (22%), the compiz window manager (6%), and the X display server (4%) are consuming most of the CPU. If you decided you wanted to kill the totem process, you could type k followed by the process ID of totem (11338) and the number 9 signal (if for some reason you couldn’t just close the Totem window normally).

If you want information about the processor itself, you can view information directly from the /proc/cpuinfo file. Here is an example:

$ cat /proc/cpuinfo View CPU information from /proc

Processor : 0

vendor_id : GenuineIntel

cpu family : 6

model : 23

model name : Pentium(R) Dual-Core CPU E6300 @ 2.80GHz


Processor : 1

vendor_id : GenuineIntel

cpu family : 6

model : 23

model name : Pentium(R) Dual-Core CPU E6300 @ 2.80GHz


Flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr

pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht

tm pbe syscall nx lm constant_tsc arch_perfmon pebs bts rep_good

nopl aperfmperf pni dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16

xtpr pdcm xsave lahf_lm dtherm tpr_shadow vnmi flexpriority

bogomips : 5585.95

clflush size : 64


This particular machine has two processors, so information is repeated for each processor. For each processor, you can see the vendor’s name, the model name, and its speed (2.80GHz) in this case. Interesting things to note about your CPU are the flags that represent features that it supports. Some features in Ubuntu require that particular CPU extensions associated with those flags be on for the Ubuntu feature to work. For example, to use the Xen virtualization para-virtualized guests, the pae flag must be set. To run fully virtualized guests, the CPU must have either the vmx flag (for Intel processors) or svm flag (for AMD processors) extension support.

Similar information about your processor(s) is collected by the system at the very beginning of the boot process and can be obtained by looking at the beginning of the output of the dmesg command.

Monitoring Storage Devices

Basic information about storage space available to your Linux filesystems can be seen using commands such as du and df (as described in Chapter 7). If you want details about how your storage devices are performing, however, commands such as vmstat and iostat can be useful.

Some of the same kind of output from the iostat command shown earlier can be used to tell if bottlenecks occur while doing disk reads and writes. Here’s an example:

$ iostat 3 Check disk reads and writes per disk

Linux 3.2.0-38-generic (ubuntutb) 05/20/2013 _x86_64_ (2 CPU)

avg-cpu: %user %nice %system %iowait %steal %idle

0.48 0.02 0.13 0.23 0.00 99.15

Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn

sda 0.45 4.38 26.63 1400105 8517944

dm-0 0.68 4.37 26.62 1396897 8517196

dm-1 0.00 0.00 0.00 1152 736

scd0 0.39 24.94 0.00 7978782 0

sdb 0.01 0.01 1.49 2073 476784

Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn

sda 17.00 1909.33 0.00 5728 0

dm-0 17.00 1909.33 0.00 5728 0

dm-1 0.00 0.00 0.00 0 0

scd0 0.00 0.00 0.00 0 0

sdb 46.33 0.00 5462.67 0 16388

avg-cpu: %user %nice %system %iowait %steal %idle

0.00 0.00 0.34 74.41 0.00 25.25

Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn

sda 11.00 1257.33 0.00 3772 0

dm-0 11.00 1257.33 0.00 3772 0

dm-1 0.00 0.00 0.00 0 0

scd0 0.00 0.00 0.00 0 0

sdb 42.33 0.00 5021.33 0 15064


The first part of the output of iostat shows averages of CPU usage since the last reboot. The next part reflects processing that occurs when a large amount of data is copied from the first disk (sda) to the second disk (sdb). The dm-0 device is actually an LVM logical volume on sda that is connected to the root filesystem.

High iowait values indicate that disk input/output is the bottleneck on the system. In other words, faster disk writing would improve performance more than a faster CPU.

The vmstat command can also list statistics about your disks. Here’s an example of using vmstat to list information about disk reads and writes:

$ vmstat -d Display disk read, write, and input/output stats

disk- -----------reads------------ ------------writes----------- --IO---

total merged sectors ms total merged sectors ms cur sec


sda 80736 21757 9133194 1075864 101513 67798 27543352 97896 0 702

dm-0 101740 0 9127290 1568064 153265 0 27522840 24100 0 702

dm-1 288 0 2304 1680 2561 0 20488 6688 0 3

sr0 195504 697318 24962380 7629216 0 0 0 0 0 2129

sdb 457 126 4714 848 31786 307 7492448 075688 0 767

The Linux system in this example has two hard disks (sda and sdb). You can see the total number of sectors successfully read and written from those hard disks. You can also see how many seconds were spent on input/output (IO) for those disks. Furthermore, you can see if there are any I/O operations in progress, and you can also list read/write information for selected disk partitions. Here is an example:

$ vmstat -p sda1 Display read/write stats for a disk partition

sda1 reads read sectors writes requested writes

296 2172 10 24

Unfortunately the preceding command does not work with softraid md partitions, lvm partitions, and some hardware RAID driver-specific devices.

If you want to find out what files and directories are currently open on your storage devices, you can use the lsof command. This command can be particularly useful if you are trying to unmount a filesystem that keeps telling you it is busy. You can check what open file is preventing the unmount and decide if you want to kill the process holding that file open and force an unmount of the filesystem. Here is an example of lsof:

$ lsof | less List processes holding files/directories open


init 1 root cwd DIR 252,0 4096 2 /

init 1 root rtd DIR 252,0 4096 2 /

init 1 root txt REG 252,0 16719 3445 /sbin/init


bash 166 chris cwd DIR 8,17 4096 2 /mnt/a

The first files shown as being open are those held open by the init process (the first running process on the system). Files held open by system processes (such as udevd) and daemons (such as sshd and syslogd) follow init. Eventually, you will see files held open by individual users (which are probably the ones you are interested in if you are unable to unmount a disk partition).

Note You may see permission restrictions unless you run the sudo command first:

$ sudo lsof | less

When you are looking at the lsof output, you want to see the name of the file or directory that is open (NAME), the command that has it open (COMMAND), and the process ID of that running command (PID). In the preceding example, the current directory of a running bash shell is holding a directory open in the filesystem I want to unmount (/mnt/a). When a filesystem won’t unmount, it is often a bash shell that has its current directory on that filesystem that is preventing the unmount. Instead of piping lsof output to less or grep, here are a few other ways to find what you are looking for from lsof output:

$ lsof -c bash List files open by bash shells

$ lsof -d cwd List directories open as current directory

$ lsof -u chris List files and directories open by user chris

$ lsof /mnt/sda1 List anything open on /mnt/sda1 file system

$ lsof +d /mnt/sda1/dx/ List anything open under /mnt/sda1/dx/

As noted previously, you may need to use the sudo command to acquire the root permissions to view all the output of the lsof command.

Mastering Time

Keeping correct time on your Linux system is critical to the system’s proper functioning. Your computer running Linux keeps time in two different ways: a system clock (which Linux uses to keep track of time) and a hardware clock (which sets the system time when Linux boots up).

The system time is used to set timestamps for file creation, process run times, and anything else where date and time are used. System time can be viewed and set manually (with the date command) or automatically (with the ntpdservice).

The hardware clock is part of the motherboard’s CMOS and runs on a battery attached to the motherboard when the system is powered off. You set the hardware clock with the hwclock command.

There are many other tools that can be used to work with time in Linux systems. For example, there are tools for checking time in different ways, such as using clockdiff (to measure clock difference between computers) and uptime (to see how long your system has been up).

Changing Time/Date with Graphical Tools

Graphical tools in Ubuntu and other Linux systems for changing the date, time, and time zone used on your system include the Time & Date window (accessible by selecting Time & Date from the Ubuntu Dashboard). That window can also be used to enable the Network Time Protocol (NTP), to automatically synchronize your Linux system’s date and time over the network.

The Time & Date Properties window saves the settings and choices you make. During Ubuntu startup, the system reads these settings to set your time zone and whether your system is using UTC time.

Your Linux system’s time zone is set based on the contents of the /etc/localtime file. You can set a new time zone immediately by copying the file representing your time zone from a subdirectory of /usr/share/zoneinfo. For example, to change the current time zone to that of America/Chicago, you could do the following:

$ sudo cp /usr/share/zoneinfo/America/Chicago /etc/localtime

This can also be accomplished by creating a symlink:

$ sudo ln -s /usr/share/zoneinfo/America/Chicago /etc/localtime

To change the time zone permanently, set it in the Time & Date window to the time zone you want—for example, New York, United States.

Displaying and Setting Your System Clock

The date command is the primary command-based interface for viewing and changing date and time settings, if you are not having that done automatically with NTP. Here are examples of date commands for displaying dates and times in different ways:

$ date Display current date/time/time zone

Sun Jul 7 20:13:00 EDT 2013

$ date '+%A %B %d %G' Display day, month, day of month, year

Sunday July 7 2013

$ date '+The date today is %F.' Add words to the date output

The date today is 2013-07-07.

$ date --date='4 weeks' Display date four weeks from today

Sun Aug 4 20:16:09 EDT 2013

$ date --date='8 months 3 days' Display date 8 months 3 days from today

Mon Mar 10 20:27:46 EDT 2014

$ date --date='4 Jul' +%A Display day on which July 4 falls


Although the primary interest in this section is time, while I’m on the subject of dates, the cal command is also a quick way to display dates by month. Here are examples:

$ cal Show current month calendar (today is highlighted)

July 2013

Su Mo Tu We Th Fr Sa

1 2 3 4 5 6

7 8 9 10 11 12 13

14 15 16 17 18 19 20

21 22 23 24 25 26 27

28 29 30 31

$ cal 2013 Show whole year's calendar


January February March

Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa

1 2 3 4 5 1 2 1 2

6 7 8 9 10 11 12 3 4 5 6 7 8 9 3 4 5 6 7 8 9

13 14 15 16 17 18 19 10 11 12 13 14 15 16 10 11 12 13 14 15 16

20 21 22 23 24 25 26 17 18 19 20 21 22 23 17 18 19 20 21 22 23

27 28 29 30 31 24 25 26 27 28 24 25 26 27 28 29 30



$ cal -j Show Julian calendar (numbered from January 1)

July 2013

Su Mo Tu We Th Fr Sa

182 183 184 185 186 187

188 189 190 191 192 193 194

195 196 197 198 199 200 201

202 203 204 205 206 207 208

209 210 211 212

The date command can also be used to change the system date and time. For example:

$ sudo date 081215212013 Set date Aug. 12, 2:21PM, 2013

Mon Aug 12 15:21:00 EDT 2013

$ sudo date --set='+7 minutes' Set time to 7 minutes later

Mon Aug 12 15:28:39 EDT 2013

$ sudo date --set='-1 month' Set date/time to a month earlier

Fri Jul 12 15:29:11 EDT 2013

The next time you boot Ubuntu, the system time will be reset based on the value of your hardware clock (or your NTP server, if NTP service is enabled). And the next time you shut down, the hardware clock will be reset to the system time in order to preserve that time while the machine is powered off. To change the hardware clock, you can use the hwclock command.

Displaying and Setting Your Hardware Clock

Anyone can use the hwclock command to view hardware clock settings; however, you must have root privileges to change those settings. To use hwclock to view the current time from your computer’s hardware clock, type the following:

$ hwclock -r Display current hardware clock settings

Mon 20 May 2013 08:14:55 PM EDT -0.312931 seconds

Even if your hardware clock is set to UTC time, hwclock displays local time by default. If your system time strays from your hardware clock (for example, if you tried some of the date commands shown previously), you can reset your system clock from your hardware clock as follows:

$ sudo hwclock --hctosys Reset system clock from hardware clock

Likewise, if your hardware clock is set incorrectly (for example, if you replaced the CMOS battery on your motherboard), you can set the hardware clock from your system clock as follows:

# hwclock --systohc Reset hardware clock from system clock

Over time your hardware clock can drift. Because the clock tends to drift the same amount each day, hwclock can keep track of this drift time (which it does in the /etc/adjtime file). You can adjust the hardware clock timebased on the adjtime file as follows:

$ sudo hwclock --adjust Adjust hardware clock time for drift

To set the hardware clock to a specific time, you can use the --set option. Here is an example:

$ sudo hwclock --set --date="8/12/13 18:22:00" Set new date/time

In this example, the hardware clock is set to August 12, 2013 at 6:22 p.m. This update does not immediately affect the system clock.

Using Network Time Protocol to Set Date/Time

If you didn’t configure Network Time Protocol (NTP) to set time for your system when you first installed your Linux system, you can do so later by turning on the ntpd service. You can install and configure the ntpd service (ntpddaemon) with just a few commands.

Use the following command to enable the service from the shell:

$ sudo apt-get install ntp Install ntp package if necessary,

start the service

The ntpd service uses information in the /etc/ntpd.conf file. You can specify the name or IP address of the time server to query for the time, for example.

The resulting NTP setup turns your machine into a time server, listening on UDP port 123. Unless you have very specific needs (and your own GPS or atomic clock), running ntpd on your machine can be both a waste of resources and a security risk. For that reason, some system administrators prefer using ntpdate (often in a daily cronjob) to set their system time via NTP:

$ sudo ntpdate

08 July 20:21:19 ntpdate[16121]:

step time server offset 31565336.613857 sec

If you try running ntpdate while ntpd is running, you will get the following error:

$ sudo ntpdate

08 July 00:37:00 ntpdate[9695]: the NTP socket is in use, exiting

Note that the ntpdate command has been marked as deprecated and will disappear in the future.

Managing the Boot Process

When a computer first starts up, the basic input/output system (BIOS) looks to its boot order settings to determine where to find the operating system to boot. Typically, if a bootable medium has not been inserted into a removable drive (CD, DVD, floppy disk, and so on), the BIOS looks to the master boot record (MBR) on the first bootable hard disk. At this point, for most Linux systems, control of the boot process is handed to the boot loader.

For Ubuntu, and, in fact, most Linux systems these days, the Grand Unified Boot Loader (GRUB) is the boot loader that is used by default. GRUB can be set up to boot not only your Linux system, but also any other operating systems installed on your hard disks (Windows, BSD, or others). GRUB can also include boot options with each bootable operating system to refine the boot process, such as to turn on or off support for a particular type of hardware.

Ubuntu 9.10 and beyond use the second version of GRUB (GRUB 2) as its boot loader. GRUB 2 is much faster and configurable than the previous version (often called GRUB legacy). It is also harder to learn.

Once a Linux system is selected to boot from the boot loader, the boot loader loads the kernel. The following dilemma then occurs: The kernel needs to mount the root filesystem on the hard drive. This requires the appropriate storage drivers (block device kernel modules). And those drivers are located on the hard drive itself!

To break that vicious cycle, a small initial ramdisk (initrd) containing the block device modules (along with various tools and configuration files) is mounted by the boot loader. This allows the Linux kernel to read the root filesystem. After that, the init process takes over and begins starting the system services, based on the runlevel that is set for the system.

The following sections describe commands for modifying the boot loader, startup scripts, and run levels associated with your Linux system.

Understanding the GRUB Boot Loader

If GRUB was set up when you first installed Ubuntu, the settings for your boot loader are stored in the /boot/grub/grub.cfg file. Unlike the main configuration files in GRUB Legacy (menu.lst or grub.cfg), you should not edit the grub.cfg file directly. The grub.cfg file is overwritten from the contents of files in the /etc/grub.d directory any time a new kernel is installed or when you run the update-grub command.

The /boot/grub/grub.cfg file contains settings to control how GRUB behaves and how modules must be loaded, as well as other information required to start the operating system. Of most interest, however, are the entries that launch the kernel to start Ubuntu (or other operating system). The following example shows entries in the grub.cfg file that are used to create menu entries on your GRUB boot menu:

menuentry 'Ubuntu, with Linux 3.2.0-38-generic' --class ubuntu

--class gnu-linux --class gnu --class os



gfxmode $linux_gfx_mode

insmod gzio

insmod part_msdos

insmod ext2

set root='(hd0,msdos1)'

search --no-floppy --fs-uuid

--set=root 0d2494ad-62e6-43d4-8aff-780630dd65d

linux /vmlinuz-3.2.0-38-generic root=/dev/mapper/ubuntutb-root ro

initrd /initrd.img-3.2.0-38-generic


This example shows a menuentry for a bootable operating system ('Ubuntu, with Linux 3.2.0-38-generic') to identify the version of the kernel that runs when this menu item is selected. Information on the menuentry line identifies the entry as an Ubuntu Linux operating system. Within that section, the screen resolution of the Terminal window is set (gfxmode) and modules are loaded to uncompress zip files (gzio), handle DOS partition structure (part_msdos), and support ext2 filesystem types needed to use the /boot partition (ext2).

The root partition is on the first hard disk (hd0) and its partition table is formatted as a DOS partition (part_msdos). The most critical data, however, is the identity of the kernel (/vmlinuz-3.2.0-38-generic), the device representing the root (/) filesystem (/dev/mapper/ubuntub-root), and the location of the initial RAM disk (/initrd.img-3.2.0-38-generic).

Modifying the GRUB Boot Loader

Because the grub.cfg file is created from files in the /etc/grub.d directory, to change your grub configuration you can do one of the following instead of editing the file directly:

· Custom file—Add entries to the /etc/grub.d/40_custom file that you want included in your grub.cfg file.

· Default file—The /etc/default/grub file contains settings, such as how long GRUB should pause before starting the boot process (to give you time to interrupt it).

Once you have changed Custom or Default files, you can make those changes take effect by either installing a new kernel or by running the update-grub command.

Other activities that didn’t happen previously with GRUB Legacy now automatically take place with GRUB 2. For example, if GRUB detects a Windows system or other operating system on any of your disks, it will add an entry to the GRUB menu so that when you reboot, you have the option of selecting that operating system to boot from.

Controlling Startup and Run Levels

After the kernel has started up, it hands control of the system to the init process. The init process becomes the first running process on the system (PID 1), directing the startup of other processes based on the contents of the /etc/init/ directory, the default run level, and the init scripts set to run at that run level.

The default run level is typically set to 2 for Ubuntu systems (based on the value of the DEFAULT_RUNLEVEL line in the /etc/init/rc-sysinit.conf file). That value can be overridden by adding the runlevel you want to the env RUNLEVEL= line in the rc-sysinit.conf file.

Ubuntu 12.04 LTS supports both the old style System V init scripts (stored in the /etc/init.d/directory) and the newer Upstart-style configuration files (stored in the /etc/init directory). As an administrator, you can change the default runlevel, change the current runlevel, or change which services start and stop at each runlevel.

Most Linux administrators leave the basic startup features alone and focus on which services are turned on or off at the selected run level. This section contains commands for working with system initialization scripts and changing run levels.

As root, you can use the runlevel command to see the current run level:

$ runlevel Display current and previous run levels

N 2

Because the system in this example booted directly to run level 2, there is no previous run level (N). To change the current run level, you can use the init command. For example, from the console, if you want to shut down the GUI and network and then go into single-user mode for system maintenance, you can type the following:

$ sudo init 1 Change run level to 1 (single user mode)

In this example, the current run level changes from the previous level (in this case, 2) to run level 1 (which puts the system in single-user mode).To manage services, you can use the update-rc.d and service commands. For example, to start the NTP service immediately, you can type this:

$ sudo /etc/init.d/ntp start Start NTP service immediately

Starting NTP server ntpd [ OK ]

Each System V service comes with a shell script in the /etc/init.d directory. You can pass start or stop options to each service. The format is as follows:

sudo /etc/init.d/service_to_control start

sudo /etc/init.d/service_to_control stop

Most of the scripts in the /etc/init.d directory support start and stop options, while some support other features as well. Here’s how to use service to start and stop services:

$ /etc/init.d/ntp Show usage statement (no options)

Usage: /etc/init.d/ntp {start|stop|restart|try-restart|


$ sudo /etc/init.d/ntp restart Restart NTP service

* Stopping NTP server ntpd [ OK ]

* Starting NTP server ntpd [ OK ]

$ sudo /etc/init.d/ntp try-restart Restart NTP if already running

* Stopping NTP server ntpd [ OK ]

* Starting NTP server ntpd [ OK ]

$ sudo /etc/init.d/ntp force-reload Reload config file to daemon

* Stopping NTP server ntpd [ OK ]

* Starting NTP server ntpd [ OK ]

$ sudo /etc/init.d/ntp status Check if the NTP is running

* NTP server is running.

$ sudo /etc/init.d/ntp stop Stop NTP service

* Stopping NTP server ntpd [ OK ]

Any of the init scripts contained in /etc/init.d can be started in this way, but not all scripts support all the features just shown. Most init scripts, however, will show their usage statement with no option (as shown in the first example in the preceding code).

Although the previous commands start the run level script service immediately, to have a service start automatically at boot time or during a run level change, you can use the update-rc.d command. In addition, most installation scripts for services will automatically turn a service on for the next time you boot. With update-rc.d, you can turn services on, or turn them off on a per-runlevel basis. For a service that is not yet configured to run at all, you can use the defaults option:

$ sudo update-rc.d ntp defaults Turn on the NTP service

Although you can use the init command to change to any run level, including init 0 (shut down) and init 6 (reboot), there are also specific commands for stopping Linux. The advantage of commands such as halt, reboot, poweroff, and shutdown is that they include options that enable you to stop some features before shutdown occurs. For example:

Warning Don’t try the following commands if you don’t intend to actually turn off your system, especially on a remote system.

$ sudo reboot Reboot the computer

$ sudo halt -n Don’t sync hard drives before shutdown

$ sudo halt -h Put hard drives in standby before halting

$ sudo shutdown 10 Shutdown in ten minutes after warning users

$ sudo shutdown -r 10 Reboot ten minutes after warning users

$ sudo shutdown 10 ‘Bye!’ Send ‘Bye’ to users before shutdown

Besides the reboot and init 6 commands, you can also use the old PC keystrokes Ctrl+Alt+Del to reboot your computer.

Straight to the Kernel

In general, when the kernel starts up on your Linux system, you shouldn’t have to do too much with it. However, there are tools for checking the kernel that is in use and for seeing information about how the kernel started up. Also, if something goes wrong or if there is some extra support you need to add to the kernel, there are tools to do those things.

To find out what kernel is currently running on your system, type the following:

$ uname -r Display name of kernel release


$ uname -a Display all available kernel info

Linux ubuntutb 3.2.0-38-generic #61-Ubuntu SMP

Tue Feb 19 12:18:21 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

When the kernel starts, messages about what occurs are placed in the kernel ring buffer. You can display the contents of the kernel ring buffer using the dmesg command:

$ dmesg |less

[ 0.000000] Linux version 3.2.0-38-generic (buildd@akateko)

(gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5) ) #61-Ubuntu SMP

Tue Feb 19 12:18:21 UTC 2013 (Ubuntu 3.2.0-38.61-generic 3.2.37)

[ 0.000000] Command line: BOOT_IMAGE=/vmlinuz-3.2.0-38-generic

root=/dev/mapper/ubuntutb-root ro

[ 0.000000] KERNEL supported cpus:

[ 0.000000] Intel GenuineIntel

[ 0.000000] AMD AuthenticAMD

[ 0.000000] Centaur CentaurHauls


If that buffer fills up, it may no longer contain the beginning of the recorded information. In that case, you can use less /var/log/dmesg.

You can find additional information about kernel processing in the /var/log files, in particular the messages file. You can page through those files as follows:

$ sudo less /var/log/messages Page through messages file

May 23 21:36:05 ubuntutb kernel: [12567.769587] Inbound

IN=eth0 OUT= MAC=40:61:86:c3:35:f8:48:5b:39:e7:95:72:08:00


S=0x00 PREC=0x00

TTL=63 ID=58788 DF PROTO=TCP SPT=49304 DPT=22 WINDOW=14600


In the best circumstances, all the hardware connected to your computer should be detected and configured with the proper Linux drivers. In some cases, however, either the wrong driver is detected or the necessary driver may not be available on your system. For those cases, Linux offers ways of listing loadable kernel modules and adding new ones to your system.

The lsmod command enables you to view the names of the loaded modules, their size, and what other modules are using them. Consider the following example:

$ lsmod

Module Size Used by


ext2 73795 1

dm_crypt 23125 0

iptable_nat 13229 0

nf_nat 25891 2 ipt_MASQUERADE,iptable_nat

nf_conntrack_ipv4 19716 9 iptable_nat,nf_nat


snd_hda_codec_realtek 224173 1

snd_hda_intel 33773 3

snd_hda_codec 127706 2 snd_hda_codec_realtek,snd_hda_intel

snd_hwdep 17764 1 snd_hda_codec

lp 17799 0

parport 46562 3 parport_pc,ppdev,lp

If you want to find out more information about a particular module, you can use the modinfo command. Here’s an example:

$ modinfo parport_pc



license: GPL

description: PC-style parallel port driver

author: Phil Blundell, Tim Waugh, others


If you decide you need to add or remove a loadable module to get some hardware item on your system working properly, you can use the modprobe command. You can also use modprobe to list all available modules and remove modules. Consider the following examples:

$ modprobe -l | grep c-qcam Show c-qcam from module list


$ sudo modprobe c-qcam Load module for Color QuickCam

$ sudo modprobe -r c-qcam Remove module for Color QuickCam

Note You may hear about the command insmod for loading modules. While insmod loads an individual module, modprobe loads any additional modules required to get the requested module working as well. Therefore, modprobe is the preferred command in most cases.

You can control kernel parameters with the system running using the sysctl command. You can also add parameters permanently to the /etc/sysctl.conf file, so they can load as a group or at each reboot. Consider the following examples:

$ sudo sysctl -a | less List all kernel parameters

kernel.panic = 0

kernel.core_uses_pid = 0


$ sudo sysctl kernel.hostname List value of particular parameter

$ sudo sysctl -p Load parms from /etc/sysctl.conf

$ sudo sysctl -w kernel.hostname=joe Set value of kernel.hostname

As noted earlier, if you want to change any of your kernel parameters permanently, you should add them to the /etc/sysctl.conf file. Parameter settings in that file are in the form parameter = value. Parameters set with sysctl -wwill return to their default values the next time you reboot your system.

Poking at the Hardware

If you just generally want to find out more about your computer’s hardware, you can use the following commands. The lspci command lists information about PCI devices on your computer:

$ lspci List PCI hardware items

00:00.0 Host bridge: Intel Corporation 4 Series Chipset

DRAM Controller (rev 03)

00:02.0 VGA compatible controller: Intel Corporation 4 Series

Chipset Integrated Graphics Controller (rev 03)

00:02.1 Display controller: Intel Corporation 4 Series Chipset

Integrated Graphics Controller (rev 03)

00:1b.0 Audio device: Intel Corporation N10/ICH 7 Family

High Definition Audio Controller (rev 01)


$ lspci -v List PCI hardware items with more details

$ lspci -vv List PCI hardware items, even more details

Using the dmidecode command, you can display information about your computer’s hardware components, including information about what features are supported in the BIOS. Consider the following example:

$ sudo dmidecode | less List hardware components

SMBIOS 2.5 present.

41 structures occupying 1535 bytes.

Table at 0x0009F400Handle 0x0000, DMI type 0, 24 bytes.

BIOS Information

Vendor: MSI

Version: 130

Release Date: 10/06/2013


Processor Information

Socket Designation:


Type: Central Processor

Family: Unknown

Manufacturer: Intel

ID: 7A 06 01 00 FF FB EB BF

Version: Pentium(R) Dual-Core CPU E6300 @ 2.80GHz

You can use the hdparm command to view and change information relating to your hard disk.

Warning Although it’s safe to view information about features of your hard disks, it can potentially damage your hard disk to change some of those settings.

Here are some examples of printing information about your hard disks:

$ sudo hdparm /dev/sda Display hard disk settings (SATA or SCSI)


multcount = 16 (on)

IO_support = 1 (32-bit)

readonly = 0 (off)

readahead = 256 (on)

geometry = 19457/255/63, sectors = 312581808, start = 0

$ sudo hdparm /dev/hda Display hard disk settings (IDE drive)

$ sudo hdparm -I /dev/sda Display detailed drive information


ATA device, with non-removable media

Model Number: ST3160815AS

Serial Number: 6RA5EGGJ

Firmware Revision: 3.AAD



Ubuntu and other Linux systems make it easy for you to watch and modify many aspects of your running system to make sure it is operating at peak performance. Commands such as free, top, vmstat, slabtop, iostat, and dstatenable you to see how your system is using its memory, CPU, and storage devices. Using commands such as date, hwclock, and cal, as well as services such as NTP, you can watch and manage your system’s date and time settings.

To manage the features that are set and services that come up when you boot your system, you can modify features associated with your GRUB boot loader and system run levels. You can start, stop, list, add, and remove individual system services using commands such as service and update-rc.d. Commands such as reboot, halt, and shutdown enable you to safely stop or reboot your computer.

When it comes to managing your computer’s hardware, commands such as lsmod, modinfo, and modprobe enable you to work with loadable modules. You can view information about your hardware with such commands as lspci, dmidecode, and hdparm.