Linux System Management tutorial

1. Linux System Administration Essentials #

  • To view the Linux system release

    • Use lsb_release (this command is available for all Linux distributions)
    • Use cat /etc/redhat-release (this method is only available for Redhat Linux)
  • Check CPU information - use cat /proc/cpuinfo

  • Reboot the Linux operating system - use reboot

  • Quit the shell and return the given value - use exit

  • Shutdown the system - use shutdown

  • View or set the system time and date - use date

  • Mount a file system - use mount

  • Unmount a file system - use umount

  • View the current process status of the system - use ps

  • Delete the currently running process - use kill

  • Start, stop, restart, shutdown, display system services (Centos7) with systemctl

  • Start, stop, restart, shutdown, and display system services (Centos7 and earlier), using service

  • To manage tasks that need to be executed periodically, use crontab

2. Common command usage #

2.1. lsb_release #

lsb_release is not a bash default command and needs to be installed first if you want to use it.

To install it, do the following.

  1. Execute yum provides lsb_release to see which packages support the lsb_release command.
  2. Select the appropriate version and execute an installation command like this: yum install -y redhat-lsb-core-4.1-27.el7.centos.1.x86_64

2.2. reboot #

The reboot command is used to reboot a running Linux operating system.

Example.

reboot # Reboot the machine.
reboot -w # Do a reboot simulation (only records and does not really reboot).

2.3. exit #

The exit command is the same as exiting the shell and returns the given value. The current script execution can be terminated in a shell script. Executing exit causes the shell to exit with the specified status value. If the status value parameter is not set, the shell exits with the preset value. A status value of 0 means execution was successful, other values mean execution failed.

Example.

# exit the current shell
[root@localhost ~]# exit
logout

# In the script, go to the directory where the script is located, otherwise exit
cd $(dirname $0) || exit 1

# In the script, determine the number of arguments, print how to use them if they don't match, and exit
if [ "$#" -ne "2" ]; then
    echo "usage: $0 <area> <hours>"
    exit 2
fi

# In the script, delete the temporary file on exit
trap "rm -f tmpfile; echo Bye." EXIT

# Check the exit code of the previous command
. /mycommand.sh
EXCODE=$?
if [ "$EXCODE" == "0" ]; then
    echo "O.K"
fi

2.4. shutdown #

The shutdown command is used to shut down the system. shutdown shuts down all programs and reboots or shuts down the system according to the user's needs.

Example.

# Specify shutdown now
shutdown -h now

# Specify shutdown after 5 minutes, and send a warning message to the logged-in user
shutdown +5 "System will shutdown after 5 minutes"

2.5. date #

The date command is to display or set the system time and date.

Example.

# Formatted output
date + "%Y-%m-%d"
2009-12-07

# Output yesterday's date
date -d "1 day ago" +"%Y-%m-%d"
2012-11-19

# Output after 2 seconds
date -d "2 second" +"%Y-%m-%d %H:%M.%S"
2012-11-20 14:21.31

# The legendary 1234567890 seconds
date -d "1970-01-01 1234567890 seconds" + "%Y-%m-%d %H:%m:%S"
2009-02-13 23:02:30

# Normal to format
date -d "2009-12-12" + "%Y/%m/%d %H:%M.%S"
2009/12/12 00:00.00

# apache format conversion
date -d "Dec 5, 2009 12:00:37 AM" + "%Y-%m-%d %H:%M.%S"
2009-12-05 00:00.37

# Time wander after format conversion
date -d "Dec 5, 2009 12:00:37 AM 2 year ago" + "%Y-%m-%d %H:%M.%S"
2007-12-05 00:00.37

# Add or subtract operations
date +%Y%m%d # Show the previous day's month, year and day
date -d "+1 day" +%Y%m%d # Show the previous day's date
date -d "-1 day" +%Y%m%d # Show the date of the day after
date -d "-1 month" +%Y%m%d # Show the date of the previous month
date -d "+1 month" +%Y%m%d # Show the date of the next month
date -d "-1 year" +%Y%m%d # Show the date of the previous year
date -d "+1 year" +%Y%m%d # Show the date of the next year

# Set the time
date -s # Set the current time, only root access can set it, others can only view it
date -s 20120523 # Set to 20120523, this will set the specific time to empty 00:00:00
date -s 01:01:01 # Set the specific time, will not change the date
date -s "01:01:01 2012-05-23" # This sets the full time
date -s "01:01:01 20120523" # This sets the full time
date -s "2012-05-23 01:01:01" # This sets the full time
date -s "20120523 01:01:01" # This sets the full time

# Sometimes you need to check the time spent on a set of commands
#! /bin/bash

start=$(date +%s)
nmap man.linuxde.net &> /dev/null

end=$(date +%s)
difference=$(( end - start ))
echo $difference seconds.

2.6. mount #

The mount command is used to mount a filesystem to a specified mount point. This command is most commonly used to mount a cdrom so that we can access the data in the cdrom, because when you insert a CD into the cdrom, Linux does not mount it automatically and you must use the Linux mount command to do the mount manually.

Example.

# Mount /dev/hda1 under /mnt
mount /dev/hda1 /mnt

# Mount /dev/hda1 in read-only mode under /mnt
mount -o ro /dev/hda1 /mnt

# Mount the image file of the /tmp/image.iso disc in loop mode under /mnt/cdrom
# Use this method to view the contents of a Linux ISO that is normally available on the network without burning it to a CD
mount -o loop /tmp/image.iso /mnt/cdrom

2.7. umount #

The umount command is used to unmount an already mounted file system. You can umount a file system using either the device name or the mount point, but it is better to unmount via the mount point to avoid confusion when using bundled mounts (one device, multiple mount points).

Example.

# Uninstall by device name
umount -v /dev/sda1
/dev/sda1 umounted

# Uninstall by mount point
umount -v /mnt/mymount/
/tmp/diskboot.img umounted

2.8. ps #

The ps command is used to report the current process status of the system. It can be used with the kill command to interrupt and delete unnecessary programs at any time. ps is the most basic and powerful process viewer command, which can be used to determine which processes are running and their status, whether the process is finished, whether the process is dead, which processes are taking up too many resources, etc. In short, most of the information can be obtained by executing this command.

Example.

# Sort processes by memory resource usage
ps aux | sort -rnk 4

# Sort processes by CPU resource usage
ps aux | sort -nk 3

2.9. kill #

The kill command is used to delete an executing program or job. kill sends the specified message to the program. The preset message is SIGTERM(15), which will terminate the specified program. If the program cannot be terminated, you can use the SIGKILL(9) message to try to force the program to be deleted. The program or job number can be viewed with the ps command or job command.

Example.

# List all signal names
 kill -l
 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL
 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE
 9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2
13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGSTKFLT
17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU
25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH
29) SIGIO 30) SIGPWR 31) SIGSYS 34) SIGRTMIN
35) sigrtmin+1 36) sigrtmin+2 37) sigrtmin+3 38) sigrtmin+4
39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8
43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12
47) SIGRTMIN+13 48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14
51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10
55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7 58) SIGRTMAX-6
59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2
63) SIGRTMAX-1 64) SIGRTMAX

# Find the process with ps first, then kill it with kill
ps -ef | grep vim
root 3268 2884 0 16:21 pts/1 00:00:00 vim install.log
root 3370 2822 0 16:21 pts/0 00:00:00 grep vim

kill 3268
kill 3268
-bash: kill: (3268) - there is no such process

2.10. systemctl #

The systemctl command is a system service manager command that actually combines the service and chkconfig commands into one.

Example.

# 1. Start the nfs service
systemctl start nfs-server.service

# 2. set the boot-up
systemctl enable nfs-server.service

# 3. Stop the boot-up
systemctl disable nfs-server.service

# 4. Check the current status of the service
systemctl status nfs-server.service

# 5. Restart a service
systemctl restart nfs-server.service

# 6. View all started services
systemctl list -units --type=service

# 7. turn on the firewall port 22
iptables -I INPUT -p tcp --dport 22 -j accept

# 8. completely shut down the firewall
sudo systemctl status firewalld.service
sudo systemctl stop firewalld.service
sudo systemctl disable firewalld.service
sudo systemctl disable firewalld.service

2.11. service #

The service command is a utility used in Redhat Linux-compatible distributions to control system services, to start, stop, restart, and shut down system services, and to display the current status of all system services.

Example.

service network status
Configure the device.
lo eth0
Currently active devices: Lo eth0
lo eth0

service network restart
Shutting down interface eth0: [ OK ]
Closing the loopback interface: [ OK ]
Set network parameters: [ OK ]
Eject loopback interface: [ OK ]
Pop-up interface eth0: [ OK ]

Tags:

Linux