Pages

Saturday 8 February 2014

Linux Basic Commands – File System Navigation

cd – Change directory

pwd – Print name of current working directory

ls – List directory contents

file – Print a brief description of the file’s contents.

CD Commands

cd command, is the main command you use to navigate around the file system.  While moving, the pwd command, will tell you where you are now in the current file system. ls command will list the files and directory contents in your current directory.

Root Directory

The Root Directory or “/” is the start of the file system directory, or parent of all directories.

Absolute Pathnames

An absolute pathname begins with the root directory and follows the tree branch by branch until the path to the desired directory or file is completed.  For example,

$ cd /etc/network    (changes your current directory to root --> etc --> network directory)
$ pwd
/etc/network

Relative Pathnames

The shell uses special symbols to represent relative positions in the file system tree.

  • “.” symbol refers to the working directory
  • “..” symbol refers to the parent directory of the current working directory.
  1. “cd ..” means move up to parent directory
  2. “cd ./bin” means from current directory move down to a directory name “bin”
  3. “cd bin” and point 2 does the same thing.

In general if you do not specify a pathname to something, the working directory will be assumed.

cd Shortcuts

  • cd
    Changes the working directory to your home directory
  • cd -
    Changes the working directory to the previous working directory.
  • cd ~user_name 
    Changes the working directory to the home directory of user-name.

Facts about Filenames

  1. Filenames that begin with a period character are hidden. ls command will not list them unless you use ls -a.
  2. Filenames are case sensitive.
  3. There is no concept of “file extension”.
  4. You can, but try not to use punctuation characters in filenames.  Limit the punctuation characters in the filename  you create to “.” period, “-“ dash and “_” underscore. 
  5. You can, but try not to use spaces between filename. Try to use “_” (underscore) to replace your spaces between filename.

The ls Command

  • ls
    List current directory.  List filename only.
  • ls /usr
    List directory under /usr directory
  • ls ~
    List home directory
  • ls -a
    List all files, including hidden files.
  • ls -l or ll
    List current directory with long format.  File permission rights, owner, group, size, date time last modified, filename, links.

The file Command

As there is no concept of “file extension”, you can use the “file” command to print out a brief description of the file’s contents.

$ file /bin/ls
ls: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=0xf31e99218b4d7034cf8257055686bca22f5a3c01, stripped

$file * (list all file type in the current directory)

Base Directories

  • /
    The root directory.  Where everything begins.
  • /boot
    Contains the Linux kernel (vmlinuz), initial RAM disk image (initrd.img) and boot loader (grub).
  • /bin
    Contain common binaries programs. Example, bash, cat, chmod, chgrup, chown, date, grep, kill, less, login, ls, mkdir, rm, rmdir, ping, sed, su, tar.
  • /sbin
    This directory contains system binaries. These are programs that perform system tasks that are generally
    reserved for the superuser. Example, fsck, ifconfig, ifdown, ifup, mkfs, reboot, reload, route, runlevel, start, stop, shutdown.
  • /lib
    Contains shared library files used by the core system programs. These are similar to DLLs in Windows.
  • /usr
    The /usr directory tree is likely the largest one on a Linux system. It contains all the programs and support files used
    by regular users.
      • /usr/bin, contains the executable programs installed by your Linux distribution.
      • /usr/sbin, contains more system administration programs
      • /usr/lib, contains shared libraries for the programs in /usr/bin
  • /opt
    The /opt directory is used to install “optional” software. This is mainly used to hold commercial software products
    that may be installed on your system.  Most of the software will be placed here after system setup.
  • /etc
    The /etc directory contains all of the system-wide configuration files. It also contains a collection of shell
    scripts which start each of the system services at boot time. Everything in this directory should be readable text.
      • /etc/crontab, a file that defines when automated jobs will run
      • /etc/fstab, a table of storage devices and their associated mount points.
      • /etc/passwd, a list of the user accounts
      • /etc/hostname, define of system name
      • /etc/hosts, IP to name hosts file
      • /etc/profile, bash system-wide profile for users
  • /dev
    This is a special directory which contains device nodes. “Everything is a file” also applies to devices. Here is where
    the kernel maintains a list of all the devices it understands.
  • /media
    On modern Linux systems the /media directory will contain the mount points for removable media such USB
    drives, CD-ROMs, etc. that are mounted automatically at insertion.
  • /mnt
    On older Linux systems, the /mnt directory contains mount points for removable devices that have been mounted
    manually.
  • /proc
    The /proc directory is special. It's not a real file system in the sense of files stored on your hard drive. Rather, it is a
    virtual file system maintained by the Linux kernel. The “files” it contains are peepholes into the kernel itself. The
    files are readable and will give you a picture of how the kernel sees your computer.
  • /lost+found
    Each formatted partition or device using a Linux file system, such as ext3, will have this directory. It is used in the case of a partial recovery from a file system corruption event. Unless something really bad has happened to your system, this directory will remain empty.
  • /home
    In normal configurations, each user is given a directory in /home. Ordinary users can only write files in their home
    directories. This limitation protects the system from errant user activity.
  • /root
    This is the home directory for the root account.
  • /tmp
    The /tmp directory is intended for storage of temporary, transient files created by various programs. Some
    configurations cause this directory to be emptied each time the system is rebooted.
  • /var
    The /var directory tree is where data that is likely to change is stored. Various databases, spool files, user mail, etc. are located here.
      • /var/log, contains log files, records of various system activity. These are very important and should be monitored from time to time.

Friday 7 February 2014

Linux Shell Basic – bash shell (note 1)

For most linux system, the default shell is bash, an acronym for Bourne Again Shell.  bash is an enhanced replacement for sh, the original Unix shell program written by Steve Bourne.

The shell is a program that takes your commands from the keyboard and gives them to the operating system to perform.

The bash shell is located in /bin directory.  In /bin directory, you will see a file named “bash”, this is the binary program for bash shell. 

Shell Operations

A shell is a program that interprets commands and allows a user to execute commands by typing them manually at a terminal. A shell is not an operating system. It is a way to interface with the operating system and run commands.

bash shell is just like DOS command prompt.  It has some built-in understanding of “shell commands” and “rules”.  shell script files are like the .bat files in DOS, it contains basic programming logic and utility commands for the shell to interpret and execute.

The following is a brief description of the shell’s operation when it reads and executes a command.  Basically, the shell does the following:

  1. Reads its input from a file or interactive console.
  2. Breaks the input into words and operators or refer to as tokens. 
  3. Parses the input tokens into simple and compound commands.
  4. Breaking the expanded tokens into lists of filenames and commands and arguments.
  5. Performs any necessary redirections and removes the redirection operators and their operands from the argument list.
  6. Executes the command.
  7. Waits for the command to complete and collects its exit status.

For more details on bash shell, you can refer to http://www.gnu.org/software/bash/manual/bashref.html

How is BASH different form the DOS command prompt?

  1. Case Sensitivity:
    In Linus, commands and filenames are case sensitive. 
  2. “\” vs “/”:
    in DOS “/” is use for command argument delimiter and “\” is a directory separator.  In Linux, the “/” is the directory separator and “\” is an escape character (see special characters for BASH on the use of “\”).
  3. Filenames:
    The DOS uses “eight . three” filename convention.  In Linux, there is no file extension.  Periods can be placed at any part of the filename.

Special Characters for BASH

  • \
    Escape character.  If you want to reference a special character, you must “escape” it with a backslash first before you can put in the special character for BASH to understand.
  • /
    Directory separator.
  • .
    Current directory.  Or “hided” files when it is the first character in a filename.
  • ..
    Parent directory.
  • ~
    User’s home directory.
  • *
    Represents 0 or more characters in a filename, or by itself, represents all files in a directory.
  • ?
    Represents a single character in a filename.
  • [ ]
    Can be used to represent a range of values. e.g [0-9] [A-Z]
  • |
    ”Pipe”.  Redirect the output of one command into another command.
  • >
    Redirect output of a command into a new file.
  • >>
    Redirect output of a command onto the end of an existing file.  Or append.
  • <
    Redirect the output of a command into a program.
  • ;
    Command separator.  Allows you to execute multiple commands on a single line.
  • &&
    Command separator as “;”, but only runs the second command if the first one finished without errors.
  • &
    Execute a command in the background and immediately get your shell back.

Executing Commands

  • Most common/utility commands are located in your shell’s “PATH”, meaning that you can just type the name of the program to execute it and the shell will search for the program in the “PATH” and do the execution.   
    You can run the following command to see your current shell’s “PATH”.
    # echo $PATH
  • To execute commands that are not in your PATH, you have to give the complete locations of the command.
    /home/john/myprogram
    ./myprogram (Execute a program in the current directory)
    ~/myprogram (Execute a program from a personal directory)

Do note that you need “./” before you run any program that is not in your “PATH”

Thursday 6 February 2014

Ubuntu Server 13.1 - Install Telnet and SSH Server for remote connection (Post Setup 3)

By default, telnet server and ssh server is not install.  You will have to install it yourself. 
1. Before installation of telnet and ssh, do a ping test to ensure that you are able to connect to Internet from Ubuntu Server.
2. Update and refresh the latest software packages list in Ubuntu Server. This is to ensure that you will install the latest copy of Telnet Server and SSH Server.
$ sudo apt-get update
You will see a list of connections to http://us.archive.ubuntu.com ….  ended with “Reading package lists… Done”.
If you don’t have any error message, you are good to go.
3. Install Telnet Server
$ sudo apt-get install telnetd
After this step, you should be able to telnet to your Ubuntu Server.
If for any reasons that you need to restart your telnetd services, enter the following command
$ sudo /etc/initd/openbsd-inetd restart
4. Install SSH Server
$ sudo apt-get install openssh-server
After this step, you should be able to ssh to your Ubuntu Server.
If for any reason that you need to restart your ssh services, enter the following command
$ sudo /etc/init.d/ssh restart

Ubuntu Server 13.1 - Shutdown and Reboot (Post Setup 2)

Reboot after shutdown
$ sudo shutdown –r now
or
$ sudo reboot
Shutdown and Power off
$ sudo shutdown –P now
or
$ sudo poweroff

Ubuntu Server 13.1 – Basic Networking, Changing IP Address and Hostname (Post Setup 1)

Change IP address from DHCP to Static
The network interface config file is located at /etc/network/interfaces
The default setting of the interfaces file is

# This file describes the network interfaces available on your system

# and how to activate them. For more information, see interfaces(5).

# The loopback network interface

auto lo

iface lo inet loopback

# The primary network interface

auto eth0

iface eth0 inet dhcp

Modify the setting as per below after the entry iface eth0 inet.  I think the below setting is clear enough, don’t have to explain what is IP address, broadcast and gateway.

$ sudo su -

[sudo] password for xxx: (enter your password here for su to root)

$ nano /etc/network/interfaces

iface eth0 inet static

address 192.168.1.51

netmask 255.255.255.0

network 192.168.1.0

broadcast 192.168.1.255

gateway 192.168.1.1

dns-nameservers 192.168.200.1

dns-nameservers 165.21.83.88

$ service networking restart

Note: The /etc/resolv.conf name server entries will be replaced/updated with the dns-nameservers setting in /etc/network/interfaces after rebooting.  Therefore, no need to modify /etc/resolv.conf with the name server entries again.

 

Changing Hostname and Fully Qualified Domain Name (FQDN)

The host name file is located at /etc/hostname, you can modify the entry in this file to the required hostname. Do note that you will also have to modify the hostname in  /etc/hosts file for the 127.0.0.1 entry, if not your hostname and your hosts file entries will be different.

The command service networking restart doesn’t seem to update the hostname changes, best to reboot the system.