UNIX / Linux keyboard.

Commands for Linux System Administration

Know These Commands

These are the commands that a Linux system administrator needs to know. There is no way to fully explain how to use all of these commands on one page, but I can show you a few examples and jog your memory.

Remember that there are many ways to solve most any problem on the UNIX family of operating systems. So these might be the more obvious tools, or maybe the easiest to use, but there will always be other ways of accomplishing these tasks.

Are you really ready?

You have no business trying to do system administration unless you are comfortable using that operating system from its command line. Otherwise you are just asking for big trouble!

See the dedicated page of Linux/Unix commands and its suggestions for further reading in order to prepare yourself.


Booting

Boot in Unusual Ways

Stop the GRUB boot process by pressing <Escape> during the brief time before it boots the default kernel in the default way. Then you can:

Boot an alternative kernel by highlighting your choice with the arrow keys and then pressing <Enter>.

Modify the select kernel booting method by pressing <E> to edit the kernel. You can then add or change paramenters by adding these to the boot command:

s Boot to single-user mode — root on the console with no networking. You can also use upper-case S or the digit 1 or the word single or Single.
selinux=0 Disable the NSA Security-Enhanced Linux policy enforcement if that's causing you trouble.
enforcing=0 Leave the NSA Security-Enhanced Linux policy features enabled but don't enforce the policy, just log things that would have been prevented.
3 Boot to run level 3 (everything except graphical display manager, server-style) instead of the default run level 5 (graphical), or vice-versa.

From here on, everything is typed at the command prompt as root.

Query and Control the Run Level

Query the previous and current run level:
# runlevel

Change to a run level:
# init N
where:

N Meaning
0 Shut down and turn off power
1 Single-user mode
3 Full server with text console
5 Full server plus graphical console
6 Shut down and reboot

You can also shut down with:
# halt
# shutdown -h +t 0

You can also reboot with:
# reboot
# shutdown -r +t 0
<Ctrl><Alt><Del>
(if you're on a text console)

All this changes with systemd in RHEL/CentOS 7. See the changes.

Control init boot scripts

# /etc/init.d/servicename parameter

Where parameter is something like stop or start or restart. Run the script with no parameter to see what choices are available.

Control whether a service is started by default, or query the levels in which it will be started, with chkconfig.

# chkconfig --list servicename
# chkconfig servicename on
# chkconfig servicename off


Process Monitoring, Control, and Logging

Measure Running Processes

# ps axuw
# ps -elf

# pstree
# pstree -au

# pgrep process-name

Monitor Running Processes

# top

# vmstat 10

# iostat -o 10

What Files (and sockets and pipes and devices) Do Processes Have Open?

# lsof /var/log/*
# lsof -p PID
# lsof -i

What Processes Are Using This File System?

# fuser -m /dev/dvd

Send Signals to Processes

# kill -TERM PID

# pkill -TERM process-name

Log Events

The rsyslog daemon logs, started by /etc/init.d/rsyslogd and configured by /etc/rsyslog.conf and /etc/sysconfig/rsyslog.

The scheduled logwatch job (0400 nightly) mails "interesting" events to root and the scheduled logrotate job (0400 Sundays) rotates the log files.


Users and Groups

Defined in:

/etc/passwd
/etc/shadow
/etc/group

Created, modified, and deleted with:

# groupadd -g GID newgroup
# groupmod -g new-GID -n new-name group
# groupdel old-group
# vim /etc/group

# useradd -m -u UID -c "Real Name" new-user
# usermod -c "Real Name" -e 2015-12-31 -a -G wheel,staff -s /bin/tcsh [....] user
# userdel -r old-user

# passwd username
# chage username

Investigated with:

# id username
# groups username

The PAM files in /etc/pam.d/* control authentication, using shared libraries in /lib*/security/*.


Storage and File Systems

Detect hot-plugged storage devices:

# for FILE in $( find /sys/devices -name scan )
> do
>     echo '- - -' > $FILE
> done

Manipulate disk partitions or labels:

# fdisk /dev/sdX
# parted /dev/sdX

Manipulate Logical Volume Management (LVM):

# pvcreate /dev/sdX
# vgcreate name /dev/sdX /dev/sdY
# lvcreate -l size --name name volgroup

# vgextend name /dev/sdZ
# lvextend -l newsize name

Investigate Logical Volume Management (LVM):

# pvscan
# vgscan
# lvscan
# pvdisplay
# vgdisplay
# lvdisplay

Destroy Logical Volume Management (LVM):

# pvremove /dev/sdX
# vgremove volgroup
# lvremove name

Also see how to rescue a Linux system with an LVM root file system for more about logical volume management.

Create File Systems:

# mkfs.ext2 /dev/sdXN
# mkfs.ext3 /dev/sdXN
# mkfs.ext4 /dev/sdXN
# mkfs.msdos /dev/sdXN
# mkfs.vfat /dev/sdXN

# mkfs.ext4 -L label -U $( uuidgen ) /dev/sdXN

Investigate and Tune Ext2/3/4 File Systems:

# dumpe2fs /dev/sdXN
# tune2fs [ several parameters ] /dev/sdXN

Create and Use Swap:

# mkswap -L label /dev/sdXN
# vim /etc/fstab
# swapon -a
# swapon -s

Use and Check File Systems:

# mount /dev/sdXN /path/to/mount/point
# vim /etc/fstab
# mount -a
# df -h
# du /var/log
# du -h /var/log
# du -s /var/log
# du -sh /var/log


Package Management

# lsb_release -a

# cd /etc
# tar cvfj /tmp/archive.tar.bz2 *

# tar tvf /tmp/archive.tar.bz2

# cd /some/where/else
# tar xvf /tmp/archive.tar.bz2

See the dedicated page for rpm/yum and dpkg/apt-get package management.


Building Software From Source

Build from tar archives:

# ./configure
# make
# make install

Build from source RPM:

# cd ~/rpmbuild/SPECS
# rpmbuild -bb package.spec
# tree ../RPMS
# rpmbuild --clean package.spec
# rpm -Uvh ../RPMS/arch/package-file.rpm


Network Configuration

See the dedicated page for network configuration commands.


LAN / Intranet Services

Print jobs:

# lpr -Pprinter file
# lpq -Pprinter
# lprm job

# lp -d printer file
# lpstat -p printer
# cancel job

Check NFS service availability and use:

# showmount -e host
# showmount -a host

Be a client to SMB/CIFS service:

# smbclient -L //server -U user
# smbclient -U user //server/user

Be a SMB/CIFS server with Samba:

# smbpasswd -a new-samba-user
# smbfind


Hardware Support and the Kernel

See the dedicated page for building Linux kernels.

Investigate this kernel:

# dmesg
# uname -a
# more /proc/cmdline

Investigate this platform:

# dmidecode
# lscpu
# more /proc/cpuinfo
# more /proc/meminfo
# lspci
# lspci -vt
# lsusb
# systool -v -b usb
# systool -v -b scsi

Manipulate kernel modules:

# lsmod
# modinfo module
# modprobe module
# rmmod module

Examine and manipulate kernel data structures:

# sysctl -a
# sysctl -w net.ipv4.tcp_fin_timeout=5

See the dedicated page on kernel stack tuning for security and performance.