Operating systems

A simple user primer for init

When working with Unix-like systems at the command line, understanding the basics of the init command and why it works can help.

Many Unix-like systems -- particularly those that follow the SysV model -- make use of the concept of the runlevel. On these systems, runlevels are different modes of operation, some of which can be customized by the system administrator.

In the Linux world, the typical assignment of functionality to runlevels is:

  • 0: system halted
  • 1: single user mode
  • 2: single user mode with networking
  • 3: text-only multi-user mode
  • 4-5: multi-user modes
  • 6: restart

Switching runlevels is simple from the command line. The init command takes a number as an argument that can be used to switch runlevels.

telinit

The actual init daemon starts when the system starts, and manages process startup and shutdown for the current runlevel. When you use the init command within a root user shell, it executes telinit, however. The telinit program can be used to switch to the runlevel corresponding to the numeric argument given to the init command. This means that the command init 0 will shut down the system, init 1 will shut down processes and enter single user mode, and init 6 will restart the system.

Three non-numeric arguments can also be used.

  • The letter q requests that init reload its configuration. It is largely unnecessary in many current Linux-based operating system configurations.
  • The letter s, can be used to enter single user mode as well. Care should be taken when doing so, however; init s does not shut down current processes the way init 1 does.
  • The letter u requests that init re-execute itself.

For the most part, numeric values will be the only arguments you will need to give the init command (and, by extension, the telinit command). In fact, most often you would not need anything but init 0 or init 6, with an occasional need to use init 1. It is typical for Linux-based systems to be set up to automatically boot into the appropriate runlevel for normal operation.

Configuration of which processes are started and stopped with a given runlevel is primarily handled by the contents of /etc/rcN.d directories. Within these directories, symlinks to scripts in the /etc/init.d directory indicate which processes should be started or stopped when entering or leaving a given runlevel.

BSD Unix init

The BSD Unix init command serves a similar role, but it does not use the SysV init system. On BSD Unix systems, init is actually a utility that executes the rc utility. In some ways much like SysV init, BSD rc manages startup of processes on boot. The init command is used with a somewhat different set of arguments, however, because it does not use SysV runlevels:

  • init 0: shut down the system
  • init 1: enter single user mode
  • init 6: restart the system
  • init c: block further logins
  • init q: rescan the ttys file

The q option serves a purpose similar to the same argument to the Linux/SysV version of the init command.

Configuration of the rc system can vary across systems that use it. In the case of FreeBSD, most relevant configuration is handled by the /etc/rc.conf file, and by rc scripts in the /etc/rc.d directory. See the rc.conf manpage for details.

shutdown

Many Unix-like systems provide a shutdown command that performs much the same purpose as certain init commands, and typically adds some convenient features such as sending warnings to user shells, delaying change of operating mode for a specified period of time or at a particular time of day, and kicking all users out of their logins and preventing all new logins. The shutdown command varies from system to system, and its manpage should be consulted for specifics on a given Unix-like OS.

This is not a comprehensive guide.

Obviously, an in-depth, comprehensive survey and explanation of the entire system related to the init command is beyond the scope of a single article. With a little bit of enthusiasm and time, however, a lot can be learned about how to manage system operation modes via commands like init and shutdown, and to configure the underlying system, from manpages.

About

Chad Perrin is an IT consultant, developer, and freelance professional writer. He holds both Microsoft and CompTIA certifications and is a graduate of two IT industry trade schools.

7 comments
apotheon
apotheon

I find that init is simpler in concept and requires less typing; that's good enough for me. I only use shutdown on the very rare occasion that I need one of its convenience features, which is almost never. I find init 0 easier to remember and use than shutdown -h now, for instance, and init 6 easier than shutdown -r now. Then, of course, there's always the issue of subtle differences between shutdown implementations, such as the fact that -P on Ubuntu is equivalent to -p on FreeBSD. Your mileage may vary.

robo_dev
robo_dev

that only allow console access in single-user mode. Therefore you need to goto runlevel 1 to fix something. Shutdown won't help, unless you interrupt it on bootup or change init scripts. Windows Recovery Console is effectively runlevel 1 for that OS.

Sterling chip Camden
Sterling chip Camden

... shut down services the way shutdown does, or is it more like 'reboot'?

Sterling chip Camden
Sterling chip Camden

You always have access to runlevel 1 on boot, if you want it. There was some discussion a while back on the freebsd-questions list about the difference between shutdown and reboot. The general conclusion was that shutdown is safer, because of services getting stopped properly, etc. I wonder how init fits into that equation.

Sterling chip Camden
Sterling chip Camden

... it seems that init does everything that shutdown would do -- assuming that I'm reading it correctly. If so, I'll start using it -- less typing!

nwallette
nwallette

shutdown and init were pretty much the same. I saw "shutdown -h now" as an alias to "init 0". In Solaris, they are (were?) a little different. From what I read, the safest thing to do was to use init. But, I think most of that preference had to do with versions old enough that you had to use sync before shutting down to flush the FS cache. Also on Solaris, init 0 and init 1 both halt the system, but '1' adds a power-off command, whereas 0 takes you to the boot PROM.