Tuesday, July 12, 2011

Making a Mount Point

Until now, we've only talked about Fedora Cores convention for mount points Other Linux systems use different conventions, in order to make mount points name slightly more recognizable SuSE uses just /floppy as opposed to /media/floppy on Fedora. As you just figured out, naming convention is not standard. Remember, a mount point is merely another directory on the system. Why not use your own convention?

For example, you can use the SuSE convention as mentioned above on your Fedora system;
$ su
# mkdir /floppy
# mounr -t vfat /dev/fd0 /floppy

You could edit the fstab file to make it permanent.

Monday, July 11, 2011

The Filesystem Table (fstab) File

Many of the configuration options for your servers filesystem are contained in a single text file, /etc/fstab

fstab, which is an abbreviation of "filesystem table", provides instruction to the operating system as to where devices should me mounted.

Each line of the fstab file contains five fields which, together, specify the configuration of a single device. Lets have a look;
  1. Specifies the device
  2. The mount point
  3. What filesystem Linux should expect
  4. Mounting options
    • auto
      • mounted automatically
    • noauto
      • not mounted automatically
    • owner
      • may only be mounted by owner
    • kudzu
      • will be checked for changes by Redhat kudzu system
    • rw
      • The filesystem will provide read and write access
    • ro
      • The filesystem will provide read only access
    • There are many more options but these are the one that we are interested in. In Fedora Default is equivalent to auto, owner, kudzu, rw.
  5. The fifth colum is used by the dump backup utility to determine if this filesystem should be included in its backups the 0 value tells dump to ignore this filesystem for backup purposes
  6. The final column indicates whether the filesystem should be checked with the fsck (filesystem check) utility ex3 filesystems very rarely benifit from such a check if you want to perform such a check you should number the filesystems in the order which you´d like them checked, 1 for first, 2 for second, and so on.
With a correctly formatted fstab file, using the mount command becomes much easier;

$ su
# mount /media/flobby

Here we've specified only the mount point mount is able to look in fstab to identify the device to which this mount point relates

Mounting and Filesystem

The concept of devices - mount point and filesystem.

Mounting a Filesystem with the mount Command

Lets look at the process of mounting a floppy disk from the command line:

$ su
# mount -t vfat /dev/fd0 /media/floppy


The mount command loads a device´s filesystem into our servers filesystem.
The floppy device at /dev/fd0, and the devices filesystem is loaded in to /media/floppy mount point. We also need to tell Linux what kind of filesystem it needs to expect, in this case its a FAT32 filesystem (vfat).

Before removing a floppy we could unmount it, thereby removing it from the filesystem We can do so using the unmount command.

$ unmount /media/floppy

Thursday, July 7, 2011

Managing Users from the command line

Adding groups and accounts:
Adding accounts from the command line is not as easy as using the ls command. Because the commands used, useradd and groupadd, is not part of the default PATH variable. To use the command you can; first use the, export PATH=$PATH: /usr/sbin. This will ad the PATH for the active session. Or you could add the export command to .bashrc, a hidden file that is automatic run every time you run the bash sell. I will use the second option in this tutorial.

$ su
$ nano .bashrc
add the, export PATH=$PATH:/usr/sbin, at the bottom of the file and save/exit
$ groupadd testgroup
$ useradd -G testgroup -c "test user" \
>testuser
$ passwd testuser

-G flag adds testuser to testgroup, -c gives full name. When we create new users the passwords are locked so you use the passwd and user to assign a password for that user.

Deleting accounts and groups:
Its basically the same format as creating groups and users. You use the commands, userdel and groupdel. One must love the simplicity of Linux =) Note however that the users home directory is intact; you might want to delete this directory as root.

$ su
$ groupdel testgroup
$ userdel testuser
$ rm -rf /home/testuser/

Wednesday, July 6, 2011

The PATH Environment Variable

All of the command line tools are binary files located in the filesystem. ls command, for example, actually lives in the directory /bin. But how does the shell know to find /bin/ls when you type in at the prompt?

The shell looks in all the directories listed in its built in PATH variable, in search for file that has the same name as the command you'r after. The PATH environment variable for default Ubuntu installation will look something like this:

$ echo $PATH
/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin

So when you type a command the shell will first look at /opt/local/bin, is it there, no. Is it in /opt/local/sbin, no. and so forth untill it finds the binary file that matches the command.



In the article prior to this one, we made the script counter.sh. When we wanted to execute it we had to ./counter.sh. If that file would be in one of the PATH directories could just write counter.sh to run the script.

Built-in variables such as PATH are known as environment variables. These are the variables that describe the environment in which a processs is running. Examples of other environment variables HOME, which contains the path of the user's home dir, and MAIL, which is the name of the file that stores the user's email. Environment variables work just like normal shell variables, exepr they can be accessed by programs that are launched from the shell too.

To modify an environment variable, you use the export command:

$ export PATH=$PATH:/home/user_name
$ echo $PATH
/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/home/user_name

We should now be able to run our scripts that we save in our home dir for the current session. However once you exit the shell its lost. Later in our guide we will have a look at how to save its default PATH variable whenever a shell is launched.

Programming the Shell

The Shell itself can be quite a powerful little programmng language. It supports looping, branching, variableseverything that constitutes a worthwhile programming environment. These shell programs, called shell scripts, are just text files on which executes permissions are set. The text files contain commands just like those you'd type on the command line.

Lets look at an example,

$ counter=0
$ echo counter
0

Once the variable counter is created, it's there until you close the terminal window. The value may change but the variable will be there untill declared shell variable.

You can increment it with the operator (++)

$ let counter++
$ echo counter
1

Lets build a simple script, called counter_test.sh, that will keep on counting until we tell it to stop by pressing Ctrl+C (note- Ctrl+C aborts any process on the command line (sending interrupt signal)).


The first line of any chell script is what's commonly called a shebang: a pound sign, followed by an exclamation point, followed by the path to the shell.
  • /bin/sh is a standard shortcut to the shell program on Linux systems. Usually, this file is just a link to the actual shell program )e.g. /bin/bash) but using the standard name ensures that you r script till stull work on a system that uses a different shell.
$nano counter.sh 


And then write the program 


#!/bin/sh

Every shell script must begin with this. The next line of our script will set a value fo the counter vartiable:

counter=0

In other words we are starting at zero. Next we'll add a loop to the program. We'll make it an infinite loop, so the user will have to send an interrupt signal to quit the program.

while true;
do
  # content of loop
done  


Now all we have to do is the content. Printing the varianle counter and incrementing it with the let command


while true;
do
  echo $counter
  let counter++
 sleep 1;
done


Save the file

The last thing we must do is change its permissions. 

$ chmod u+x counter.sh


Execute it


./counter.sh


This will tell the shell to execute a file named counter.sh within the current working directory.

Command History

The bash shell records a limited history of recently issued commands (up to 1000 default). You can scroll through the command history.

To se the fill history,

$ history

Will give you the recent command in chronological order.

The history is also searchable. Hitting Ctrl+R will change the prompt to reverse-i-search; now,  the Shell  will find within the history the most recent command that contains that entered string.  Enter to execute, esc to return to normal mode.

!! executes the last command in the history file.

!partial - command will execute the last command beginning with partial -command. !ech would re-run the last echo command.

Tab Completion

One of the most useful features of many shells is tab completion. When a partial command or filename is entered on the command line, and the user presses the Tab key, the command or filename will be completed for you. Ex, typing cat hello, followed by tab, will result in cat Hello\ World.txt if such a file exist in the directory.

If you double click tab while typing you get the full command, or if there is more options you get sugestions on them.

Thursday, June 30, 2011

Introducing the Shell

Both in a GUI interface and a CLI we interact with the system. In this chapter we take a insight look at the advanced features of the command line.

The default shell is often Bash (Bourne Again Shell). Bash is a modern rewrite of the original Bourne shell, sh, witch was written 1977. Even tho there are many shells available, like tcsh, csh and ksh, bash is increasingly popular due to its useful featureset. We will look in detail of some of those features here.

Deleting files with rm command

You remove files at the prompt by using the pm command, like so

$ rm my_copy.txt


In order to remove a directory and everything in it, we must ass the "recursive" flag (-r)

$ rm -r my_dir


Normally we must have write permission in order to delete it; however, if you have write permissions on the directory containing the file, we can if we confirm the action! 
If you attempt to delete a file you don´t have read permissions, you´ll be asked if you really want to deleter it.


To force a deletion without further cocnfirmation, add th -f flag to the comand, like so:



$ rm -rf _my_dir/my_copy.txt

Tuesday, June 28, 2011

Changing file permissions with chmod

To change the permissions on a file, follow:

$ chmod o+w a_file

Here we grant the other users the write access. The o character stands for other,  w stands for write.

list of flags:

  • u = user
  • g = group
  • o = other 
  • r  = read
  • w = write
  • x = execute
Note that this is just one way we can change permissions to files and directories. Another way is to a numerical syntax, however my goal is not to learn you every single way we can make operations on our system. If you like to read up on other ways to do it, pleas do as my old teacher you to say "Google it!"

Switching user woth the su and exit command

You can switch to root user, follow:
$ su
password:
note the changes at the prompt as you change user. You exit back to the original user by, follow:
$ exit
Switch to a user of choice, follow
$ su user
Where user is the name of the account you want to use.

Copying and moving Files with cp & mv

To copy a file, follow the syntax:

$ cp my_file a_dir/file_name


Where my_file is the name of the file you want to copy and a_dir/file_name is the name and direcoty you want to use.


To copy an entire directory, follow:


$ cp -r a_dir a_dir/file_name


To move a file use the mv command, follow:


mv a_file a_dir/file_name


If you just want to rename the file just place it in the same directory.  Effectively you can also rename the file as you move it.



Tuesday, June 21, 2011

Printing - echo and cat

The echo command simply sends output to the screen.


[ray@domain ~]$ echo "Hello, World!"
Hello, wold!


The cat command opens a file and writes its content to the screen. Keep in mind that certain characters are reserved for special use on the command line. You´ll often want to use these characters in command options or in filenames. To use theses characters, you must escape them, prefix them with a backslash caracter, like so

[ray@domain ~]$ cat Hello\ World.txt
Hello, wold!



Monday, June 20, 2011

Change directory

The cd (Change Directory) command, changes the current working directory to the one specified immediately after the command, ex


[ray@domain ~]$ cd /home/ray/Movies
[ray@domain Movies]$


Used with ~ or by itself it returns you to your home directory.


[ray@domain Movies]$ cd ~/
[ray@domain ~]$


You can move to the directory´s parent by using cd .. command


[ray@domain ~]$cd ..                          
[ray@domain home]$                           

Listing Files

The ls (list) command, used by itself, lists the content of the working directory.

[ray@domain ~]$ ls
Desktop helloworld.txt


You can also retrieve a list of the files in another directory by adding the directory name, ex


[ray@domain ~]$ ls /home/ray
movies pictures documents music public desktop


Using ls by itself will show you the names of tiles in the directory, if you want  all  details you can add the -l flag to the command, ex.


[ray@domain ~]$ ls -l
total 16 files
drwxr-xr-x 2 ray staff 4096 5 Sep 15:21 Desktop
-rw-rw-r-- 1 ray staff 13 8 Sep 07:30 Hello World.txt


The data found above:

  • The first column represents the file permissions in the format discussed in section 1.2 File Persmissions. The first character tells us if the file is a directory or other (d equals a directory). The next nine characters show the permissions to (in order) Owner, Group, and Others.
  • The next column only useful for directories, as it tells you the number of files within it.
  • The next two columns identifies the owner and group
  • Next column is the size of the files in bytes
  • Next is the date and time of last modified, Hello World.txt above is modified September 8 at 07:30
  • Finally, the name of file
Hidden files in Linux start with a period. If you want to hide the Hello World.txt file you would change the name to .Hello World.txt (mind the dot in front of the filename!). Programs store user-specific configuration files by hidden using the period in front of the file. To see hidden files you use the flag -a, which can be used in conjunction with -l flag, ex.

[ray@domain ~]$ ls -la
total 16 files
drwx------ 13 ray  staff 714 19 Jun 20:24  .
drwxr-xr-x  5 root admin 306 17 June 14:44 ..
-re-------  1 ray  staff  14 08 June 15:55 .trash
drwxr-xr-x 2 ray staff 4096 5 Sep 15:21 Desktop
-rw-rw-r-- 1 ray staff 13 8 Sep 07:30 Hello World.txt

At the top of the listing are two directories, "." and "..", These are shortcuts to the current directory and the parent directory, respectively we will have a look at theses later on.

Orienting in the CLI

With a GUI you can have several directories open at once. However, when using the command line, we can only work within one directory at a time. The active directory is called the working directory. Use the pwd (Print Working Directory) to find out which directory you are currently in.

[ray@my.domain ~]$ pwd
/home/ray


Tip - Your home directory, /home/your_account, is referred to as ~. If you look at the syntax above, you can se the ~ at the prompt. If you move around in the filesystem, you will see the command prompt change to reflect the directory that you are in, so you don´t have to constantly use the pwd command to find out where you are.





Wednesday, June 15, 2011

--- LOGGING IN AS ROOT ---

To login as root you need to enter the command su (switch user).
------------------------------------------
[ray@my_system ~]$ su
password:
------------------------------------------
You are prompted for the password to the root account. It maybe that you need to do sudo su I will explain why later on. Don´t stay logged in as root no more than you need. If you only need to do one command as root its a better practice to simply use the command sudo in front of the command you want executed. Sudo is an escape to root just for that command, after which you are returned to you normal user mode. If you have administrative tasks to do and switch to root using su you simply go back to a account by executing the command su user. You could also simply issue exit to return to your account.

--- USING THE COMMAND LINE ---

The command line can be views as an alternative to the window view (GUI). The Command line is known as CLI Command Line Interface. In the command line gives you access to a much more powerful, low level operating system functions. The GUI Graphic User Interface, is a high-lever abstraction environment don´t give you this power. Icons and menues limits the access to what the people who made it has given you. When you click a icon, underneath actual commands are run. The GUI is a limited additional layer between the operator and operating system, in which a restricted range of functions are represented by pretty pictures. By using the CLI you operate in the same lever as your system.

Tip: Low-level and high-level, implies a hierarchy och functionality. Functions of the lowest level communicate with the kernel (system core) directly. High-level tools do more work themselves, offering more functionality and increase safety. Imagen you use the rm command, its fast end effective, but you there is no easy way to recover that file. Using a GUI to remove files insist that you move the file to trash instead of actually removing the file from the filesystem. This gives you a chans to recover it with ease. Of course you could (and I do) simply move files to a directory you name trash before you remove it from you system.  

--- UNDERSTANDING THE COMMAND LINE ---

Unlike windows, the GUI is completely optional in Linux. You can, if you like, run your Linux systen with no graphical environment at all. As Linux evolved different GUIs have been built on top of it. The command line will be your best friend and weapon, it allows you to perform actions that are not easy to administrate from the desktop (or GUI Graphic User Interface).

We will use Terminal to gain access to the command line. Now lets look at a real-life example that uses the command line to meet specific need. Once you understand the components of the command line and the complexity of the GUI, you will start to love the practical utility of the command line.

[ray@mycomputer ~]$ find /var/backup/* -ctime +5 -exec rm {} \;


This command removes my system backups that are more than 5 days old. The command uses the find command to search for files in the /var/backup directory. For each file that the the find tools locates, it runs the rm command, wich stand for and executes remove. 


This looks complicated, however would you do the same in the GUI you would have to complete up to 9 steps to have the same result.

In real life you would not actually run this command three times a week, no the beauty of the command line is that you can automate it with scripts

--- EDITING TEXT FILES ---

A reasonable command of a Linux-compatible text editor is crucial for you as a administrator. Most of the configuration is done via text-files. Linux is replete with text editors.

In fedora you have two robust GUI text editors: gedit and Kate. gedit is hte default for Gnome desktop environment, and Kate is for KDE (On linux machines you can choose witch desktop environment you want to use).

gedit
Gnomes default text editor, it offers full range of features, including:

  • Full support for internationalized text, including the important UTF-8
  • Tabbed multi-document interface
  • Syntax highlighting
  • Plugins and a plugin manger
  • A complete preference interface
In addition to these standard features, plugins can be added to give gedit the following functions
  • Spell checker
  • Insert date and time
  • Word count
  • Change case of selected text
  • Indent or unident blocks of text
  • Ascertain the difference between two documents or files
  • Insert output from the command line
  • Markup language tag list for common markup languages such as HTML, LaTeX, and more.
Tip
Most GUI applications are written for one desktop environment or another. They ofter follow a naming convention for consistency.

Kate
The KDE counterpart, KDE Advanced Text Editor.

Like gedit, Kate is a multi-view editor: it allows you to open and edit multiple documents in the same window. Kate offers a full range of other capabilities that make it a very powerful text editor
  • Edit all kind of text files
  • Powerful syntax highlighting engine is extensible via XML files
  • Offers code folding capabilities for many programming languages
  • Split window view
  • Choose the encoding you want to use
  • Boasts built-in terminal emulation.
  • Sidebar display a list of open documents, a filesystem browser and more
  • Handy plugin interface for third-party plugins
  • Projekt handling capabilities
Kate is a bit more friendly, and offer greater flexibility for editing configuration files and writing shell scripts, than many other editors. The syntax highlighting are unsurpassed, as Kate makes available a full range of languages and styles. Kate provide such features as a open document listing

--- SYMLINK ---

Most People understand the concept of shortcuts. A shortcut is really a pointer from one location in to a file in another area of the system. Shortcut in Windows and Linux are about the same thing. They are mere abstract representation of the original file.

One important difference between Win and Linux is that Linux creates a symbolic link (symlink) that can be treated exactly as if it were the file for most purposes. Symlinks are powerful peace of abstraction because it seams as if a file is in multiple places at once.

You create it in GUI by right-clicking on the file or directory, and select Make Link. You can then drag drop and even rename it if you like. In a shell use ln with attribute -s

--- FILE PERMISSIONS ---

Linux uses the most granular file permissions of any system today. As we
have seen the granular is due, in part, to the fact that everything gin
Linux is a file. Everything is editable, depending on who is editing and
how the permissions are set.

In the most basic Linux permissions is broken down to three sets of
Permissions:

Owner (or user)

The permissions granted to the user who owns the file

Group

The permissions granted to specific group of users. Users can be
Added to any number of available groups.

Other

The permissions granted to anyone who is not the owner, ot
member of available groups.

Each of these sets can grant any combination of the following
permissions:

Read
Write
Execute

What’s especially interesting in the Linux permission structure is how
these sets of three permissions interact. A particular user may belong
to a group that can only read, however the user might be the owner and
can then write.

-- Text view and Number view

I will only go in to text and number view, if you want to se permissions
in the graphical view simply right click it. To view permissions in a
shell use the command ls with the attribute -l

The text view is made up of ten characters. First indicates the file
type: if the file is a directory, this character till be d; if its a
regular file, it will be -. The remaining nice characters indicate the
read, write, and execute permissions for the owner, group, and other.
For example, -rw-rw-r-- represent a regular file with read and write
permissions to owner and group. And only read for other.

The numbers are more compact then text view. The numbers represent, from
left to right, the permissions given to the owner, the group, and other.

-- Last change

This field identifies the date and time at which the file was last
changed

Tuesday, June 14, 2011

---- THE LINUX FILESYSEM ----


My first post at this blog will be about the linux filesystem. Its important for you to understand, and should be the first thing you learn wen undertaking the art of administrating a Linux Server. I hope to be a good resource for you and that you will enjoy your stay at this site!


There is no drives, only partitions under Linux. To muddy the 
Waters a Little more, the partitions actual locations on the hard drive 
are not identified clearly. 

Linux hides the unnecessary details from you. The operating kernel is 
sorted in a directory called /boot/, Firefox is in /us/lib/, your 
personal data like photos and stuff is stored in /home/username/photos 
and temporary files are stored in /tamp/. There are no signs of where 
these files actually reside on different hard drives.

Linux filesystem assigns each partition a different mount point: a 
directory through which we can access the partitions. The / is the top 
of the hierarchy

--- The ext3 Filesystem

Ext3 is the default Filesystem under Fedora, Ubuntu and many others 
Distributions. Ext3 is a journaling Filesystem. Journaling Filesystem 
Facilitates quick system recovery, and ensures a high level of data 
Integrity. It constant updates with notes of file actions that are 
Pending, and those that have been completed. 

Journaling protects against corruption with speed and ease. All pending 
And completed operations are logged to the journal. The system checks 
the journal when rebooting, and completes those operations that was 
pending at the time of a system failure or dirty shutdown. This protects 
the consistency of the data that was buffered at the time of failure. 

Recovery is also decreased by the use of journaling layer. Rather than 
checking each file, bit for bit, the system merely completes any 
Pending writes noted in the journal. This reduces a recovery from 20 to 
30 minutes to seconds. 

- note
Other distributions may use different filesystems, like Suse uses 
ReiserFS.

You can find a complete list at www.linuxjournal.com

--- A Quick Tour of the filesystem

Lets look at a outline of the filesystem structure, and explore its 
various functions and elements.

/ (the root directory
This is the top level of any Linux system, known as the root 
directory, unfortunately, there is also a directory named root.
Don’t worry about this: I will explain this to you in a minute

/boot

This directory contains all the files necessary to boot the 
operation system, including the Linux kernel.

/bin and /sbin

These directories’ are similar in their consistency both contain 
executable binary files but differs in purpose. /bin contains 
executables that you are likely to use from the command line.
Command such as ls, cp mkdir and so on. /sbin contains commands 
and processes that are used by the operation system, so stay 
away from this directory when you are starting out!

/dev

These are the device files abstractions of all the devices that 
are on the system, as well ass all the devices that could be added 
to the system. These files provide the operation system with a 
description of, and instructions for handling each device.

/etc

This directory contains system-specific configuration files.

As an administrator.you are likely to spend quite some time in 
the /etc directory, because it contains configuration 
Instructions for most of the applications on the system.

/home

This directory contains the home directories for each user of 
the system.

/root

This is the home directory of the root user.

/lib

The lib directory contains all the shares libraries that will be 
accessed by various applications in the system. It also contains
libraries that will be used by the kernel

/media /mnt

These directories serve as mount point for temporary mounted 
filesystem. For example, the cd-rom drive will be mounted in
/media/cdrom

/opt

This directory offers storage for packages that have been added 
to the system

/var

Contains variable data files: Files that may change during the 
operation of an application with which they are associated, 
including log files and mailbox files.

- note

The concept of mount point might be confusing. You can think of them as 
containers in which the content of a device is emptied.