Rebooting Directly Into Single User Mode

Latest response

Hello. Is there some one to perform a full reboot on a node that is in runlevel 5 and have it come up directly into single user mode without modifying grub.conf, inittab, or manual intervention (stopping grub at boot, typing 'single')?

Some of our test systems have poorly coded init scripts that don't properly clean themselves up (which is another issue altogether), so doing the usual 'init S' doesn't always bring the system down to a true, stable state, so a fully CTRL-ALT-DELETE type of reboot directly into single user mode is preferred.

We've experimented with changing the RUNLEVEL variable but are wondering if there isn't an easier way to do this akin to the old Solaris method of typing 'reboot -- -s' at the command prompt.

Thank you,

SC

Responses

Hi there Stephen. Long-story short: there is no pre-made solution for this in any version of RHEL. (You didn't mention what version of RHEL you're on, but I'm assuming 6...?)

If I needed to do this, the way I would approach it is with a custom script that uses grubby, e.g.:

[root@r67 ~]# ls -l /usr/local/sbin/Sreboot
-rwx------. 1 root root 1394 Oct 23 10:11 /usr/local/sbin/Sreboot

[root@r67 ~]# cat /usr/local/sbin/Sreboot
#!/bin/bash
# Sreboot v0.0.0 last mod 2015/10/23
# Ryan Sawhill Aroha <rsaw@redhat.com>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#    General Public License <gnu.org/licenses/gpl.html> for more details.
#-------------------------------------------------------------------------------

if ! grubby --update-kernel=DEFAULT --args=S; then
  echo Error using grubby to modify default kernel to boot to single-user mode
  echo Aborting ...
  exit 2
fi
echo Modified default kernel to boot to single-user mode
echo Relevant kernel line:
grep --color -w S /etc/grub.conf
echo

cat >/etc/cron.d/grub-disable-single <<EOF
@reboot root /usr/local/sbin/grub-disable-single
EOF

cat >/usr/local/sbin/grub-disable-single <<EOF
#!/bin/bash
/sbin/grubby --update-kernel=ALL --remove-args=S
/bin/rm /etc/cron.d/grub-disable-single 2>/dev/null
EOF
chmod 700 /usr/local/sbin/grub-disable-single

echo Configured crond to disable single-user mode after next multi-user boot
shutdown -r +1

Here's what it looks like:

[root@r67 ~]# Sreboot 
Modified default kernel to boot to single-user mode
Relevant kernel line:
    kernel /vmlinuz-2.6.32-573.el6.x86_64 ro root=UUID=cf39816d-004e-4422-b32d-001afb92ba1a rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD console=ttyS0,115200 console=tty0 console=ttyS0,115200 rd_NO_PLYMOUTH crashkernel=auto SYSFONT=latarcyrheb-sun16  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM S

Configured crond to disable single-user mode after next multi-user boot

Broadcast message from root@r67.example.com
    (/dev/ttyS0) at 10:12 ...

The system is going down for reboot in 1 minute!

...

Setting hostname r67.example.com:  [  OK  ]
Setting up Logical Volume Management: [  OK  ]
Checking filesystems
Checking all file systems.
[/sbin/fsck.ext4 (1) -- /] fsck.ext4 -a /dev/vda3 
/dev/vda3: clean, 39108/294912 files, 374665/1179392 blocks
[/sbin/fsck.ext4 (1) -- /boot] fsck.ext4 -a /dev/vda1 
/dev/vda1: clean, 38/32768 files, 14885/131072 blocks
[  OK  ]
Remounting root filesystem in read-write mode:  [  OK  ]
Mounting local filesystems:  EXT4-fs (vda1): mounted filesystem with ordered data mode. Opts: 
[  OK  ]
Enabling local filesystem quotas:  [  OK  ]
Enabling /etc/fstab swaps:  Adding 1048572k swap on /dev/vda2.  Priority:-1 extents:1 across:1048572k 
[  OK  ]
init: rcS main process (350) terminated with status 1
[root@r67 /]# runlevel
N S
[root@r67 /]# init 3

...

Red Hat Enterprise Linux Server release 6.7 (Santiago)
Kernel 2.6.32-573.el6.x86_64 on an x86_64

r67.example.com login: root
Password: 
[root@r67 ~]# tail -1 /var/log/cron 
Oct 23 10:16:09 r67 CROND[1589]: (root) CMD (/usr/local/sbin/grub-disable-single)
[root@r67 ~]# date
Fri Oct 23 10:16:39 EDT 2015
[root@r67 ~]# cat /etc/cron.d/grub-disable-single
cat: /etc/cron.d/grub-disable-single: No such file or directory
[root@r67 ~]# grep -w S /etc/grub.conf 
[root@r67 ~]# 
Close

Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.