Upload
others
View
13
Download
0
Embed Size (px)
Citation preview
OPERATING SYSTEMSOPERATING SYSTEMSLINUXLINUX
Božo Krstajić, PhD, University of Montenegro Podgorica
Process management
Linux operating systems work with processes.
Basically a process consists of program code(named text), data (used by a program) and a stack. The stack is used by the program to store variables.
Programs are at least one process.
You have control over all the processes that you start.
A user can control a process by sending signals to the process.
Process management
ps
The ps command is used to report which processes are currently active.
The ps without any parameters shows which processes are active in the current user session.
$ psPID TTY TIME CMD1191 pts/2 00:00:00 bash1216 pts/2 00:00:00 ps
Process managementAs you can see each process has a process ID (PID). All running processes are given a unique identifier (PID). On 2.2.x kernels, this process ID can be anywhere between 1 and 32767.
You will need the process number if you want to send a signal to a process, for example a kill signal (for stopping process execution).
The TTY column indicated which terminal the process is running on.
The TIME column indicated how much CPU time the process has been running.Finally, the CMD column shows what the program actually is. It only lists the base name of the program, not any command line options.
Process managementThe ps has many parameters to modify the output.
Have a look at the ps manual pages.
You can get a complete listing of the processes running on your system using the right combination of options:
ps –axThere is a new column: STAT. It shows the status of
the process. S stands for sleeping (the process is waiting for something to happen); R stands for running process.
For example type and explain: ps –aux
Process management
kill
The kill sends a signal to a process. If no signal is specified the TERM signal is send,
which asks a process to exit gracefully. kill [–sig_numb] PID
Example 1:Kill process which started with command mcps x | grep mckill 1024ps x | grep mc
Process management
The kill -l displays a list of signals that can be sent.
The SIGKILL signal is often used to kill processes that refuse to terminate with the default SIGTERM signal.
The signal can be specified by using the number as a parameter:
kill -9 PID
Background processesNormally a process takes control over the screen and
keyboard after it is started. Programs started from the command line start up in the foreground.
It is also possible to start processes as a background process, this means that the shell starts the process, but keeps control over the terminal.
In most shells a process can be started as a background process by placing an ampersand (&) after the command:
command arguments &
Example 1Create script file named EHO.sc which will:
- ‘Say’: Hi, my friend. Type your name, surname and your personal ID, please:- Assign input values to the property variables.- Show on the screen entered data.- Reenter same data if they are wrong.- ‘Say’: Thanks. have nice day if they are correct.
Start this script as background process.
Example 1function inp {
echo Type Your NAME, SURENAME nad pIDread N S Pecho Hi $N $S with your pID $P !}
function qu {echo Is these date correct? [no/yes or Enter]read answer}
inpqu
if [ $answer = 'no' ];theninp
elseecho Thank, have nice day
fi
Background processesA process that runs in the background can be
brought to the foreground using the command:fg %<job ID>
You can see which jobs are running, with their job numbers, using the jobs command.
For example:./EHO.sc &jobsfg %1
Stopping processesA process that is running can be stopped by pressing
the <Control> and <z> keys simultaneously.Stopped processes can be moved to the foreground
with the fg command. Running fg without any parameters moves the last process that was stopped to the foreground. Other processes can be moved to the foreground by specifying the job ID as a parameter to fg.
A stopped process can also be told to continue as a background process, by executing bg <job ID>.
Executing bg without any parameter will move the last stopped process to the background.
Process management
Finally, there's a command you can use to display updating information about the processes running on the system.
This command is called top, and is started like so:
$ top
This will display a full screen of information about the processes running on the system, as well as some overall information about the system.
Process managementThis includes:
load average, number of processes, the CPU status, free memory information, and details about processes including PID, user, priority, CPU and memory usage information, running time, and program name.
It's called top because the most CPU intensive programs will be listed at the top.
Essential System Administration -User management
You are the administrator of any computers that you have root on.
GNU/Linux is a multi-user operating system. This means that multiple users can use the system, and they can use the system simultaneously.
First of all, there are several user accounts on each system.
Users can be members of groups.
User managementThere are a few reserved users and groups on eachsystem.
The most important of these is the root account.The root user is the system administrator.
The available user accounts are specified in the file: /etc/passwd.
There are no passwords in this file. Passwords are kept in the separate /etc/shadow, as an encrypted string.
Default System User Namesroot – The superuser account (UID=0) with
unrestricted access.daemon – Used for system processes.bin – Owns executablesys – Owns executableadm – Owns accounting and log filesuucp – Used for UNIX to UNIX copy
communication access and files
User managementInformation about groups is stored in /etc/groupand /etc/gshadow.
It is generally speaking not a good idea to edit these files directly.
There are some excellent tools that can help you with user and group administration:
adduser, userdel, passwd, groupadd, groupdel, groupmod, …
Adding and removing usersadduser
The adduser command combines useradd and passwd in an interactive script.
It will ask you to fill in information about the account to-be created.
The screen listing below shows a sample session.
# adduser
Adding and removing users• Login name for new user []: john• User ID (’UID’) [ defaults to next available ]: <Enter>• Initial group [ users ]: <Enter>• Additional groups (comma separated) []: <Enter>• Home directory [ /home/john ] <Enter>• Shell [ /bin/bash ] <Enter>• Expiry date (YYYY-MM-DD) []: <Enter>• New account will be created as follows:• ---------------------------------------
Adding and removing users• Login name.......: john• UID..............: [ Next available ]• Initial group....: users• Additional groups: [ None ]• Home directory...: /home/john• Shell............: /bin/bash• Expiry date......: [ Never ]• This is it... if you want to bail out, hit Control-C.
Otherwise, press ENTER to go ahead and make the account.
• <Enter>
Adding and removing usersCreating new account...• Changing the user information for john• Enter the new value, or press ENTER for the
default• Full Name []: John Doe• Room Number []: <Enter>• Work Phone []: <Enter>• Home Phone []: <Enter>• Other []: <Enter>
Adding and removing usersChanging password for johnEnter the new password (minimum of 5, maximum of
127 characters)Please use a combination of upper and lower case
letters and numbers.• New password: password• Re-enter new password: password
Change a password passwd
The passwd command is used to set a password for a user. Running this command as a user without a parameter will change the password for this user.
The password command will ask for the old password, once and twice for the new password:
$ passwdThe root user can set passwords for all users. The
passwd command will only ask for the new password.
# passwd username
Remove a user accountuserdel
Linux offers the userdel tool to remove a user account. Just specify the username as a parameter to remove that user from the system.
# userdel bob
This will only remove the user account, not the user’s home directory and mail spool. Just add the -rparameter to delete the user’s home directory and mail spool too.
# userdel -r bob
SU commandIt is a good idea to avoid logging in as root. There are many reasons for not doing this. Fortunately the su can give you temporal root
privileges.Example:
$ whoamibob$ suPassword:# whoamiroot# exit$ whoamibob
Adding and removing groupsThe programs to add and remove groups are very
simple: groupadd will just add another entry to the /etc/group file with a unique group ID, while groupdel will remove the specified group.
You create a group like so:# groupadd cvs
And remove it like so:# groupdel cvs
By handOf course, it is possible to add, modify, and remove
users and groups by hand.
First, we'll add a new user to the /etc/passwd, /etc/shadow, and /etc/group files.
After that use the passwd command to create a new password for the user.
Finally, use mkdir to create the new user's home directory (/home/username or in the location you entered into the /etc/passwd file).
Example • Find and show files:/etc/passwd, /etc/shadow,
and /etc/group files.• Check content of /home directory• Add new user (stud1, users group)• Again find and show files:/etc/passwd,
/etc/shadow, and /etc/group files and notice diference.
• Check again content of /home directory.• Change the new user password.• Remove the new user account and home
directory
Shutting Down ProperlyIt is very important that the system is shut down
properly. Simply turning the power off can cause serious filesystem damage.
While the system is on, files are in use even if you aren't doing anything. Remember that there are many processes running in the background all the time.
So, when you go to reboot or power down your computer, it is important to do so the right way.
Shutting Down ProperlyThe first method is through the shutdown program,
and it is probably the most popular.Basic usage of shutdown to turn off the computer is:
# shutdown -h now (-h +60)
Rebooting the system uses the same command, but substitutes “-r” for “-h”:
# shutdown -r now
The second way of shutting down or powering off the computer is to use the halt and reboot
commands.
The /etc/rc.d directory
The system initialization files are stored in the /etc/rc.d directory.
Each task or runlevel is given its own rc file.
There are several categories of initialization files:- system startup, - runlevels, - network initilization, - and System V compatibility.
System StartupThe first program to run under Slackware besides the
Linux kernel is init. This program reads the /etc/inittab file to see how to
run the system. It runs the /etc/rc.d/rc.S script to prepare thesystem before going into your desired runlevel.
The rc.S file enables your virtual memory, mounts your filesystems, cleans up certain log directories, initializes Plug and Play devices, loads kernel modules, configures PCMCIA devices, sets up serial ports, and runs System V init scripts (if found).
Runlevel Initialization ScriptsAfter system initialization is complete, init moves on to
runlevel initialization. A runlevel describes the state that your machine will be running in (the runlevel tells init if you will be accepting multiuser logins or just a single user, whether or not you want network services, and if you will be using the X Window, …).
The files below define the different runlevels in SlackwareLinux:
rc.0 - Halt the system (runlevel 0). By default, this is symlinked to rc.6.
rc.4 - Multiuser startup (runlevel 4), but in X11 with KDM, GDM, or XDM as the login manager.
Runlevel Initialization Scripts
rc.6 - Reboot the system (runlevel 6).
rc.K - Startup in single user mode (runlevel 1).
rc.M - Multiuser mode (runlevels 2 and 3), but with the standard text-based login. This is the default runlevel in Slackware.
Network InitializationRunlevels 2, 3, and 4 will start up the network
services. The following files are responsible for the network
initialization:rc.inet1- Created by netconfig, this file is
responsible for configuring the actual network interface.
rc.inet2- Runs after rc.inet1 and starts up basic network services.
rc.httpd- Starts up the Apache web server.rc.samba- Starts up Windows file and print sharing
services.rc.news - Starts up the news server.
Other Filesrc.cdrom - If enabled, this script will scan for a CD-
ROM in a drive and mount it under /cdrom if it finds one.
rc.gpm - Starts up general purpose mouse services. Allows you to copy and paste at the Linux console.
rc.font - Loads the custom screen font for the console.
rc.local - Contains any specific startup commands for your system. This is empty after a fresh install, as it is reserved for local administrators. This script is run after all other initialization has taken place.
Enabling/disabling scripts
Automatic:
To enable a script, all you need to do is add the execute permissions to it.
To disable a script, remove the executepermissions from it.
Manual:rc.name start
rc.name stop
Networking configurationDrivers for NICs are installed as kernel modules.
The module for your NIC has to be loaded duringthe initialization of Slackware Linux. On most systems the NIC is automatically detected
and configured during the installation of SlackwareLinux.
You can reconfigure your NIC with the netconfigcommand.
The netconfig adds the driver (module) for the detected card to /etc/rc.d/rc.netdevice.
netconfig
Configuration of interfaces
Network cards are available under Linux through so-called “interfaces”.
The ifconfig command can be used to display the available interfaces details:
# ifconfig –a
Network cards get the name ethn, in which n is a number, starting with 0.
Interfaces can be configured in the /etc/rc.d/rc.inet1.conf file.
You can simply read the comments, and fill in the required information.
ResolvingEach computer on the internet has a hostname and
IP address-.
/etc/hosts is a table of IP addresses with associated hostnames. This file can be used to name computers in a small network.
An example of the /etc/hosts file:
127.0.0.1 localhost192.168.1.1 tazzy.slackfans.org tazzy192.168.1.2 gideon.slackfans.org
ResolvingThe /etc/resolv.conf file is used to specify which
nameservers the system should use.
A nameserver converts hostnames to IP addresses.
Your provider should have given you at least twonameserver addresses (DNS servers).
You can add these nameservers to /etc/resolv.confby adding the line nameserver ipaddress for each nameserver.
For example:nameserver 192.168.1.1nameserver 192.168.1.69
The internet super serverThere are two ways to offer TCP/IP services: by
running server applications standalone as a daemon or by using the internet super server, inetd. The inetd is a daemon which monitors a range of ports.
If a client attempts to connect to a port inetd handles the connection and forwards the connection to the server software which handles that kind of connection.
The inetd can be configured using the/etc/inetd.conf file.
The internet super serverFor example: How loaded FTP server?
Let’s have a look at an example line (for FTP) frominetd.conf:
# File Transfer Protocol (FTP) server:# ftp stream tcp nowait root /usr/sbin/tcpd proftpd
This line specifies that inetd should accept FTP connections and pass them to tcpd.
So, just remove # and reenable initd or reboot host!
Apache – web serverApache is the most popular web server since 1996.
Apache can be installed automatically by adding the apache package from the “n” disk set.
After installing Apache it can be started automatically while booting the system by making the /etc/rc.d/rc.httpd file executable.
The Apache configuration can be changed in the /etc/apache/httpd.conf file.
Apache can be stopped/started/restarted every moment with the apachectl command, and the stop, start and
restart parameters. For example, the command to restart Apache:
# rc.httpd start/stop/restart# apachectl restart