How to detect single user mode in FreeBSD ?
Polytropon
freebsd at edvax.de
Wed Jun 13 13:47:01 UTC 2018
On Wed, 13 Jun 2018 18:04:08 +0530, Manish Jain wrote:
> On 06/13/18 15:55, Shane Ambler wrote:
> > When in single user mode PID 1 should be "/sbin/init -s" which becomes
> > "/sbin/init --" in multi user mode.
> >
> > The next closet to knowing would be looking at mount, in single user
> > mode you will only have / mounted read only and /dev. It is rare for any
> > system to be past single user mode with only that mount setup but the
> > user can mount the filesystems before starting your script and still be
> > in single user mode.
>
> Hi Shane,
>
> Tx for replying. But don't you think there should ideally be a sysctl to
> be detect the runlevel, particularly single user mode ?
Currently, there is no such sysctl availablr (or another
utility that prints the current runlevel - see runlevel-
related comment later on).
> It makes things
> easily documentible, just as when we can use sysctl to find out if the
> OS is virtualized (I think kern.vm_guest).
You can probably work with the information from "man 8 init", such as
If the system security level (see security(7)) is initially nonzero, then
init leaves it unchanged. Otherwise, init raises the level to 1 before
going multi-user for the first time. Since the level cannot be reduced,
it will be at least 1 for subsequent operation, even on return to single-
user. If a level higher than 1 is desired while running multi-user, it
can be set before going multi-user, e.g., by the startup script rc(8),
using sysctl(8) to set the kern.securelevel variable to the required
security level.
[...]
If run as a user process as shown in the second synopsis line, init will
emulate AT&T System V UNIX behavior, i.e., super-user can specify the
desired run-level on a command line, and init will signal the original
(PID 1) init as follows:
Run-level Signal Action
0 SIGUSR2 Halt and turn the power off
1 SIGTERM Go to single-user mode
6 SIGINT Reboot the machine
c SIGTSTP Block further logins
q SIGHUP Rescan the ttys(5) file
See "man 8 init" for further details.
For details about the run-levels (which aren't that common to
the FreeBSD boot & startup mechanism, which is BSD-style init,
whereas runlevels appear in System V sysvinit-like mechanisms),
check:
https://docs.freebsd.org/doc/7.4-RELEASE/usr/share/doc/en_US.ISO8859-1/articles/linux-users/startup.html
https://www.freebsd.org/doc/en_US.ISO8859-1/articles/linux-users/startup.html
You could summarize it as "FreeBSD doesn't have runlevels". :-)
However, you can still check for a process that is typucally
run in multi-user mode. If it's _not_ running - well, you are
in single-user mode.
Such a test could look like this:
if ! ps -auxww | grep -v "grep" | grep "adjkerntz" > /dev/null; then
echo "Single user";
else
echo "Multi-user";
fi
In this example, a running instance of adjkerntz is being
process-grepped for.
Suggestion found here:
https://superuser.com/questions/457111/how-to-get-run-level-in-freebsd
--
Polytropon
Magdeburg, Germany
Happy FreeBSD user since 4.0
Andra moi ennepe, Mousa, ...
More information about the freebsd-questions
mailing list