"UNIX 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.
1. System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 2. Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 3. File System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 4. Network . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5. SSH SCP 7. RSYNC ..................................... ...................................... ................................... 6. VPN with SSH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8. SUDO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9. Encrypt Files 10. Encrypt Partitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11. SSL Certificates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12. CVS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13. SVN ....................................... 14. Useful Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15. Install Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16. Convert Media . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17. Printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18. Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19. Disk Quota 21. Scripting .................................... ..................................... 20. Shells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22. Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23. Online Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 20 23 25 26 27 27 30 31 34 36 40 41 43 43 45 46 48 50 52
Unix Toolbox revision 11 The latest version of this document can be found at http://cb.vu/unixtoolbox.xhtml. Replace .xhtml on the link with .pdf for the PDF version and with .book.pdf for the booklet version. On a duplex printer the booklet will create a small book ready to bind. Error reports and comments are most welcome - c@cb.vu Colin Barschel.
— System —
1 SYSTEM
Hardware (p2) | Statistics (p2) | Users (p3) | Limits (p3) | Runlevels (p4) | root password (p5) | Compile kernel (p6) Running kernel and system information
# uname -a # cat /etc/SuSE-release # cat /etc/debian_version # Get the kernel version (and BSD version) # Get SuSE version # Get Debian version
Use /etc/DISTR-release with DISTR= lsb (Ubuntu), redhat, gentoo, mandrake, sun (Solaris), and so on.
# # # # # uptime hostname hostname -i man hier last reboot # # # # # Show how long the system has been running + load system's host name Display the IP address of the host. Description of the file system hierarchy Show system reboot history
1 .1 H ard ware Informations
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 cat /proc/meminfo grep MemTotal /proc/meminfo watch -n1 'cat /proc/interrupts' free -m cat /proc/devices lspci -tv lsusb -tv lshal dmidecode # # # # # # # # # # CPU model Hardware memory Display the physical memory Watch changeable interrupts continuously Used and free memory (-m for MB) Configured devices Show PCI devices Show USB devices Show a list of all devices with their properties Show DMI/SMBIOS: hw info from the BIOS
FreeBSD
# # # # # # # # # sysctl hw.model sysctl hw sysctl vm dmesg | grep "real mem" sysctl -a | grep mem sysctl dev pciconf -l -cv usbdevs -v atacontrol list # # # # # # # # # CPU model Gives a lot of hardware information Memory usage Hardware memory Kernel memory settings and info Configured devices Show PCI devices Show USB devices Show ATA devices
1 .2 Load, s tatistics and messa ge s
The following commands are useful to find out what is going on on the system.
# # # # # top mpstat vmstat iostat systat 1 2 2 -vmstat 1 # # # # # display and update the top cpu processes display processors related statistics display virtual memory statistics display I/O statistics (2 s intervals) BSD summary of system statistics (1 s intervals)
2
— System —
# # # # # # systat -tcp 1 systat -netstat 1 systat -ifstat 1 systat -iostat 1 tail -n 500 /var/log/messages tail /var/log/warn # # # # # # BSD tcp connections (try also -ip) BSD active network connections BSD network traffic through active interfaces BSD CPU and and disk throughput Last 500 kernel/syslog messages System warnings messages see syslog.conf
1 .3 Us e rs
# # # # # # # # # # # # id # last # who # groupadd admin # useradd -c "Colin Barschel" -g admin userdel colin # adduser joe # rmuser joe # pw groupadd admin # pw groupmod admin -m newmember # pw useradd colin -c "Colin Barschel" pw userdel colin; pw groupdel admin Show the active user id with login and group Show last logins on the system Show who is logged on the system Add group "admin" and user colin (Linux/Solaris) -m colin Delete user colin (Linux/Solaris) FreeBSD add user joe (interactive) FreeBSD delete user joe (interactive) Use pw on FreeBSD Add a new member to a group -g admin -m -s /bin/tcsh
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.
# echo "Sorry no login now" > /etc/nologin # echo "Sorry no login now" > /var/run/nologin # (Linux) # (FreeBSD)
1 .4 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 asterisk hard nofile 409600 # Limit user processes # Limit application open files
System wide Kernel limits are set with sysctl. Permanent limits are set in /etc/sysctl.conf.
# # # # sysctl -a sysctl fs.file-max sysctl fs.file-max=102400 cat /etc/sysctl.conf # View all system limits # View max open files limit # Change max open files limit
3
— System —
fs.file-max=102400 # cat /proc/sys/fs/file-nr # Permanent entry in sysctl.conf # 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 # sysctl kern.maxfiles=XXXX kern.ipc.nmbclusters=32768 kern.maxfiles=65536 kern.maxfilesperproc=32768 kern.ipc.somaxconn=8192 # sysctl kern.openfiles # sysctl kern.ipc.numopensockets # # # # View all system limits maximum number of file descriptors Permanent entry in /etc/sysctl.conf Typical values for Squid
# TCP queue. Better for apache/sendmail # How many file descriptors are in use # How many open sockets are in use
See The FreeBSD handbook Chapter 111 for details. Solaris The following values in /etc/system will increase the maximum file descriptors per proc:
set rlim_fd_max = 4096 set rlim_fd_cur = 1024 # Hard limit on file descriptors for a single proc # Soft limit on file descriptors for a single proc
1 .5 Runle vels
Linux Once booted, the kernel starts init which then starts rc which starts all scripts belonging to a runlevel. The scripts are stored in /etc/init.d and are linked into /etc/rc.d/rcN.d with N the runlevel number. The default runlevel is configured in /etc/inittab. It is usually 3 or 5:
# grep default: /etc/inittab id:3:initdefault:
The actual runlevel (the list is shown below) can be changed with init. For example to go from 3 to 5:
# init 5 # Enters runlevel 5
0 Shutdown and halt 1 Single-User mode (also S) 2 Multi-user without network 3 Multi-user with network 5 Multi-user with X 6 Reboot Use chkconfig to configure the programs that will be started at boot in a runlevel.
# chkconfig --list # chkconfig --list sshd # List all init scripts # Report the status of sshd
1.http://www.freebsd.org/handbook/configtuning-kernel-limits.html
4
— System —
# chkconfig sshd --level 35 on # chkconfig sshd off # Configure sshd for levels 3 and 5 # Disable sshd for all runlevels
Debian and Debian based distributions like Ubuntu or Knoppix use the command update-rc.d to manage the runlevels scripts. Default is to start in 2,3,4 and 5 and shutdown in 0,1 and 6.
# # # # update-rc.d update-rc.d update-rc.d shutdown -h sshd defaults # Activate sshd with the default runlevels sshd start 20 2 3 4 5 . stop 20 0 1 6 . # With explicit arguments -f sshd remove # Disable sshd for all runlevels now (or # poweroff) # Shutdown and halt the system
FreeBSD The BSD boot approach is different from the SysV, there are no runlevels. The final boot state (single user, with or without X) is configured in /etc/ttys. All OS scripts are located in /etc/ rc.d/ and in /usr/local/etc/rc.d/ for third-party applications. The activation of the service is configured in /etc/rc.conf and /etc/rc.conf.local. The default behavior is configured in /etc/defaults/rc.conf. The scripts responds at least to start|stop|status.
# /etc/rc.d/sshd status sshd is running as pid 552. # shutdown now # exit # shutdown -p now # shutdown -r now
# # # #
Go into single-user mode Go back to multi-user mode Shutdown and halt the system Reboot
The process init can also be used to reach one of the following states level. For example # init 6 for reboot. 0 Halt and turn the power off (signal USR2) 1 Go to single-user mode (signal TERM) 6 Reboot the machine (signal INT) c Block further logins (signal TSTP) q Rescan the ttys(5) file (signal HUP)
1 .6 Res e t root password
Linux method 1 At the boot loader (lilo or grub), enter the following boot option:
init=/bin/sh
The kernel will mount the root partition and init will start the bourne shell instead of rc and then a runlevel. Use the command passwd at the prompt to change the password and then reboot. Forget the single user mode as you need the password for that. If, after booting, the root partition is mounted read only, remount it rw:
# # # # mount -o remount,rw / passwd sync; mount -o remount,ro / reboot # or delete the root password (/etc/shadow) # sync before to remount read only
FreeBSD and Linux method 2 FreeBSD won't let you go away with the simple init trick. The solution is to mount the root partition from an other OS (like a rescue CD) and change the password on the disk. • Boot a live CD or installation CD into a rescue mode which will give you a shell. • Find the root partition with fdisk e.g. fdisk /dev/sda • Mount it and use chroot:
# mount -o rw /dev/ad4s3a /mnt # chroot /mnt # chroot into /mnt
5
— System —
# passwd # reboot
Alternatively on FreeBSD, boot in single user mode, remount / rw and use passwd.
# mount -u /; mount -a # passwd # reboot # will mount / rw
1 .7 Kerne l modules
Linux
# lsmod # modprobe isdn # List all modules loaded in the kernel # To load a module (here isdn)
FreeBSD
# kldstat # kldload crypto # List all modules loaded in the kernel # To load a module (here crypto)
1 .8 Compile Kernel
Linux
# # # # # # # # # cd /usr/src/linux make mrproper make oldconfig make menuconfig make make modules make modules_install make install reboot # # # # # # # Clean everything, including config files Create a new config file from the current kernel or xconfig (Qt) or gconfig (GTK) Create a compressed kernel image Compile the modules Install the modules Install the kernel
FreeBSD To modify and rebuild the kernel, copy the generic configuration file to a new name and edit it as needed. It is however also possible to edit the file GENERIC directly.
# # # # # cd /usr/src/sys/i386/conf/ cp GENERIC MYKERNEL cd /usr/src make buildkernel KERNCONF=MYKERNEL make installkernel KERNCONF=MYKERNEL
To rebuild the full OS:
# # # # # # # # make buildworld make buildkernel make installkernel reboot mergemaster -p make installworld mergemaster reboot # Build the full OS but not the kernel # Use KERNCONF as above if appropriate # Compares only files known to be essential # Update all configuration and other files
For small changes in the source, sometimes the short version is enough:
# make kernel world # mergemaster # reboot # Compile and install both kernel and OS
6
— Processes —
2 PROCESSES
Listing (p7) | Priority (p7) | Background/Foreground (p7) | Top (p7) | Kill (p8)
2 .1 Lis ting and PIDs
Each process has a unique number, the PID. A list of all running process is retrieved with ps.
# ps -auxefw # Extensive list of all running process
However more typical usage is with a pipe or with pgrep:
# ps axww | grep cron 586 ?? Is 0:01.48 /usr/sbin/cron -s # pgrep -l sshd # Find the PIDs of processes by (part of) name # fuser -va 22/tcp # List processes using port 22 # fuser -va /home # List processes accessing the /home partiton # strace df # Trace system calls and signals # truss df # same as above on FreeBSD/Solaris/Unixware # history | tail -50 # Display the last 50 used commands
2 .2 P riority
Change the priority of a running process with renice. Negative numbers have a higher priority, the lowest is -20 and "nice" have a positive value.
# renice -5 586 # Stronger priority 586: old priority 0, new priority -5
Start the process with a defined priority with nice. Positive is "nice" or weak, negative is strong scheduling priority. Make sure you know if /usr/bin/nice or the shell built-in is used (check with # which nice).
# nice -n -5 top # nice -n 5 top # nice +5 top # Stronger priority (/usr/bin/nice) # Weaker priority (/usr/bin/nice) # tcsh builtin nice (same as above!)
2 .3 Background/ Foregroun d
When started from a shell, processes can be brought in the background and back to the foreground with [Ctrl]-[Z] (^Z), bg and fg. For example start two processes, bring them in the background, list the processes with jobs and bring one in the foreground.
# ping cb.vu > ping.log ^Z # bg # jobs -l [1] - 36232 Running [2] + 36233 Suspended (tty output) # fg %2 # ping is suspended (stopped) with [Ctrl]-[Z] # put in background and continues running # List processes in background ping cb.vu > ping.log top # Bring process 2 back in foreground
Use nohup to start a process which has to keep running when the shell is closed (immune to hangups).
# nohup ping -i 60 > ping.log &
2 .4 T op
The program top displays running information of processes.
# top
7
— File System —
While top is running press the key h for a help overview. Useful keys are: • u [user name] To display only the processes belonging to the user. Use + or blank to see all users • k [pid] Kill the process with pid. • 1 To display all processors statistics (Linux only) • R Toggle normal/reverse sort.
2 .5 Signals /Kill
Terminate or send a signal with kill or killall.
# ping -i 60 cb.vu > ping.log & [1] 4712 # kill -s TERM 4712 # killall -1 httpd # pkill -9 http # pkill -TERM -u www # fuser -k -TERM -m /home
# # # # #
same Kill Kill Kill Kill
as kill -15 4712 HUP processes by exact name TERM processes by (part of) name TERM processes owned by www every process accessing /home (to umount)
Important signals are: 1 HUP (hang up) 2 INT (interrupt) 3 QUIT (quit) 9 KILL (non-catchable, non-ignorable kill) 15 TERM (software termination signal)
3 FILE SYSTEM
Disk info (p8) | Boot (p9) | Disk usage (p9) | Opened files (p9) | Mount/remount (p10) | Mount SMB (p11) | Mount image (p11) | Burn ISO (p11) | Create image (p12) | Memory disk (p13) | Disk performance (p13)
3 .1 P e rmis sions
Change permission and ownership with chmod and chown. The default umask can be changed for all users in /etc/profile for Linux or /etc/login.conf for FreeBSD. The default umask is usually 022. The umsak is subtracted from 777, thus umask 022 results in a permission 0f 755.
1 --x execute 2 -w- write 4 r-- read ugo=a # # # # # # # # chmod [OPTION] MODE[,MODE] FILE chmod 640 /var/log/maillog chmod u=rw,g=r,o= /var/log/maillog chmod -R o-r /home/* chmod u+s /path/to/prog find / -perm -u+s -print chown user:group /path/to/file chgrp group /path/to/file # Mode 764 = exec/read/write | read/write | read # For: |-- Owner --| |- Group-| |Oth| u=user, g=group, o=others, a=everyone # # # # # # # # MODE is of the form [ugoa]*([-+=]([rwxXst])) Restrict the log -rw-r----Same as above Recursive remove other readable for all users Set SUID bit on executable (know what you do!) Find all programs with the SUID bit Change the user and group ownership of a file Change the group ownership of a file
3 .2 Dis k information
# # # # diskinfo -v /dev/ad2 hdparm -I /dev/sda fdisk /dev/ad2 smartctl -a /dev/ad2 # # # # information information Display and Display the about disk (sector/size) FreeBSD about the IDE/ATA disk (Linux) manipulate the partition table disk SMART info
8
— File System —
3 .3 Boot
FreeBSD To boot an old kernel if the new kernel doesn't boot, stop the boot at during the count down.
# unload # load kernel.old # boot
3 .4 Sys tem mount points/ D i sk usa ge
# mount | column -t # df # cat /proc/partitions # Show mounted file-systems on the system # display free disk space and mounted devices # Show all registered partitions (Linux)
Disk usage
# # # # du du du ls -sh * -csh -ks * | sort -n -r -lSr # # # # Directory sizes as listing Total directory size of the current directory Sort everything by size in kilobytes Show files, biggest last
3 .5 Who ha s which files ope ne d
This is useful to find out which file is blocking a partition which has to be unmounted and gives a typical error of:
# umount /home/ umount: unmount of /home failed: Device busy # umount impossible because a file is locking home
FreeBSD and most Unixes
# fstat -f /home # fstat -p PID # fstat -u user # for a mount point # for an application with PID # for a user name
Find opened log file (or other opened files), say for Xorg:
# ps ax | grep Xorg | awk '{print $1}' 1252 # fstat -p 1252 USER CMD PID FD MOUNT root Xorg 1252 root / root Xorg 1252 text /usr root Xorg 1252 0 /var
INUM 2 216016 212042
MODE drwxr-xr-x -rws--x--x -rw-r--r--
SZ|DV R/W 512 r 1679848 r 56987 w
The file with inum 212042 is the only file in /var:
# find -x /var -inum 212042 /var/log/Xorg.0.log
Linux Find opened files on a mount point with fuser or lsof:
# fuser -m /home # lsof /home COMMAND PID USER tcsh 29029 eedcoba lsof 29140 eedcoba # List processes accessing /home FD cwd cwd TYPE DEVICE DIR 0,18 DIR 0,18 SIZE 12288 12288 NODE NAME 1048587 /home/eedcoba (guam:/home) 1048587 /home/eedcoba (guam:/home)
9
— File System —
About an application:
ps ax | grep Xorg | awk '{print $1}' 3324 # lsof -p 3324 COMMAND PID USER FD TYPE DEVICE Xorg 3324 root 0w REG 8,6
SIZE 56296
NODE NAME 12492 /var/log/Xorg.0.log
About a single file:
# lsof /var/log/Xorg.0.log COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME Xorg 3324 root 0w REG 8,6 56296 12492 /var/log/Xorg.0.log
3 ...."
You need to upgrade your Flash Player , or try to enable javascript in order see this document properly.
Cheat sheets for Unix, Linux & BSD for sys admins & users alike. linux unix terminal commands reference command sheet cheat line bsd Technology-UNIX
Text sample from document: "UNIX 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.
1. System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 2. Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 3. File System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ...."
Unix / Linux / BSD Cheat Sheets I
Cheat sheets for Unix, Linux & BSD for sys admins & users alike. linux unix terminal commands reference command sheet cheat line bsd Technology-UNIX...