How to reboot Linux from command line

How to Reboot Linux from Command Line Rebooting a Linux system from the command line is a fundamental system administration skill that every Linux user should master. Whether you're managing a remote server, working in a terminal-only environment, or simply prefer command-line efficiency, knowing how to properly restart your Linux system is essential for system maintenance, applying updates, and troubleshooting various issues. This comprehensive guide will walk you through multiple methods to reboot Linux systems safely and effectively, covering everything from basic commands to advanced scheduling options and troubleshooting scenarios. Understanding Linux Reboot Commands Linux provides several commands to restart your system, each with its own advantages and use cases. The most commonly used commands include `reboot`, `shutdown`, `systemctl`, and `init`. Understanding when and how to use each command will help you manage your Linux systems more effectively. Why Reboot from Command Line? There are numerous scenarios where command-line rebooting is necessary or preferred: - Remote server management: When managing servers via SSH - Headless systems: Servers without graphical interfaces - Automation scripts: Scheduled maintenance and updates - System troubleshooting: Resolving system issues - Resource management: Clearing memory and resetting services - Security updates: Applying kernel patches that require restart Method 1: Using the `reboot` Command The `reboot` command is the most straightforward way to restart a Linux system. It's simple, direct, and widely supported across all Linux distributions. Basic Reboot Syntax ```bash sudo reboot ``` This command immediately initiates the system reboot process. The system will: 1. Send termination signals to all running processes 2. Unmount file systems safely 3. Restart the system Reboot Command Options The `reboot` command supports several useful options: ```bash Force immediate reboot (use with caution) sudo reboot -f Reboot and halt instead of restart sudo reboot --halt Reboot with custom message for logged users sudo reboot --message="System maintenance reboot" ``` When to Use the Reboot Command The `reboot` command is ideal for: - Quick system restarts - Emergency reboots when other methods fail - Simple automation scripts - When you need immediate restart without delay Method 2: Using the `shutdown` Command The `shutdown` command offers more control and flexibility compared to the basic `reboot` command. It allows you to schedule reboots, send messages to users, and provide graceful shutdown procedures. Basic Shutdown with Restart ```bash Reboot immediately sudo shutdown -r now Alternative syntax sudo shutdown --reboot now ``` Scheduled Reboots One of the most powerful features of the `shutdown` command is the ability to schedule reboots: ```bash Reboot in 10 minutes sudo shutdown -r +10 Reboot at specific time (24-hour format) sudo shutdown -r 23:30 Reboot tomorrow at 2 AM sudo shutdown -r 02:00 ``` Adding Messages for Users When scheduling reboots, it's good practice to inform logged-in users: ```bash Reboot in 15 minutes with message sudo shutdown -r +15 "System will reboot for maintenance" Immediate reboot with message sudo shutdown -r now "Emergency reboot - please save your work" ``` Canceling Scheduled Reboots If you need to cancel a scheduled reboot: ```bash sudo shutdown -c ``` Method 3: Using `systemctl` Command (systemd) Modern Linux distributions use systemd as their init system, making `systemctl` the preferred method for system management, including reboots. Basic systemctl Reboot ```bash sudo systemctl reboot ``` systemctl Reboot Options ```bash Force reboot (bypass normal shutdown process) sudo systemctl reboot --force Immediate reboot (even more forceful) sudo systemctl reboot --force --force Reboot into firmware setup (UEFI/BIOS) sudo systemctl reboot --firmware-setup ``` Checking System State Before Reboot Before rebooting, you might want to check the system state: ```bash Check failed services systemctl --failed Check system status systemctl status Check boot time systemd-analyze ``` Method 4: Using Legacy `init` Command On older systems or those not using systemd, you can use the `init` command: ```bash Reboot using init runlevel 6 sudo init 6 Alternative using telinit sudo telinit 6 ``` Advanced Reboot Techniques Emergency Reboot Methods When normal reboot methods fail, you might need to use more forceful approaches: ```bash Magic SysRq key sequence (if enabled) echo 1 | sudo tee /proc/sys/kernel/sysrq echo b | sudo tee /proc/sysrq-trigger ``` Warning: Use emergency methods only when normal reboot procedures fail, as they may cause data loss. Rebooting with Specific Kernel If you need to boot into a specific kernel version: ```bash List available kernels awk -F\' '$1=="menuentry " {print i++ " : " $2}' /etc/grub2.cfg Set specific kernel for next boot (GRUB2) sudo grub2-set-default 1 sudo reboot ``` Scheduling Automatic Reboots Using Cron for Regular Reboots You can schedule regular reboots using cron jobs: ```bash Edit root's crontab sudo crontab -e Add entry for weekly reboot (Sunday at 3 AM) 0 3 0 /sbin/reboot Monthly reboot (first day of month at 2 AM) 0 2 1 /sbin/shutdown -r now ``` Using systemd Timers Create a systemd timer for scheduled reboots: ```bash Create timer unit sudo nano /etc/systemd/system/weekly-reboot.timer Add timer configuration [Unit] Description=Weekly Reboot [Timer] OnCalendar=weekly Persistent=true [Install] WantedBy=timers.target ``` Best Practices for Linux Reboots Pre-Reboot Checklist Before rebooting any Linux system, consider these steps: 1. Check running processes: `ps aux | grep important_service` 2. Save work: Ensure all important work is saved 3. Notify users: Use wall command to broadcast messages 4. Check disk space: `df -h` to ensure adequate space 5. Review logs: Check system logs for any issues Safe Reboot Procedures ```bash Notify all users sudo wall "System will reboot in 10 minutes for maintenance" Check and stop critical services gracefully sudo systemctl stop critical-service Sync file systems sync Perform the reboot sudo shutdown -r +10 "Scheduled maintenance reboot" ``` Post-Reboot Verification After reboot, verify system functionality: ```bash Check system uptime uptime Verify services are running systemctl status important-service Check system logs journalctl -b Verify disk mounts df -h ``` Distribution-Specific Considerations Ubuntu/Debian Systems ```bash Check if reboot required ls /var/run/reboot-required View packages requiring reboot cat /var/run/reboot-required.pkgs ``` CentOS/RHEL Systems ```bash Check if reboot is required (RHEL 8+) dnf needs-restarting -r List services needing restart dnf needs-restarting -s ``` SUSE Systems ```bash Check reboot requirement zypper ps -s ``` Troubleshooting Reboot Issues Common Reboot Problems System Won't Reboot If standard reboot commands fail: ```bash Try more forceful approach sudo systemctl reboot --force If still stuck, try emergency reboot sudo reboot -f ``` Hung Processes Preventing Reboot ```bash Identify problematic processes sudo fuser -v / Kill specific processes sudo killall -9 process_name Then attempt reboot sudo reboot ``` File System Issues ```bash Force file system sync sync Check for file system errors sudo fsck /dev/sda1 Remount file systems sudo mount -o remount,rw / ``` Monitoring Reboot Process ```bash Monitor system messages during reboot sudo journalctl -f Check last reboot information last reboot View boot messages dmesg | less ``` Security Considerations Permissions and sudo Reboot commands require administrative privileges: ```bash Check sudo permissions sudo -l | grep reboot Allow specific user to reboot without password Add to /etc/sudoers: username ALL=(ALL) NOPASSWD: /sbin/reboot ``` Audit Trail Keep track of system reboots: ```bash Check reboot history last reboot | head -10 View system boot logs journalctl --list-boots ``` Automation and Scripting Creating Reboot Scripts ```bash #!/bin/bash safe_reboot.sh - Safe system reboot script echo "Preparing system for reboot..." Stop non-critical services systemctl stop non-critical-service Clear temporary files rm -rf /tmp/* Sync file systems sync Notify and reboot wall "System rebooting in 2 minutes" shutdown -r +2 "Automated maintenance reboot" ``` Integration with Monitoring Systems ```bash Send notification before reboot curl -X POST "monitoring-system/api/alert" \ -d "System $(hostname) rebooting for maintenance" Reboot and log echo "$(date): Reboot initiated" >> /var/log/maintenance.log sudo reboot ``` Conclusion Mastering Linux command-line reboots is essential for effective system administration. Whether you choose the simple `reboot` command for quick restarts, the flexible `shutdown` command for scheduled maintenance, or the modern `systemctl` approach for systemd-managed systems, each method has its place in a system administrator's toolkit. Remember to always follow best practices: notify users, save important work, check system status, and verify functionality after reboot. With proper planning and execution, command-line reboots become a powerful tool for maintaining healthy, secure, and up-to-date Linux systems. The key to successful Linux system management is understanding not just how to reboot, but when and why to use each method. By combining these techniques with proper monitoring, scheduling, and safety procedures, you'll be well-equipped to handle any reboot scenario your Linux systems may require.