Results 1 to 5 of 5

Thread: Linux LPI 101 - Part I

  1. #1
    Banned
    Join Date
    Aug 2001
    Location
    Yes
    Posts
    4,424

    Linux LPI 101 - Part I

    I'm assuming you have a fully functional Linux-system (with x installed). I'm playing around with RH8 right now, but most of this tutorial should be the same for the major Linux-distributions.

    This tutorial is based on the structure of the LPI-project.

    Preparation.

    Assumption: you are logged in to X (the Graphical User Interface) either as root or as a normal user.
    Now press Ctrl-Alt-F1. You'll be taken to a 'virtual console' (tty1), saying something like this:
    Code:
    Red Hat Linux release 8.0 (Psyche)
    Kernel 2.4.18-19.8.0 on an i686
    
    localhost login:
    Type 'root', and enter your root-password.
    You'll see something like this:
    Code:
    Last login: Saturday Jan 26 14:49:51 on tty1
    You have mail.
    [root@localhost root]#
    Warning: 'root' is a power-user/administrator/God on a Linux-system. If you log in as root, the system assumes you know what you're doing. If you don't, there's a big chance you'll screw up your system...

    Our first task will be to add a new user called 'test'.
    To do this, type:
    Code:
    [root@localhost root]# useradd -m test
    Now we have a user called 'test', but he doesn't have a password yet. To assign a password, type:

    Code:
    [root@localhost root]#passwd test
    New password:
    To keep it simple, assign the password 'test' to the 'test'.
    The system will tell you the password is too simple: you can ignore that by typing 'test' again and hitting enter.

    Attention: this isn't exactly a good password policy. Linux warns you about it, and for the more serious work, you should take Linux' advice.

    Now let's test this new user:
    Hit ctrl-alt-F2 to open a second virtual console (tty2).
    Enter 'test' both as user and password.
    You can type whatever command there without having to be affraid to harm your system. From now on, we'll be working with this user.

    Summary: you should now have two virtual consoles: tty1 with root logged in, and tty2 with test logged in.

    You can go back to X by pressing Ctrl-Alt-F7.

    1. Part I. LPI 101.

    1.1. Effective use of the Unix command line.

    To leave a shell, type exit

    Directories.

    Once you logged in to Linux, you'll find yourself in your home-directory.
    To see what directory you're in, use pwd:
    Code:
    [test@localhost test]$ pwd
    /home/test
    Notice that the rootdirectory is known as /
    Notice that Linux is case-sensitive: PWD will give you an error.

    Moving to another directory

    To go to another directory, use cd
    Code:
    [test@localhost test]$ cd /
    [test@localhost test]$ pwd
    /
    [test@localhost test]$
    cd without options takes you back to your home-directory.

    The actual directory

    The actual directory in Linux is symbolized by .
    Code:
    [test@localhost test]$pwd
    /home/test
    [test@localhost test]$cd .
    [test@localhost test]$pwd
    /home/test
    [test@localhost test]$
    So cd . doesn't have any effect. The dot can be handy though. More about that later.

    The parent-directory

    Apart from . for the actual directory, Linux also knows .., symbolizing the parent-directory.

    Code:
    [test@localhost test]$pwd
    /home/test
    [test@localhost test]$cd ..
    [test@localhost test]$pwd
    /home
    [test@localhost test]$
    Help!

    Code:
    [test@localhost test]$man pwd
    man pwd (man is short for 'manual') will give you a summary of the pwd-command. Type q to exit the man-page(s).

    File overview

    ls will give you an overview of present files.
    Code:
    [test@localhost test]$ls
    [test@localhost test]$
    As you can see, there are no (visible) files in our test-directory yet.
    Code:
    [test@localhost test]$ls /
    bin dev home lib misc opt root tmp var
    boot etc initrd lost+found mtn proc sbin usr
    These are the contents of the root-directory.
    Notice the home-directory. To see what users are on your system (ie. users who have a home-directory), use ls /home

    Invisible files

    Invisible files are proceeded by a .. That's the reason you don't see the earlier-mentioned files . and .. with a simple ls.
    To see those invisible files, use ls -a:
    [quote]
    [test@localhost test]$ls -a
    . .bash_history .bash_profile .emacs .kde
    .. .bash_logout .bashrc .gtkrc
    [test@localhost test]$
    [/code]

    The shell/the environment.

    Your prompt is a part of the shell. The shell is a 'buffer' between the user and the rest of the system. The most-used shell on Linux-systems is bash (Bourne Again Shell). Other shells are ksh (Korne Shell), csh (C Shell) en sh (the standard Unix-shell).

    The shell knows a so-called environment. The environment contains a lot of variables that influence the shell.
    Code:
    [test@localhost test]$set
    /cut/
    [test@localhost test]$
    Each line contains a variable, followed by = and the contents of the variable. It's common use to use capitals for those variables.
    You can set your own variables:
    Code:
    [test@localhost test]$TEST=negative
    [test@localhost test]$
    You can use those variables at any time by putting a $ in front of them:
    Code:
    [test@localhost test]$echo $TEST
    negative
    [test@localhost test]$
    You can use echo to show the contents of a single variable:
    Code:
    [test@localhost test]$echo $PATH
    /usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/test/bin
    [test@localhost test]$
    The command-history.

    The command-history contains the last 500 commands. You can easily navigate through this list by using the arrow-keys.

    Aliases.

    An alias is a 'shorter variant' of a command.
    Let's say you can't get used to using ls instead of DOS's dir, and want the dir-command to do the same thing ls does.
    Code:
    [test@localhost test]alias dir='ls'
    [test@localhost test]dir /
    bin dev home lib misc opt root tmp var
    boot etc initrd lost+found mtn proc sbin usr
    [test@localhost test]
    Warning: although this might seem as a pretty handy command, aliasing your DOS-commands will only get you weird looks in the Linux-world...

    Some various commands.

    Write
    Code:
    [test@localhost test]write root
    Hello!
    <ctrl-D>
    [test@localhost test]
    This will send root a message saying 'Hello!'. You can check this by changing to tty1 (Ctrl-Alt-F1):
    Code:
    [root@localhost root]#
    Message from test@localhost.localdomain on tty2 at 16.15...
    Hello!
    EOF
    <ctrl-D> is the EOF (End of File)-character.

    w

    w will display the current users of the system, and what they are doing:
    Code:
    [test@localhost test]w
    5:02pm  up  2:36,  3 users,  load average: 0.02, 0.02, 0.00
    USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU  WHAT
    root     tty1     -                 4:15pm   44:54   0.01s  0.01s  -bash
    test     tty2     -                 3:24pm   46:49   0.03s  0.03s  -bash
    neg      :0       -                 2:29pm     ?     0.00s   ?     -
    [test@localhost test]
    whoami

    whoami is not a command to combat an identity-crisis, but a way to figure out under what username you're currently logged in:
    Code:
    [test@localhost test]whoami
    test
    [test@localhost test]
    type
    type is a way to figure out where a certain file is located:
    Code:
    [test@localhost test]type ls
    ls is /bin/ls
    [test@localhost test]
    More likely, you'll get something like this on RH 8:
    Code:
    [test@localhost test]type ls
    ls is aliased to `ls --color=tty'
    [test@localhost test]
    Code:
    [test@localhost test]type pwd
    pwd is a shell builtin
    [test@localhost test]
    file
    This command tries to take a guess at what kinda file something is:
    Code:
    [test@localhost test]file /etc/passwd
    /etc/passwd: ASCII text
    [test@localhost test]
    1.2. Editing text.

    The vi-editor

    Vi is the standard Linux-editor. Vi is short for visual, but don't get your hopes too high: vi is very primitive...

    Vi has two different modes: the command-mode and the input-mode.

    The command-mode:
    Commands are preceeded by a ::
    ° :w --> write the changes to a file
    ° :q --> exit vi
    ° :q! --> exit vi without saving
    ° :wq --> write and exit
    ° :$ --> jump to the end of the file

    Let's do a little exercise:
    Code:
    [test@localhost test]vi file1
    Hit the esc-key to enter the command-mode, then type :i - this will take you to insert-mode.
    Enter some random text, then hit esc to go to command-mode again, and type :wq to save the file and exit vi.

    Remark: make sure you're in your own home-directory before trying this.
    Type ls and you should see your newly-created text-file.

    cat
    cat is a primitive browser:
    Code:
    [test@localhost test]cat file1
    test
    test1
    test2
    [test@localhost test]
    tac
    tac is the opposite of cat and will do what one could expect: display the contents of a file starting with the last line.

    more
    more is used for files that take up more than one screen:
    Code:
    [test@localhost test]more file1
    ...
    [test@localhost test]
    less
    less is a powerfull alternative to more (Less is more, I guess?): it allows you to browse a file by using the arrow-keys.

    tail
    tail displays the last 10 lines of a file.

    head
    head displays the first 10 lines of a file.
    You can change that number by using this syntax: head -n x file where x is the number of lines to be displayed.

    wc
    wc is short for word count:
    Code:
    [test@localhost test]wc test1
    4       3      22 test1
    [test@localhost test]
    where 4 is the number of lines, 3 the number of words and 22 the number of letters in test1.

    grep
    grep is a search-function: it'll search a file for a string:
    Code:
    [test@localhost test]grep ing test1
    testing
    [test@localhost test]
    sort
    sort is a sort-function: in its basic form, it'll sort a file starting from the most left text.

    od
    od without options will give the contents of a file in octal form:
    Code:
    [test@localhost test]od test1
    0000000 062564 072163 072012 071545 064564 063556 072012 071545
    0000020 062564 062553 005012
    0000026
    [test@localhost test]
    The option -h will display the hexadecimal form of a file.

    Other commands required for LPI: split, fmt, expand, paste, join, pr, tr and sed.
    Use the man-command to obtain more info on them...

    1.3. File management

    Assumption: you're logged in as 'test' in you are in your own home-directory (/home/test).

    ls
    We already know ls and ls -a.
    An other interesting option is ls -l:
    Code:
    [test@localhost test]ls -l
    total 4
    -rw-rw-r-- 1 test test 22 Jan 26 17:49 test1
    [test@localhost test]
    The first column shows you the user-rights of the file (more on them later), the second column ('1') shows you the links to the file. Next is the owner of the file, and the group the user belongs to. '22' is the length of the file in bytes.

    cp
    cp is used to copy files:
    Code:
    [test@localhost test]cp file1 file2
    [test@localhost test]ls -l
    total 8
    -rw-rw-r-- 1 test test 22 Jan 26 17:49 test1
    -rw-rw-r-- 1 test test 22 Jan 26 17:52 test2
    [test@localhost test]
    Warning: the standard cp will NOT prompt you if it's about to overwrite a file. The option -i takes care of that.

    mv
    mv is used to move a file.
    Code:
    [test@localhost test]mv file1 file4
    [test@localhost test]
    is equivalent to copying the file.

    mkdir
    mkdir is short for make directory:
    Code:
    [test@localhost test]mkdir dir1
    [test@localhost test]ls -l
    total 12
    drwxrwxr-r 2 test test 4096 Jan 26 17:54 dir1
    -rw-rw-r-- 1 test test 22 Jan 26 17:49 test1
    -rw-rw-r-- 1 test test 22 Jan 26 17:52 test2
    [test@localhost test]
    the first 'd' indicates we're dealing with a directory.

    rm
    rm is used to remove a file.

    Warning: there is no standard 'undelete' function in Linux! Use rm with caution!

    rmdir
    rmdir is used to remove a directory. Without options, it won't remove a non-empty directory. To circumvent this, use rm -r directory.

    Warning: the command rm -r / run as root will definitely remove all files from your system!!!

    Tip: Norton Commander is a well-know DOS-program to take a lot of work out of your hands. Linux has its own equivalent: Midnight Commander.
    You can start it by typing mc.
    Although it's pretty handy, LPI requires you to do everything by hand...

    Output redirection
    Code:
    [test@localhost test]ls -al > file5
    [test@localhost test]
    will redirect the output of ls -al to a new file, file5.
    Use >> to add the new content to a file.

    Other commands needed for LPI: xargs and tee.

    1.4. Process management.

    Processes

    Basically, a Linux-system consists of only a few parts: one of them is the kernel, and the other two are files and processes.
    The kernel is the core, with all kinds of processes 'surrounding' it.

    Abort
    To abort the executing of a program, you can use Ctrl-C
    Code:
    [test@localhost test]yes 'hello'
    <ctrl-C>
    [test@localhost test]
    Foreground processes
    Vi is a nice example: starting vi means starting a second process (the first would be your shell). Vi has a 'controlling terminal' - your keyboard and monitor if you want, therefore it is a foreground process.

    Background processes.
    Background processes are processes without a controlling terminal. You can start a process in the background by adding & at the end of the command.
    Code:
    [test@localhost test]sleep 5
    [test@localhost test]
    [test@localhost test]sleep 5 &
    [1]2102
    [test@localhost test]
    [test@localhost test]
    [1]+ Done	sleep 5
    [test@localhost test]
    sleep 5: 5 seconds will past while nothing happens. After 5 seconds, the prompt comes back.
    Starting the process in the background (by adding &), will make a line of text appear ([1]2102). Hit the enter-button a couple of times, and you'll see the [1]+ Done sleep 5-line appear after 5 seconds.

    Process ID's
    The number in the previous example (2102) is a PID. The kernel uses this ID to control the process.

    Jobs
    jobs will display the processes started from the shell.

    From foreground to background
    Using <Ctrl-Z>, you can move a process from the foreground to the background. You can try it out with vi:
    Code:
    [1]+ Stopped 	vi
    [test@localhost test]jobs
    [1]+ Stopped 	vi
    From background to foreground
    OK, we have vi running in the background now. But how do we bring it back?
    fg takes care of that:
    Code:
    [test@localhost test]fg 1
    The number used here (1) is NOT a Process-ID, it's the number jobs uses.
    bg is the opposite of fg and will put a process in the background.

    Hang-up
    If you're running a background process and you log out, the shell will terminate the process. You can avoid this with nohup (short for 'no hang-up').
    Code:
    [test@localhost test]nohup sleep 5000 &
    The sleep-process starts and will keep on running, even if you log out.

    System processes

    ps
    ps is used for an overview of processes:
    Code:
    [test@localhost test]ps
    PID  TTY      TIME     CMD
    2110 tty1     00:00:00 bash
    2136 tty1     00:00:00 ps
    [test@localhost test]
    This gives you an overview of all processes started by you, knowing the shell (bash) and ps.
    From left to right: the Process ID, the terminal related to the process, used CPU-time and the name of the process.
    ps comes with a lot of options: man ps indeed...

    top
    top is not a standard Unix-command, but it is present at most Linux-systems.
    It'll give you a continuous list of processes:
    Code:
    [test@localhost test]top
    7:08pm  up  4:42,  3 users,  load average: 0.00, 0.04, 0.20
    84 processes: 80 sleeping, 4 running, 0 zombie, 0 stopped
    CPU states:  3.7% user,  0.9% system,  0.0% nice, 95.2% idle
    Mem:   255408K av,  243748K used,   11660K free,       0K shrd,   16860K buff
    Swap:  522072K av,       0K used,  522072K free                  117804K cached
    PID USER PRI NI SIZE  RSS SHARE STAT %CPU %MEM TIME COMMAND
    967 root 5  -10	88276 21M 9004	S <  3.7  8.6  2:53 X
    /cut/
    On top of your screen, you'll see some status-info for your system.
    The third line (CPU states)indicates how much of its time the CPU spends on running user- or system-processes, and how much of the time it's not doing anything ('idle').

    kill
    To terminate a process, one can use kill.
    Code:
    [test@localhost test]sleep 5000 &
    [test@localhost test]ps
     PID  TTY     TIME     CMD
     2146 tty1    00:00:00 bash
     2172 tty1    00:00:00 sleep
     2173 tty1    00:00:00 ps
    [test@localhost test]kill 2172
    [1]+ Terminated sleep 5000
    [test@localhost test]
    Warning: Being a regular user, you're not allowed to influence processes from other users. As root, you're allowed to do anything!
    'God or root, what's the difference? God is more forgiving'...

    Signals
    What kill basically does, is send a signal to a running process. Standard, it's the TERM-signal. An alternative for TERM is 15
    Code:
    [test@localhost test]kill -TERM 2172
    [test@localhost test]kill -15 2172
    TERM nicely asks the process to stop.
    You can get an overview of signals with man 7 signal or kill -l
    If a standard kill does not work, the heavy stuff is needed.
    Code:
    [test@localhost test]sleep 5000 &
    [1]2198
    [test@localhost test]kill -sigkill 2198
    [test@localhost test]
    This signal is not dealt with by the process, but more by the kernel. The kernel will remove the process from the system.

    Priorities
    All processes on a Linux-system have a priority. They receive this priority when they are started, but their priority can be changed. The priority of a process should be seen as a delay-factor. A low delay-factor means a high priority, and the other way around. Normally, a process receives priority 0. The value ranges from -20 to 19. A normal user can only lower the priority of a process.
    The commands used to influence priorities are nice and renice.

    nice
    nice allows you to lower the priority of a program when you start it. It's called 'nice' because... well, it's very nice to other users to start a program with a low priority
    Code:
    [test@localhost test]nice sleep 5000 &
    Standard, nice uses a priority of 10.
    Remember: -20 gives a process the HIGHEST priority.

    renice
    renice is used to change a processes priority while it's already running.
    Code:
    [test@localhost test]renice -5 2210
    renice: 2210: setpriority: Permission denied
    [test@localhost test]renice 12 2210
    2210: old priority 10, new priority 12
    [test@localhost test]
    With the first command, I tried to give the process a higher priority. Since I'm not root, I'm not allowed to do this.

    That's about it for now
    Coming up: Devices, partities and filesystems; advanced filemanagement; runlevels; user-management; system-management (all of them are LPI 101). After that, I'll continue with LPI 102: PC configuration, Linux install, software-management, kernelmanagement; shellscripts; X; Elementary TCP/IP; PPP; inetd,...

  2. #2
    Member
    Join Date
    Dec 2002
    Posts
    58
    Well done

    Expecting
    Linux LPI 10x | more
    God is Love

  3. #3
    Trumpet-Eared Gentoo Freak
    Join Date
    Jan 2003
    Posts
    992
    Negative,

    You read my mind what I needed, as a Linux - newbie I really can use this kind of stuff.

    Greetz,

    Btw, Dit is nog eens vakwerk...
    Come and check out our wargame-site @ http://www.rootcontest.org
    We chat @ irc.smdc-network.org #lobby

  4. #4
    Thanx MaN really a Good Tut. of Basic Linux Cmd's

  5. #5
    Just Another Geek
    Join Date
    Jul 2002
    Location
    Rotterdam, Netherlands
    Posts
    3,401
    The "standard" /bin/sh shell is actually called the Bourne Shell.. Guess why bash is the Bourne Again SHell
    Oliver's Law:
    Experience is something you don't get until just after you need it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •