RC Linux Tutorial

RC Linux Tutorial

This tutorial has been put together by Research Computing to help familiarize new users with the Linux operating system. At the end of each chapter there will be a short quiz. It is recommended that you take some time to do the quizzes to aid in remembering the main points of the chapter.

About this tutorial

What is Linux?

Linux is a Unix-like Operating System originally created by Linus Torvalds while he was a student at the University of Helsinki. His main goal was to create a Minix clone and make the operating system, with the entire source code, freely available.

Here is the original quote that Linus made in comp.os.minix about his new operating system.

Do you pine for the nice days of Minix-1.1, when men were men and wrote their own device drivers? Are you without a nice project and just dying to cut your teeth on an OS you can try to modify for your needs? Are you finding it frustrating when everything works on Minix? No more all-nighters to get a nifty program working? Then this post might be just for you.

As I mentioned a month ago, I'm working on a free version of a Minix-look-alike for AT-386 computers. It has finally reached the stage where it's even usable (though may not be, depending on what you want), and I am willing to put out the sources for wider distribution. It is just version 0.02...but I've successfully run bash, gcc, gnu-make, gnu-sed, compress, etc. under it.

Soon afterwards, Linus received lots email from people that where interested in his project. From there Linux continued to become what it is today, a full-scale multitasking, multiuser operating system.

Who uses Linux?

Linux users come in a variety of different people, although most of its users are fairly knowledgeable about computers and how they work.

Linux is user-friendly. It just chooses its friends wisely.

Many people are instantly turned off by the fact that Linux is a text-based operating system. It doesn't have all the graphical interfaces that one would expect to see on a MacIntosh® or Windows® operating system. Most of the home-users turn away from Linux when they hear about this. What they don't know is that many companies have put together distributions of Linux that have many graphical equivalents of the text-based utilities, making Linux a real option for the new user.

Linux has become popular for many different uses. Among these uses include web, ftp, dns, and other Internet related servers, software development, database uses, and is also popular with the scientific research community. The reliability of Linux makes it strong in most of these areas. Very few operating systems can stay running stable for months at a time without requiring a reboot of the machine. The free compilers for just about every programming language make Linux an excellent platform for software development, instead of having to pay thousands of dollars for site licenses, Linux users get everything for free. The speed of the operating system makes Linux a good choice for database and scientific related software. For instance, a reputable source claims that Maple® runs three times faster under Linux than it does in Windows®.

Multiple Method Disclaimer

This tutorial is designed to give you a basic idea of how to use Linux so you may program on the CIRCE/SC clusters at USF. It is not solely intended to make you a Linux expert, but should make you comfortable enough to use the system. Since this tutorial will be specific to the basic setup of a user account on the cluster, the examples and programs that are taught will not cover all alternative software. Please note that because we show you one way of doing something doesn't mean that it is the only way. This tutorial will use the bash shell for its examples, there are many other shells that you may choose from, such as: tcsh, korn shell, sh, csh. For the basic commands chapter, the commands will be the same on all Linux distributions. Please note that we will be using RedHat as the distribution for the cluster, there are very little differences between RedHat and other distributions. The main difference being the package system that they use, but you won't really need to use that on the cluster. If you have ever used a distribution other than RedHat you should be able to use our distribution just as easily as the other one.

Getting Started

Since most people have never even seen Linux, it might take a while to get used to. If you have used MS-DOS you should be able to recognize some similarities. Those who have experience using other types of UNIX's should adapt right away as Linux is almost the same in every way.

For those people who have only used windows be prepared to relearn most ways of doing things. You may not be used to using a command line, which is used for most of the basic programs such as listing the files in a directory, and moving and copying files. If you don't like the text based environment you will have to get used to it, although there are many graphical utilities that can do just about anything you want.

Logging In

Linux is a multi-user operating system. This means that many people can log into the system and use it simultaneously. Each user is given certain permissions on what files and programs they can or can't run, they may also have different shells, environment variables, etc.

When you connect to, or boot-up, your system you will be prompted to enter a user name and password:

RedHat Enterprise Linux 6.8
Kernel 2.6.32-696.18.7 on i686

host login: root 

Next you will be prompted for the password for that user:

password: 

After logging in you will be given a command prompt. This is where you will be entering commands to run programs.

[root@host ~]#

Users

In the above login example we logged into the system as the user root. The root user is the super-user of the system. Root generally is used to do administrative tasks on the system, install, uninstall, and configure programs for the users. The root user can modify any of the files on the system, which is why you should know what you are doing when using the root account. If you have your own computer with Linux installed on it, you will have access to the root account. The password should have been set during the installation of your distribution. The root account should rarely be used as it can cause serious damage to the system if you are not careful.

You may be asking then, "How do I add a standard user to work with?". This can be done by using the adduser program, while you are logged into the system as root. Simply type adduser into the command-line. He is an example, we will add the user bob to the system.

[root@host /home]# adduser bob 

Depending on what distribution of Linux you are using the command passwd will be run for the user that you just added. The passwd program changes the password for a user account. If after running the adduser command, you aren't asked to change the password for the user you will want to type in the command 'passwd <user name>' to set the user's password. Otherwise the user can login to the machine with no password, and this is a BAD thing. By default, usually this command will add the user to the system's password file and the adduser command will create them a user home directory under /home/username. So if we look at /home there should now be a directory called bob. Log out as root by typing logout and then login as the user bob.

Setting up the Environment

Each user that is on the system has different settings for their account. These settings are typically what type of shell program they are given upon logging into the system to user-specific settings for globally install programs on the computer. Most of the time the settings are stored in files that begin with a '.' or they are set in the shell program as variables.

Variables

Lets take a second to look at how to use environment variables to specify paths, aliases, and other useful information that we might need.

Variables are used by the shell to temporarily hold information, this way the user can save time by not having to specify something every time a program needs to be found or what the command line should look like. To see a list of all the variables that are set type set in your command prompt, this should list all the variables and their values in the form of "variable=value". If you ever want to check the value of just one variable you can type echo $variablename, where $variablename is the name of variable with the $ in front of it. Typing echo $SHELL will output /bin/bash (or whatever your shell program is), the value for the variable SHELL.

We can create and modify variables to give us more flexibility when working with the shell. To create a usable variable we have to declare the name of the variable and then assign a value to it. I will be showing how to do this under the bash shell, other shells follow a similar way of declaring variables and usually you can find out how by typing man yourshell.

To change the value of a variable you would type the name of the variable and then assign it a value using the = sign. Here is an example where we tell the shell a path.

[bob@host ~]$ export PATH="/bin:/usr/local/bin:/opt/gnome/bin
[bob@host ~]$ echo $PATH
/bin:/usr/local/bin:/opt/gnome/bin
[bob@host ~]$ 

As you can see, we now have a variable called path that stores the directories where the shell should look to find files. This way instead of having to type /opt/gnome/bin/gcalc we can now just simply type gcalc from anywhere and the program will run, this saves us a lot of typing in the future if we are using a certain program frequently.

What if we already had a previous value for PATH and we would like to save that value and add our new directories along with it. We can just simply use the variable name with a $ in front of it to use the previous value. So if our path was originally '/usr/bin:/usr/sbin' it is now '/usr/bin:/usr/sbin:/bin:/usr/local/bin:/opt/gnome/bin'. The new line would be:

export PATH="$PATH:/bin:/usr/local/bin:/opt/gnome/bin" 

There are other things that you can do with the command prompt, you can find more information by typing 'man bash'.

Configuration Files

Lets begin with an example on configuration files and what they are. These are the files in the user's home directory that begin with a '.'. Most of the time when the user logs into their account a file is read be the shell program to set up user settings such as paths, what the command prompt looks like, and other settings that change from user to user.

For an example we will take a look at the configuration file for the bash shell. This file is called .bashrc under the users home directory. If you are using another shell other than bash the file will be different but usually will follow the same naming scheme, i.e. tcsg uses .tcshrc as a configuration file. So, once inside your home directory use the command ls -al to display all the contents of the current directory.

[bob@host ~]$ ls -al
total 8
drwxr-xr-x 2 bob other 512 Apr 22 16:03 .
drwxr-xr-x 9 root root 512 Apr 22 15:54 ..
-rw------- 1 bob other 397 Apr 22 16:09 .ssh
-rw-r--r-- 1 bob other 176 Apr 22 16:09 .bash_profile
-rw-r--r-- 1 bob other 124 Apr 22 16:09 .bashrc

[bob@host ~]$

The .bashrc and .bash_profile files hold settings that should be applied whenever the shell is started. Typical settings in here at set commands for specifying paths and the prompt, and other programs that should be run at login. To see what is in this file you can type cat .bashrc and cat .bash_profile. Here is an example:

[bob@host ~]$ cat .bashrc
# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
      . /etc/bashrc
fi

# User specific aliases and functions

[bob@host ~]$ cat .bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH

As you can see mostly everything in the configuration file is from the previous section. With this configuration file it is possible to save those settings that we prefer and use them whenever we log into the user account. With out a .tcshrc file the shell uses the default settings. You can edit these files to your desired settings with any text editor. If you are new to Unix and have no clue about editing text try a program called pico, this is a fairly straight-forward editor; if have ever use the email program called pine you should be somewhat familiar to it. Editing text will be given in more detail in a future section.

Most configuration files will be commented, providing an explanation about how to use them.

Basic commands

In this chapter we will talk about some of the basic commands to help a new user quickly get a grasp on using a Linux system. We will discuss how to view, create and modify directories and files. You will also learn how to move around the system and change file permissions.

Basic commands for navigating the filesystem

The 'ls' command

This is perhaps one of the most basic commands that there is, you will use it all the time and it is nice to know a few different ways on how to use it. What the command does is outputs to the screen a listing of all the files in your current directory. Here is an example of the ls command in action:

[bob@host ~] ls
dir1 dir2 file1 file2 file3 file4 file5
[bob@host ~]

As you can see this gives us a listing of all the files and directories that are in the current directory. This isn't all the information that can be given by the ls command though, lets say that you wanted to know more information about a file... such as the date it was created/modified, who owns the file, what permissions are set on the file, and maybe you would like to know the size of the file. To get this information we can simply pass something called an argument to the ls command. The argument that we give the command is -l, which lists in long format. So when we piece the command together with the argument we get ls -l. Let's try it out:

[bob@host ~] ls -l
total 8
drwxr-xr-x 2 bob other 4096 Apr 25 11:05 dir1
drwxr-xr-x 2 bob other 4096 Apr 25 11:05 dir2
-rw-r--r-- 1 bob other 0 Apr 25 11:11 file1
-rw-r--r-- 1 bob other 0 Apr 25 11:05 file2
-rw-r--r-- 1 bob other 0 Apr 25 11:05 file3
-rw-r--r-- 1 bob other 0 Apr 25 11:05 file4
-rw-r--r-- 1 bob other 0 Apr 25 11:05 file5
[bob@host ~]

Let's take a minute to explain what we are seeing here. The first line as you can see, tells us the size of all the files in the directory in kilobytes (1 kilobyte = 1000 bytes). The second line that we get is a directory called dir1, we know this is a directory because if we look at the first bit of information we are given, the permissions on the file, it has a 'd' for the first letter in the set. The second piece of information we are given is the number of links to the file (explained later in this chapter). Next is the user and group that the file is owned by. Then comes the size of the file, this is given in bytes. And finally the date and file of the last modification of the file.

Another argument that is nice to know with the ls command is -a. This shows all hidden files in a directory. These hidden files are the ones that begin with a period and are usually configuration files that are used mostly by installed programs. Here is what we get when we use the command:

[bob@host ~] ls -la
total 12
drwxr-xr-x 5 bob other 512 Apr 25 16:29 .
drwxr-xr-x 9 root root 512 Apr 22 15:54 ..
drwx------ 2 bob other 512 Apr 24 14:46 .ssh
-rw-r--r-- 1 bob other 176 Apr 24 16:12 .bash_profile
-rw-r--r-- 1 bob other 124 Apr 24 16:12 .bashrc
drwxr-xr-x 2 bob other 512 Apr 25 16:29 dir1
drwxr-xr-x 2 bob other 512 Apr 25 16:29 dir2
-rw-r--r-- 1 bob other 0   Apr 25 16:29 file1
-rw-r--r-- 1 bob other 0   Apr 25 16:29 file2
-rw-r--r-- 1 bob other 0   Apr 25 16:29 file3
-rw-r--r-- 1 bob other 0   Apr 25 16:29 file4
-rw-r--r-- 1 bob other 0   Apr 25 16:29 file5

[bob@host ~]

Now we see three more files called .ssh, .bash_profile, and .bashrc. These are used for configuration of programs. We also see . and .., these are both directories. The . stands for the current directory, and the .. stands for the previous directory.

There are other arguments that can be used with the ls command, you can type 'man ls' for more information.

The 'cd' command

This command is used to change to a different directory. From the previous example we have two directories that we can move into called dir1 and dir2. If we wanted to change into dir1 we would type:

[bob@host ~] cd dir1
[bob@host ~/dir1] 

See how we are now in the dir1 directory? Let's now use the command 'ls -la' to see what files are in this directory.

[bob@host ~/dir1] ls -la
total 6
drwxr-xr-x 2 bob other 512 Apr 25 16:52 .
drwxr-xr-x 5 bob other 512 Apr 25 16:29 ..
-rw-r--r-- 1 bob other 13 Apr 25 16:52 linux

[bob@host ~/dir1]

You may be wondering how you get back to the previous directory. This is when the .. directory comes into play.

[bob@host ~/dir1] cd ..
[bob@host ~] 

As you see, we told cd to change to .., which stands for the previous directory, and it places us back in the home directory for bob. (Note: A user's home directory is often denoted by the tilde, ~). Had we told cd to change to . we would remain in the current directory.

The 'pwd' command

Sometimes it is needed to know the full path of the directory that you are in and your command prompt isn't set up to give you that information. You can use the command pwd to help you with this problem. Here is an example:


[bob@host ~/dir1] ls
total 6
drwxr-xr-x 2 bob other 512 Apr 25 16:52 .
drwxr-xr-x 5 bob other 512 Apr 25 16:29 ..
-rw-r--r-- 1 bob other 13 Apr 25 16:52 linux

[bob@host ~/dir1] pwd
/home/bob/dir1

[bob@zedd ~/dir1]

Here the command prints to the screen for us the full path of the current working directory.

Basic commands for managing files

Linux has some basic commands that you should learn to manage your files. You can copy, delete, move/delete files and also create directories. We will show you how to use each of these and will also talk about what links are and how to use them.

The 'cp' command

This command is used to copy files. If you have ever used the copy command in MS-DOS you will find this somewhat familiar. Let's explain the basic uses for the cp command.

To copy a file to another location you would use cp in it's most basic form: cp source target. For example,


[bob@host ~] ls
total 10
drwxr-xr-x 4 bob other 512 Apr 26 14:57 .
drwxr-xr-x 9 root root 512 Apr 22 15:54 ..
drwx------ 2 bob other 512 Apr 24 14:46 .ssh
-rw-r--r-- 1 bob other 176 Apr 22 16:09 .bash_profile
-rw-r--r-- 1 bob other 124 Apr 22 16:09 .bashrc
drwxr-xr-x 2 bob other 512 Apr 25 16:52 dir1
-rw-r--r-- 1 bob other 5 Apr 26 14:57 file1
    
[bob@host ~] cp file1 file2
[bob@host ~] ls
total 10
drwxr-xr-x 4 bob other 512 Apr 26 14:57 .
drwxr-xr-x 9 root root 512 Apr 22 15:54 ..
drwx------ 2 bob other 512 Apr 24 14:46 .ssh
-rw-r--r-- 1 bob other 176 Apr 22 16:09 .bash_profile
-rw-r--r-- 1 bob other 124 Apr 22 16:09 .bashrc
drwxr-xr-x 2 bob other 512 Apr 25 16:52 dir1
-rw-r--r-- 1 bob other 5 Apr 26 14:57 file1
-rw-r--r-- 1 bob other 5 Apr 26 14:57 file2
    
[bob@host ~]

Like the ls command you can have several switches that change the way the program operates. Let's take a look at a few common ones...

cp -i: -i tells cp to use interactive mode. Which will prompt you if it needs to overwrite a file.

cp -r source target: -r will copy a directory and all its contents.

cp -R source target: -r will copy a directory and all its contents, but pipes are only replicated not read from.

cp -p: -p will preserve the owner group id, permissions, etc. The user copying the files must be of equal or higher access to do this.

There are other arguments that can be used with the cp command, you can type 'man cp' for more information.

The 'rm' command

The 'mv' command

The 'mkdir' command

====The 'ln' command ===Basic commands for managing file permissions

The 'chown' command

The 'chmod' command