File System

  Disk info | Boot | Disk usage | Opened files | Mount/remount | Mount SMB | Mount image |
  Burn ISO | Create image | 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 -u /                   # FreeBSD

  Copy the raw data from a cdrom into an iso image (default 512 blocksize might cause
  problems):
# dd if=/dev/cd0c of=file.iso bs=2048

Virtualbox

  Allow a share on the host:
# VBoxManage sharedfolder add "GuestName" --name "share" --hostpath "C:\hostshare"

  Mount share on guest (linux, FreeBSD)
# sudo mount -t vboxsf share /home/vboxshare # -o uid=1000,gid=1000 (as appropriate)
share /home/colin/share vboxsf defaults,uid=colin 0 0 # fstab entry

OSX

# diskutil list                      # List the partitions of a disk
# diskutil unmountDisk /dev/disk1    # Unmount an entire disk (all volumes)
# chflags hidden ~/Documents/folder  # Hide folder (reverse with unhidden)

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/OSX

# 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

  Mound Samba share through ssh tunnel
# ssh -C -f -N -p 20022 -L 445:127.0.0.1:445 me@server  # connect on 20022, tunnel 445
# mount -t smbfs //colin@localhost/colin ~/mnt
# mount_smbfs //colin:[email protected]/private /Volumes/private # I use this on OSX + ssh

  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 image

# hdiutil mount image.iso                               # OS X

Linux loop-back

# mount -t iso9660 -o loop file.iso /mnt                # Mount a CD image
# mount -t ext3 -o loop file.img /mnt                   # Mount an image 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

Create and burn an ISO image

  This will copy the cd or DVD sector for sector. Without conv=notrunc, the image will be
  smaller if there is less content on the cd. See below and the dd examples.
# dd if=/dev/hdc of=/tmp/mycd.iso bs=2048 conv=notrunc

  Use mkisofs to create a CD/DVD image from files in a directory. To overcome the file names
  restrictions: -r enables the Rock Ridge extensions common to UNIX systems, -J enables
  Joliet extensions used by Microsoft systems. -L allows ISO9660 filenames to begin with a
  period.
# mkisofs -J -L -r -V TITLE -o imagefile.iso /path/to/dir
# hdiutil makehybrid -iso -joliet -o dir.iso dir/       # OS X

  On FreeBSD, mkisofs is found in the ports in sysutils/cdrtools.

Burn a CD/DVD ISO image

FreeBSD

  FreeBSD does not enable DMA on ATAPI drives by default. DMA is enabled with the sysctl
  command and the arguments below, or with /boot/loader.conf with the following entries:
hw.ata.ata_dma="1"
hw.ata.atapi_dma="1"

  Use burncd with an ATAPI device (burncd is part of the base system) and cdrecord (in
  sysutils/cdrtools) with a SCSI drive.
# burncd -f /dev/acd0 data imagefile.iso fixate      # For ATAPI drive
# cdrecord -scanbus                  # To find the burner device (like 1,0,0)
# cdrecord dev=1,0,0 imagefile.iso

Linux

  Also use cdrecord with Linux as described above. Additionally it is possible to use the
  native ATAPI interface which is found with:
# cdrecord dev=ATAPI -scanbus

  And burn the CD/DVD as above.

dvd+rw-tools

  The dvd+rw-tools package (FreeBSD: ports/sysutils/dvd+rw-tools) can do it all and includes
  growisofs to burn CDs or DVDs. The examples refer to the dvd device as /dev/dvd which could
  be a symlink to /dev/scd0 (typical scsi on Linux) or /dev/cd0 (typical FreeBSD) or
  /dev/rcd0c (typical NetBSD/OpenBSD character SCSI) or /dev/rdsk/c0t1d0s2 (Solaris example
  of a character SCSI/ATAPI CD-ROM device). There is a nice documentation with examples on
  the FreeBSD handbook chapter 18.7http://www.freebsd.org/handbook/creating-dvds.html.
                      # -dvd-compat closes the disk
# growisofs -dvd-compat -Z /dev/dvd=imagefile.iso     # Burn existing iso image
# growisofs -dvd-compat -Z /dev/dvd -J -R /p/to/data  # Burn directly

Convert a Nero .nrg file to .iso

  Nero simply adds a 300Kb header to a normal iso image. This can be trimmed with dd.
# dd bs=1k if=imagefile.nrg of=imagefile.iso skip=300

Convert a bin/cue image to .iso

  The little bchunk programhttp://freshmeat.net/projects/bchunk/ can do this. It is in the
  FreeBSD ports in sysutils/bchunk.
# bchunk imagefile.bin imagefile.cue imagefile.iso

Create a file based image

  For example a partition of 1GB using the file /usr/vdisk.img. Here we use the vnode 0, but
  it could also be 1.

FreeBSD

# dd if=/dev/random of=/usr/vdisk.img bs=1K count=1M
# mdconfig -a -t vnode -f /usr/vdisk.img -u 0         # Creates device /dev/md1
# bsdlabel -w /dev/md0
# newfs /dev/md0c
# mount /dev/md0c /mnt
# umount /mnt; mdconfig -d -u 0; rm /usr/vdisk.img    # Cleanup the md device

  The file based image can be automatically mounted during boot with an entry in /etc/rc.conf
  and /etc/fstab. Test your setup with # /etc/rc.d/mdconfig start (first delete the md0
  device with # mdconfig -d -u 0).
  Note however that this automatic setup will only work if the file image is NOT on the root
  partition. The reason is that the /etc/rc.d/mdconfig script is executed very early during
  boot and the root partition is still read-only. Images located outside the root partition
  will be mounted later with the script /etc/rc.d/mdconfig2.
  /boot/loader.conf:
md_load="YES"

  /etc/rc.conf:
# mdconfig_md0="-t vnode -f /usr/vdisk.img"          # /usr is not on the root partition

  /etc/fstab: (The 0 0 at the end is important, it tell fsck to ignore this device, as is
  does not exist yet)
/dev/md0                /usr/vdisk      ufs     rw              0       0

  It is also possible to increase the size of the image afterward, say for example 300 MB
  larger.
# umount /mnt; mdconfig -d -u 0
# dd if=/dev/zero bs=1m count=300 >> /usr/vdisk.img
# mdconfig -a -t vnode -f /usr/vdisk.img -u 0
# growfs /dev/md0
# mount /dev/md0c /mnt                                # File partition is now 300 MB larger

Linux

# dd if=/dev/zero of=/usr/vdisk.img bs=1024k count=1024
# mkfs.ext3 /usr/vdisk.img
# mount -o loop /usr/vdisk.img /mnt
# umount /mnt; rm /usr/vdisk.img                      # Cleanup

Linux with losetup

  /dev/zero is much faster than urandom, but less secure for encryption.
# dd if=/dev/urandom of=/usr/vdisk.img bs=1024k count=1024
# losetup /dev/loop0 /usr/vdisk.img                   # Creates and associates /dev/loop0
# mkfs.ext3 /dev/loop0
# mount /dev/loop0 /mnt
# losetup -a                                          # Check used loops
# umount /mnt
# losetup -d /dev/loop0                               # Detach
# rm /usr/vdisk.img

Create a memory file system

  A memory based file system is very fast for heavy IO application. How to create a 64 MB
  partition mounted on /memdisk:

FreeBSD

# mount_mfs -o rw -s 64M md /memdisk
# umount /memdisk; mdconfig -d -u 0                   # Cleanup the md device
md     /memdisk     mfs     rw,-s64M    0   0         # /etc/fstab entry

Linux

# mount -t tmpfs -osize=64m tmpfs /memdisk

Disk performance

  Read and write a 1 GB file on partition ad4s3c (/home)
# time dd if=/dev/ad4s3c of=/dev/null bs=1024k count=1000
# time dd if=/dev/zero bs=1024k count=1000 of=/home/1Gb.file
# hdparm -tT /dev/hda      # Linux only