Basic Linux Operation
Basic Linux Operation
There is no need to be a Linux expert in order to develop Web application. Only a handful of
basic server administration skills are needed for typical Web application developer. Here we
cover these minimum skills to administer Linux server. Since we chose Linux server without
any GUI (Graphical User Interface), we will use text-based Linux console commands for all
the administration tasks, regardless that you operate directly on the Linux server computer
or operate remotely from other PCs using SSH ( 1.4). It might be a little bit challenging for
the first time, but this is actually the preferred way to administer any Linux-based server.
(GUI is originally intended for desktop computers).
> su -
Password: Enter password for root (eg, student, stuictti)
#
Now you are root
Once you have finished your work on the server, you will log out from the server by using
exit command.
# exit
Note that if you used su command to be root, you need to exit twice, once from root and
another from the normal user.
# su --help
Usage: su [OPTION]... [-] [USER [ARG]...]
Change the effective user id and group id to that of USER.
If this brief description is not enough for you, you can refer to the detailed online manual
page of that command by issuing the man command, followed by the command name that
you want to know like shown below.
# man su
man su
Reformatting su(1), please wait...
SU(1) User Commands SU(1)
NAME
su - run a shell with substitute user and group IDs
SYNOPSIS
su [OPTION]... [-] [USER [ARG]...]
DESCRIPTION
Change the effective user id and group id to that of USER.
-, -l, --login
make the shell a login shell
-c, --command=COMMAND
pass a single COMMAND to the shell with -c
-f, --fast
pass -f to the shell (for csh or tcsh)
-m, --preserve-environment
Manual page su(1) line 1
In this case, it will show the man (online manual) page of the su command. A man page is a
screen-full of information that you can go forth and back by using up / down arrow keys or
page up / page down keys. Press q key to exit from the man page.
If you want to search the string in man pages, press / key followed by the search text, then
press Enter. Press n to search the next match of the string.
DESCRIPTION
Change the effective user id and group id to that of USER.
-, -l, --login
make the shell a login shell
-c, --command=COMMAND
pass a single COMMAND to the shell with -c
-f, --fast
pass -f to the shell (for csh or tcsh)
-m, --preserve-environment
/login to search the string (login)
and then press n key to search the next match of the string.
Reformatting su(1), please wait...
make the shell a login shell
-c, --command=COMMAND
pass a single COMMAND to the shell with -c
-f, --fast
pass -f to the shell (for csh or tcsh)
Press q key to exit from the man page and to clear the screen, please enter clear command
and then Enter key.
# ifconfig
eth0 Link encap:Ethernet HWaddr 00:16:76:CD:AD:CA
inet addr:192.168.0.123 Bcast:192.168.0.255
Mask:255.255.255.0
inet6 addr: fe80::216:76ff:fecd:adca/64 Scope:Link
UP BROADCAST NOTRAILERS RUNNING MULTICAST MTU:1500 Metric:1
RX packets:11246 errors:0 dropped:0 overruns:0 frame:0
TX packets:2041 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:2211847 (2.1 Mb) TX bytes:285586 (278.8 Kb)
Normally ifconfig command should show two or more network interfaces including lo,
which is the local loopback interface (and is not physical network interface device). If you
see only lo in the result of ifconfig command, it means that no physical network
interface device is active or working, thus Web application server software cannot work
correctly. In case your network interface device is not working, or if your network
configuration needs modification, the first thing you should do is to use yast, the central
utility for SUSE Linux administration.
Using yast
yast is NOT a standard Linux command, but a dedicated system management utility
designed for SUSE Linux. Using yast, you can do almost any system configuration change
including adding new hardware / software, updating modules to the latest version (even
automatically), setting up network configuration including firewall settings, and so on. If you
need any change in core system configuration, yast is the first command you should use in
SUSE Linux. Invoking yast from the command prompt will bring the on-screen interface like
shown below.
Fig: 3.1 Yast main screen in text mode
In order to setup network devices, choose “Network Devices” from the menu on the left,
then choose “Network Card” from the menu on the right.
Fig: 3.2 Choose “Network Devices” from the menu of the left.
Fig: 3.3 Choose “Network Card” from the menu on the right.
You will see the same network configuration screen as you saw during the installation of
SUSE Linux..
Once you have connected to the Linux server by SSH client, you can login to and operate on
that Linux server from your PC just like as you did on the server directly.
Firstly, you should download putty-0.58-installer.exe from the internet or other server.
And then run this .exe file (PuTTY) and we will occur PuTTY Configuration.
Fig: 3.6 PuTTY Configuration from client.
Press the Open button. If network connection works properly, you will see the following text
mode screen.
boot … Stores static files required to boot the system, such as the Linux kernel.
etc … Stores non-binary configuration files that are local to the machine.
lib … Stores libraries needed to execute the binaries in /bin/ and /sbin/
mnt … Reserved for temporarily mounted file systems such as NFS
proc … Stores special files that communicate with the kernel
sbin … Stores executables used by the root user
subdomain*1 … Stores sub-domain data for Apache Web server
tmp … Stores temporary files
var … Stores variable, frequently written data files
bin … Stores essential executables for all users
dev … Stores special or device-specific files such as device drivers
home … Stores each user‟s home directory
media … Contains mount points for removable media such as 3.5 diskettes and CD-ROM
opt … Stores large, static application software packages
root … Home directory for root user
srv … Stores the server-specific data such as for WWW, FTP, etc.
sys*2 … Special directory used by Linux file system
usr … Stores files that can be shared across multiple machines
*1 … Apache-specific directory *2 … Not standard in FHS
Fig: 3.9 Directory structure of SUSE Linux 10.2 configured for Web application
For the minimum Linux administration for Web application server, Web application
developers don‟t have to know all the details of these directories. But he/she normally works
only with the following directories.
/etc … This is the most important directory for Linux system administration. All the
configuration files for server software including Apache, MySQL, PHP and SSH are
stored here, and you must work with those configuration files.
/srv … This is the location where all the Web contents are stored. All the HTML, PHP
and CSS files should go here at /srv/www/htdocs/ sub-directory.
/var … This is the location that you should check the working status of all the server
software by inspecting log files stored at /var/log/ sub-directory.
/home … This is the place for home directory (the first place you are in when
you login to the server) for all normal users
/root …This is the home directory for root user
# cd var
To switch back to your home directory, you could type
# cd /home/var
but there is shortcut you will find useful. This is the double-dot (..) notation, as in
# cd ..
This will move you one level up, to the parent directory.
For full-directory path starting from the root, you should use “/” such as “/var/log”,
or the name of subdirectory starting from the current location without “/” such as
“log/apache2”.
How Do We List Linux Files?
The ls command lists the names of all files in the current directory, and tells you about the
attributes of those files. Here are some examples:
# ls var list the name of all files and sub-directories in the var
directory.
By default, ls prints a simple, columnar list of our files; but it will list your files in
many different formats if you add one or more flags on the command line. To use any of
them, simply type the ls command at the prompt, followed by a space, and then add a
switch by typing a hyphen followed by the flag, like this:
# ls –al
Here are some of the most commonly used ls flags:
r : Read permission
rwx r-x r-x w : Write permission
x : Execute permission
Fig: 3.10. 3 groups of 3 kinds of permissions
To understand how to read file permissions, let's start by splitting apart those ten
characters for .bash history :
For the permission, you can specify a particular permission at a time, or the
combination of all 9 permissions at once. In order to change a particular permission, you use
the following syntax.
# chmod u + r file_or_directory_name
g - w
o = x
o For all others = give only that access x For execute access
For example, if you want to add write permission to other users for a file, you enter
the command like this.
On the other hand, if you want to assign the combination of all 9 kinds of permissions
at once, you use octet-encoded 3-digit number like shown below.
The first digit corresponds to the permission for owner. The second digit is for group,
and the third digit is for other users. Each digit is the addition of the following numbers.
Read permission = 4
Write permission = 2
Execute permission = 1
So, the number “750” in the example above means that you specify read, write and
execute permission to owner (4 + 2 + 1 = 7), read and execute permission to group (4 + 1
= 5), and no permission to other users (0).
Sometimes you may need to change the owner of a file itself for server software to
correctly handle the file. In order to change the owner of a file or a directory, you use chown
command.
Table 2 – Important configuration files in SUSE Linux 10.2 for Web application
developers
File name Location Description
httpd.conf /etc/apache2 Main configuration file for Apache server
*.conf /etc/apache2/conf.d All included configuration files for Apache
my.cnf /etc MySQL server configuration file
php.ini /etc/php5/apache2 PHP configuration file
Sshd_config /etc/ssh SSH server configuration file
# cp filename backup_filename
If you want to create new file, or already made backup file, then you can start vi with
the file name that you want to edit like this.
# vi filename
You will see surprisingly simple screen almost filled with the content of the file, except
for the bottom row of the screen that shows brief information of the editing like shown
below.
#password = your_password
port = 3306
socket = /var/lib/mysql/mysql.sock
# Here follows entries for some specific programs
"my.cnf" 160L, 4984C 18,1 28%
File name Size of the file (in lines and characters) Current position in the file
You can use arrow keys, PgUp / PgDn, Home / End keys to navigate through the file
just like any other text file editors. However, when you wish to modify the content, you must
be very careful because the operation of editing in vi is quite different from normal text
editors. Basically, in order to edit something in vi, you MUST first press a key that indicates
what kinds of editing operation you will make. Unlike normal text editors, you cannot even
just type characters to edit the content. This is very confusing for beginners, but once
accustomed; you can use it on any Linux/Unix computer. Below is the list of basic operation
commands in vi. You should at least memorize these basic commands. Be careful that they
are all case-sensitive.
Table 3 – The minimum operation commands you need in vi
Operation How to do it
Browsing content Use arrow keys, PgUp/PgDn, Home/End keys
Insert text Press i key, type the content, then press Esc key when finished.
Append text Press a key, type the content, then press Esc key when finished.
Delete a character Press x key or Del key
Delete a line Press d key 2 times (dd)
Join two lines into one Press J key on the upper line
Undo Press u key
Search for a text Press / key, type search text, then press Enter
Search next match Press n key
Copy a line into clipboard Press y key 2 times (yy)
Paste a line from clipboard Press p key
Save and exit vi Press :wq + Enter or press Z key (Shift-z) two times (ZZ)
Quit without saving file Press :q! + Enter
Linux-v848:~ # ps
PID TTY TIME CMD
4688 pts/2 00:00:00 bash
4706 pts/2 00:00:00 ps
Since this does not give enough information − generally, at least a hundred
processes are running on your system. In Linux, all running software is managed as
“processes”. There are normally many processes running on the same Linux server. In order
to see all running processes, you can use ps command with aux option (without hyphen “-“).
# ps aux
USER PID %CPU %MEM VSZRSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 716172 ? S 09:36 0:03 init [3]
root 2 0.0 0.0 0 0 ? SN 09:36 0:00 [ksoftirqd/0]
root 3 0.0 0.0 0 0 ? S< 09:36 0:01 [events/0]
...
root 4357 0.0 1.4 4132 1892pts/0 Ss 18:28 0:00 -bash
root 4586 0.0 0.6 2384 836pts/0 R+ 20:06 0:00 ps aux
# Note that the last line of the list always shows the process of your own console (see
the COMMAND column above), because the console is also one of the running processes.
We will usually select particular processes out of the list of all processes, using the
grep command in a pipe, which will select and display all processes owned by a particular
user.
ps −ef | grep username
This example shows all processes with a process name of bash, the most common
login shell on Linux systems:
linux-v848:~ # ps aux | grep bash
brenda 31970 0.0 0.3 6080 1556 tty2 S Feb23 0:00 −bash
root 32043 0.0 0.3 6112 1600 tty4 S Feb23 0:00 −bash
theo 32581 0.0 0.3 6384 1864 pts/1 S Feb23 0:00 bash
theo 32616 0.0 0.3 6396 1896 pts/2 S Feb23 0:00 bash
theo 32629 0.0 0.3 6380 1856 pts/3 S Feb23 0:00 bash
theo 2214 0.0 0.3 6412 1944 pts/5 S 16:18 0:02 bash
theo 4245 0.0 0.3 6392 1888 pts/7 S 17:26 0:00 bash
theo 5427 0.0 0.1 3720 548 pts/7 S 19:22 0:00 grep bash
In these cases, the grep command finding lines containing the string bash is often
displayed as well on systems that have a lot of idle time. If you don't want this to happen,
use the pgrep command.
# Note that this command will also show your own console process as the last line and
don‟t confuse the line with the process you are looking for. In other words, if you see only
one line (console process) as the result of this command, it means that the process is NOT
running.
In order to restart a process after you modify the configuration, use killall command
with –HUP option. This command will first kill all the matched processes and then restart
them automatically.
In SUSE Linux 10.2, all these logs are stored in /var/log directory. Below is an example of
the content of /var/log directory.
# ls /var/log
YaST2 mail.err zmd-backend.log-20060806.bz2
Acpid mail.info zmd-backend.log-20060807.bz2
apache2 mail.warn zmd-backend.log-20060808.bz2
boot.log messages zmd-backend.log-20060809.bz2
boot.msg mysqld.log zmd-messages.log
boot.omsg news zmd-messages.log.2006-07-31
faillog scpm zmd-messages.log.2006-08-04
firewall warn zmd-messages.log.2006-08-06
krb5 wtmp zmd-messages.log.2006-08-07
lastlog z md-backend.log zmd-messages.log.2006-08-08
The bold letter means a sub-directory. You can see that there are many log files in this
directory, and the logs of particular software are sometimes stored in its own sub-directory
(such as apache2). There are also older, archived log files (with the extension of .bz2).
These log archives will be regularly and automatically created by Linux system.
In order to view the full content of a log file, you can open it with vi, or use cat command
together with less command like shown below.
This will let you browse through the log file with arrow keys and PgUp/PgDn keys. If you
want to quit this browsing display (created by less command), you can press q key.
Sometimes you may wish to see only the last (the newest) few lines of a log with real-time
update, so that you can see the live display of a frequently appended log file. You can do
that by tail command with –f option like this.
# tail -f log_file_name
To quit from the real-time display of tail command, press ctrl + c key.
Example:
You can view common log file /var/log/messages using any one of the following command:
# tail -f /var/log/messages
# less /var/log/messages
# more -f /var/log/messages
# vi /var/log/messages
# less -f /var/log/messages
Jan 12 20:27:03 linux-j0fj auditd: Config file /etc/audit/auditd.conf
doesn't exist, skipping
Jan 12 20:27:03 linux-j0fj kernel: audit(1168608423.488:3):
audit_backlog_limit=256 old=64 by auid=4294967295
Jan 12 20:27:03 linux-j0fj kernel: klogd 1.4.1, ---------- state
change ----------
Jan 12 20:27:03 linux-j0fj kernel: audit(1168608423.564:4):
audit_pid=3192 old=0 by auid=4294967295
Jan 12 20:27:03 linux-j0fj SuSEfirewall2: Warning: ip6tables does not
support state matching. Extended IPv6 support disabled.
Jan 12 20:27:03 linux-j0fj SuSEfirewall2:
/var/lock/SuSEfirewall2.booting exists which means system boot in
progress, exit.
# shutdown -r now
On the other hand, you can completely shutdown the server by using shutdown command
with –h option as follows.
# shutdown -h now
Note, however, that this command will physically turn off the computer hardware (if the
computer supports ACPI standard), and you must manually turn the computer hardware on
again in order to make the server running back again. This will be a problem if you place the
server at distant place (such as at server room or at web hosting service company).
1.11. Linux commands for Web application developer
Below is the list of all Linux commands that Web application developer should use often.