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.
Unix Toolbox revision 14.2
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. This XHTML page can be converted into a nice PDF document with a CSS3 compliant application (see the script example). See also the about page.
Error reports and comments are most welcome - c@cb.vu Colin Barschel.
© Colin Barschel 2007-2009. Some rights reserved under Creative Commons.
1. System
2. Processes
3. File System
4. Network
5. SSH SCP
6. ××× with SSH
7. RSYNC
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
20. Shells
21. Scripting
22. Programming
23. Online Help
white black
System
Hardware | Statistics | Users | Limits | Runlevels | root password | Compile kernel | Repair grub
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 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 # 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
# tail -n 500 /var/log/messages # Last 500 kernel/syslog messages
# tail /var/log/warn # System warnings messages see syslog.conf
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 <group> <user> # Add existing user to group (Debian)
# groupmod -A <user> <group> # 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 11http://www.freebsd.org/handbook/configtuning-kernel-limits.html for details.
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
Runlevels
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 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 # List all init scripts
# chkconfig --list sshd # Report the status of sshd
# chkconfig sshd --level 35 on # Configure sshd for levels 3 and 5
# chkconfig sshd off # 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 sshd defaults # Activate sshd with the default runlevels
# update-rc.d sshd start 20 2 3 4 5 . stop 20 0 1 6 . # With explicit arguments
# update-rc.d -f sshd remove # Disable sshd for all runlevels
# shutdown -h 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 # Go into single-user mode
# exit # Go back to multi-user mode
# shutdown -p now # Shutdown and halt the system
# shutdown -r now # 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)
Windows
Start and stop a service with either the service name or "service description" (shown in the Services Control Panel) as follows:
net stop WSearch
net start WSearch # start search service
net stop "Windows Search"
net start "Windows Search" # same as above using descr.
Reset 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 # or delete the root password (/etc/shadow)
# sync; mount -o remount,ro / # sync before to remount read only
# reboot
FreeBSD method 1
On FreeBSD, boot in single user mode, remount / rw and use passwd. You can select the single user mode on the boot menu (option 4) which is displayed for 10 seconds at startup. The single user mode will give you a root shell on the / partition.
# mount -u /; mount -a # will mount / rw
# passwd
# reboot
Unixes and FreeBSD and Linux method 2
Other Unixes might not 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
# passwd
# reboot
Kernel modules
Linux
# lsmod # List all modules loaded in the kernel
# modprobe isdn # To load a module (here isdn)
FreeBSD
# kldstat # List all modules loaded in the kernel
# kldload crypto # To load a module (here crypto)
Compile Kernel
Linux
# cd /usr/src/linux
# make mrproper # Clean everything, including config files
# make oldconfig # Reuse the old .config if existent
# make menuconfig # or xconfig (Qt) or gconfig (GTK)
# make # Create a compressed kernel p_w_picpath
# make modules # Compile the modules
# make modules_install # Install the modules
# make install # Install the kernel
# reboot
FreeBSD
Optionally update the source tree (in /usr/src) with csup (as of FreeBSD 6.2 or later):
# csup <supfile>
I use the following supfile:
*default host=cvsup5.FreeBSD.org # www.freebsd.org/handbook/cvsup.html#CVSUP-MIRRORS
*default prefix=/usr
*default base=/var/db
*default release=cvs delete tag=RELENG_7
src-all
To modify and rebuild the kernel, copy the generic configuration file to a new name and edit it as needed (you can also edit the file GENERIC directly). To restart the build after an interruption, add the option NO_CLEAN=YES to the make command to avoid cleaning the objects already build.
# 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 # Build the full OS but not the kernel
# make buildkernel # Use KERNCONF as above if appropriate
# make installkernel
# reboot
# mergemaster -p # Compares only files known to be essential
# make installworld
# mergemaster -i -U # Update all configurations and other files
# reboot
For small changes in the source you can use NO_CLEAN=yes to avoid rebuilding the whole tree.
# make buildworld NO_CLEAN=yes # Don't delete the old objects
# make buildkernel KERNCONF=MYKERNEL NO_CLEAN=yes
Repair grub
So you broke grub? Boot from a live cd, [find your linux partition under /dev and use fdisk to find the linux partion] mount the linux partition, add /proc and /dev and use grub-install /dev/xyz. Suppose linux lies on /dev/sda6:
# mount /dev/sda6 /mnt # mount the linux partition on /mnt
# mount --bind /proc /mnt/proc # mount the proc subsystem into /mnt
# mount --bind /dev /mnt/dev # mount the devices into /mnt
# chroot /mnt # change root to the linux partition
# grub-install /dev/sda # reinstall grub with your old settings
Processes
Listing | Priority | Background/Foreground | Top | Kill
Listing 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
# ps axjf # All processes in a tree format (Linux)
# ps aux | grep 'ss[h]' # Find all ssh pids without the grep pid
# pgrep -l sshd # Find the PIDs of processes by (part of) name
# echo $$ # The PID of your shell
# fuser -va 22/tcp # List processes using port 22 (Linux)
# pmap PID # Memory map of process (hunt memory leaks) (Linux)
# fuser -va /home # List processes accessing the /home partition
# strace df # Trace system calls and signals
# truss df # same as above on FreeBSD/Solaris/Unixware
Priority
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 # Stronger priority (/usr/bin/nice)
# nice -n 5 top # Weaker priority (/usr/bin/nice)
# nice +5 top # tcsh builtin nice (same as above!)
While nice changes the CPU scheduler, an other useful command ionice will schedule the disk IO. This is very useful for intensive IO application (e.g. compiling). You can select a class (idle - best effort - real time), the man page is short and well explained.
# ionice c3 -p123 # set idle class for pid 123 (Linux only)
# ionice -c2 -n0 firefox # Run firefox with best effort and high priority
# ionice -c3 -p$$ # Set the actual shell to idle priority
The last command is very useful to compile (or debug) a large project. Every command launched from this shell will have a lover priority. $$ is your shell pid (try echo $$).
FreeBSD uses idprio/rtprio (0 = max priority, 31 = most idle):
# idprio 31 make # compile in the lowest priority
# idprio 31 -1234 # set PID 1234 with lowest priority
# idprio -t -1234 # -t removes any real time/idle priority
Background/Foreground
When started from a shell, processes can be brought in the background and back to the foreground with [Ctrl]-[Z] (^Z), bg and fg. List the processes with jobs.
# ping cb.vu > ping.log
^Z # ping is suspended (stopped) with [Ctrl]-[Z]
# bg # put in background and continues running
# jobs -l # List processes in background
[1] - 36232 Running ping cb.vu > ping.log
[2] + 36233 Suspended (tty output) top
# fg %2 # 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 &
Top
The program top displays running information of processes. See also the program htop from htop.sourceforge.net (a more powerful version of top) which runs on Linux and FreeBSD (ports/sysutils/htop/). 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.
Signals/Kill
Terminate or send a signal with kill or killall.
# ping -i 60 cb.vu > ping.log &
[1] 4712
# kill -s TERM 4712 # same as kill -15 4712
# killall -1 httpd # Kill HUP processes by exact name
# pkill -9 http # Kill TERM processes by (part of) name
# pkill -TERM -u www # Kill TERM processes owned by www
# fuser -k -TERM -m /home # Kill 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)
File System
Disk info | Boot | Disk usage | Opened files | Mount/remount | Mount SMB | Mount p_w_picpath | Burn ISO | Create p_w_picpath | Memory disk | Disk performance
Permissions
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 umask is subtracted from 777, thus umask 022 results in a permission 0f 755.
1 --x execute # Mode 764 = exec/read/write | read/write | read
2 -w- write # For: |-- Owner --| |- Group-| |Oth|
4 r-- read
ugo=a u=user, g=group, o=others, a=everyone
# chmod [OPTION] MODE[,MODE] FILE # MODE is of the form [ugoa]*([-+=]([rwxXst]))
# chmod 640 /var/log/maillog # Restrict the log -rw-r-----
# chmod u=rw,g=r,o= /var/log/maillog # Same as above
# chmod -R o-r /home/* # Recursive remove other readable for all users
# chmod u+s /path/to/prog # Set SUID bit on executable (know what you do!)
# find / -perm -u+s -print # Find all programs with the SUID bit
# chown user:group /path/to/file # Change the user and group ownership of a file
# chgrp group /path/to/file # Change the group ownership of a file
# chmod 640 `find ./ -type f -print` # Change permissions to 640 for all files
# chmod 751 `find ./ -type d -print` # Change permissions to 751 for all directories
Disk information
# diskinfo -v /dev/ad2 # information about disk (sector/size) FreeBSD
# hdparm -I /dev/sda # information about the IDE/ATA disk (Linux)
# fdisk /dev/ad2 # Display and manipulate the partition table
# smartctl -a /dev/ad2 # Display the disk SMART info
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
System mount points/Disk usage
# mount | column -t # Show mounted file-systems on the system
# df # display free disk space and mounted devices
# cat /proc/partitions # Show all registered partitions (Linux)
Disk usage
# du -sh * # Directory sizes as listing
# du -csh # Total directory size of the current directory
# du -ks * | sort -n -r # Sort everything by size in kilobytes
# ls -lSr # Show files, biggest last
Who has which files opened
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 # umount impossible because a file is locking home
failed: Device busy
FreeBSD and most Unixes
# fstat -f /home # for a mount point
# fstat -p PID # for an application with PID
# fstat -u user # 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 INUM MODE SZ|DV R/W
root Xorg 1252 root / 2 drwxr-xr-x 512 r
root Xorg 1252 text /usr 216016 -rws--x--x 1679848 r
root Xorg 1252 0 /var 212042 -rw-r--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 # List processes accessing /home
# lsof /home
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
tcsh 29029 eedcoba cwd DIR 0,18 12288 1048587 /home/eedcoba (guam:/home)
lsof 29140 eedcoba cwd DIR 0,18 12288 1048587 /home/eedcoba (guam:/home)
About an application:
ps ax | grep Xorg | awk '{print $1}'
3324
# lsof -p 3324
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
Xorg 3324 root 0w REG 8,6 56296 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
Mount/remount a file system
For example the cdrom. If listed in /etc/fstab:
# mount /cdrom
Or find the device in /dev/ or with dmesg
FreeBSD
# mount -v -t cd9660 /dev/cd0c /mnt # cdrom
# mount_cd9660 /dev/wcd0c /cdrom # other method
# mount -v -t msdos /dev/fd0c /mnt # floppy
Entry in /etc/fstab:
# Device Mountpoint FStype Options Dump Pass#
/dev/acd0 /cdrom cd9660 ro,noauto 0 0
To let users do it:
# sysctl vfs.usermount=1 # Or insert the line "vfs.usermount=1" in /etc/sysctl.conf
Linux
# mount -t auto /dev/cdrom /mnt/cdrom # typical cdrom mount command
# mount /dev/hdc -t iso9660 -r /cdrom # typical IDE
# mount /dev/scd0 -t iso9660 -r /cdrom # typical SCSI cdrom
# mount /dev/sdc0 -t ntfs-3g /windows # typical SCSI
Entry in /etc/fstab:
/dev/cdrom /media/cdrom subfs noauto,fs=cdfss,ro,procuid,nosuid,nodev,exec 0 0
Mount a FreeBSD partition with Linux
Find the partition number containing with fdisk, this is usually the root partition, but it could be an other BSD slice too. If the FreeBSD has many slices, they are the one not listed in the fdisk table, but visible in /dev/sda* or /dev/hda*.
# fdisk /dev/sda # Find the FreeBSD partition
/dev/sda3 * 5357 7905 20474842+ a5 FreeBSD
# mount -t ufs -o ufstype=ufs2,ro /dev/sda3 /mnt
/dev/sda10 = /tmp; /dev/sda11 /usr # The other slices
Remount
Remount a device without unmounting it. Necessary for fsck for example
# mount -o remount,ro / # Linux
# mount -o ro / # FreeBSD
Copy the raw data from a cdrom into an iso p_w_picpath:
# dd if=/dev/cd0c of=file.iso
Add swap on-the-fly
Suppose you need more swap (right now), say a 2GB file /swap2gb (Linux only).
# dd if=/dev/zero of=/swap2gb bs=1024k count=2000
# mkswap /swap2gb # create the swap area
# swapon /swap2gb # activate the swap. It now in use
# swapoff /swap2gb # when done deactivate the swap
# rm /swap2gb
Mount an SMB share
Suppose we want to access the SMB share myshare on the computer smbserver, the address as typed on a Windows PC is //smbserver/myshare/. We mount on /mnt/smbshare. Warning> cifs wants an IP or DNS name, not a Windows name.
Linux
# smbclient -U user -I 192.168.16.229 -L //smbshare/ # List the shares
# mount -t smbfs -o username=winuser //smbserver/myshare /mnt/smbshare
# mount -t cifs -o username=winuser,password=winpwd //192.168.16.229/myshare /mnt/share
Additionally with the package mount.cifs it is possible to store the credentials in a file, for example /home/user/.smb:
username=winuser
password=winpwd
And mount as follow:
# mount -t cifs -o credentials=/home/user/.smb //192.168.16.229/myshare /mnt/smbshare
FreeBSD
Use -I to give the IP (or DNS name); smbserver is the Windows name.
# smbutil view -I 192.168.16.229 //winuser@smbserver # List the shares
# mount_smbfs -I 192.168.16.229 //winuser@smbserver/myshare /mnt/smbshare
Mount an p_w_picpath
Linux loop-back
# mount -t iso9660 -o loop file.iso /mnt # Mount a CD p_w_picpath
# mount -t ext3 -o loop file.img /mnt # Mount an p_w_picpath with ext3 fs
FreeBSD
With memory device (do # kldload md.ko if necessary):
# mdconfig -a -t vnode -f file.iso -u 0
# mount -t cd9660 /dev/md0 /mnt
# umount /mnt; mdconfig -d -u 0 # Cleanup the md device
Or with virtual node:
# vnconfig /dev/vn0c file.iso; mount -t cd9660 /dev/vn0c /mnt
# umount /mnt; vnconfig -u /dev/vn0c # Cleanup the vn device
Solaris and FreeBSD
with loop-back file interface or lofi:
# lofiadm -a file.iso
# mount -F hsfs -o ro /dev/lofi/1 /mnt
# umount /mnt; lofiadm -d /dev/lofi/1 # Cleanup the lofi device
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.
Unix Toolbox revision 14.2
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. This XHTML page can be converted into a nice PDF document with a CSS3 compliant application (see the script example). See also the about page.
Error reports and comments are most welcome - c@cb.vu Colin Barschel.
© Colin Barschel 2007-2009. Some rights reserved under Creative Commons.
1. System
2. Processes
3. File System
4. Network
5. SSH SCP
6. ××× with SSH
7. RSYNC
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
20. Shells
21. Scripting
22. Programming
23. Online Help
white black
System
Hardware | Statistics | Users | Limits | Runlevels | root password | Compile kernel | Repair grub
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 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 # 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
# tail -n 500 /var/log/messages # Last 500 kernel/syslog messages
# tail /var/log/warn # System warnings messages see syslog.conf
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 <group> <user> # Add existing user to group (Debian)
# groupmod -A <user> <group> # 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 11http://www.freebsd.org/handbook/configtuning-kernel-limits.html for details.
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
Runlevels
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 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 # List all init scripts
# chkconfig --list sshd # Report the status of sshd
# chkconfig sshd --level 35 on # Configure sshd for levels 3 and 5
# chkconfig sshd off # 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 sshd defaults # Activate sshd with the default runlevels
# update-rc.d sshd start 20 2 3 4 5 . stop 20 0 1 6 . # With explicit arguments
# update-rc.d -f sshd remove # Disable sshd for all runlevels
# shutdown -h 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 # Go into single-user mode
# exit # Go back to multi-user mode
# shutdown -p now # Shutdown and halt the system
# shutdown -r now # 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)
Windows
Start and stop a service with either the service name or "service description" (shown in the Services Control Panel) as follows:
net stop WSearch
net start WSearch # start search service
net stop "Windows Search"
net start "Windows Search" # same as above using descr.
Reset 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 # or delete the root password (/etc/shadow)
# sync; mount -o remount,ro / # sync before to remount read only
# reboot
FreeBSD method 1
On FreeBSD, boot in single user mode, remount / rw and use passwd. You can select the single user mode on the boot menu (option 4) which is displayed for 10 seconds at startup. The single user mode will give you a root shell on the / partition.
# mount -u /; mount -a # will mount / rw
# passwd
# reboot
Unixes and FreeBSD and Linux method 2
Other Unixes might not 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
# passwd
# reboot
Kernel modules
Linux
# lsmod # List all modules loaded in the kernel
# modprobe isdn # To load a module (here isdn)
FreeBSD
# kldstat # List all modules loaded in the kernel
# kldload crypto # To load a module (here crypto)
Compile Kernel
Linux
# cd /usr/src/linux
# make mrproper # Clean everything, including config files
# make oldconfig # Reuse the old .config if existent
# make menuconfig # or xconfig (Qt) or gconfig (GTK)
# make # Create a compressed kernel p_w_picpath
# make modules # Compile the modules
# make modules_install # Install the modules
# make install # Install the kernel
# reboot
FreeBSD
Optionally update the source tree (in /usr/src) with csup (as of FreeBSD 6.2 or later):
# csup <supfile>
I use the following supfile:
*default host=cvsup5.FreeBSD.org # www.freebsd.org/handbook/cvsup.html#CVSUP-MIRRORS
*default prefix=/usr
*default base=/var/db
*default release=cvs delete tag=RELENG_7
src-all
To modify and rebuild the kernel, copy the generic configuration file to a new name and edit it as needed (you can also edit the file GENERIC directly). To restart the build after an interruption, add the option NO_CLEAN=YES to the make command to avoid cleaning the objects already build.
# 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 # Build the full OS but not the kernel
# make buildkernel # Use KERNCONF as above if appropriate
# make installkernel
# reboot
# mergemaster -p # Compares only files known to be essential
# make installworld
# mergemaster -i -U # Update all configurations and other files
# reboot
For small changes in the source you can use NO_CLEAN=yes to avoid rebuilding the whole tree.
# make buildworld NO_CLEAN=yes # Don't delete the old objects
# make buildkernel KERNCONF=MYKERNEL NO_CLEAN=yes
Repair grub
So you broke grub? Boot from a live cd, [find your linux partition under /dev and use fdisk to find the linux partion] mount the linux partition, add /proc and /dev and use grub-install /dev/xyz. Suppose linux lies on /dev/sda6:
# mount /dev/sda6 /mnt # mount the linux partition on /mnt
# mount --bind /proc /mnt/proc # mount the proc subsystem into /mnt
# mount --bind /dev /mnt/dev # mount the devices into /mnt
# chroot /mnt # change root to the linux partition
# grub-install /dev/sda # reinstall grub with your old settings
Processes
Listing | Priority | Background/Foreground | Top | Kill
Listing 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
# ps axjf # All processes in a tree format (Linux)
# ps aux | grep 'ss[h]' # Find all ssh pids without the grep pid
# pgrep -l sshd # Find the PIDs of processes by (part of) name
# echo $$ # The PID of your shell
# fuser -va 22/tcp # List processes using port 22 (Linux)
# pmap PID # Memory map of process (hunt memory leaks) (Linux)
# fuser -va /home # List processes accessing the /home partition
# strace df # Trace system calls and signals
# truss df # same as above on FreeBSD/Solaris/Unixware
Priority
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 # Stronger priority (/usr/bin/nice)
# nice -n 5 top # Weaker priority (/usr/bin/nice)
# nice +5 top # tcsh builtin nice (same as above!)
While nice changes the CPU scheduler, an other useful command ionice will schedule the disk IO. This is very useful for intensive IO application (e.g. compiling). You can select a class (idle - best effort - real time), the man page is short and well explained.
# ionice c3 -p123 # set idle class for pid 123 (Linux only)
# ionice -c2 -n0 firefox # Run firefox with best effort and high priority
# ionice -c3 -p$$ # Set the actual shell to idle priority
The last command is very useful to compile (or debug) a large project. Every command launched from this shell will have a lover priority. $$ is your shell pid (try echo $$).
FreeBSD uses idprio/rtprio (0 = max priority, 31 = most idle):
# idprio 31 make # compile in the lowest priority
# idprio 31 -1234 # set PID 1234 with lowest priority
# idprio -t -1234 # -t removes any real time/idle priority
Background/Foreground
When started from a shell, processes can be brought in the background and back to the foreground with [Ctrl]-[Z] (^Z), bg and fg. List the processes with jobs.
# ping cb.vu > ping.log
^Z # ping is suspended (stopped) with [Ctrl]-[Z]
# bg # put in background and continues running
# jobs -l # List processes in background
[1] - 36232 Running ping cb.vu > ping.log
[2] + 36233 Suspended (tty output) top
# fg %2 # 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 &
Top
The program top displays running information of processes. See also the program htop from htop.sourceforge.net (a more powerful version of top) which runs on Linux and FreeBSD (ports/sysutils/htop/). 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.
Signals/Kill
Terminate or send a signal with kill or killall.
# ping -i 60 cb.vu > ping.log &
[1] 4712
# kill -s TERM 4712 # same as kill -15 4712
# killall -1 httpd # Kill HUP processes by exact name
# pkill -9 http # Kill TERM processes by (part of) name
# pkill -TERM -u www # Kill TERM processes owned by www
# fuser -k -TERM -m /home # Kill 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)
File System
Disk info | Boot | Disk usage | Opened files | Mount/remount | Mount SMB | Mount p_w_picpath | Burn ISO | Create p_w_picpath | Memory disk | Disk performance
Permissions
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 umask is subtracted from 777, thus umask 022 results in a permission 0f 755.
1 --x execute # Mode 764 = exec/read/write | read/write | read
2 -w- write # For: |-- Owner --| |- Group-| |Oth|
4 r-- read
ugo=a u=user, g=group, o=others, a=everyone
# chmod [OPTION] MODE[,MODE] FILE # MODE is of the form [ugoa]*([-+=]([rwxXst]))
# chmod 640 /var/log/maillog # Restrict the log -rw-r-----
# chmod u=rw,g=r,o= /var/log/maillog # Same as above
# chmod -R o-r /home/* # Recursive remove other readable for all users
# chmod u+s /path/to/prog # Set SUID bit on executable (know what you do!)
# find / -perm -u+s -print # Find all programs with the SUID bit
# chown user:group /path/to/file # Change the user and group ownership of a file
# chgrp group /path/to/file # Change the group ownership of a file
# chmod 640 `find ./ -type f -print` # Change permissions to 640 for all files
# chmod 751 `find ./ -type d -print` # Change permissions to 751 for all directories
Disk information
# diskinfo -v /dev/ad2 # information about disk (sector/size) FreeBSD
# hdparm -I /dev/sda # information about the IDE/ATA disk (Linux)
# fdisk /dev/ad2 # Display and manipulate the partition table
# smartctl -a /dev/ad2 # Display the disk SMART info
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
System mount points/Disk usage
# mount | column -t # Show mounted file-systems on the system
# df # display free disk space and mounted devices
# cat /proc/partitions # Show all registered partitions (Linux)
Disk usage
# du -sh * # Directory sizes as listing
# du -csh # Total directory size of the current directory
# du -ks * | sort -n -r # Sort everything by size in kilobytes
# ls -lSr # Show files, biggest last
Who has which files opened
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 # umount impossible because a file is locking home
failed: Device busy
FreeBSD and most Unixes
# fstat -f /home # for a mount point
# fstat -p PID # for an application with PID
# fstat -u user # 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 INUM MODE SZ|DV R/W
root Xorg 1252 root / 2 drwxr-xr-x 512 r
root Xorg 1252 text /usr 216016 -rws--x--x 1679848 r
root Xorg 1252 0 /var 212042 -rw-r--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 # List processes accessing /home
# lsof /home
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
tcsh 29029 eedcoba cwd DIR 0,18 12288 1048587 /home/eedcoba (guam:/home)
lsof 29140 eedcoba cwd DIR 0,18 12288 1048587 /home/eedcoba (guam:/home)
About an application:
ps ax | grep Xorg | awk '{print $1}'
3324
# lsof -p 3324
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
Xorg 3324 root 0w REG 8,6 56296 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
Mount/remount a file system
For example the cdrom. If listed in /etc/fstab:
# mount /cdrom
Or find the device in /dev/ or with dmesg
FreeBSD
# mount -v -t cd9660 /dev/cd0c /mnt # cdrom
# mount_cd9660 /dev/wcd0c /cdrom # other method
# mount -v -t msdos /dev/fd0c /mnt # floppy
Entry in /etc/fstab:
# Device Mountpoint FStype Options Dump Pass#
/dev/acd0 /cdrom cd9660 ro,noauto 0 0
To let users do it:
# sysctl vfs.usermount=1 # Or insert the line "vfs.usermount=1" in /etc/sysctl.conf
Linux
# mount -t auto /dev/cdrom /mnt/cdrom # typical cdrom mount command
# mount /dev/hdc -t iso9660 -r /cdrom # typical IDE
# mount /dev/scd0 -t iso9660 -r /cdrom # typical SCSI cdrom
# mount /dev/sdc0 -t ntfs-3g /windows # typical SCSI
Entry in /etc/fstab:
/dev/cdrom /media/cdrom subfs noauto,fs=cdfss,ro,procuid,nosuid,nodev,exec 0 0
Mount a FreeBSD partition with Linux
Find the partition number containing with fdisk, this is usually the root partition, but it could be an other BSD slice too. If the FreeBSD has many slices, they are the one not listed in the fdisk table, but visible in /dev/sda* or /dev/hda*.
# fdisk /dev/sda # Find the FreeBSD partition
/dev/sda3 * 5357 7905 20474842+ a5 FreeBSD
# mount -t ufs -o ufstype=ufs2,ro /dev/sda3 /mnt
/dev/sda10 = /tmp; /dev/sda11 /usr # The other slices
Remount
Remount a device without unmounting it. Necessary for fsck for example
# mount -o remount,ro / # Linux
# mount -o ro / # FreeBSD
Copy the raw data from a cdrom into an iso p_w_picpath:
# dd if=/dev/cd0c of=file.iso
Add swap on-the-fly
Suppose you need more swap (right now), say a 2GB file /swap2gb (Linux only).
# dd if=/dev/zero of=/swap2gb bs=1024k count=2000
# mkswap /swap2gb # create the swap area
# swapon /swap2gb # activate the swap. It now in use
# swapoff /swap2gb # when done deactivate the swap
# rm /swap2gb
Mount an SMB share
Suppose we want to access the SMB share myshare on the computer smbserver, the address as typed on a Windows PC is //smbserver/myshare/. We mount on /mnt/smbshare. Warning> cifs wants an IP or DNS name, not a Windows name.
Linux
# smbclient -U user -I 192.168.16.229 -L //smbshare/ # List the shares
# mount -t smbfs -o username=winuser //smbserver/myshare /mnt/smbshare
# mount -t cifs -o username=winuser,password=winpwd //192.168.16.229/myshare /mnt/share
Additionally with the package mount.cifs it is possible to store the credentials in a file, for example /home/user/.smb:
username=winuser
password=winpwd
And mount as follow:
# mount -t cifs -o credentials=/home/user/.smb //192.168.16.229/myshare /mnt/smbshare
FreeBSD
Use -I to give the IP (or DNS name); smbserver is the Windows name.
# smbutil view -I 192.168.16.229 //winuser@smbserver # List the shares
# mount_smbfs -I 192.168.16.229 //winuser@smbserver/myshare /mnt/smbshare
Mount an p_w_picpath
Linux loop-back
# mount -t iso9660 -o loop file.iso /mnt # Mount a CD p_w_picpath
# mount -t ext3 -o loop file.img /mnt # Mount an p_w_picpath with ext3 fs
FreeBSD
With memory device (do # kldload md.ko if necessary):
# mdconfig -a -t vnode -f file.iso -u 0
# mount -t cd9660 /dev/md0 /mnt
# umount /mnt; mdconfig -d -u 0 # Cleanup the md device
Or with virtual node:
# vnconfig /dev/vn0c file.iso; mount -t cd9660 /dev/vn0c /mnt
# umount /mnt; vnconfig -u /dev/vn0c # Cleanup the vn device
Solaris and FreeBSD
with loop-back file interface or lofi:
# lofiadm -a file.iso
# mount -F hsfs -o ro /dev/lofi/1 /mnt
# umount /mnt; lofiadm -d /dev/lofi/1 # Cleanup the lofi device
转载于:https://blog.51cto.com/dadloveu/669129