Category: Unix

  • mkdir -p is your friend

    mkdir -p is a command second only to touch in succinct utility.

    touch creates a file if it does not exist, or updates its timestamp if it does. It’s handy if you want to write to a file without checking for its existence, as otherwise you’d need to determine whether or not append is the correct mode. It’s also handy for setting flags for yourself on the filesystem.

    mkdir -p creates a path if it does not exist, or does nothing if the path already exists. mkdir -p /foo/bar/baz will create /foo, /foo/bar, and /foo/bar/baz for you. Conversely, mkdir -p /usr/local/bin will not complain because those directories already exist.

    Why would you need this? A couple reasons that came up for me tonight:

    • You cannot redirect output to a file if the file is in a directory that does not yet exist
    • You cannot create a symbolic link in a directory that does not yet exist
  • Startup order of services on RHEL or CentOS

    The startup order of services on Red Hat Enterprise Linux (and very likely other flavours) is determined by the numerical prefix on the symbolic links in /etc/rc3.d/ (for run level 3), /etc/rc5.d/ (for run level 5), and so on.

    When adding a service (chkconfig –add my_service –level 35), you can specify startup order by including a chkconfig clause in the header:

    #!/bin/sh
    #
    # chkconfig: 35 59 80

    The above service should start at run level 3 and 5 as the 59th service to run (the numbering is sparse, so there isn’t necessarily as a 58th service).

    The 80 specifies the shut down order.

    Obviously, do not rename the symbolic links yourself. Use chkconfig to manage them.

  • cpio: chown failed – Invalid argument

    I ran into this error while installing the IBM Java RPM on Red Hat Enterprise Linux:

    error: unpacking of archive failed on file /opt/ibm/java-x86_64-60: cpio: chown failed - Invalid argument

    The issue is due to /opt/ibm being an NFS mount on the system. There are known issues with running the chown command on NFS 4. One workaround is to specify NFS protocol version 3:

    mount -t nfs -o vers=3 127.0.0.1:/share /mnt

    In my case, the NFS mount was specified in /etc/fstab, so I modified the relevant line there to say:

    127.0.0.1:/opt/ibm /opt/ibm nfs rw,vers=3 0 0
  • Linux Basics: Navigate with ls, cd, and pwd

    The Linux command line can be a bit intimidating at first, but it gets much easier once you learn a few basic building blocks. The power of the command line lies in combining many basic commands in interesting ways.

    Open up a Linux terminal (or, if you want to follow along on Windows, Cygwin). The darkness of the abyss stares at you, but it’s really not as unfriendly as all that.

    First things first. Where am I? Type “pwd” without quotes and hit enter:

    $ pwd
    
    /home/leonsp

    You should see something like “/home/leonsp”. That’s your home directory, similar to your Documents folder on Windows. “pwd” stands for “print working directory”, and is a handy way to find out your current location.

    Let’s try going somewhere else. Enter “cd ..”, followed by “pwd”.

    $ cd ..
    $ pwd
    
    /home

    Two dots means one directory up. You just changed the directory to one directory up from /home/leonsp, which is /home.

    Let’s get back to your home directory. There’s a few ways to do this. The following commands will all do the same thing:

    $ cd /home/leonsp
    $ cd ~
    $ cd

    ~ is convenient shorthand that refers to the home directory of the current user — you.

    What’s in all these directories? Let’s list the contents using the “ls” command:

    $ ls

    Not much will show up, as your home directory starts out with few files in it. It will, however, have a bunch of hidden files. Let’s list all of these:

    $ ls -a
    
    .  ..  .bash_history  .bash_profile  .bashrc  .inputrc  .lesshst  .ssh  .subversion

    By convention, Linux treats all filenames starting with a dot as hidden. They will only show in the listing when you ask to see them.

    .bash_history, .bash_profile, and .bashrc are configuration files for my shell, Bash. Which shell you have mostly affects scripting (or automation), which is a more advanced topic. Bash, ksh, and sh are similar, while csh and tcsh are a bit different.

    . and .. will show up everywhere. Single dot “.” refers to the current directory, and two dots “..” refer to the parent directory.

    It’s possible to get a long, more detailed listing:

    $ ls -la
    
    drwxr-xr-x+ 1 leonsp None     0 Feb 22 15:30 .
    drwxrwxrwt+ 1 leonsp root     0 Jan 28  2010 ..
    -rw-------  1 leonsp None 17431 Apr  5 00:16 .bash_history
    -rwxr-xr-x  1 leonsp None  1150 Jan 28  2010 .bash_profile
    -rwxr-xr-x  1 leonsp None  3754 Jan 28  2010 .bashrc
    -rwxr-xr-x  1 leonsp None  1461 Jan 28  2010 .inputrc
    -rw-------  1 leonsp None    35 Jan 10 16:48 .lesshst
    drwx------+ 1 leonsp None     0 Mar 30 13:05 .ssh
    drwxr-xr-x+ 1 leonsp None     0 Feb 22 15:30 .subversion
    -rw-r--r--  1 leonsp None     0 Aug 26  2010 blah

    We’ll get into what these columns mean later.

  • Find out your SLES version and service pack level from command line

    I recently needed to find out the version of SLES that was running using the command line. This did the trick:

    cat /etc/SuSE-release
    
  • Enable NFS services for HADR

    You might need to enable network file system (NFS) services when configuring the High Availability Disaster Recovery (HADR) or Database Partioning (DPF) DB2 features. For example, with HADR, you might want to set up a slave database that shares a backup with the master database through NFS.

    These commands should enable NFS services on most distributions of Linux:

    sudo /sbin/chkconfig nfs on
    sudo /sbin/chkconfig nfslock on
    sudo /sbin/chkconfig rpcgssd on
    sudo /sbin/chkconfig rpcidmapd on
    sudo /sbin/chkconfig portmap on
    sudo /etc/init.d/iptables restart
    sudo /etc/init.d/nfslock restart
    sudo /etc/init.d/portmap restart
    sudo /etc/init.d/nfs restart
    

    Some of the services might not exist on your distribution. Additional steps may be needed.

    IBM Smart Business Cloud for Test & Development might be one of the environments where you’d have to do this.

  • iptables pitfall

    An important thing to remember about rulesets in /etc/sysconfig/iptables is that they are chains. The first rule is applied, followed by the second, and so on. It’s the opposite of CSS that way. More specific rules should go first, while all-encompassing rules should go last.

    I was trying to open the usual DB2 ports on RHEL. For some reason, nothing was working.

    It turned out that this line was at fault:

    -A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited
    

    This rejects all incoming traffic not otherwise allowed. The line has to go last in the file, after all the open port definitions.

  • device br0 already exists

    Here’s how you solve either of the following errors on Ubuntu (and possibly Debian):

    device br0 already exists; can't create bridge with the same name
    device eth0 is already a member of a bridge; can't enslave it to bridge br1.
    

    Removing the device specs from /etc/network/interfaces and restarting the network doesn’t actually remove the device if already active. You need to do it manually.

    List the active devices:

    ifconfig | more
    

    And then do this to any that you don’t want there, such as br0, eth0, eth1, etc:

    ifconfig br0 down
    # and so on
    

    This deactivates the device. At this point you’ll need to restart the network layer twice:

    sudo /etc/init.d/networking restart
    sudo /etc/init.d/networking restart
    

    And you should be sitting pretty. On some systems, networking is known as network, as in:

    sudo /etc/init.d/network restart
    sudo /etc/init.d/network restart
    
  • Disabling PHP in a specific directory

    To disable the PHP processor in a given directory, put the following in an .htaccess file. If one doesn’t exist, create it.

    # Disable PHP
    AddHandler default-handler php
    RemoveType application/x-httpd-php php
    
    # Make .php files display as plain text
    AddType text/plain php
    

    This assumes an Apache server. PHP on IIS may involve different steps.

    Files starting with a . are hidden by default on *nix OSes. To see them in listings, use ls -a.

  • History meme

    Substantial content is in the pipes, but in the meantime here’s Arve Bersvendsen’s history meme:

    history | awk '{a[$2]++ } END{for(i in a){print a[i] " " i}}'|sort -rn|head
    

    In my cygwin:

    52 python
    44 ssh
    33 exit
    33 cd
    18 ls
    7 java
    5 diff
    4 nano
    2 ping
    1 svn
    

    And on a Linux machine I administrate:

    211 sudo
    92 ls
    80 cd
    34 locate
    14 nano
    6 rm
    6 exit
    6 cp
    3 tar
    3 python2.4
    

    I can survive in vi if pressed, but nano is my text-mode editor of choice. I do serious Linux development in Eclipse, Kate, or Kdevelop.