2-3 linux lab
2-3 linux lab
$ ls
file1.txt file2.txt directory1 directory2
o To show hidden files (those starting with a dot):
bash
$ ls -a
3. cd (Change Directory)
o Changes the current working directory.
bash
$ cd /path/to/directory
o To go to the home directory:
bash
$ cd ~
o To go up one level in the directory structure:
bash
$ cd ..
4. mkdir (Make Directory)
o Creates a new directory.
bash
$ mkdir new_directory
5. rmdir (Remove Directory)
o Removes an empty directory.
bash
$ rmdir directory_name
6. rm (Remove)
o Removes a file or directory.
bash
$ rm file_name.txt
o To remove a directory and its contents:
bash
$ rm -r directory_name
7. cp (Copy)
o Copies files or directories from one location to another.
bash
$ cp source_file.txt destination_directory/
o To copy a directory:
bash
$ cp -r source_directory/ destination_directory/
8. mv (Move)
o Moves or renames files and directories.
bash
$ mv old_file_name.txt new_file_name.txt
o To move a file to another directory:
bash
$ mv file.txt /path/to/destination/
9. touch (Create an Empty File)
o Creates an empty file or updates the timestamp of an existing file.
bash
$ touch newfile.txt
10. cat (Concatenate)
o Displays the contents of a file.
bash
$ cat file_name.txt
o To create a new file and add text to it:
bash
$ ps
o To see all processes:
bash
$ ps aux
16. top (Task Manager)
o Displays real-time information about running processes and system
resources.
bash
$ top
17. kill (Terminate Process)
o Terminates a process by its process ID (PID).
bash
$ kill PID
18. df (Disk Free)
o Displays information about disk space usage.
bash
$ df -h
19. du (Disk Usage)
o Shows disk usage for files and directories.
bash
$ du -sh directory_name
20. grep (Search)
o Searches for a specific pattern or text in files.
bash
$ history
23. wget (Download Files)
o Downloads files from the web.
bash
$ wget http://example.com/file.zip
24. curl (Transfer Data)
o Transfers data to or from a server (often used for APIs).
bash
$ curl http://example.com
25. sudo (Superuser Do)
o Executes commands as the superuser (root).
bash
$ ls | grep ".txt"
Example: Using Redirection
Redirection (>, >>) allows you to write the output of a command to a file.
Example: Save the output of a command to a file:
bash
$ ls > file_list.txt
Example: Append output to an existing file:
bash
$ sudo command
This will prompt for your password (the password of the current user) and, if correct, execute
the command with root privileges.
o Example:
bash
$ sudo -i
3. Logging in as Root (directly):
o If the root account is enabled, you can switch to root directly by using:
bash
$ su -
o It will ask for the root password.
Note: Many modern Linux distributions disable direct root login and recommend using sudo
for security reasons.
$ su -
o The - option makes the shell behave as if it was a login shell, setting the
environment variables as if you logged in as root.
2. Switch to Another User:
o To switch to another user without using root, simply run:
bash
$ su username
o You will be prompted to enter the password of the user you're switching to.
3. Exit Root User or Other User:
o Once you're finished using su, type exit to return to the previous user:
bash
$ exit
d. Administrative Commands
System administrators use many commands to manage users, processes, and system
configuration. Here are some important ones:
1. useradd - Add a new user.
bash
$ ps aux
8. top - Monitor system processes in real-time.
bash
$ top
9. df - Display disk space usage.
bash
$ df -h
10. du - Display disk usage for files and directories.
bash
$ du -sh /path/to/directory
11. systemctl - Manage system services (init systems).
bash
$ sudo ifconfig
14. ip - Used to manage network interfaces.
bash
$ ip a
15. shutdown - Shut down the system.
bash
$ sudo reboot
$ cat /etc/passwd
2. /etc/group - Contains group information for the system.
bash
$ cat /etc/group
3. /etc/fstab - Contains information about disk drives and partitions to be mounted at
boot.
bash
$ cat /etc/fstab
4. /etc/sudoers - Specifies the users and groups that have sudo privileges and their
permissions.
bash
$ sudo visudo
5. /etc/hostname - Contains the system's hostname.
bash
$ cat /etc/hostname
6. /etc/hosts - Maps IP addresses to hostnames.
bash
$ cat /etc/hosts
7. /etc/network/interfaces (Debian/Ubuntu-based) - Used to configure network
interfaces.
bash
$ cat /etc/network/interfaces
8. /etc/sysctl.conf - Used to configure kernel parameters at boot.
bash
$ cat /etc/sysctl.conf
9. /etc/crontab - Contains system-wide cron jobs.
bash
$ cat /etc/crontab
10. /etc/ssh/sshd_config - Configuration file for the OpenSSH server.
bash
$ sudo cat /etc/ssh/sshd_config
11. /etc/apt/sources.list (Debian/Ubuntu-based) - Contains the list of repositories used
by package managers like apt.
bash
$ cat /etc/apt/sources.list
Conclusion:
Becoming superuser (root) is a critical aspect of system administration in Linux,
either by using sudo or the su command.
Graphical tools like GNOME and KDE system settings can help simplify
administrative tasks.
Administrative commands allow you to manage users, processes, system services,
and perform routine maintenance tasks.
Configuration files in the /etc directory hold key system settings that need to be
modified for various administrative tasks.
By mastering these tools and commands, you can perform effective Linux system
administration tasks.
4.Configuring NICs with Network Device Configuration Utilities (ip and ifconfig)
ifconfig
You should see output similar to the following, showing interfaces like eth0, lo (loopback), or
other network devices on the system:
bash
ip addr show
Or, for a short version:
bash
ip a
The output will display similar information as ifconfig, but with more detailed and structured
information about each network interface.
Assign an IP Address to an Interface Using ip
To assign an IP address to an interface (for example, eth0), use:
bash
ip route show
Remove an IP Address
If you want to remove an IP address from an interface:
bash
ip route show
This will show the routes available on your system and the default gateway.
Configure Multiple IPs on a Single Interface
You can assign multiple IP addresses to the same network interface using ip:
bash
ip -s link
This will display statistics like received and transmitted packets, errors, etc., for each
network interface.
auto eth0
iface eth0 inet static
address 192.168.1.100
netmask 255.255.255.0
gateway 192.168.1.1
For Netplan (for newer versions of Ubuntu):
Edit the configuration in /etc/netplan/01-netcfg.yaml.
yaml
network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: no
addresses:
- 192.168.1.100/24
gateway4: 192.168.1.1
Apply changes with:
bash
DEVICE=eth0
BOOTPROTO=static
ONBOOT=yes
IPADDR=192.168.1.100
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
Restart the network service to apply changes:
bash
ip link show
Check Interface IP Configuration:
bash
ip addr show
Check Routing Table:
bash
ip route show
Check Connectivity with Ping:
To test the connectivity of a host:
bash
ping 192.168.1.1
Both ifconfig and ip utilities are used to manage network interfaces in Linux, but ip is more
modern and preferred for newer systems.
Use ifconfig for basic tasks like checking interfaces and setting static IPs (though it's
being deprecated in many Linux distributions).
Use ip for more advanced features and network configurations, such as routing,
setting up multiple IP addresses, and handling interface states.
# Define the subnet and IP range for the DHCP server to assign
subnet 192.168.1.0 netmask 255.255.255.0 {
range 192.168.1.100 192.168.1.200; # IP Range to Assign
option routers 192.168.1.1; # Default Gateway
option broadcast-address 192.168.1.255;
option domain-name-servers 8.8.8.8, 8.8.4.4; # DNS Servers
}
INTERFACES="eth0"
Step 4: Start and Enable the DHCP Server
Start the DHCP Server:
bash
sudo dhclient eth0 # Replace eth0 with your network interface name
This will automatically request an IP address from the DHCP server.
On CentOS/RHEL/Fedora:
On RHEL/CentOS/Fedora, the dhclient command works similarly:
bash
Copy
sudo dhclient eth0 # Replace eth0 with your network interface name
Step 2: Verify DHCP Client IP Assignment
After running the dhclient command, check the assigned IP address using:
bash
4. Troubleshooting
If your DHCP server or client isn't working as expected, check the following:
1. Check DHCP Server Logs:
o On Ubuntu/Debian: /var/log/syslog or /var/log/daemon.log.
o On CentOS/RHEL/Fedora: /var/log/messages.
2. Verify Firewall Rules: Make sure UDP ports 67 and 68 are open on both the server
and client. If using iptables, add the following rule:
bash
You have successfully installed and configured a DHCP server to dynamically assign IP
addresses to clients. Additionally, you’ve configured a DHCP client to automatically
receive an IP address from the server.This setup ensures that devices on your network can
easily join without needing manual IP address configuration, which is especially useful in
large environments. You can extend this setup to manage a more complex network
infrastructure, including reservations, static IPs, and additional configuration options.
Prerequisites
For this guide, we’ll use Ubuntu/Debian as the base system, but the steps for
CentOS/RHEL/Fedora are quite similar.
Postfix is a powerful and flexible mail transfer agent used to send and receive email on
Linux.
bash
Copy
During the installation, you'll be prompted to choose a configuration type for Postfix.
Choose "Internet Site" and set the mail name (e.g., mydomain.com).
bash
Copy
bash
Copy
sudo systemctl enable postfix
bash
Copy
bash
Copy
# Basic settings
myorigin = $mydomain
# Mail routing
smtpd_sasl_auth_enable = yes
smtpd_sasl_security_options = noanonymous
smtp_tls_security_level = may
smtpd_tls_security_level = may
If you need additional configuration options (like enabling TLS, virtual mailboxes, or
custom configurations), you can add those here.
bash
Copy
Dovecot is a high-performance IMAP and POP3 server used for receiving and storing
email.
bash
Copy
bash
Copy
bash
Copy
sudo systemctl enable dovecot
bash
Copy
bash
Copy
mail_location = maildir:~/Maildir
# Enable SSL
ssl = required
bash
Copy
bash
Copy
disable_plaintext_auth = no
bash
Copy
mail_location = maildir:~/Maildir
bash
Copy
Roundcube is a web-based email client that allows users to access their emails via a
browser.
bash
Copy
bash
Copy
sudo yum install roundcubemail
Roundcube requires a MySQL or MariaDB database to store user information. You can
create a new database for Roundcube.
bash
Copy
sql
Copy
FLUSH PRIVILEGES;
bash
Copy
php
Copy
$config['db_dsnw'] =
'mysql://roundcube:roundcube_password@localhost/roundcubemail';
php
Copy
$config['smtp_port'] = 587;
Roundcube requires a web server to serve its interface. We will configure Apache for this
purpose.
1. Install Apache:
bash
Copy
bash
Copy
bash
Copy
3. Restart Apache:
bash
Copy
sudo systemctl restart apache2
To make sure your mail server is accessible and delivers emails correctly, configure MX
(Mail Exchange) records in your DNS settings.
Example:
Copy
mail.mydomain.com. IN MX 10 mail.mydomain.com.
2. SPF Record: A Sender Policy Framework (SPF) record helps prevent email spoofing.
Example:
arduino
Copy
3. Reverse DNS Record: Set up a reverse DNS record for your mail server IP to ensure
email deliverability.
Once everything is set up, it's important to test the mail server's functionality.
You have successfully installed and configured a Mail Server with Postfix (MTA),
Dovecot (IMAP/POP3), and Roundcube (Webmail Interface). The setup includes:
This configuration will allow users to send, receive, and manage their emails through
various means: via a mail client (IMAP/SMTP), via the web (Roundcube), or through the
terminal using tools like mail.
A firewall is an essential security tool for managing network traffic and protecting your
system from unauthorized access. In Linux, the Netfilter framework, combined with
iptables, is commonly used to configure firewalls.
This guide will help you set up a simple firewall using iptables to secure a Linux system.
1. Install iptables.
2. Configure basic firewall rules.
3. Set up default policies to secure the network.
4. Allow/deny specific network traffic.
5. Save the firewall rules to persist after reboot.
Prerequisites:
In most modern Linux distributions, iptables is installed by default. However, you can
check if it's available and install it manually if needed.
On Ubuntu/Debian:
bash
bash
bash
The iptables command allows you to filter network traffic based on various parameters
such as IP address, port, and protocol.
First, set the default policy for the INPUT, OUTPUT, and FORWARD chains to DROP.
This means all traffic will be blocked unless explicitly allowed.
bash
We need to allow traffic that is part of an already established connection (e.g., a response
to a request made from your system).
bash
Allow incoming SSH (port 22) so that you can access the system remotely. This rule will
ensure you can log in to the server via SSH.
bash
Allow incoming HTTP (port 80) and HTTPS (port 443) traffic for serving websites. If
your system will be hosting a web server, you should allow these ports.
bash
Allow traffic to/from the loopback interface (localhost) for local processes. This is
necessary for internal services to communicate.
bash
You can log dropped packets for debugging purposes, so you can see what traffic is being
blocked. Be cautious with logging too many packets as it can generate a large log file.
bash
The iptables rules defined above will not persist after a reboot unless explicitly saved.
The process of saving iptables rules depends on your distribution.
On Ubuntu/Debian:
bash
2. After installing, you will be prompted to save your current iptables rules. You can
also manually save the rules later:
bash
bash
On CentOS/RHEL/Fedora:
bash
bash
bash
Now that your basic firewall is configured, it’s time to test it.
bash
sudo iptables -L -v -n
This will display the list of rules, showing traffic statistics (how many packets have
matched each rule).
2. Test Connectivity:
o From another machine, try to ping your server.
o Test SSH access by attempting to connect to port 22.
o Test access to HTTP/HTTPS by accessing your server’s IP address or domain
in a web browser.
3. Test Blocked Ports: Try to access a port that is not allowed, such as port 23 (Telnet),
and verify that the connection is blocked.
bash
telnet <your-server-ip> 23
You can further customize the firewall by adding more advanced rules, such as limiting
connections, rate-limiting, or blocking traffic from specific IP addresses.
Limit SSH Connections (Rate Limiting)
To prevent brute-force attacks, you can limit the number of SSH connection attempts
from a single IP address:
bash
sudo iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --set
sudo iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --update --
seconds 60 --hitcount 5 -j REJECT --reject-with tcp-reset
The first rule adds IPs to the recent list when they try to initiate a new SSH
connection.
The second rule rejects IPs that try to initiate more than 5 SSH connections within 60
seconds.
bash
bash
To monitor the iptables firewall, you can use the following commands:
sudo iptables -L -v -n
bash
Or, on CentOS/RHEL:
bash
Conclusion
successfully configured a basic firewall using iptables on a Linux system to secure your
network. With iptables,
Set default policies (DROP all traffic, allow only specific traffic).
Allow essential services such as SSH, HTTP, and HTTPS.
Log traffic for debugging and auditing purposes.
Save and load firewall rules to persist after reboot.
Customize firewall rules with more advanced options like rate-limiting, IP
whitelisting, and blacklisting.
10.Setting Up Samba Server to Share Files and Printers with Windows-based and Other
Operating Systems
Samba is a powerful open-source software that allows Linux systems to interact with
Windows-based systems and other operating systems over the SMB (Server Message Block)
protocol. With Samba, you can share files, printers, and other resources between Linux and
Windows systems, enabling seamless cross-platform interaction.
setting up Samba on a Linux server to share files and printers with Windows and other
operating systems.
Prerequisites:
On Ubuntu/Debian:
bash
On CentOS/RHEL/Fedora:
bash
Once installed, the Samba services (smbd and nmbd) should automatically start. If not, you
can start them manually:
bash
Samba uses the configuration file /etc/samba/smb.conf to define its shares and settings.
bash
Give proper permissions so that Samba can access and share it:
bash
This grants full read/write/execute access to all users. You may adjust these permissions
according to your security needs.
Now, we need to edit the Samba configuration file to define the share.
bash
In the smb.conf file, under the [global] section, ensure the following parameters are
configured (you can add or adjust the settings if needed):
ini
[global]
map to guest = bad user # Allow guest login for users who don't exist
dns proxy = no # Disable DNS proxy
ini
[shared]
You can modify the share's properties, such as allowing access to certain users or limiting it
to read-only, by adjusting the configuration file as needed.
bash
testparm
bash
To restrict access to shared files, Samba can use user authentication. You can create a Samba
user by adding a Linux user and then creating a corresponding Samba user.
1. Create a Linux User:
bash
bash
bash
bash
[printers]
path = /var/spool/samba
browseable = yes
printable = yes
guest ok = yes
Once CUPS is configured with printers, they will be shared over Samba automatically.
bash
If your system has a firewall enabled (such as ufw on Ubuntu or firewalld on CentOS), you
need to allow Samba traffic.
bash
bash
sudo firewall-cmd --permanent --add-service=samba
pgsql
\\<ip-address-of-linux-server>\<share-name>
For example, if your Linux server’s IP address is 192.168.1.10 and the shared directory is
shared, type:
vbnet
\\192.168.1.10\shared
2. Login with your Samba user credentials (sambauser and the password you set).
3. You should now have access to the shared folder from the Windows machine.
On another Linux system, you can mount the Samba share using the following command:
bash
Replace 192.168.1.10 with the actual IP address of your Samba server, sambauser with the
username, and password with the corresponding password.
To automatically mount the Samba share on boot, add an entry to the /etc/fstab file:
bash
//192.168.1.10/shared /mnt/samba cifs username=sambauser,password=password 0 0
This will mount the share automatically when the system boots up.
If you run into any issues, here are a few steps to verify:
bash
bash
sudo testparm
Ensure your Linux server is reachable from the client (Windows or another Linux system) by
pinging the IP address.
bash
ping 192.168.1.10
bash
Apache HTTP Server, commonly referred to as Apache, is one of the most widely used web
servers. It is open-source software that allows you to host websites and serve web content
over the HTTP protocol. the installation, configuration, and hosting of websites using
Apache on a Linux-based server.
Prerequisites
A Linux-based server (Ubuntu/Debian, CentOS/RHEL, or Fedora).
Root or sudo access to the server.
Basic knowledge of web hosting and web servers.
Apache is available in the default repositories of most Linux distributions. install it using the
package manager for your system.
On Ubuntu/Debian:
bash
On CentOS/RHEL/Fedora:
bash
After installing Apache, the service should automatically start. verify this by checking its
status:
bash
bash
bash
By default, Apache communicates over ports 80 (HTTP) and 443 (HTTPS). If your server
has a firewall enabled, you'll need to allow traffic on these ports.
bash
On CentOS/RHEL/Fedora (Firewalld):
bash
Once Apache is installed and the firewall is configured, you should verify that Apache is
working.
1. Open a web browser and type the server’s IP address in the address bar:
cpp
http://<server-ip>
cpp
http://192.168.1.100
You should see the default Apache welcome page, which means Apache is running correctly.
Apache is configured using a file called httpd.conf or apache2.conf, but for virtual hosts (for
hosting multiple websites), you generally configure individual site configuration files.
On Ubuntu/Debian:
On CentOS/RHEL/Fedora:
On Ubuntu/Debian:
bash
sudo mkdir -p /var/www/html/example.com/public_html
On CentOS/RHEL/Fedora:
bash
2. Set Permissions:
bash
bash
html
Copy
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Welcome to example.com!</title>
</head>
<body>
</body>
</html>
On Ubuntu/Debian:
bash
apache
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName example.com
DocumentRoot /var/www/html/example.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
</VirtualHost>
On CentOS/RHEL/Fedora:
bash
apache
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName example.com
DocumentRoot /var/www/example.com/public_html
ErrorLog /var/log/httpd/error_log
</VirtualHost>
bash
bash
7. Restart Apache:
bash
If you want to test the website using example.com instead of the IP address, you can add an
entry in your /etc/hosts file on your local machine.
On your local computer (not the server), add this line to the hosts file (located at /etc/hosts on
Linux or C:\Windows\System32\drivers\etc\hosts on Windows):
bash
<server-ip> example.com
arduino
http://example.com
cpp
http://<server-ip>
You should see the page with the message: "Hello, world! Welcome to example.com!"
1. Install Certbot:
On Ubuntu/Debian:
bash
On CentOS/RHEL/Fedora:
bash
Run the following command to automatically configure HTTPS for your website:
bash
Certbot automatically sets up a cron job to renew the SSL certificate. To test the renewal
process, run:
bash
Conclusion
successfully installed and configured Apache to host websites on your Linux server. You
learned how to:
5.Installing and Configuring a DNS Server with a Domain Name of Your Choice
DNS (Domain Name System) is essential for translating domain names (e.g., example.com)
into IP addresses that computers can understand. Setting up your own DNS server allows you
to manage and resolve domain names for your local network or public-facing services. We'll
walk you through the steps to install and configure BIND (Berkeley Internet Name Domain),
one of the most popular DNS server software, to create your own DNS server.
Prerequisites:
BIND (Berkeley Internet Name Domain) is the most widely used DNS server software. We
will install it and configure it to serve DNS for a domain of our choice.
On Ubuntu/Debian:
bash
On CentOS/RHEL/Fedora:
bash
This installs the BIND DNS server and utilities to manage DNS queries.
Step 2: Configuring BIND for a Domain
Before making any changes, it’s a good idea to back up the default configuration file:
bash
For this example, we’ll use example.com. You need to define your domain and its records in
a zone file.
On Ubuntu/Debian:
bash
On CentOS/RHEL/Fedora:
bash
text
$TTL 86400
2023031501 ; Serial
; Nameserver Information
IN NS ns1.example.com.
IN NS ns2.example.com.
@ IN MX 10 mail.example.com.
; A record for mail server
SOA Record: Contains administrative information about the domain and the DNS
server.
NS Records: Define the authoritative name servers for the domain.
A Records: Map domain names to IP addresses (for example, the domain name
example.com to 192.168.1.100).
MX Record: Defines the mail exchange server for the domain.
Now, need to tell BIND about your zone file. Open the configuration file for BIND:
On Ubuntu/Debian:
bash
Copy
On CentOS/RHEL/Fedora:
bash
text
zone "example.com" {
type master;
};
This tells BIND to use the file /etc/bind/db.example.com (or /var/named/example.com.db for
CentOS/RHEL/Fedora) as the authoritative source for the example.com domain.
4. Check the Configuration:
It’s important to verify that there are no syntax errors in your configuration.
bash
If everything is fine, you should see a confirmation that the zone file was loaded successfully.
On Ubuntu/Debian:
bash
On CentOS/RHEL/Fedora:
bash
On Ubuntu/Debian:
bash
bash
If your server is running a firewall, make sure to allow DNS traffic (port 53) through.
On Ubuntu/Debian (UFW):
bash
On CentOS/RHEL/Fedora (Firewalld):
bash
Once the DNS server is up and running, you can test it using the dig or nslookup command.
bash
This should return the IP address 192.168.1.100 (or whatever you set for example.com in
your zone file).
2. Testing with nslookup:
bash
To test your DNS server, you’ll need to configure the clients (other computers on your
network or your local machine) to use your server as the DNS resolver.
1. On Linux Clients:
bash
Add the following line (replace <your-server-ip> with the IP address of your DNS server):
text
nameserver <your-server-ip>
2. On Windows Clients:
Open Control Panel → Network and Sharing Center → Change adapter settings.
Right-click on the network connection, select Properties.
Select Internet Protocol Version 4 (TCP/IPv4) and click Properties.
In the Preferred DNS server field, enter the IP address of your DNS server.
If you want your DNS server to resolve queries for domains outside your own zone (i.e.,
provide recursive DNS resolution), you need to enable recursive queries.
To allow recursive queries, add the following to your BIND configuration file (typically
/etc/bind/named.conf.options on Ubuntu/Debian or /etc/named.conf on
CentOS/RHEL/Fedora):
text
options {
recursion yes;
};
Step 8: Troubleshooting
bash
You can also use journalctl to view the BIND service logs:
bash
successfully set up a DNS server with BIND to resolve domain names for a domain of your
choice. This configuration allows you to serve your domain locally or publicly, depending on
your needs. You can also add more DNS records (such as A, MX, CNAME, etc.) and enable
additional features like DNS forwarding and recursive queries for more advanced use cases.