Linux Articles November 11, 2009 0

Enter the Linux

Introduction to Linux

Linux TUX Linux is the name usually given to any Unix-like computer operating system that uses the Linux kernel. Linux is one of the most prominent examples of free software and open source development: typically all underlying source code can be freely modified, used, and redistributed by anyone.

The name “Linux” comes from the Linux kernel started in 1991 by Linus Torvalds. The system’s utilities and libraries usually come from the GNU operating system, announced in 1983 by Richard Stallman. The GNU contribution is the basis for the alternative name GNU/Linux.

Predominantly known for its use in servers, Linux is supported by corporations such as Dell, Hewlett-Packard, IBM, Novell, Oracle Corporation, Red Hat, and Sun Microsystems. It is used as an operating system for a wide variety of computer hardware, including desktop computers, supercomputers, and embedded devices such as E-book readers, video game systems (PlayStation 2, PlayStation 3 and XBox, mobile phones and routers.

Let’s roll…

Article Index:

1. Enter the Linux
2. Navigating the file system
3. File viewing commands
4. Commands ps and top
5. Combining commands together
6. Other useful stuff
7. Linux file structure

  • Two things about remote accessing the Linux server

– Using a terminal e.g. using putty application (recommended at all times)

– Using an SFTP client e.g. using WinScp (good software also recommended)

  • Access using a terminal session

– Start putty.exe

1. Enter hostname or IP Address
2. Select ssh
3. Enter session name: “myserver1”
4. Save and open the session

putty session

  • Access using an SFTP client – define connection

– Start winscp.exe

1. Enter hostname

2. Check port (should be 22)
3. Username
4. Check protocol (SFTP)
5. Save

WinScp session

– Connecting – general remarks for putty and winscp

· You might get questions about server ssh keys
· Those keys are used for securely identifying the server
· If it is the first time you connect to the specific server: accept the key
· The key gets stored locally on your PC

General Linux remarks

· Path separator on Linux is: / (slash, or forward slash)
· Linux is case-sensitive: always use the correct case:
· aFileName is not the same as afilename
· Text files on Linux have a different line ending than text files on windows (Linux: n, Windows: rn)
· This can cause problems when transferring files:
· Transfer text files as text files, line endings are automatically corrected
· Otherwise you get weird characters in your files (^M) or the whole file is on one line
· Transfer non-text (binary) files as binary files
· Otherwise the files may get corrupted!
· There is no Recycle Bin, think twice before you’ll perform delete action!
· Everything that its name starts with a . (dot) is a hidden file/directory

– Main tasks in terminal session

• Change your password: passwd
• Execute programs


2. Commands for navigate the file system

Look at directory contents ls
Change directory cd
Find files find
Copy and move files cp and mv
Remove files rm
Create directories mkdir
Remove directories rmdir (dir must be empty)
  • View files and search in files:
To determine the type of a file file
Dump a file to the screen cat
Page thru a file less
Search in a file grep
Tail a file tail
Count words or lines in a file wc or wc –l
View activity on the server ps and to


Explanations:

  • Command: passwd

The passwd command allows you to change your password:
• Enter your old password (when not a root user)
• Enter your required new password
• Enter your required new password again

  • Command: ls

The ls commands lets you retrieve the contents of a directory
o ls Shows directory contents
o ls -a Shows all directory contents (including hidden files starting with a dot
o ls -l Shows directory contents as a list with extra info
o ls -al Shows all directory contents as list
o ls -lh Shows directory contents as list, sizes in a readable format (Kb Mb)
o ls -lt Shows directory contents as list, ordered by file-change time
o ls -ltr Shows directory contents as list, ordered by file-change time (reversed, last changed file at the bottom)

ls -l output:

$ls -la

drwxr-xr-x 1 file_owner owners_group 946 2010-11-11 11:11 filename

Fields explanation (from left)

Access rights (files permissions)

• special indicator:
d = directory
= regular file
l = symbolic link
s = UNIX domain socket
p = named pipe
c = character device file
b = block device file

• rwx rwx rwx – rights regarding to user group and others
r = read permission
w = write permission
x = execute permission
= no permission

Examples
:

rwxr-xr-x File: owner has read, write, execute permissions, group: only read and execute permissions, others: only read and execute permissions.
dr-x — — Directory: owner has read and execute access, group and others have no access
drwxr-xr-x 1 file_owner owners_group 946 2010-11-11 11:11 filename

Continuing fields explanation for ls -l output:

• Number of links/directories
• File owner
• Owners group
• File size
• Date last change
• Time last change
• Filename

  • Directory commands

– The cd command lets you switch directories.

cd without a parameter leads you to your home dir
cd ~ (with tilde) does the same
pwd shows the current directory
mkdir lets you create directories
rmdir lets you remove a directory (dir must be empty)
  • Command: find

• The find command lets you find files

find . – name somename Find file with name somename starting from current directory
find /dir – name ‘some*’ Find file with name starting some with some starting from /dir. Use the quotes!
find . – iname somename Find file with name somename starting from current directory, ignoring case of the filename
  • Commands: cp and mv

• The cp command lets you copy files and directories

cp srcfile tgtfile Copy file ‘srcfile’ to file ‘tgtfile’
cp *.log target_dir Copy all logfiles to directory ‘target_dir’
cp one.log two.log target_dir Copy ‘one.log’ and ‘two.log’ to directory ‘target_dir’
cp –R source_dir target_dir Copy source_dir and all its content to directory target_dir’
cp –R source_dir/* target_dir Copy the content of source_dir to directory ‘target_dir’

• The mv command lets you move (and rename) files and directories

mv srcfile tgtfile Move (rename) file ‘srcfile’ to file ‘tgtfile’
mv *.log target_dir Move all logfiles to directory ‘target_dir’
mv one.log two.log target_dir Move ‘one.log’ and ‘two.log’ to directory ‘target_dir’
mv –R source_dir target_dir Move source_dir and all its content to directory ‘target_dir’
mv –R source_dir/* target_dir Move the content of source_dir to directory ‘target_dir’
  • Command: rm

• The rm command lets you delete files

rm logfile.log Delete a file.
rm –rf /dir_1 Deletes all files and directories recursively in directory /dir_1 including. (Attention! Never perform something like this: rm –rf / ).
  • Commands: tar & gzip – Packing & unpacking files

• The tar command lets you archiving files and directories into a single file, it does not compress them

tar –cvf logs.tar *.log Archive all log files into logs.tar (no compression yet)
tar –xvf logs.tar Unarchive all files from logs.tar

• The gzip command compresses a file

gzip logs.tar Compresses logs.tar to logs.tar.gz (removing logs.tar)
gunzip logs.tar.gz Decompresses logs.tar.gz to logs.tar (removing logs.tar.gz)

• Combined (you could combine tar with gzip in one command)

tar –cvzf logs.tar.gz *.log Packs and compresses all log files into logs.tar.gz
tar –xvzf logs.tar.gz Decompresses and unpacks all files from logs.tar.gz
  • File viewing commands

less – shows the content on your screen and allows you to page through it
grep – searches a file for a string
tail – shows the end of a file
wc – counts words or lines in a file
file – displays the type of a file
cat – dumps the contents of a file on your screen

  • Command: less

• The less command shows the content on your screen and allows you to page through it

$ less filename

Shows the file content on the screen (scroll using cursors)

– Keys (use thise keys while browsing the file):
space Next page
b (char) Previous page
/ Search forwards
? Search backwards
q Quits

  • Command: grep

• The grep command searches for strings in files

grep error logfile.log Searches for the string ‘error’ in ‘logfile.log’
grep –i error logfile.log Searches case-insensitive for the string ‘error’ in ‘logfile.log’ (so finds Error, eRror and error)
grep error *.log Searches for the string ‘error’ in all files ending with .log in the current directory
  • Command: tail

• The tail command shows the last part of a file

tail -n 50 logfile.log Shows the last 50 lines of the file ‘logfile.log’
tail -f logfile.log Follows the end of ‘logfile.log’, every line that is appended by another process is shown on your screen (very handy tracking files in real time)
  • Command: wc

• The wc command counts words or lines in a file

wc -l logfile.log – Count the number of lines in the file ‘logfile.log’

  • Command: ps

• The ps command shows a snapshot of current processes

For continuous information: use top

ps Show all processes for the current user at the current terminal
ps -e Show all processes on the system PID, TTY, TIME and CMD
ps -ef Show all processes on the system with extended info UID, PID, PPID, C, STIME, TTY, TIME and CMD
ps -efH Show all processes on the system with extended info as a tree UID, PID, PPID, C, STIME, TTY, TIME and CMD
ps auxf it’s also cool display

UID The user id
PID The process id
PPID The parent process id
C Percentage CPU usage
STIME Starting time of the process. If the elapsed time is > 24 hours, the starting date
TTY The terminal that started the process
TIME The CPU time used
CMD The executing command

  • Command: top

• The top command displays active system tasks

Example of output: screen with a header and top-active tasks

$ top
09:49:31 up 41 days, 16:26, 4 users, load average: 0.21, 0.27, 0.23 Tasks: 100 total, 2 running, 98 sleeping, 0 stopped, 0 zombie Cpu(s): 0.9%us, 0.1%sy, 5.3%ni, 93.5%id, 0.1%wa, 0.0%hi, 0.1%si, 0.0%st Mem: 8309568k total, 8215884k used, 93684k free, 233920k buffers Swap: 1052248k total, 70764k used, 981484k free, 504504k cached

us: user cpu time

sy: system cpu time
ni: user nice cpu time
id: idle cpu time
wa: io wait cpu time
hi: hardware irq (servicing hardware interrupts)
si: software irq (servicing software interrupts)
st: steal time (time in involuntary wait by virtual cpu while hypervisor is servicing another processor)

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1946 user1 16 0 3320 1056 792 R 1 0.0 0:00.24 top
1410 root 18 0 1249m 180m 13m S 23 5.9 0:31.85 rm -rf /

Columns explained:

PID The process id
USER The user executing the process
PR Priority value
NI Nice value
VIRT Amount of virtual memory used by a task
RES The non-swapped physical memory used by a task
SHR The amount of shared memory used by a task
S Status: D = Uninterruptable sleep, R = Running, S = Sleeping, T = traced or stopped, Z = zombie
%CPU Percentage of CPU used
%MEM Percentage of physical memory used by a task
TIME+ CPU time used by this task since started
COMMAND The command that is being executed in the process
  • Combining commands together

A lot of small commands that all do a single thing combined together they are much more powerful “|” a vertical pipe mark is being used

Examples:

Count the number of lines in logfile.log that contain the string ‘error’

grep error logfile.log | wc –l

Count the number of logfiles

ls –l *.log | wc -l

View a paginated list of logfiles

ls –l *.log | less

• Tail a logfile only showing lines that contain the string ‘error’

tail –f logfile.log | grep error

Gets a list of active processes Filter lines that contain the string ‘java’ Count the lines

ps –ef | grep –I java | wc -l

Executing programs

• If the program is in the PATH: just enter the program name

# ifconfig

• If  the program is not in the path: enter full path + program name

# /sbin/ifconfig

• The current directory is NEVER in the path, use: ./

# cd /sbin
# ./ifconfig

• Shell scripts that are executable: just execute as a normal program

# cd /home/myscripts
# ls -la
-rwxr-xr-x 1 user69 users 0 Nov 11 16:33 myscript.sh

# ./myscript.sh

• Shell scripts that are not executable, use: sh 

# cd /home/myscripts
# ls -la
-rw-r--r-- 1 user69 users 0 Nov 11 16:33 myscript.sh

# sh myscript.sh

• Check whether the program is in your $PATH

# which ifconfig
/sbin/ifconfig
  • Other useful stuff

ctrl+c cancels a running program (most of the time)
ctrl+d ends a session (as does the exit command)
screen: (must be installed) Use multiple screens, and keep sessions alive between (dis)connects

while in screen mode:

ctrl+a c create a new screen (taping order: ctrl+a then release ctrl+a and press “c” button separate)
ctrl+a switch to screen (keyboard’s numbers)
screen -wipe shows actual screens
screen -x connects to detached screen
ctrl+d end a screen
ctrl+a esc scroll through a screen (esc ends scrolling)

man : Asks for the manual page of a command / program e.g. man cp
which : find out the path of the command
tab key auto completion

For instance:
taping “cd /home/userone” could be written much faster with help of tab key functionality:

cd /ho[press tab]/use[press tab] – observe that the right existing path will be auto filled up – very handy! use it!

Short Description of the Linux file structure

Filesystems

Description

root “/”

Basic operating system and maintenance tools. The content of this file system should be sufficient to start up the system and perform emergency maintenance and repairs if they were necessary.

The parts of the root file system are:

/bin

–executables (binaries) needed during bootup that might be used by normal users.

/sbin

–executables (system binaries) not intended for use by general users (users may still use them, but this directory is not on their PATH).

/etc

–system-wide configuration files for your operating system.

/root

–the home directory of the system administrator (called super-user or root).

/dev

–device files. Devices appear on Linux as files so that hardware is abstracted and it is easy to write to them or read from them.

/mnt

–mount points for removable media (floppy, cdrom, zip drive), partitions of other operating systems (e.g. MS Windows), network shares, and anything else that is mounted on the file system temporarily. It normally contains a separate subdirectory for each mounting share. The contents of these drives/shares appear in these subdirectories–there are no drive letters on Linux.

/lib

–shared libraries for programs that reside on the root filesystem and kernel modules.

/boot

–files used by the bootstrap loader (LILO or GRUB), the thing that loads first when the computer is booted and perhaps gives you the option of which operating system to boot, if you have more than one OS on your computer). It typically also contains the Linux kernel (compressed, file vmlinuz), but this can be stored somewhere else, only if LILO/GRUB is configured to know where it is.

/opt

–optional large applications, for example kde under RedHat 5.2 (under RedHat 6.0, kde is distributed as any other X-windows distribution, main executables are in the /usr/bin directory).

/tmp

–temporary files. This directory may clean up automatically.

/lost+found

–files recovered during the filesystem repair.

/usr

All commands, libraries, documentation, and other files that do not change during normal operation. This will also contain major applications that come with your Linux distribution, for example Netscape.

/var

Files that change: spool directories, log files, lock files, temporary files, and formatted (on use) manual pages.

/home

User files (users’ own settings, customization files, documents, data, mail, caches, etc). The contents of this directory should be preserved on an operating system upgrade. ~ (tilde) Is the alias for your home directory ex. cd ~ or cd will lead you to your home directory instead cd /home/your_user

/proc

Entirely illusionary files. They do not really exist on the disk and do not take up any space there (although ls -l will show their size). When viewing them, you really access information stored in the memory. It is used to access information about the system.

  • PuTTY Connection Manager – seems that project has been abandoned. But you could find some old versions on th einternet here