Never Ending Security

It starts all here

Tag Archives: Starters Guide

Introduction To Linux (Starters Guide)



WHY USE LINUX?


The first question is – what are the benefits of using Linux instead of Windows? This is in fact a constant debate between the Windows and Linux communities and while we won’t be taking either side, you’ll discover that our points will favour the Linux operating system because they are valid :)

AND THE REASONS FOR USING LINUX ….

While we could list a billion technical reasons, we will focus on those that we believe will affect you most:

•  Linux is free. That’s right – if you never knew it, the Linux operating system is free of charge. No user or server licenses are required*! If, however, you walk into an IT shop or bookstore, you will find various Linux distributions on the shelf available for purchase, that cost is purely to cover the packaging and possible support available for the distribution.

* We must note that the newer ‘Advanced Linux Servers’, now available from companies such as Redhat, actually charge a license fee because of the support and update services they provide for the operating system. In our opinion, these services are rightly charged since they are aimed at businesses that will use their operating system in critical environments where downtime and immediate support is non-negotiable.

•Linux is developed by hundreds of thousands of people worldwide. Because of this community development mode there are very fresh ideas going into the operating system and many more people to find glitches and bugs in the software than any commercial company could ever afford (yes, Microsoft included).

•Linux is rock solid and stable, unlike Windows, where just after you’ve typed a huge document it suddenly crashes, making you loose all your work!

Runtime errors and crashes are quite rare on the Linux operating system due to the way its kernel is designed and the way processes are allowed to access it. No one can guarantee that your Linux desktop or server will not crash at all, because that would be a bit extreme, however, we can say that it happens a lot less frequently in comparison with other operating systems such as Windows.

For the fanatics of the ‘blue screen of death’ – you’ll be disappointed to find out there is no such thing in the world of Linux. However, not all is lost as there have been some really good ‘blue screen of death’ screen savers out for the Linux graphical X Windows system.

You could also say that evidence of the operating system’s stability is the fact that it’s the most widely used operating system for running important services in public or private sectors. Worldwide statistics show that the number of Linux web servers outweigh by far all other competitors:

linux-introduction-why-use-linux-1

Today, netcraft reports that for the month of June 2005, out of a total of 64,808,485 Web servers, 45,172,895 are powered by Apache while only 13,131,043 use Microsoft’s IIS Web server!

•Linux is much more secure than Windows, there are almost no viruses for Linux and, because there are so many people working on Linux, whenever a bug is found, a fix is provided much more quickly than with Windows. Linux is much more difficult for hackers to break into as it has been designed from the ground up with security in mind.

•Linux uses less system resources than Windows. You don’t need the latest, fastest computer to run Linux. In fact you can run a functional version of Linux from a floppy disk with a computer that is 5-6 years old! At this point, we can also mention that one of our lab firewalls still runs on a K6-266 -3DNow! processor with 512 MB Ram! Of course – no graphical interfaces are loaded as we only work on in CLI mode!

•Linux has been designed to put power into the hands of the user so that you have total control of the operating system and not the other way around. A person who knows how to use Linux has the computer far more ‘by the horns’ than any Windows user ever has.

•Linux is fully compatible with all other systems. Unlike Microsoft Windows, which is at its happiest when talking to other Microsoft products, Linux is not ‘owned’ by any company and thus it keeps its compatibility with all other systems. The simplest example of this is that a Windows computer cannot read files from a hard-disk with the Linux file system on it (ext2 & ext3), but Linux will happily read files from a hard-disk with the Windows file system (fat, fat32 or ntfs file system), or for that matter any other operating system.

Now that we’ve covered some of the benefits of using Linux, let’s start actually focusing on the best way to ease your migration from the Microsoft world to the Linux world, or in case you already have a Linux server running – start unleashing its full potential!

The first thing we will go over is the way Linux deals with files and folders on the hard-disk as this is completely different to the way things are done in Windows and is usually one of the challenges faced by Linux newbies


THE LINUX FILE SYSTEM


A file system is nothing more than the way the computer stores and retrieves all your files. These files include your documents, programs, help files, games, music etc. In the Windows world we have the concept of files and folders.

A folder (also known as a directory) is nothing more than a container for different files so that you can organise them better. In Linux, the same concept holds true — you have files, and you have folders in which you organise these files.

The difference is that Windows stores files in folders according to the program they belong to (in most cases), in other words, if you install a program in Windows, all associated files — such as the .exe file that you run, the help files, configuration files, data files etc. go into the same folder. So if you install for example Winzip, all the files relating to it will go into one folder, usually c:\Program Files\Winzip.

In Linux however, files are stored based on the function they perform. In other words, all help files for all programs will go into one folder made just for help files, all the executable (.exe) files will go into one folder for executable programs, all programs configuration files will go into a folder meant for configuration files.

This layout has a few significant advantages as you always know where to look for a particular file. For example, if you want to find the configuration file for a program, you’ll bound to find it in the actual program’s installation directory.

With the Windows operating system, it’s highly likely the configuration file will be placed in the installation directory or some other Windows system subfolder. In addition, registry entries is something you won’t be able to keep track of without the aid of a registry tracking program – something that does not exist in the Linux world since there is no registry!

Of course in Linux everything is configurable to the smallest level, so if you choose to install a program and store all its files in one folder, you can, but you will just complicate your own life and miss out on the benefits of a file system that groups files by the function they perform rather than arbitrarily.

Linux uses an hierarchical file system, in other words there is no concept of ‘drives’ like c: or d:, everything starts from what is called the ‘/’ directory (known as the root directory). This is the top most level of the file system and all folders are placed at some level from here. This is how it looks:

linux-introduction-file-system-1

As a result of files being stored according to their function on any Linux system, you will see many of the same folders.

These are ‘standard’ folders that have been pre-designated for a particular purpose. For example the ‘bin’ directory will store all executable programs (the equivalent of Windows ‘.exe ‘ files).

Remember also that in Windows you access directories using a backslash (eg c:\Program Files) whereas in Linux you use a forward slash (eg: /bin ).

In other words you are telling the system where the directory is in relation to the root or top level folder.

So to access the cdrom directory according to the diagram on the left you would use the path /mnt/cdrom.

To access the home directory of user ‘sahir’ you would use /home/sahir.

So it’s now time to read a bit about each directory function to help us get a better understanding of the operating system:

• bin – This directory is used to store the system’s executable files. Most users are able to access this directory as it does not usually contain system critical files.

• etc – This folder stores the configuration files for the majority of services and programs run on the machine. These configuration files are all plain text files that you can open and edit the configuration of a program instantly. Network services such as samba (Windows networking), dhcp, http (apache web server) and many more, rely on this directory! You should be careful with any changes you make here.

• home – This is the directory in which every user on the system has his own personal folder for his own personal files. Think of it as similar to the ‘My Documents’ folder in Windows. We’ve created one user on our test system by the name of ‘sahir’ – When Sahir logs into the system, he’ll have full access to his home directory.

• var – This directory is for any file whose contents change regularly, such as system log files – these are stored in /var/log. Temporary files that are created are stored in the directory /var/tmp.

• usr – This is used to store any files that are common to all users on the system. For example, if you have a collection of programs you want all users to access, you can put them in the directory /usr/bin. If you have a lot of wallpapers you want to share, they can go in /usr/wallpaper. You can create directories as you like.

• root – This can be confusing as we have a top level directory ‘/’ which is also called ‘the root folder’.

The ‘root’ (/root) directory is like the ‘My Documents’ folder for a very special user on the system – the system’s Administrator, equivalent to Windows ‘Administrator’ user account.

This account has access to any file on the system and can change any setting freely. Thus it is a very powerful account and should be used carefully. As a good practice, even if you are the system Administrator, you should not log in using the root account unless you have to make some configuration changes.

It is a better idea to create a ‘normal’ user account for your day-to-day tasks since the ‘root’ account is the account for which hackers always try to get the password on Linux systems because it gives them unlimited powers on the system. You can tell if you are logged in as the root account because your command prompt will have a hash ‘#’ symbol in front, while other users normally have a dollar ‘$‘ symbol.

• mnt – We already told you that there are no concepts of ‘drives’ in Linux. So where do your other hard-disks (if you have any) as well as floppy and cdrom drives show up?

Well, they have to be ‘mounted’ or loaded for the system to see them. This directory is a good place to store all the ‘mounted’ devices. Taking a quick look at our diagram above, you can see we have mounted a cdrom device so it is showing in the /mnt directory. You can access the files on the cdrom by just going to this directory!

• dev – Every system has its devices, and the Linux O/S is no exeption to this! All your systems devices such as com ports, parallel ports and other devices all exist in /dev directory as files and directories! You’ll hardly be required to deal with this directory, however you should be aware of what it contains.

• proc – Think of the /proc directory as a deluxe version of the Windows Task Manager. The /proc directoy holds all the information about your system’s processes and resources. Here again, everything exists as a file and directory, something that should’t surprise you by now!

By examining the appropriate files, you can see how much memory is being used, how many tcp/ip sessions are active on your system, get information about your CPU usage and much more. All programs displaying information about your system use this directory as their source of information!

• sbin – The /sbin directory’s role is that similar to the /bin directory we covered earlier, but with the difference its only accessible by the ‘root’ user. Reason for this restriction as you might have already guessed are the sensitive applications it holds, which generally are used for the system’s configuration and various other important services. Consider it an equivelant to the Windows Administration tools folder and you’ll get the idea.

Lastly, if you’ve used a Linux system, you’ll have noticed that not many files have an extension – that is, the three letters after the dot, as found in Windows and DOS: file1.txt , winword.exe , letter.doc.

While you can name your files with extensions, Linux doesn’t really care about the ‘type’ of file. There are very quick ways to instantly check the type of file anything is. You can even make just about any file in Linux an executable or .exe file at whim!

Linux is smart enough to recognise the purpose of a file so you don’t need to remember the meaning of different extensions.

You have now covered the biggest hurdle faced by new Linux users. Once you get used to the file system you’ll find it is a very well organised system that makes storing files a very logical process. There is a system and, as long as you follow it, you’ll find most of your tasks are much simpler than other operating system tasks.


THE LINUX COMMAND LINE


You could actually skip this whole section for those who are already familiar with the topic, but we highly recommend you read it because this is the heart of Linux. We also advise you to go through this section while sitting in front of the computer.

Most readers will be familiar with DOS in Windows and opening a DOS box. Well, let’s put it this way.. comparing the power of the Linux command line with the power of the DOS prompt is like comparing a Ferrari with a bicycle!

People may tell you that the Linux command line is difficult and full of commands to remember, but it’s the same thing in DOS and just remember – you can get by in Linux without ever opening a command line (just like you can do all your work in Windows without ever opening a DOS box !). However, the Linux command line is actually very easy, logical and once you have even the slightest ability and fluency with it, you’ll be amazed as to how much faster you can do complicated tasks than you would be able to with the fancy point-and-click graphics and mouse interface.

To give you an example, imagine the number of steps it would take you in Windows to find a file that has the word “hello” at the end of a line, open that file, remove the first ten lines, sort all the other lines alphabetically and then print it. In Linux, you could achieve this with a single command! – Have we got your attention yet ?!

Though you might wonder what you could achieve by doing this – the point is that you can do incredibly complicated things by putting together small commands, exactly like using small building blocks to make a big structure.

We’ll show you a few basic commands to move around the command line as well as their equivalents in Windows. We will first show you the commands in their basic form and then show you how you can see all the options to make them work in different ways.

THE BASIC COMMANDS

As a rule, note that anything typed in ‘single quotes and italics‘ is a valid Linux command to be typed at the command line, followed by Enter.

We will use this rule throughout all our tutorials to avoid confusion and mistakes. Do not type the quotes and remember that, unlike Windows, Linux is case sensitive, thus typing ‘Document’ is different from typing ‘document’.

•  ls – You must have used the ‘dir’ command on Windows… well this is like ‘dir’ command on steroids! If you type ‘ls‘ and press enter you will see the files in that directory, there are many useful options to change the output. For example, ‘ls -l‘ will display the files along with details such as permissions (who can access a file), the owner of the file(s), date & time of creation, etc. The ‘ls‘ command is probably the one command you will use more than any other on Linux. In fact, on most Linux systems you can just type ‘dir‘ and get away with it, but you will miss out on the powerful options of the ‘ls‘ command.

linux-introduction-cmd-line-1

•  cd – This is the same as the DOS command: it changes the directory you are working in. Suppose you are in the ‘/var/cache’ directory and want to go to its subfolder ‘samba’ , you can type ‘cd samba‘ just as you would if it were a DOS system.

linux-introduction-cmd-line-2

Imagine you were at the ‘/var/cache’ directory and you wanted to change to the ‘/etc/init.d’ directory in one step, you could just type ‘cd /etc/init.d‘ as shown above. On the other hand, if you just type ‘cd‘ and press enter, it will automatically take you back to your personal home directory (this is very useful as all your files are usually stored there).

We also should point out that while Windows and DOS use the well known back-slash ‘ \ ‘ in the full path address, Linux differentiates by using the forward-slash ‘ / ‘. This explains why we use the command ‘cd /etc/init.d‘ and notcd \etc\init.d‘ as most Windows users would expect.

•  pwd – This will show you the directory you are currently in, should you forget. It’s almost like asking the operating system ‘Where am I right now ?’. It will show you the ‘present working directory’.

linux-introduction-cmd-line-3

•  cp – This is the equivalent of the Windows ‘copy’ command. You use it to copy a file from one place to another. So if you want to copy a file called ‘document’ to another file called ‘document1’ , you would need to type ‘cp document document1‘. In other words, first the source, then the destination.

linux-introduction-cmd-line-4

The ‘cp’ command will also allow you to provide the path to copy it to. For example, if you wanted to copy ‘document’ to the home directory of user1, you would then type ‘cp document /home/user1/‘. If you want to copy something to your home directory, you don’t need to type the full path (example /home/yourusername), you can use the shortcut ‘~’ (tilda), so to copy ‘document’ to your home directory, you can simply type ‘copy document ~‘ .

•  rm – This is the same as the ‘del’ or ‘delete’ command in Windows. It will delete the files you input. So if you need to delete a file named ‘document’, you type ‘rm document’. The system will ask if you are sure, so you get a second chance! If you typed ‘rm –f then you will force (-f) the system to execute the command without requiring confirmation, this is useful when you have to delete a large number of files.

linux-introduction-cmd-line-5

In all Linux commands you can use the ‘*’ wildcard that you use in Windows, so to delete all files ending with .txt in Windows you would type ‘del *.txt‘ whereas in Linux you would type ‘rm -f *.txt. Remember, we used the ‘-f‘ flag because we don’t want to be asked to confirm the deletion of each file.

linux-introduction-cmd-line-6

To delete a folder, you have to give rm the ‘-r‘ (recursive) option; as you might have already guessed, you can combine options like this: ‘rm -rf mydirectory‘. This will delete the directory ‘mydirectory’ (and any subdirectories within it) and will not ask you twice. Combining options like this works for all Linux commands.

•mkdir / rmdir – These two commands are the equivalent of Windows’ ‘md’ and ‘rd’, which allow you to create (md) or remove (rd) a directory. So if you type ‘mkdir firewall‘, a directory will be created named ‘firewall’. On the other hand, type ‘rmdir firewall‘ and the newly created directory will be deleted. We should also note that the ‘rmdir‘ command will only remove an empty directory, so you might be better off using ‘rm -rf‘ as described above.

linux-introduction-cmd-line-7

•mv – This is the same as the ‘move’ command on Windows. It works like the ‘cp‘ or copy command, except that after the file is copied, the original source file is deleted. By the way, there is no rename command on Linux because technically moving and renaming a file is the same thing!

In this example, we recreated the ‘firewall‘ directory we deleted previously and then tried renaming it to ‘firewall-cx‘. Lastly, the new directory was moved to the ‘/var’ directory:

linux-introduction-cmd-line-8

That should be enough to let you move around the command line or the ‘shell’, as it’s known in the Linux community. You’ll be pleased to know that there are many ways to open a shell window from the ‘X’ graphical desktop, which can be called an xterm, or a terminal window.

•  cat / more / less – These commands are used to view files containing text or code. Each command will allow you to perform a special function that is not available with the others so, depending on your work, some might be used more frequently than others.

The ‘cat‘ command will show you the contents of any file you select. This command is usually used in conjunction with other advanced commands such as ‘grep‘ to look for a specific string inside a large file which we’ll be looking at later on.

When issued, the ‘cat’ command will run through the file without pausing until it reaches the end, just like a file scanner that examines the contents of a file while at the same time showing the output on your screen:

linux-introduction-cmd-line-9

In this example, we have a whopper 215kb text file containing the system’s messages. We issued the ‘cat messages‘ command and the file’s content is immediately listed on our screen, only this went on for a minute until the ‘cat’ command reached the end of the file and then exited.

Not much use for this example, but keep in mind that we usually pipe the output to other commands in order to give us some usable results :)

more‘ is used in a similar way, but will pause the screen when it has filled with text, in which case we need to hit the space bar or enter key to continue scrolling per page or line. The ‘up’ or ‘down’ arrow keys are of no use for this command and will not allow you to scroll through the file – it’s pretty much a one way scrolling direction (from the beginning to the end) with the choice of scrolling per page (space bar) or line (enter key).

The ‘less‘ command is an enhanced version of ‘more‘, and certainly more useful. With the less command, you are able to scroll up or down a file’s content. To scroll down per page, you can make use of the space bar, or CTRL-D. To scroll upwards towards the beginning of the file, use CTRL-U.

It is not possible for us to cover all the commands and their options because there are thousands! However, we will teach you the secret to using Linux — that is, how to find the right tool (command) for a job, and how to find help on how to use it.

CAN I HAVE SOME HELP PLEASE?

To find help on a command, you type the command name followed by ‘–help. For example, to get help on the ‘mkdir‘ command, you will type ‘mkdir –help. But there is a much more powerful way…

For those who read our previous section, remember we told you that Linux stores all files according to their function? Well Linux stores the manuals (help files) for every program installed, and the best part is that you can look up the ‘man pages’ (manuals) very easily. All the manuals are in the same format and show you every possible option for a command.

To open the manual of a particular command, type ‘man‘ followed by the command name, so to open the manual for ‘mkdir’ type ‘man mkdir‘:

linux-introduction-cmd-line-10

Interestingly, try getting help on the ‘man’ command itself by typing ‘man man. This is the most authoritative and comprehensive source of help for anything you have in Linux, and the best part is that every program will come with its manual! Isn’t this so much better than trying to find a help file or readme.txt file :) ?

Here’s another incredibly useful command — if you know the task you want to perform, but don’t know the command or program to use, use the ‘apropos‘ command. This command will list all the programs on the system that are related to the task you want to perform. For example, say you want to send email but don’t know the email program, you can type ‘apropos email‘ and receive a list of all the commands and programs on the system that will handle email! There is no equivalent of this on Windows.

WHERE IS THAT FILE?

Another basic function of any operating system is knowing how to find or search for a missing or forgotten file, and if you have already asked yourself this question, you’ll be pleased to find out the answer :)

The simplest way to find any file in Linux is to type ‘locate‘ followed by the filename. So if you want to find a file called ‘document’ , you type ‘locate document‘. The locate command works using a database that is usually built when you are not using your Linux system, indexing all your files and directories to help you locate them.

You can use the more powerful ‘find‘ command, but I would suggest you look at its ‘man’ page first by typing ‘man find‘. The ‘find‘ command differs from the ‘locate‘ command in that it does not use a database, but actually looks for the file(s) requested by scanning the whole directory or file system depending on where you execute the command.

Logically, the ‘locate‘ command is much faster when looking for a file that has already been indexed in its database, but will fail to discover any new files that have just been installed since they haven’t been indexed! This is where the ‘find‘ command comes to the rescue!


INSTALLING SOFTWARE ON LINUX


Installing software in Linux is very different from Windows for one very simple reason: most Linux programs come in ‘source code’ form. This allows you to modify any program (if you’re a programmer) to suit your purposes! While this is incredibly powerful for a programmer, for most of us who are not- we just want to start using the program!

SHOW ME THE WAY MASTER….

Most programs will come ‘zipped’ just like they do in Windows, in other words they pack all the files together into one file and compress it to a more manageable size. Depending on the zipping program used, the method of unzipping may vary, however, each program will have step by step instructions on how to unpack it.

Most of the time the ‘tar’ program will be used to unpack a package and unzipping the program is fairly straightforward. This is initiated by typing ‘tar -zxvf file-to-unzip.tgz‘ where ‘file-to-unzip.tgz’ is the actual filename you wish to unzip. We will explain the four popular options we’ve used (zxvf) but you can read the ‘tar man‘ page if you are stuck or need more information.

As mentioned, the ‘tar‘ program is used to unpack a package we’ve downloaded and would like to install. Because most packages use ‘tar’ to create one file for easy downloads, gzip (Linux’s equivalent to the Winzip program) is used to compress the tar file (.gz), reducing the size and making it easier to transfer. This also explains the reason most files have extensions such as ‘.tgz’ or ‘.tar.gz’.

To make life easy, instead of giving two commands to decompress (unzip) and unpack the package, we provide tar with the -z option to automatically unzip to package and then proceed with unpacking it (-x). Here are the options in greater detail:

-z : Unzip tar package before unpacking it.

-x : Extract/Unpack the package

-v : Verbosely list files processed

-f : use archive file (filename provided)

linux-introduction-installing-software-1

Because the list of files was long, we’ve cut the bottom part to make it fit in our small window.

Once you have unzipped the program, go into its directory and look for a file called INSTALL, most programs will come with this file. It contains detailed instructions on how to install it, including the necessary commands to be typed, depending on the Linux distribution you have. After you’ve got that out of the way, you’re ready to use the three magic commands that install 99% of all software in Linux :)

Open the program directory and type ./configure. [1st magic command]

linux-introduction-installing-software-2

You’ll see a whole lot of output that you may not understand; this is when the software you’re installing is automatically checking your system to analyze the options that will work best. Unlike the Windows world, where programs are made to work on a very general computer, Linux programs automatically customize themselves to fit your system.

Think of it as the difference between buying ready-made clothes and having tailor made clothes especially designed for you. This is one of the most important reasons why programs are in the ‘source code’ form in Linux.

In some cases, the ./configure command will not succeed and will produce errors that will not allow you to take the step and compile your program. In these cases, you must read the errors, fix any missing library files (most common causes) or problems and try again:

linux-introduction-installing-software-3

As you can see, we’ve run into a few problems while trying to configure this program on our lab machine, so we looked for a different program that would work for the purpose of this demonstration!

linux-introduction-installing-software-4

This ./configure finished without any errors, so the next step is to type make. [2nd magic command]

linux-introduction-installing-software-5

This simple command will magically convert the source code into a useable program… the best analogy of this process is that in the source code are all the ingredients in a recipe, if you understand programming, you can change the ingredients to make the dish better. Typing the make command takes the ingredients and cooks the whole meal for you! This process is known as ‘compiling’ the program

If make finishes successfully, you will want to put all the files into the right directories, for example, all the help files in the help files directory, all the configuration files in the /etc directory (covered in the pages that follow).

To perform this step, you have to log in as the superuser or ‘root’ account, if you don’t know this password you can’t do this.

Assuming you are logged in as root, type make install. [3rd magic command]

linux-introduction-installing-software-6

Lastly, once our program has been configured, compiled and installed in /usr/local/bin with the name of ‘bwn-ng’, we are left with a whole bunch of extra files that are no longer useful, these can be cleaned using the make clean command – but this, as you might have guessed, is not considered a magic command :)

linux-introduction-installing-software-7

There, that’s it!

Now here’s the good news… that was the old hard way!

All the people involved with Linux realised that most people don’t need to read the source code and change the program and don’t want to compile programs, so they have a new way of distributing programs in what is known as ‘rpm’ (red hat package manager) format.

This is one single file of a pre-compiled program, you just have to double click the rpm file (in the Linux graphical interface – X) and it will install it on your system for you!

In the event that you find a program that is not compiling with ‘make‘ you can search on the net (we recommend www.pbone.net ) for an rpm based on your Linux distribution and version. Installation then is simply one click away for the graphical X desktop, or one command away for the hardcore Linux enthusiasts!

Because the ‘rpm’ utility is quite complex with a lot of flags and options, we would highly recommend you read its ‘man’ page before attempting to use it to install a program.

One last note about rpm is that it will also check to see if there are any dependent programs or files that should or shouldn’t be touched during an install or uninstall. By doing so, it is effectively protecting your operating system from accidentally overwriting or deleting a critical system file, causing a lot of problems later on!


ADVANCED LINUX COMMANDS


Now that you’re done learning some of the Basic Linux commands and how to use them to install Linux Software, it’s time we showed you some of the other ways to work with Linux. Bear in mind that each distribution of Linux (Redhat, SUSE, Mandrake etc) will come with a slightly different GUI (Graphical User Interface) and some of them have done a really good job of creating GUI configuration tools so that you never need to type commands at the command line.

•

VI EDITOR

For example, if you want to edit a text file you can easily use one of the powerful GUI tools like Kate, Kwrite etc., which are all like notepad in Windows though much more powerful; they have features such as multiple file editing and syntax highlighting (if you open an HTML file it understands the HTML tags and highlights them for you). However, you can also use the very powerful vi editor.

When first confronted by vi most users are totally lost, you open a file in vi (e.g vi document1) and try to type, but nothing seems to happen.. the system just keeps beeping!

linux-introduction-avd-cmd-line-1

Well that’s because vi functions in two modes, one is the command mode, where you can give vi commands such as open a file, exit, split the view, search and replace etc., and the other mode is the insert view where you actually type text!

Don’t be put off by the fact that vi doesn’t have a pretty GUI interface to go with it, this is an incredibly powerful text editor that would be well worth your time learning… once you’re done with it you’ll never want to use anything else!
Realising that most people would find vi hard to use straight off, there is a useful little walk-through tutorial that you can access by typingvimtutor at a command line. The tutorial opens vi with the tutorial in it, and you try out each of the commands and shortcuts in vi itself. It’s very easy and makes navigating around vi a snap. Check it out.

linux-introduction-avd-cmd-line-2

•GREP

Another very useful Linux command is the grep command. This little baby searches for a string in any file. The grep command is frequently used in combination with other commands in order to search for a specific string. For example, if we wanted to check our web server’s log file for a specific URL query or IP address, the ‘grep’ command would do this job just fine.

If, on the other hand, you want to find every occurence of ‘hello world’ in every .txt file you have, you would type grep “hello world” *.txt

You’ll see some very common command structures later on that utilise ‘grep’. At the same time, you can go ahead and check grep’s man page by typing man grep , it has a whole lot of very powerful options.

linux-introduction-avd-cmd-line-3

•

PS – PROCESS ID (PID) DISPLAY

The ps command will show all the tasks you are currently running on the system, it’s the equivalent of Windows Task Manager and you’ll be happy to know that there are also GUI versions of ‘ps’.

If you’re logged in as root in your Linux system and type ps -aux , you’ll see all processes running on the system by every user, however, for security purposes, users will only be able to see processes owned by them when typing the same command.

linux-introduction-avd-cmd-line-4

Again, man ps will provide you with a bundle of options available by the command.

•KILL

The ‘kill’ command is complementary to the ‘ps’ command as it will allow you to terminate a process revealed with the previous command. In cases where a process is not responding, you would use the following syntax to effectively kill it: kill -9 pid where ‘pid’ is the Process ID (PID) that ‘ps’ displays for each task.

linux-introduction-avd-cmd-line-5

In the above example, we ran a utility called ‘bandwidth’ twice which is shown as two different process IDs (7171 & 13344) using the pscommand. We then attempted to kill one of them using the command kill -9 7171 . The next time we ran the ‘ps’, the system reported that a process that was started with the ‘./bandwidth’ command had been previously killed.

Another useful flag we can use with the ‘kill’ command is the -HUP. This neat flag won’t kill the process but pause it and at the same time force it to reload its configuration. So, if you’ve got a service running and need to restart it because of changes made in its configuration file, then the -HUP flag will do just fine. Many people look at it as an alternative ‘reload’ command.

The complete syntax to make use of the flag is: kill -HUP pid where ‘pid’ is the process ID number you can obtain using the ‘ps’ command, just as we saw in the previous examples.

 

CHAINING COMMANDS, REDIRECTING OUTPUT, PIPING

In Linux, you can chain groups of commands together with incredible ease, this is where the true power of the Linux command line exists, you use small tools, each of which does one little task and passes the output on to the next one.

For example, when you run the ps aux command, you might see a whole lot of output that you cannot read in one screen, so you can use the pipe symbol ( | ) to send the output of ‘ps’ to ‘grep’ which will search for a string in that output. This is known as ‘piping’ as it’s similar to plumbing where you use a pipe to connect two things together.

linux-introduction-avd-cmd-line-6

Say you want to find the task ‘antispam’ : you can run ps aux | grep antispam . Ps ‘pipes’ its output to grep and it then searches for the string, showing you only the line with that text.

If you wanted ps to display one page at a time you can pipe the output of ps to either more or less . The advantage of less is that it allows you to scroll upwards as well. Try this: ps aux | less . Now you can use the cursors to scroll through the output, or use pageup, pagedown.

•ALIAS

The ‘alias’ command is very neat, it lets you make a shortcut keyword for another longer command. Say you don’t always want to type ps aux | less, you can create an alias for it.. we’ll call our alias command ‘pl’. So you type  alias pl=’ps aux | less’ .

Now whenever you type pl , it will actually run ps aux | less  – Neat, is’nt it?

linux-introduction-avd-cmd-line-7

You can view the aliases that are currently set by typing alias:

linux-introduction-avd-cmd-line-8

As you can see, there are quite a few aliases already listed for the ‘root’ account we are using. You’ll be suprised to know that most Linux distributions automatically create a number of aliases by default – these are there to make your life as easy as possible and can be deleted anytime you wish.

OUTPUT REDIRECTION

It’s not uncommon to want to redirect the output of a command to a text file for further processing. In the good old DOS operating system, this was achieved by using the ‘>‘ operator. Even today, with the latest Windows operating systems, you would open a DOS command prompt and use the same method!

The good news is that Linux also supports these functions without much difference in the command line.

For example, if we wanted to store the listing of a directory into a file, we would type the following: ls > dirlist.txt:

linux-introduction-avd-cmd-line-9

As you can see, we’ve taken the output of ‘ls’ and redirected it to our file. Let’s now take a look and see what has actually been stored in there by using the command cat dirlist.txt :

linux-introduction-avd-cmd-line-10

As expected, the dirlist.txt file contains the output of our previous command. So you might ask yourself ‘what if I need to append the results?’ – No problem here, as we’ve already got you covered.

When there’s a need for appending files or results, as in DOS we simply use the double >> operator. By using the command it will append the new output to the file we have specified in the command line:

linux-introduction-avd-cmd-line-11

The above example clearly shows the content of our file named ‘document2’ which is then appended to the previously created file ‘dirlist.txt’. With the use of the ‘cat’ command, we are able to examine its contents and make sure the new data has been appended.

Note:

By default, the single > will overwrite the file if it exists, so if you give the ls > dirlist.txt command again, it will overwrite the first dirlist.txt. However, if you specify >> it will add the new output below the previous output in the file. This is known as output redirection.

In Windows and DOS you can only run one command at a time, however, in Linux you can run many commands simultaneously. For example, let’s say we want to see the directory list, then delete all files ending with .txt, then see the directory list again.

This is possible in Linux using one statement as follows : ls -l; rm -f *.txt; ls -l . Basically you separate each command using a semicolon, ‘;‘. Linux then runs all three commands one after the other. This is also known as command chaining.

BACKGROUND PROCESSES

If you affix an ampersand ‘&’ to the end of any command, it will run in the background and not disturb you, there is no equivalent for this in Windows and it is very useful because it lets you start a command in the background and run other tasks while waiting for that to complete.

The only thing you have to keep in mind is that you will not see the output from the command on your screen since it is in the background, but we can redirect the output to a file the way we did two paragraphs above.

For example, if you want to search through all the files in a directory for the word ‘Bombadil’, but you want this task to run in the background and not interrupt you, you can type this: grep “Bombadil” *.* >> results.txt& . Notice that we’ve added the ampersand ‘&’ character to the end of the command, so it will now run in the background and place the results in the file results.txt . When you press enter, you’ll see something like this :

$ grep “Bombadil” *.* >> results.txt&

[1] 1272

linux-introduction-avd-cmd-line-12

Our screen shot confirms this. We created a few new files that contained the string ‘Bombadil’ and then gave the command grep “Bombadil” *.* >> results.txt& . The system accepted our command and placed the process in the background using PID (Process ID) 14976. When we next gave the ‘ls’ command to see the listing of our directory we saw our new file ‘results.txt’ which, as expected, contained the files and lines where our string was found.

If you run a ‘ps‘ while this is executing a very complex command that takes some time to complete, you’ll see the command in the list. Remember that you can use all the modifiers in this section with any combination of Linux commands, that’s what makes it so powerful. You can take lots of simple commands and chain, pipe, redirect them in such a way that they do something complicated!


LINUX FILE & FOLDER PERMISSIONS


File & folder security is a big part of any operating system and Linux is no exception!

These permissions allow you to choose exactly who can access your files and folders, providing an overall enhanced security system. This is one of the major weaknesses in the older Windows operating systems where, by default, all users can see each other’s files (Windows 95, 98, Me).

For the more superior versions of the Windows operating system such as NT, 2000, XP and 2003 things look a lot safer as they fully support file & folder permissions, just as Linux has since the beginning.

Together, we’ll now examine a directory listing from our Linux lab server, to help us understand the information provided. While a simple ‘ls’ will give you the file and directory listing within a given directory, adding the flag ‘-l’ will reveal a number of new fields that we are about to take a look at:

linux-introduction-file-permissions-1

It’s possible that most Linux users have seen similar information regarding their files and folders and therefore should feel pretty comfortable with it. If on the other hand you happen to fall in to the group of people who haven’t seen such information before, then you either work too much in the GUI interface of Linux, or simply haven’t had much experience with the operating system :)

Whatever the case, don’t disappear – it’s easier than you think!!

SO WHAT DOES ALL THIS OUTPUT MEAN ? ESPECIALLY ALL THOSE ‘RWX’ LINES?!

Let’s start from scratch, analysing the information in the previous screenshot.

linux-introduction-file-permissions-2

In the yellow column on the right we have the file & directory names (dirlist.txt, document1, document2 etc.) – nothing new here. Next, in the green column, we will find the time and date of creation.

Note that the date and time column will not always display in the format shown. If the file or directory it refers to was created in a year different from the current one, it will then show only the date, month and year, discarding the time of creation.

For example, if the file ‘dirlist.txt’ was created on the 27th of July, 2004, then the system would show:

Jun 27 2004 dirlist.txt

instead of

Jun 27 11:28 dirlist.txt

A small but important note when examining files and folders! Lastly, the date will change when modifying the file. As such, if we edited a file created last year, then the next time we typed ‘ls -l’, the file’s date information would change to today’s date. This is a way you can check to see if files have been modified or tampered with.

The next column (purple) contains the file size in bytes – again nothing special here.

linux-introduction-file-permissions-3

Next column (orange) shows the permissions. Every file in Linux is ‘owned’ by a particular user.. normally this is the user (owner) who created the file.. but you can always give ownership to someone else.

The owner might belong to a particular group, in which case this file is also associated with the user’s group. In our example, the left column labeled ‘User’ refers to the actual user that owns the file, while the right column labeled ‘group’ refers to the group the file belongs to.

Looking at the file named ‘dirlist.txt’, we can now understand that it belongs to the user named ‘root’ and group named ‘sys’.

Following the permissions is the column with the cyan border in the listing.

The system identifies files by their inode number, which is the unique file system identifier for the file. A directory is actually a listing of inode numbers with their corresponding filenames. Each filename in a directory is a link to a particular inode.

Links let you give a single file more than one name. Therefore, the numbers indicated in the cyan column specifies the number of links to the file.

As it turns out, a directory is actually just a file containing information about link-to-inode associations.

Next up is a very important column, that’s the first one on the left containing the ‘-rwx—-w-‘ characters. These are the actual permissions set for the particular file or directory we are examining.

To make things easier, we’ve split the permissions section into a further 4 columns as shown above. The first column indicates whether we are talking about a directory (d), file (-) or link (l).

In the newer Linux distributions, the system will usually present the directory name in colour, helping it to stand out from the rest of the files. In the case of a file, a dash (-) or the letter ‘f’ is used, while links make the use of the letter ‘l’ (l). For those unfamiliar with links, consider them something similar to the Windows shortcuts.

linux-introduction-file-permissions-4

Column 2 refers to the user rights. This is the owner of the file, directory or link and these three characters determine what the owner can do with it.

The 3 characters on column 2 are the permissions for the owner (user rights) of the file or directory. The next 3 are permissions for thegroup that the file is owned by and the final 3 characters define the access permissions for the others group, that is, everyone else not part of the group.

So, there are 3 possible attributes that make up file access permissions:

r – Read permission. Whether the file may be read. In the case of a directory, this would mean the ability to list the contents of the directory.

w – Write permission. Whether the file may be written to or modified. For a directory, this defines whether you can make any changes to the contents of the directory. If write permission is not set then you will not be able to delete, rename or create a file.

x – Execute permission. Whether the file may be executed. In the case of a directory, this attribute decides whether you have permission to enter, run a search through that directory or execute some program from that directory.

Let’s take a look at another example:

linux-introduction-file-permissions-5

Take the permissions of ‘red-bulb’, which are drwxr-x—. The owner of this directory is user david and the group owner of the directory is sys. The first 3 permission attributes are rwx. These permissions allow full read, write and execute access to the directory to user david. So we conclude that david has full access here.

The group permissions are r-x. Notice there is no write permission given here so while members of the group sys can look at the directory and list its contents, they cannot create new files or sub-directories. They also cannot delete any files or make changes to the directory content in any way.

Lastly, no one else has any access because the access attributes for others are .

If we assume the permissions are drw-r–r– you see that the owner of the directory (david) can list and make changes to its contents (Read and Write access) but, because there is no execute (x) permission, the user is unable to enter it! You must have read and execute(r-x) in order to enter a directory and list its contents. Members of the group sys have a similar problem, where they seem to be able toread (list) the directory’s contents but can’t enter it because there is no execute (x) permission given!

Lastly, everyone else can also read (list) the directory but is unable to enter it because of the absence of the execute (x) permission.

Here are some more examples focusing on the permissions:

-r–r–r– :This means that owner, group and everyone else has only read permissions to the file (remember, if there’s no ‘d‘ or ‘l‘, then we are talking about a file).

-rw-rw-rw- : This means that the owner, group and everyone else has read and write permissions.

-rwxrwxrwx : Here, the owner, group and everyone else has full permissions, so they can all read, write and execute the file (-).

MODIFYING OWNERSHIP & PERMISSIONS

So how do you change permissions or change the owner of a file?

Changing the owner or group owner of a file is very simple, you just type ‘chown user:group filename.ext‘, where ‘user’ and ‘group’ are those to whom you want to give ownership of the file. The ‘group’ parameter is optional, so if you type ‘chown david file.txt‘, you will give ownership of file.txt to the user named david.

In the case of a directory, nothing much changes as the same command is used. However, because directories usually contain files that also need to be assigned to the new user or group, we use the ‘-R‘ flag, which stands for ‘recursive’ – in other words all subdirectories and their files: ‘chown -R user:group dirname‘.

To change permissions you use the ‘chmod’ command. The possible options here are ‘u‘ for the user, ‘g‘ for the group, ‘o‘ for other, and ‘a‘ for all three. If you don’t specify one of these letters it will change to all by default. After this you specify the permissions to add or remove using ‘+‘ or ‘‘ . Let’s take a look at an example to make it easier to understand:

If we wanted to add read, write and execute to the user of a particular file, we would type the following ‘chmod u+rwx file.txt‘. If on the other hand you typed ‘chmod g-rw file.txt‘ you will take away read and write permissions of that file for the group .

While it’s not terribly difficult to modify the permissions of a file or directory, remembering all the flags can be hard. Thankfully there’s another way, which is less complicated and much faster. By replacing the permissions with numbers, we are able to calculate the required permissions and simply enter the correct sum of various numbers instead of the actual rights.

The way this works is simple. We are aware of three different permissions, Read (r), Write (w) and Execute (x). Each of these permissions is assigned a number as follows:

r (read) – 4

w (write) – 2

x (execute) – 1

Now, to correctly assign a permission, all you need to do is add up the level you want, so if you want someone to have read and write, you get 4+2=6, if you want someone to have just execute, it’s just 1.. zero means no permissions. You work out the number for each of the three sections (owner, group and everyone else).

If you want to give read write and execute to the owner and nothing to everyone else, you’d get the number 700. Starting from the left, the first digit (7) presents the permissions for the owner of the file, the second digit (0) is the permissions for the group, and the last (0) is the permissions for everyone else. You get the 7 by adding read, write and execute permissions according to the numbers assigned to each right as shown in the previous paragraphs: 4+2+1 = 7.

r, w, x Permissions
Calculated Number

0
–x
1
-w-
2
-wx
3 (2+1)
r–
4
r-x
5 (4+1)
rw-
6 (4+2)
rwx
7 (4+2+1)

If you want to give full access to the owner, only read and execute to the group, and only execute to everyone else, you’d work it out like this :

owner: rwx = 4 + 2 + 1 = 7

group: r-x = 4 + 0 + 1 = 5

everyone: –x = 0 + 0 + 1 = 1

So your number will be 751, 7 for owner, 5 for group, and 1 for everyone. The command will be ‘chmod 751 file.txt‘. It’s simple isn’t it ?

If you want to give full control to everyone using all possible combinations, you’d give them all ‘rwx’ which equals to the number ‘7’, so the final three digit number would be ‘777’:

linux-introduction-file-permissions-6

If on the other hand you decide not to give anyone any permission, you would use ‘000’ (now nobody can access the file, not even you!). However, you can always change the permissions to give yourself read access, by entering ‘chmod 400 file.txt’.

For more details on the ‘chmod’ command, please take a look at the man pages.

As we will see soon, the correct combination of user and group permissions will allow us to perform our work while keeping our data safe from the rest of the world.

For example in order for a user or group to enter a directory, they must have at least read (r) and execute (x) permissions on the directory, otherwise access to it is denied:

linux-introduction-file-permissions-7

As seen here, user ‘mailman‘ is trying to access the ‘red-bulb‘ directory which belongs to user ‘david‘ and group ‘sys‘. Mailman is not a member of the ‘sys‘ group and therefore can’t access it. At the same time the folder’s permissions allow neither the group nor everyone to access it.

Now, what we did is alter the permission so ‘everyone‘ has at least read and execute permissions so they are able to enter the folder – let’s check it out:

linux-introduction-file-permissions-8

Here we see the ‘mailman‘ user successfully entering the ‘red-bulb‘ directory because everyone has read (r) and execute (x) access to it!

The world of Linux permissions is pretty user friendly as long as you see from the right perspective :) Practice and reviewing the theory will certainly help you remember the most important information so you can perform your work without much trouble.

If you happen to forget something, you can always re-visit us – any time of the day!

Continuing on to our last page, we will provide you with a few links to some of the world’s greatest Linux resources, covering Windows to Linux migration, various troubleshooting techniques, forums and much more that will surely be of help.


FINDING MORE INFORMATION


Since this document merely scratches the surface when it comes to Linux, you will probably find you have lots of questions and possibly problems. Whether these are problems with the operating system, or not knowing the proper way to perform the task in Linux, there is always a place to find help.

On our forums you’ll find a lot of experienced people always willing to go that extra mile to help you out, so don’t hesitate to ask – you’ll be suprised at the responses!

Generally the Linux community is a very helpful one. You’ll be happy to know that there is more documentation, tutorials, HOW-TOs and FAQs (Frequently Asked Questions) for Linux than for all other operating systems in the world!

If you go to any search engine, forum or news group researching a problem, you’ll always find an answer.

To save you some searching, here are a few websites where you can find information covering most aspects of the operating system:

  • http://www.tldp.org/ – The Linux Documentation Project homepage has the largest collection of tutorials, HOW-TOs and FAQs for Linux.
  • http://www.Linux.org/docs/ – The documentation page from the official Linux.org website. Contains links to a lot of useful information.
  • http://fedora.redhat.com/docs/ – The Red Hat Fedora Linux manuals page. Almost all of this information will apply to any other version of Linux as well. All the guides here are full of very useful information. You can download all the guides to view offline.
  • http://www.justLinux.com/nhf/ – Contains a library of information for beginners on all topics from setting up hardware, installing software, to compiling the kernel
  • http://www.pbone.net – Pbone is a great search engine to find RPM packages for your Linux operating system.
  • http://www.freshmeat.net – Looking for an application in Linux? Try Freshmeat – if you don’t find it there, it’s most probably not out yet!
  • http://www.sourceforge.net – The world’s largest development and download repository of Open Source code (free) and applications. Sourceforge hosts thousands of open source projects, most of which are of course for the Linux operating system.

We hope you have enjoyed this brief introduction to the Linux operating system and hope you’ll be tempted to try Linux for yourself. You’ve surely got nothing to lose and everything to gain!

Remember, Linux is the No.1 operating system when it comes to web services and mission critical servers – it’s not a coincidence other major software vendors are doing everything they can to stop Linux from gaining more ground!