Archive for 'Unix'

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.

Unknown root password in SuSE Linux

After I installed Suse Linux Enterprise Desktop 10, I tried to update it. It prompted me for a root password even though the install hadn’t asked for one — it had only created a regular user. Sudo didn’t help.

I tried booting in single-user mode, but that also prompted me for the root password.

Finally, I appended init=/bin/bash to the Linux boot command in GRUB. That booted me in a passwordless command line, letting me run passwd and fix things.