Unix Toolbox

Unix/Linux/BSD commands and tasks which are useful for IT work or for advanced users.

Unix/Linux/BSD Toolbox

This document is a collection of Unix/Linux/BSD commands and tasks which are useful for IT work or for advanced users. This is a practical guide with concise explanations, however the reader is supposed to know what s/he is doing.

Error reports and comments are most welcome - c@cb.vu Colin Barschel.
© Colin Barschel 2007-2012. Some rights reserved under Creative Commons.

System

Running kernel and system information .

# uname -a                           # Get the kernel version (and BSD version)
# lsb_release -a # Full release info of any LSB distribution
# cat /etc/SuSE-release # Get SuSE version
# cat /etc/debian_version # Get Debian version

Use /etc/DISTR-release with DISTR= lsb (Ubuntu), redhat, gentoo, mandrake, sun (Solaris), and so on. See also /etc/issue.

# uptime                             # Show how long the system has been running + load
# hostname # system's host name
# hostname -i # Display the IP address of the host. (Linux only)
# man hier # Description of the file system hierarchy
# last reboot # Show system reboot history

Hardware Information

Kernel detected hardware
# dmesg                              # Detected hardware and boot messages
# lsdev                              # information about installed hardware
# dd if=/dev/mem bs=1k skip=768 count=256 2>/dev/null | strings -n 8 # Read BIOS
Linux
# cat /proc/cpuinfo                  # CPU model
# cat /proc/meminfo                  # Hardware memory
# grep MemTotal /proc/meminfo        # Display the physical memory
# watch -n1 'cat /proc/interrupts'   # Watch changeable interrupts continuously
# free -m                            # Used and free memory (-m for MB)
# cat /proc/devices                  # Configured devices
# lspci -tv                          # Show PCI devices
# lsusb -tv                          # Show USB devices
# lshal                              # Show a list of all devices with their properties
# dmidecode                          # Show DMI/SMBIOS: hw info from the BIOS
FreeBSD
# sysctl hw.model                    # CPU model
# sysctl hw                          # Gives a lot of hardware information
# sysctl hw.ncpu                     # number of active CPUs installed
# sysctl vm                          # Memory usage
# sysctl hw.realmem                  # Hardware memory
# sysctl -a | grep mem               # Kernel memory settings and info
# sysctl dev                         # Configured devices
# pciconf -l -cv                     # Show PCI devices
# usbdevs -v                         # Show USB devices
# atacontrol list                    # Show ATA devices
# camcontrol devlist -v              # Show SCSI devices

Load, statistics and messages

The following commands are useful to find out what is going on on the system.
# top                                # display and update the top cpu processes
# mpstat 1                           # display processors related statistics
# vmstat 2                           # display virtual memory statistics
# iostat 2                           # display I/O statistics (2 s intervals)
# systat -vmstat 1                   # BSD summary of system statistics (1 s intervals)
# systat -tcp 1                      # BSD tcp connections (try also -ip)
# systat -netstat 1                  # BSD active network connections
# systat -ifstat 1                   # BSD network traffic through active interfaces
# systat -iostat 1                   # BSD CPU and and disk throughput
# ipcs -a                            # information on System V interprocess
# tail -n 500 /var/log/messages      # Last 500 kernel/syslog messages
# tail /var/log/warn                 # System warnings messages see syslog.conf

Users

Users
# id                                 # Show the active user id with login and group
# last                               # Show last logins on the system
# who                                # Show who is logged on the system
# groupadd admin                     # Add group "admin" and user colin (Linux/Solaris)
# useradd -c "Colin Barschel" -g admin -m colin
# usermod -a -G         # Add existing user to group (Debian)
# groupmod -A           # Add existing user to group (SuSE)
# userdel colin                      # Delete user colin (Linux/Solaris)
# adduser joe                        # FreeBSD add user joe (interactive)
# rmuser joe                         # FreeBSD delete user joe (interactive)
# pw groupadd admin                  # Use pw on FreeBSD
# pw groupmod admin -m newmember     # Add a new member to a group
# pw useradd colin -c "Colin Barschel" -g admin -m -s /bin/tcsh 
# pw userdel colin; pw groupdel admin
Encrypted passwords are stored in /etc/shadow for Linux and Solaris and /etc/master.passwd on FreeBSD.
If the master.passwd is modified manually (say to delete a password), run # pwd_mkdb -p master.passwd to rebuild the database.

To temporarily prevent logins system wide (for all users but root) use nologin. The message in nologin will be displayed
(might not work with ssh pre-shared keys).

# echo "Sorry no login now" > /etc/nologin       # (Linux)
# echo "Sorry no login now" > /var/run/nologin   # (FreeBSD)

Limits

Some application require higher limits on open files and sockets (like a proxy web server, database).
The default limits are usually too low.

Linux
Per shell/script
The shell limits are governed by ulimit. The status is checked with ulimit -a. For example to change the open files
limit from 1024 to 10240 do:
# ulimit -n 10240                    # This is only valid within the shell
The ulimit command can be used in a script to change the limits for the script only.
Per user/process
Login users and applications can be configured in /etc/security/limits.conf. For example:
# cat /etc/security/limits.conf
*   hard    nproc   250              # Limit user processes
asterisk hard nofile 409600          # Limit application open files
System wide
Kernel limits are set with sysctl. Permanent limits are set in /etc/sysctl.conf.
# sysctl -a                          # View all system limits
# sysctl fs.file-max                 # View max open files limit
# sysctl fs.file-max=102400          # Change max open files limit
# echo "1024 50000" > /proc/sys/net/ipv4/ip_local_port_range  # port range
# cat /etc/sysctl.conf
fs.file-max=102400                   # Permanent entry in sysctl.conf
# cat /proc/sys/fs/file-nr           # How many file descriptors are in use
FreeBSD
Per shell/script
Use the command limits in csh or tcsh or as in Linux, use ulimit in an sh or bash shell.
Per user/process
The default limits on login are set in /etc/login.conf. An unlimited value is still limited by the system maximal value.
System wide
Kernel limits are also set with sysctl. Permanent limits are set in /etc/sysctl.conf or /boot/loader.conf.
The syntax is the same as Linux but the keys are different.
# sysctl -a                          # View all system limits
# sysctl kern.maxfiles=XXXX          # maximum number of file descriptors
kern.ipc.nmbclusters=32768           # Permanent entry in /etc/sysctl.conf
kern.maxfiles=65536                  # Typical values for Squid
kern.maxfilesperproc=32768
kern.ipc.somaxconn=8192              # TCP queue. Better for apache/sendmail
# sysctl kern.openfiles              # How many file descriptors are in use
# sysctl kern.ipc.numopensockets     # How many open sockets are in use
# sysctl net.inet.ip.portrange.last=50000 # Default is 1024-5000
# netstat -m                         # network memory buffers statistics
See The FreeBSD handbook Chapter 11 for details. And also FreeBSD performance tuning Solaris The following values in /etc/system will increase the maximum file descriptors per proc:
set rlim_fd_max = 4096               # Hard limit on file descriptors for a single proc
set rlim_fd_cur = 1024               # Soft limit on file descriptors for a single proc