Saturday, March 22, 2014

Unix - Memory and Disk Usages

  • Statistics of process, virtual memory, disk, trap and cpu activity:

    # vmstat -m

             Type InUse MemUse HighUse Requests  Size(s)
           USBdev    32     3K       -       50  32,64,128,256,2048
              USB    56    18K       -       95  16,32,64,128,512,2048,4096
           isadev    10     1K       -       10  64
         ppbusdev     3     1K       -        3  128
             cdev     7     1K       -        7  128
          entropy  1024    64K       -     1024  64
            sigio     1     1K       -        1  32
         filedesc    46    12K       -   291905  16,32,256,512,2048
          kdtrace   248    54K       -   273670  64,256
             kenv    83     7K       -       92  16,32,64,128,4096
    ...
    

  • Swap memory usage

    # swapinfo

    Device          1K-blocks     Used    Avail Capacity
    /dev/ada0p3       4194304      424  4193880     0%

  • Top processes on the system

    # top -d1

    last pid: 73598;  load averages:  0.00,  0.00,  0.00                                                                           
    26 processes:  1 running, 25 sleeping
    CPU:     % user,     % nice,     % system,     % interrupt,     % idle
    Mem: 114M Active, 1683M Inact, 234M Wired, 121M Cache, 91M Buf, 843M Free
    Swap: 4096M Total, 424K Used, 4095M Free
    
      PID USER  THR PRI NICE   SIZE    RES STATE   C   TIME   WCPU COMMAND
     1130 root    1  20    0 11296K  2944K select  0   0:39  0.00% sendmail
     1137 root    1  20    0  9584K  1380K nanslp  0   0:10  0.00% cron
     1003 root    1  20    0  9544K  1300K select  1   0:05  0.00% syslogd
    70226 vijay   1  20    0 15852K  4200K select  1   0:03  0.00% sshd
    70186 root    1  20    0 15852K  3992K select  0   0:03  0.00% sshd
      928 root    1  29    0  9544K  1364K select  0   0:02  0.00% dhclient
      966 _dhcp   1  20    0  9544K  1408K select  1   0:01  0.00% dhclient
      813 root    1  20    0 12132K  2868K select  1   0:01  0.00% devd
     1133 smmsp   1  20    0 11296K  2816K pause   1   0:01  0.00% sendmail
    71182 root    1  20    0 11256K  2940K select  1   0:01  0.00% ntpd
    70223 root    1  20    0 15852K  4056K select  1   0:00  0.00% sshd
     1127 root    1  20    0 13096K  3148K select  1   0:00  0.00% sshd
    70189 root    1  20    0 10864K  3188K pause   0   0:00  0.00% csh
     1183 root    1  20   -4 10092K  1868K ttyin   1   0:00  0.00% login
    73598 root    1  20    0  9872K  2008K CPU1    1   0:00  0.00% top
     1185 root    1  52    0  9544K  1240K ttyin   1   0:00  0.00% getty
     1187 root    1  52    0  9544K  1240K ttyin   1   0:00  0.00% getty
     1189 root    1  52    0  9544K  1240K ttyin   0   0:00  0.00% getty
     1188 root    1  52    0  9544K  1240K ttyin   0   0:00  0.00% getty
    ...
    

  • Finding size of folders (total size of files and subfolders underneath)

    • # du –sh myfolder

    • # du –sh * => Size of all the folders in the current directory

    • # du –sk | sort –n => Size of folders, sorted by size (k  kilo byte. h gives false sorting results)

Saturday, February 15, 2014

IPv4 Address Classes

Though Classless Inter-Domain Routing (CIDR) and Variable-Length Subnet Mask (VLSM) are the order of today's IPv4 networking, classification of IPv4 address spaces into classes A, B, C, or D remains common. The following table shows the classes that were designated during the days of classful routing:

Class First bits Address range Subnet mask
Class A 0 1.x.x.x ~ 127.x.x.x 255.0.0.0 (/8)
Class B 10 128.0.x.x ~ 191.255.x.x 255.255.0.0 (/16)
Class C 110 192.0.0.x ~ 223.255.255.x 255.255.255.0 (/24)
Class D 1110 224.0.0.0 ~ 239.255.255.255 -
Class E 1111 240.0.0.0 ~ 255.255.255.254 -

Saturday, January 4, 2014

Unix - Disk Management

Identifying the harddisks installed

  • Listing all the disks and drives attached to the system

    # camcontrol devlist    (or, atacontrol list)
    
    at scbus2 target 0 lun 0 (ada0,pass0)
    at scbus3 target 0 lun 0 (cd0,pass1)

  • Listing harddisks detected during the system bootup

    # dmesg | grep ad 
    
    ada0 at ata4 bus 0 scbus2 target 0 lun 0
    ada0:  ATA-8 SATA 2.x device
    ada0: 300.000MB/s transfers (SATA 2.x, UDMA5, PIO 8192bytes)
    ada0: 238418MB (488281250 512 byte sectors: 16H 63S/T 16383C)
    ada0: Previously was known as ad4
    Trying to mount root from ufs:/dev/ada0p2 [rw]...

  • Listing harddisks and their partitions

    # ls /dev | grep ad
    
    ad4
    ad4p1
    ad4p2
    ad4p3
    ada0
    ada0p1
    ada0p2
    ada0p3

  • Listing hardware level information of a harddisks

    # diskinfo -c ada0
    ada0
       512             # sectorsize
       250000000000    # mediasize in bytes (232G)
       488281250       # mediasize in sectors
       0               # stripesize
       0               # stripeoffset
       484406          # Cylinders according to firmware.
       16              # Heads according to firmware.
       63              # Sectors according to firmware.
       WD-WMAV12345678 # Disk ident.
    
    I/O command overhead:
       time to read 10MB block      0.124816 sec = 0.006 msec/sector
       time to read 20480 sectors   2.126440 sec = 0.104 msec/sector
       calculated command overhead               = 0.098 msec/sector
    

  • #fdisk ada0

    ******* Working on device /dev/ada0 *******
    parameters extracted from in-core disklabel are:
    cylinders=484521 heads=16 sectors/track=63 (1008 blks/cyl)
    
    Figures below won't work with BIOS for partitions not in cyl 1
    parameters to be used for BIOS calculations are:
    cylinders=484521 heads=16 sectors/track=63 (1008 blks/cyl)
    
    Media sector size is 512
    Warning: BIOS sector numbering starts with sector 1
    Information from DOS bootblock is:
    The data for partition 1 is:
    sysid 238 (0xee),(EFI GPT)
        start 1, size 488397167 (238475 Meg), flag 80 (active)
     beg: cyl 0/ head 1/ sector 1;
     end: cyl 1023/ head 255/ sector 63
    

Identifying the harddisk partitions

  • Identifying basic information about harddisk partitions

    # gpart show
    
    =>       34  488281183  ada0  GPT  (232G)
             34        128     1  freebsd-boot  (64k)
            162  478150528     2  freebsd-ufs  (228G)
      478150690    8388608     3  freebsd-swap  (4.0G)
      486539298    1741919        - free -  (850M)
    

  • Displaying detailed information of harddisk partitions

    # gpart list
    
    Geom name: ada0
    modified: false
    state: OK
    fwheads: 16
    fwsectors: 63
    last: 488281216
    first: 34
    entries: 128
    scheme: GPT
    Providers:
    1. Name: ada0p1
       Mediasize: 65536 (64k)
       Sectorsize: 512
       Stripesize: 0
       Stripeoffset: 17408
       Mode: r0w0e0
       rawuuid: 740aff84-9a1e-11e3-b778-002219173177
       rawtype: 83bd6b9d-7f41-11dc-be0b-001560b84f0f
       label: (null)
       length: 65536
       offset: 17408
       type: freebsd-boot
       index: 1
       end: 161
       start: 34
    2. Name: ada0p2
       Mediasize: 244813070336 (228G)
       Sectorsize: 512
       Stripesize: 0
       Stripeoffset: 82944
       Mode: r1w1e1
       rawuuid: 740b80b0-9a1e-11e3-b778-002219173177
       rawtype: 516e7cb6-6ecf-11d6-8ff8-00022d09712b
       label: (null)
       length: 244813070336
       offset: 82944
       type: freebsd-ufs
       index: 2
       end: 478150689
       start: 162
    3. Name: ada0p3
       Mediasize: 4294967296 (4.0G)
       Sectorsize: 512
       Stripesize: 0
       Stripeoffset: 17408
       Mode: r1w1e0
       rawuuid: 740eaebd-9a1e-11e3-b778-002219173177
       rawtype: 516e7cb5-6ecf-11d6-8ff8-00022d09712b
       label: (null)
       length: 4294967296
       offset: 244813153280
       type: freebsd-swap
       index: 3
       end: 486539297
       start: 478150690
    Consumers:
    1. Name: ada0
       Mediasize: 250000000000 (232G)
       Sectorsize: 512
       Mode: r2w2e3
    

  • df -h
    
    Filesystem      Size    Used   Avail Capacity  Mounted on
    /dev/ada0p2     220G    7.6G    195G     4%    /
    devfs           1.0k    1.0k      0B   100%    /dev
    nfsServer:/home 435G     15G    420G     3%    /usr/home
    

Mounting and unmounting external drives

For a USB memory stick,
  • # mount -t msdosfs /dev/da0s1 /tmp/usb
  • # umount /tmp/usb
While inserting a USB memory stick, a notification on the console will indicate the new device location like /dev/da0. But, you have to find the active partition of the drive like /dev/da0s1 to mount (# ls /dev | grep da could be used).


Note: Unix commands and file locations used here have been tested on FreeBSD systems.

Saturday, December 28, 2013

Unix - File Access Rights and Ownerships

File Attributes

#/$ ls -l /tmp
drwxr-xr-x  4 vijay  wheel           10 Apr 16  2010 testFolder
-rw-r--r--  2 vijay  wheel       368884 Feb 13  2012 news.txt
-rwx------  2 vijay  wheel         2687 Feb 13  2012 secret.txt
-rwxr-xr-x  2 vijay  wheel        11762 Nov 10  2011 code.c

Symbolic permissions


Option   Letter   Represents
(who)   u   User
(who)   g   Group owner
(who)   o   Other
(who)   a   All (“world”)
(action)   +   Adding permissions
(action)   -   Removing permissions
(action)   =   Explicitly set permissions
(permissions)   r   Read
(permissions)   w   Write
(permissions)   x   Execute
(permissions)   t   Sticky bit
(permissions)   s   Set UID or GID

To modify the access rights, modify the file attributes.
# chmod 777 testfile   Allows access by File Owner, Group Members, and All-other-users.
# chmod –R 755 /tmp/test/   -R affects access rights all the files and sub-folders in /tmp/test/
# chmod go= testfile   Symbolic permission (who) (action) (permissions)
# chmod go-w,a+x testfile

Extended file attributes

# getfacl testfile
# setfacl -k testfile
# setfacl -m u:trhodes:rwx,group:web:r--,o::--- testfile

setuid (4), setgid (2), and sticky (1) permissions

# chmod 4755 testfile
=> File will always use permissions and the same user ID of who assigned it)
-rwsr-xr-x (x of owner is replaced with s)

# chmod 2755 testfile
=> File will always use permissions of the group (of the group to assigner belongs)
-rwxr-sr-x (x of group is replaced with s)

# chmod 1777 testdir
=> Allows file deletion only by the owner (make sense for directories)
drwxrwxrwt (x of all-users is replaced with t)


File flags (FreeBSD specific)

File flags are used to prevent accidental editing/removal of files (non-directory) by the root and/or the file owners in FreeBSD.

To view them:
# ls -lo /etc | grep rc.conf
-rw-r--r--  1 root  wheel  schg 1897 Mar  1 2012  rc.conf

In the above case, the secret tool 'schg' makes the file rc.conf unmodifiable even by the superuser (root) without removing the flag. This feature provides one additional level of protection for important files from accidental modifications by the system administrator(s).

Super user (root) only assignable flags

  • sappnd, sappend : sets the system append-only flag (by super-user only)
  • sunlnk, sunlink : sets the system undeletable flag (by super-user only)
  • schg, schange, simmutable : sets the system immutable flag (by super-user only)

Normal user assignable flags

  • uappnd, uappend : sets the user append-only flag (by owner or super-user only)
  • uunlnk, uunlink : sets the user undeletable flag (by owner or super-user only)
  • uchg, uchange, uimmutable : sets the user immutable flag (by owner or super-user only)

To edit these flags chflags command is used. For example:
# chflags sunlink testfile   => Undeletable
# chflags nosunlink testfile

# chflags schg httpd.conf  => System immutable (no change) flag
# chflags noschg httpd.conf

File Ownership

To change the file ownership:
# chown –R vijay:wheel *

Note: Unix commands and file locations used here have been tested on FreeBSD systems.

Wednesday, September 11, 2013

Unix - DHCP

After addition of a new network interface card (NIC) in a DHCP client and/or changing cable from one port to another, client might not establish the network connectivity. In such cases first check whether the active NIC's MAC address is there on DHCP server's database. If it is not there then add the MAC address and restart the server. If the problem persists, refresh the DHCP client as below:

# service netif restart

# dhclient eth1