Administer Linux on the fly
时间:2007-03-11 来源:ooike
Administer Linux on the flyUse the /proc filesystem to get a handle on your system ![]() |
![]() |
Level: Intermediate
Graham White ([email protected]), IT specialist, Hursley, IBM 14 May 2003 The /proc filesystem is one of Linux's great features, and this article gives you a thorough grounding in some of its most useful aspects. With it, you can administer many details of the operating system without ever having to shut down and reboot the machine, which is a boon for those who need to keep their systems as available as possible.Anyone who has administered a system of commercial importance knows the value of uptime -- or, conversely, knows the headaches you get from users because of downtime. One of the main reasons a company will run a UNIX server is because of its reliability and stability. If managed carefully, there's usually no need to restart these servers for long periods of time. And to improve matters further, there are administrative tasks -- even at the kernel level -- that you can perform on the fly, keeping your servers available. While you may still need to restart a system to upgrade hardware or if someone trips over the power cord, it's good to know that many administrative tasks can be performed without disrupting service. This article includes hints and tips for performing various administrative tasks and changing your system without rebooting. Linux provides various ways to change underlying operating system values and settings while keeping the system up and running. These come in two basic forms, those that are general to all Linux systems and are provided in the Linux kernel (you can find more information about the Linux kernel and download kernel source at the Linux Kernel Archives; see Resources for a link), and those that are distribution specific and provided by the vendor. This article deals with both types. Note: This article is written for a 2.4-level kernel. Some of the options and features may be different for other kernel versions. Changing running kernel parameters Linux provides a really neat way for administrators to change the kernel while the system is running and without the need to reboot the kernel/system. This is done with a virtual filesystem called /proc. Linux Gazette provides one of the simplest and easiest references on /proc I have seen. (See Resources for a link.) Very basically, the /proc filesystem gives you a view into the running kernel, which can be useful for monitoring performance, discovering system information, finding out how the system is configured, and changing that configuration. This filesystem is called a virtual filesystem, because it is not really a filesystem at all. It's just a map provided by the kernel that is attached to your usual filesystem structure to give you access to it. The fact that we have some way of changing the running kernel parameters while the system is up and running gives the system administrator great power and flexibility in changing kernel settings. This sort of implementation was an inspired idea on the part of the Linux kernel developers. But can too much power be a bad thing? Sometimes. If you are going to change anything in the /proc filesystem, you must make sure that you know what you are changing and what effect this will have on the system. These are really useful techniques, but a wrong move can give you some rather undesired consequences. If you are new to this sort of thing or are not sure what effect one of your changes will have, practice on a machine that is not important to you or your business.
First, think about how not to make changes to the kernel. There are two good reasons why you should not just jump into the /proc filesystem, open a file in your text editor, make a bunch of changes, and save the file back out again. These are:
The answer to making changes to any of these files, therefore, is not to use an editor. When making changes to anything at all in the /proc filesystem, you should use the echo command and redirect the output from the command line into your chosen files under /proc. For example: echo "Your-New-Kernel-Value" > /proc/your/file Similarly, if you wish to view information from /proc, you should either use a command that is designed for the purpose or use the command line cat command.
You do not need to be a kernel hacker to get good use out of /proc, and a basic understanding of the structure of this filesystem will aid you greatly. You may find that you don't need to know about anything in /proc, until the day a user asks you for a certain bit of functionality that makes you glad you bothered to learn where to look to make changes. The /proc filesystem helps the system administrator in this respect via its structure and file permissions. Each file in /proc has a very particular set of file permissions assigned to it and will be owned by a particular user ID. This is very carefully done so that the correct functionality is presented to the administrator and to the users. The following list summarizes what particular permissions may do on individual files:
A very broad generalization about /proc is that you will find most of it read-only except for the /proc/sys directory. This directory is the one that holds most kernel parameters (rather than information) and is the one that is designed to be changed while the system is running. As a result, this is the directory that this article will look mainly at. The last thing to know about learning what to change in /proc is what you should actually be writing to these files. You will notice as you look at various files in /proc that some of them are human readable and some are data files. The data files can still be read by using specific utilities such as top, lspci, and free. You will also notice that the human-readable files take two different formats: some are binary switches and others contain more information. The binary switch files only contain a zero (off) or a one (on) for that particular kernel feature.
Detailing the exact information and usage of each file in /proc is outside the scope of this article. For more information about any /proc files not discussed in this article, one of the best sources is the Linux kernel source itself, which contains some very good documentation. The following files in /proc are more useful to a system administrator. This is not meant to be an exhaustive treatment but an easy-access reference for day-to-day use.
/proc/scsi/scsi echo "scsi add-single-device w x y z" > /proc/scsi/scsi For this command to work properly, you must get the parameter values w, x, y, and z correct, as follows:
Once your disk has been added to the system, you can mount any previously formatted filesystems or you can start formatting it, and so on. If you are not sure about what device the disk will be, or you want to check any pre-existing partitions, for example, you can use a command such as fdisk -l, which will report this information back to you. Conversely, the command to remove a device from your system without a reboot would be: echo "scsi remove-single-device w x y z" > /proc/scsi/scsi Before you enter this command and remove your hot-swap SCSI disk from your system, make sure you have unmounted any filesystems from this disk first.
/proc/sys/fs/file-max Default setting: 4096
/proc/sys/fs/file-nr
/proc/sys/fs/inode-*
/proc/sys/fs/overflowuid and /proc/sys/fs/overflowgid Default Setting: 65534
/proc/sys/fs/super-max Default setting: 256
/proc/sys/fs/super-nr
/proc/sys/kernel/acct
Default setting: 2 4 30 These values will stop accounting if there is less than 2 percent free space on the filesystem that contains the log and starts it again if there is 4 or more percent free space. Checks are made every 30 seconds.
/proc/sys/kernel/ctrl-alt-del
Default setting: 0
/proc/sys/kernel/domainname
/proc/sys/kernel/hostname
/proc/sys/kernel/msgmax Default setting: 8192
/proc/sys/kernel/msgmnb Default setting: 16384
/proc/sys/kernel/msgmni Default setting: 16
/proc/sys/kernel/panic Default setting: 0
/proc/sys/kernel/printk
Default setting: 6 4 1 7
/proc/sys/kernel/shmall Default setting: 2097152
/proc/sys/kernel/shmax Default setting: 33554432
/proc/sys/kernel/shmmni Default setting: 4096
/proc/sys/kernel/sysrq Default setting: 0
/proc/sys/kernel/threads-max Default setting: 2048
/proc/sys/net/core/message_burst Default setting: 50 (5 seconds)
/proc/sys/net/core/message_cost Default setting: 5
/proc/sys/net/core/netdev_max_backlog Default setting: 300
/proc/sys/net/core/optmem_max
/proc/sys/net/core/rmem_default
/proc/sys/net/core/rmem_max
/proc/sys/net/core/wmem_default
/proc/sys/net/core/wmem_max
/proc/sys/net/ipv4
/proc/sys/net/ipv6
/proc/sys/vm/buffermem
Default setting: 2 10 60
/proc/sys/vm/freepages
Default setting: 512 768 1024
/proc/sys/vm/kswapd
Default setting: 512 32 8
/proc/sys/vm/pagecache
Making your kernel settings persistent A handy utility is provided for making changes to any kernel parameters under the /proc/sys directory. It allows you to make changes to the running kernel (similarly to the echo and redirection method used above), but it also has a configuration file that is executed on system boot. This lets you make changes to the running kernel and add them to the configuration file so that any changes you make will remain after a system reboot. The utility is called sysctl and is fully documented in the man pages at sysctl(8). The configuration file for sysctl is /etc/sysctl.conf, which can be edited and is documented under sysctl.conf(8). Sysctl treats the files under /proc/sys as individual variables that can be changed. So, for example, the file under /proc/sys that represents the maximum number of file handles allowed on the system, /proc/sys/fs/file-max, is represented as fs.file-max. This example reveals some oddities in sysctl notation. Since sysctl can only change variables under the /proc/sys directory, that part of the variable name is missing as the variables are always assumed to be under that directory. The next change to note is that the directory separators (slash, /) have changed to periods (dot, .). There are two simple rules for converting between files in /proc/sys and variables in sysctl:
These two rules will let you swap any file name in /proc/sys for any variable name in sysctl. The general file to variable conversion is:
/proc/sys/dir/file --> dir.file You can view all the variables that are available to be changed, along with their current setting, using the command sysctl -a. Variables can also be changed using sysctl, which does exactly the same job as the echo method used above. This notation is as follows: sysctl -w dir.file="value" Using the file-max example again, we could change this value to 16384 using one of two methods as follows: sysctl -w fs.file-max="16384" Or: echo "16384" > /proc/sys/fs/file-max Don't forget that sysctl does not add changes made to the configuration file; this is left for you to do manually. If you want your changes to persist after a reboot, you must maintain this file. Note: Not all distributions provide sysctl support. If this is the case for your particular system, then you can use the echo and redirect method described above and add these commands to a start-up script so they are executed every time the system boots.
Commands for setting the system It is possible to change other non-kernel system parameters while the system is running and also get these settings to take effect without rebooting. These can mainly be classified as services, daemons, and servers that will be listed in the /etc/init.d directory. Since there is an increasingly wide range of scripts that can be listed in this directory, it is not possible to go through all the different configurations here. However, below are a few examples of how the /etc/init.d scripts can be manipulated on different distributions of Linux. Examples of where changes to a daemon and a reload of the configuration without rebooting might be useful are:
First, the generic way of manipulating system services is directly, via the scripts in /etc/init.d. These scripts take parameters to manipulate the services that they control; you can type the script name without any parameters to see what the valid options are. Common parameters are:
As an example, the following command would reload your xinetd configuration without terminating any connected user's sessions (useful if you make a change to /etc/xinetd.conf): /etc/init.d/xinetd reload Red Hat provides a command, service, that will manipulate services for you. The service command provides the same functionality as typing the script name itself. The syntax is as follows: service script-name [parameter] For example: service xinetd reload SuSE also provide a command called rc. This is similar to the service command above, but has no space between the command and the script name. The syntax is as follows: rc{script-name} parameter For example: rcapache start Similarly to changing kernel parameters, once you reboot your system, any changes made to services will be lost. More and more distributions are adopting the use of the chkconfig command, which manages the services that are started at various run levels (including on boot). At the time of this writing, the chkconfig command syntax differs slightly on different versions of Linux, but if you enter the command chkconfig without any parameters, you will get a list of how to use it. More information about chkconfig can also be found via the man pages at chkconfig(8).
Configuring the Linux kernel on the fly using the /proc filesystem isn't to be taken lightly, but once you understand its structure and how to manipulate the various files and parameters, you've gained the use of a powerful tool for keeping your servers available around the clock.
I would like to thank Mr. Adrian Fewings for proofreading this article. |