Professional Documents
Culture Documents
Linux Notes For BS Computer Science
Linux Notes For BS Computer Science
What is OS: -
“OS provides interface between user and hardware.”
Parts of OS: -
OS divides into two parts.
1. Environment
2. Kernel
Environment GUI
Text Based
Hardwar
e
Kernel Functions: -
Interrupts Types: -
In Windows OS: -
Scandisk utility is used to check the file system. This utility only
checks the lost file address (crash dump files) and fix it only and no
recovery is done.
Note: -
In Solaris, UNIX and Linux we can recover crash dump files, on the
other hand in Windows OS recovery is not done, it only fix the lost
memory address files.
Types of Shells: -
C Shell (CSh):-
CSh is used to develop c or java development tasks (development purposes).
Shell Features: -
1. Job Controlling
2. Command Line Editing
3. History File
4. File Name Completion
5. Alias
Command Line Editing, History File, File Name Completion and Alias
are the features of Korne Shell (KSh) and Bourne Shell (BSh).
1. Job Controlling: -
This feature provides the facility to edit the command with the help of
back arrow key.
3. History File: -
5. Alias: -
Alias Command: -
Syntax:
alias alias-name = “command”
Example:
alias psa = “ps -aux | more”
Shell Modes: -
# bsh
6
# exit
Logout Command: -
# logout
7
Mounting
CD Access or CD mount
Floppy Access or Floppy mount
All storage devices are called block devices.
All block devices are mounted.
Mount means attach a file system or transfer file system
/mnt/cdrom is called mount point
Mounting: -
“Mounting is a process through which file system is attached with block
devices.”
Mount Command: -
Syntax:
mount device-name mount-point
Example:
Mount Point: -
Mount point is the location where the data can be read or write.
Floppy: -
Unmount: -
In unmount, OS release the resources. For this purpose umount command is
used to release the resources occupied.
Umount Command: -
Syntax:
umount device-name mount-point
Example:
umount /dev/cdrom /mnt/cdrom
Eject: -
1. umount
2. eject
8
Eject command: -
Syntax:
eject
Partition Mount: -
1. # fdisk -l
2. # cd /mnt
3. # mkdir windows
4. # mount /dev/hda3 /mnt/windows/
9
Basic Commands
# ls
# ls -a
# ls -l
# ls -al
# man ls
6. Clear Screen: -
# clear
10
# touch file-name
8. Make Directory: -
# mkdir directory-name
9. Change Directory: -
# cd directory-name
# cd ..
In some commands space is not necessary.
Path: /root/redhat
Relative Path: -
# cd root
# cd redhat
# cd ..
# cd ..
Absolute Path: -
# cd /root/redhat
# cd / (direct switch)
# pwd
11
# cp source-path desti-path
# cp abc redhat/
# cp -r redhat test
# rm abc
# rm -r redhat
# mkdir redhat
# touch abc
# mv abc redhat/
Note: -
If we want to move any directory then in this case no flag will be used.
Note: -
In text mode, ctrl+z is used to minimize the current file.
# cat filename
# jobs
# fg %1
# less filename
# head filename
# head -15 filename // flag for first 15 lines
# tail filename
# tail -15 smb.conf
28. | (concatenation): -
# ls | less
# locate smb.conf
It shows the path & details of the search file, in the complete system.
Search speed is very fast.
# ls | grep passwd
14
# ps -aux | more
# vi filename
To write:-
Press insert key to insert mode.
Then after insertion write
To exit first press escape key (Esc), to remove insert mode
Then :wq! Where w is for write, q is for quit (with save)
:q! is used to quit only (without save)
pico package must be installed for vi
15
Directory Structure
root partition (/)
User based executable files are stored in bin. During booting process OS
initialized a user bin files. i.e. ksh, bsh, csh. All shells are stored in bin.
/bin/csh
/bin/ksh
All executable files which is loaded for OS are stored in sbin directory
(during booting), i.e. fsck.
/sbin/fsck
/dev/cdrom
/dev/hda1
/dev/fd0
4. home:-
All users’ accounts that we create, are stored in home directory. All
users’ home directories are stored in home directory.
lib is used for all executed files syntax verification. All executed files
information is stored in lib directory.
16
Other than root, all accounts are stores in home directory. Root user’s
home directory is stored in root directory.
9. usr:-
All OS commands ( after boot executable files) are stored in bin and
sbin directories which are present in usr directory. All user commands are
stored in bin directory, and all administration commands are stored in sbin
directory.
10. boot:-
OS based booting files are stored in boot directory, these files are
initialized at boot time, i.e., grub, vmlinuz etc.
11. etc:-
12. lost+found:-
All crash dump files are stored in lost+found. All partitions have their
own lost+found directory.
OS all temporary files are stored in tmp directory, i.e., job scheduling
is a temporary file.
Administration Track
1. User Administration
2. System Administration
3. Network Administration
1. User Administration:-
Account Types:
a. System Accounts
b. Custom Accounts
1. useradd
2. adduser
# useradd user-name
# useradd lab
1. user id (uid)
2. user group (group)
3. create home directory (homedir)
4. shell assignment (shell)
19
1. User ID (uid):-
First uid is started from 500 for the custom account. The
uid reserved for the custom accounts range from 500-60,000. And uid
reserved for the system accounts range from 0-499.
uid = 500
By default the name of the user will by the name of the group.
When ever a new user is created, then the group of that user will be
also created with the name of the user.
group = lab
HomeDir = /home/lab
shell = /bin/bash
uid = 500
group = lab
HomeDir = /home/lab
shell = /bin/bash
# id user-name
# id lab
a. New Account
b. Existing Account
Use the below flags with the useradd command to change the new user’s
default configurations.
Flag Description
Flag Description
# userdel user-name
# userdel test
home directory does not delete but the user will be deleted.
If we want to delete user’s home directory with the user, then we will use –r
flag with the userdel command.
# userdel -r test
22
In windows user account information is stored in the “sam” file. But in Linux
user account information is stored in the “passwd” file. Passwd file is present in
the /etc directory (/etc/passwd).
when this command execute, passwd file opens in read mode and shows the all
users’ account informations.
* if we do not want the password of any user, then edit the passwd file and delete the
x from the specified user.
# passwd user-name
# passwd lab1
after entering new password, confirm password appears to validate the new
password.
23
Only root can use user-name with passwd command, users can’t use the user-
name.
Users’ passwords are stored in shadow file, which is stored in etc directory. All
passwords are in corrupted form.
# more /etc/shadow
in shadow file
!! blank password.
$1$ password assigned
* password cannot be set.
!!$1$ password is locked
Account/password Lock
(Temporary)
Syntax:
# passwd -l user-name
Example:
# passwd -l lab1
Account/Password Unlock
# passwd -u lab
Creating a Group
Syntax:
# groupadd group-name
Example:
# groupadd finance
# groupadd tech
Delete a Group
Syntax:
# groupdel group-name
To delete a member of a group, edit the /etc/group file, and delete the
member from the file.
25
Types of Permissions:-
There are two types of permissions
1. Local Permissions
2. Network Permissions
1. Local Permissions:-
Within a system or standalone system, permissions applied are called
local permissions.
2. Network Permissions:-
Over the network, permissions applied on a system are called network
permissions, i.e., shared device permissions.
Local Permissions
1. Read ( r )
2. Write ( w )
3. Execute ( x )
# ls -l
First segment contains only the one slot (first slot only).
Second segment contains three slots (next three slots).
Third segment contains three slots (next three slots of remaining).
Forth segment contains also three slots (last three slots).
26
1st Segment:-
2nd Segment:-
3rd Segment:-
4th Segment:-
4th segment shows the others permissions, other than owner’s primary group
permissions. Others are also called world users.
# ls -l file-name
# ls -l abc
27
1. Identifier Method
2. Number System Method
1. Identifier Method:-
There are three permissions read, write and execute are available in
Linux.
Actions:-
Syntax:
Example:-
This will add a owner’s primary group permission execute on abc file.
Problem:-
Give the read, write and execute permissions to the owner, give read
and write to primary group and give read permissions to the others.
28
Owner = rwx
Group = rw_
Others = r_ _
Solution:-
output:-
_rwxrw_r_ _
r=4
w=2
x=1
4+2+1 = 7
Example:-
Example:-
Change Ownership:-
Syntax:
# chown user-name file-name
Example:
#chown adnan abc
# id adnan
Change Group:-
Syntax:
# chgrp group-name file-name
Example:
# chgrp tech abc
Single Command:-
Syntax:
# chown user-name : group-name file-name
Example:
# chown adnan : tech abc
It is used on that directory or file on which everyone has full control. i.e. ,
tmp directory.
30
Delete permission is not given. One user cannot delete other user’s files.
1 is used for numeric value of sticky bit.
Its symbol is “t”. For example, -rwxrwxrwt.
If we want to add a sticky bit on the data directory, then we will execute the
following command.
Default Permissions
# umask
# umask 002
31
Configuring NFS
Let's inspect the contents of the /etc/exports file, which serves as the configuration file for
NFS exports. Notably, lines commencing with the hash symbol (#) are comments and
hold no operational significance. In this instance, the file appears to be empty, indicating
that there are currently no specific NFS export configurations in place.
To navigate and examine the contents, use the following command:
# cat /etc/exports
33
To edit the NFS export configurations, we will employ the Nano text editor. Execute the
following command:
# sudo nano /etc/exports
This command opens the /etc/exports file in the Nano text editor, allowing you to add,
modify, or remove NFS export configurations.
Now that we've configured NFS shares, it's imperative to restart the NFS Server for the
changes to take effect. Following that, we will inspect the status to ensure smooth
operation.
Restart the NFS Server:
# sudo systemctl restart nfs-kernel-server
Verify the NFS Server status:
# sudo systemctl status nfs-kernel-server
This ensures that the recent configurations are active and the NFS Server is running
seamlessly.
Next, navigate to the /exports directory and create two text files. Populate each file with
content, save the changes, and exit. This can be accomplished with the following
commands:
# cd /exports
# sudo nano backup/test1.txt
Enter the desired content, save the file (Ctrl + O, Enter, Ctrl + X), and repeat the process
for the second file.
35
To confirm the directories shared by the NFS Server, execute the following command:
# showmount --exports <NFS_Server_IP>
Replace <NFS_Server_IP> with the actual IP address of your NFS Server. This
command will provide a list of directories being shared by the server.
The /mnt directory is a system standard and may already exist on your file system,
eliminating the need to create it.
These directories are presently empty as no mounts have been initiated. They serve as the
designated mount points for the NFS shared directories.
# ls -l /mnt/nfs/backup
This command provides a comprehensive listing of files and subdirectories within the
/mnt/nfs/backup directory, presenting details such as permissions and modification time.
Upon execution, the output will showcase any existing files and subdirectories. Confirm
the presence of the test1.txt file, which was created as a test on the NFS Server. The
contents of this file confirm the successful functionality of NFS.
Unmounting the previously mounted NFS exports is a standard practice once the required
tasks are completed and access to the shared directories is no longer needed. To initiate
the unmounting process, please execute the following commands:
For the backup directory:
# sudo umount /mnt/nfs/backup
For the documents directory:
# sudo umount /mnt/nfs/documents
Upon successful execution of these commands, the specified NFS exports, namely the
backup and documents directories, will be unmounted.
Configuring Apache
Introduction
Apache HTTP Server is a free and open-source web server that delivers web content through the
internet. It is commonly referred to as Apache and after development, it quickly became the most
40
popular HTTP client on the web. It’s widely thought that Apache gets its name from its
development history and process of improvement through applied patches and modules but that
was corrected back in 2000. It was revealed that the name originated from the respect of the
Native American tribe for its resiliency and durability.
Prerequisites
Before you begin exploring your Apache configurations,
you should have Apache installed on your server.
Apache on Ubuntu:
Open a terminal window and type the following commands:
$ sudo apt update
$ sudo apt install apache2
During the installation process, you may be prompted to confirm by typing 'Y' and pressing
Enter.
If Apache is running, you should see an output indicating that the service is active and running.
There are a number of plaintext files and some subdirectories within this directory. Here are some
useful locations to be familiar with:
apache2.conf: This is the main configuration file for the server. Almost all
configuration can be done from within this file, although it is recommended to use
41
separate, designated files for simplicity. This file will configure defaults and be the
central point of access for the server to read configuration details.
ports.conf: This file is used to specify the ports that virtual hosts should listen
on. Be sure to check that this file is correct if you are configuring SSL.
sites-available/ and sites-enabled/: The sites-
available directory contains virtual host file configurations. Configurations
within this folder will establish which content gets served for which requests. This is
enabled through linking to the sites-enabled directory, which stores activated
virtual host configuration files. When Apache starts or reloads, it reads the
configuration files and links from within the sites-enabled directory as it
compiles a full configuration.
conf-available/ and conf-enabled/: These directories house configuration
fragments that are unattached to the virtual host configurations files.
mods-enabled/ and mods-available/: These directories define modules that
can be optionally loaded. The directories contain two components: files ending
in .load, which contain fragments that load particular modules, and files ending
in .conf, which store the configurations of these modules.
Apache configuration does not take place in a single monolithic file, but instead happens through
a modular design where new files can be added and modified as needed.
Open this file with your preferred text editor. The following example uses nano:
In Ubuntu and Debian, this file is used to configure global definitions. The configuration of the
default server and virtual hosts are handled by using the Include directive.
The Include directive allows Apache to read other configuration files into the current file at the
location that the statement appears. The result is that Apache dynamically generates an
overarching configuration file on startup.
Found within this file are a number of different Include and IncludeOptional statements.
These directives load module definitions, the ports.conf document, the specific configuration
files in the conf-enabled/ directory, and the virtual host definitions in the sites-
enabled/ directory:
42
Global Configurations
There are some options you may want to modify in the Global Configuration:
Timeout
By default, this parameter is set to 300. This means that the server has a maximum of 300
seconds to fulfill each request. This parameter can safely be dropped to something
between 30 and 60 seconds.
KeepAlive
This option, if set to On, will allow each connection to remain open to handle multiple requests
from the same client. If this is set to Off, each request will have to establish a new connection,
which can result in significant overhead depending on your setup and traffic situation.
MaxKee AliveRequests
This controls how many separate requests each connection will handle before dying. Keeping this
number high will allow Apache to serve content to each client more effectively. The default
setting is set to 100. Setting this value to 0 will allow Apache to serve an unlimited amount of
requests for each connection.
KeepAliveTimeout
This setting specifies how long to wait for the next request after finishing the last one. If the
timeout threshold is reached, then the connection will die. This means that the next time content
is requested, the server will establish a new connection to handle the request for the content that
makes up the page the client is visiting. The default is set to 5.
After examining the contents of this configuration file, you can close out of it by
pressing CTRL+X.
43
The default virtual host is configured to handle any request on port 80, the standard HTTP port.
This is defined in the declaration header where it says *:80, meaning port 80 on any interface.
However, this does not mean that it will necessarily handle each request to the server on this port.
Apache uses the most specific virtual host definition that matches the request. If there was a more
specific definition, it could supersede this definition. After examining the file, you can close out
of it by pressing CTRL+X.
This file contains the Server Signature directive, which allows you to specify a contact
email that should be used when there are server problems. You can change the default option
44
from On to EMail to reveal the server admin email address. Make sure you are willing to receive
the mail if you adjust this setting:
Exit the file by pressingCTRL+X. After editing a configuration file, a prompt will ask you to
confirm your changes. Press Y to save the changes to your file or press N to discard them.
Within your virtual host file, you can add a ServerName directive that specifies the domain
name or IP address that this request should handle. This is the option that would add specificity to
the virtual host, allowing it to override the default definition if it matches
the ServerName value.
Run the following command to open your virtual host file, making sure to replace
the your_domain variable with your actual domain name:
Likewise, you can also make the virtual host apply to more than one name by using
the ServerAlias directive. This provides alternate paths to get to the same content. A good
use case for this is adding the same domain, preceded by www:
45
The DocumentRoot directive specifies where the content that is requested for this virtual host
will be located. On Ubuntu, the default virtual host is set up to serve content out of
the /var/www/ directory:
Directory Definitions
Within the virtual host definition, there are definitions for how the server handles different
directories within the file system. Apache will apply all of these directions in order from shortest
to longest, so there is again a chance to override previous options.
Open the apache2.conf file with this command:
46
The first directory definition applies rules for the /, or root, directory. This will provide the
baseline configuration for your virtual host, as it applies to all files served on the file system.
Notice the directory configuration options, along with some helpful comments, contained within
this file. This default configuration denies access to all content unless specified otherwise in
subsequent directory definitions.
The Require directive can restrict or open access to different resources within your server.
The AllowOverride directive is used to decide whether an .htaccess file can override
settings if it is placed in the content directory. This is not allowed by default, but can be useful to
enable in a variety of circumstances. After examining the contents of this file, you can close out
of it by pressing CTRL+X.
The Alias directive maps a URL path to a directory path. For example, in a virtual host that
handles requests to your_domain the following would allow access to content
47
The ScriptAlias directive operates in the same way, but is used to define directories that will
have executable components in them:
Remember to define the directory with access privileges as discussed in the previous section.
After completing your edits on the file, exit the file by pressing CTRL+X. If you made any
changes to this file, press Y to save the changes to your file or press N to leave the file as it was
before any changes to the configuration.
After enabling a site, issue the following command to tell Apache to reload its configuration files,
allowing the change to propagate:
48
There is also a companion command for disabling a virtual host. It operates by removing the
symbolic link from the sites-enabled directory. For example, with your virtual host site
enabled, you can disable the default 000-default site:
Modules can be enabled or disabled by using the a2enmod and a2dismod commands
respectively. They work in the same way as the a2ensite and a2dissite versions of these
commands. For example, to enable the info module, you can use the following command:
Remember to restart Apache after modifying configuration files and enabling or disabling
modules.
Conclusion
Apache is versatile and very modular, so configuration needs will be different depending on your
setup. After reviewing some general use cases above, you should have a good understanding of
what the main configuration files are used for and how they interact with each other. If you need
to know about specific configuration options, the provided files are well commented and Apache
provides excellent documentation. Hopefully, the configuration files will not be as intimidating
now and you’ll feel more comfortable experimenting and modifying to suit your needs.
Source: Documentation: Apache HTTP Server - The Apache HTTP Server Project
Configuring Samba
Most Linux package managers have Samba in their default repository. To configure
Samba on Ubuntu, follow the steps below:
2. Next, use apt to install Samba. Include the -y argument to auto-approve any queries
during the installation:
Command: $ sudo apt install samba -y
The output prints the directory containing Samba. Another method of verifying the
installation is by checking the Samba version:
Command: $ samba -v
The output shows that the system installed Samba version 4.16.4.
The output shows that the smbd service is enabled and running.
Note: Certain settings in the smb.conffile are marked as comments. To enable and tweak
those settings, uncomment them.
51
Note: To set the workgroup settings on Windows 10, open the Control Panel and access
the System and Security settings. The workgroup parameter is under the System section.
To configure the identification settings, uncomment the workgroup and server string
parameters and add these values:
workgroup = WORKGROUP
server string = samba_server
Networking
Use the Networking subsection to configure network interfaces that Samba binds to.
Networking contains two parameters:
• The first parameter, interfaces, sets the network interface for Samba to bind to.
• The second parameter, bind interfaces only, ensures that Samba only binds to the
interfaces listed in the file. The parameter should always be set to yes.
To set the interfaces parameter, first check the available interfaces with the ip command:
Command: $ ip link
52
The example output indicates Samba binds to two interfaces: Io, the loopback interface,
and enpOs3, the network interface.
Debugging
The Debugging subsection has four parameters. Set them as follows:
log file = /var/log/samba/log.%m
max log size = 1000
logging = file
panic action = /usr/share/samba/panic-action %d
2. Do not change any settings in the Domain subsection, but scroll down to Misc and set
the following:
usershare allow guests = yes
54
3. Save and exit the file and run the Samba utility testparm to check for syntax errors:
Command: $ testparm
The output shows the Loaded services file 0K message, which signifies no syntax errors.
With Global Settings configured, the Samba server is ready to use.
Still, not configuring the users and the directory limits the Samba functionality.
Note that the username should belong to a system user. For instance, in this example, the
system account on the Ubuntu system is saraz. Hence, the username is the same:
3. After entering and confirming the system password for new_user, create a new Samba
user with:
Command: $ sudo smbpasswd -a new_user
Next, both users need to have read, write and execute access to the sharing directory.
However, saraz has these permissions by default. On the other hand, new_userdoes not.
4. To grant read, write, and execute permissions to the sharing directory, run setfacl:
Command: $ sudo setfacl -R -m "u:new_user:rwx" /home/sharing
The command doesn't produce any output.
Each line grants specific permissions to access the directory. For instance:
[sharing}. Represents the directory name. This is the directory location Windows users
see.
comment. Serves as a shared directory description.
path. This parameter specifies the shared directory location. The example uses a
directory in /home, but users can also place the shared files under /samba.
read only. This parameter allows users to modify the directory and add or change files
when set to no.
writeable. Grants read and write access when set to yes.
browsable. This parameter allows other machines in the network to find the Samba
server and Samba share when set to yes. Otherwise, users must know the exact Samba
server name and type in the path to access the shared directory.
56
guest 0k. When set to no, this parameter disables guest access. Users need to enter a
username and password to access the shared directory.
valid users. Only the users mentioned have access to the Samba share.
The output confirms that the Samba is adequately configured. For a more verbose output,
hit enter:
3. Type the following into the Enter server address... box and select Connect:
Command: $ smb://ip-address/sharing
58
Comparative analysis of OS
Windows:
Important Attributes:
User Interface: Graphical User Interface (GUI) with the Start menu.
Compatibility: Extensive compatibility with a wide range of software and hardware.
Market Share: Dominates the desktop environment.
Key Features:
Pros:
Compatibility: Vast compatibility with software and hardware.
59
Cons:
Linux:
Important Attributes:
Open Source: Linux is opensource, allowing users to modify and distribute their
versions.
Diversity: Various distributions (distros) catering to different needs.
Key Features:
Security: Known for robust security features.
Stability: Generally stable and reliable, with long uptimes.
Customization: High degree of customization and flexibility.
Pros:
Cons:
Comparison:
Windows vs. Linux:
1. Cost:
Windows: Typically involves licensing costs.
Linux: Generally free and opensource.
2. User Interface:
Windows: GUI driven with a familiar desktop environment.
Linux: Diverse, with options for GUI and command line interfaces.
3. Software Availability:
Windows: Abundance of commercial software.
Linux: Growing availability; some proprietary software may be lacking.
4. Security:
60
5. Customization:
Windows: Limited compared to Linux.
Linux: Highly customizable, allowing users to tailor the system to their needs.
6. System Resources:
Windows: Generally, requires more system resources.
Linux: Can run on older or less powerful hardware.
7. Usage:
Windows: Dominant in desktop environments.
Linux: Widely used in servers, embedded systems, and increasingly on desktops.
Ultimately, the choice between Windows and Linux depends on specific requirements,
preferences, and use cases. Windows is often preferred for its user-friendly interface and
extensive software support, while Linux excels in customization, security, and resource
efficiency.
Configuring DHCP
Configuring DHCP Server on ubuntu
DHCP(dynamic host configuration protocol) server is used to assign dynamic IP
addresses to other machines in the network.
We can assign ips in 2 different ways, either manually or through DHCP. DHCP
server is very useful in large networks where a lot of machines are there. So, it assigns
unique IP addresses to every machine.
Update the repositories by using this command
# sudo apt update
This command should update repositories
61
To install the server, we would need the package which is called isc-dhcp-server.
To install this package type:
# sudo apt install isc-dhcp-server
This should install the server on your system.
The enxt step is to navigate to directory where configuration files are present.
62
Here we will create our own configuration file, before that we will create a backup of the
default file so we can restore it incase of a mistake.
# sudo cp dhcpd.conf dhcpd.conf.backup
Now let’s create a new configuration file
# sudo touch dhcpd.conf
#ip addr
And you should see the list of all network interfaces which are available.
64
The status you will see that it fails, there are usually two reasons, the firewall issue and
the network interface does not have an ip of itself.
We need to allow port 67 on the system
# ufw allow 67/udp
#ufw enable
65