0% found this document useful (0 votes)
6 views32 pages

Ue Linux 2023 Solving All

The document discusses various aspects of Linux file permissions, memory management, environment variables for debugging, web and FTP server services, user file creation modes, and the Linux kernel structure. It explains how to manage user accounts and their settings using commands, as well as the importance of auditing user passwords for security. Additionally, it highlights the role of the X server in providing a graphical user interface for applications in Linux.

Uploaded by

Lucky Machaba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views32 pages

Ue Linux 2023 Solving All

The document discusses various aspects of Linux file permissions, memory management, environment variables for debugging, web and FTP server services, user file creation modes, and the Linux kernel structure. It explains how to manage user accounts and their settings using commands, as well as the importance of auditing user passwords for security. Additionally, it highlights the role of the X server in providing a graphical user interface for applications in Linux.

Uploaded by

Lucky Machaba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

QUESTION 3

V)......types file permission that given to a file?


volume_up

There are three types of file permissions that can be given to a file in Linux:

• Read (r): Allows the user or group to view the contents of the file.
• Write (w): Allows the user or group to modify the contents of the file.
• Execute (x): Allows the user or group to execute the file.

Each type of permission can be granted to three different groups:

• Owner: The user who owns the file.


• Group: The group that owns the file.
• Others: All other users on the system.

File permissions are represented by a string of nine characters, three for each group.
Each character can be either r, w, or x, or a - to indicate that the permission is denied.

For example, the following file permissions string indicates that the owner has read and
write permissions, the group has read permission, and others have no permissions:

-rw-r--r--

You can use the chmod command to change the permissions of a file. For example, to
give the owner and group execute permission on the above file, you would use the
following command:

chmod +x myfile.txt

This would change the file permissions to the following:

-rwxr-xr--

It is important to note that only the owner of a file can change the permissions of that
file. If you need to change the permissions of a file that you do not own, you will need to
use the sudo command.

For more information on file permissions, you can consult the chmod man page.

share
more_vert

VI)......temporary amount of memory space that created to


allow a certain program to run smoothly
volume_up

The temporary amount of memory space that is created to allow a certain program to
run smoothly is called the stack. The stack is a region of memory that is used to store
local variables, function parameters, and return addresses. The stack is managed by
the compiler and the operating system, and it is typically located in the main memory
(RAM).

When a program is running, the stack grows and shrinks as functions are called and
returned from. When a function is called, its parameters and local variables are pushed
onto the stack. When the function returns, its parameters and local variables are
popped off the stack.

The stack is an important part of the memory management system, and it is essential
for the smooth operation of most programs.

Here are some examples of how the stack is used:

• When you call a function, the function's parameters are pushed onto the stack.
• When a function returns, its return value is pushed onto the stack.
• Local variables are stored on the stack.
• Array elements are stored on the stack.
• Recursion is implemented using the stack.

The stack is typically much smaller than the heap, another region of memory that is
used to store data. This is because the stack is used to store temporary data that is
needed only while a function is running. The heap is used to store long-lived data that
needs to be accessed by multiple functions.

If the stack becomes full, the program will crash. This can happen if the program is
recursive or if it uses too many local variables. To avoid stack overflows, it is important
to use recursion carefully and to avoid using too many local variables.

share
more_vert

.VII)........environment variable used for debugging a new


library or a non-standard library
volume_up

The environment variable used for debugging a new library or a non-standard library is
LD_LIBRARY_PATH. This variable is a colon-separated list of directories where the
dynamic linker should search for shared libraries.

To debug a new library, you can set the LD_LIBRARY_PATH variable to the directory
containing the new library. For example, if the new library is located at
/home/user/new_library, you would set the LD_LIBRARY_PATH variable to the
following:

LD_LIBRARY_PATH=/home/user/new_library:$LD_LIBRARY_PATH

This will tell the dynamic linker to search for the new library in the
/home/user/new_library directory before searching in the standard directories.

To debug a non-standard library, you can set the LD_LIBRARY_PATH variable to the
directory containing the non-standard library. For example, if the non-standard library is
located at /usr/local/lib, you would set the LD_LIBRARY_PATH variable to the
following:

LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH

This will tell the dynamic linker to search for the non-standard library in the
/usr/local/lib directory before searching in the standard directories.

Once you have set the LD_LIBRARY_PATH variable, you can start your program and
debug it as usual.

Here are some tips for debugging libraries:

• Use a debugger such as gdb or lldb.


• Set breakpoints in the library code.
• Examine the values of variables.
• Step through the code line by line.
• Use print statements to output diagnostic messages.

By following these tips, you can debug libraries and identify and fix bugs.

share
more_vert

VIII)...... a service tha acts both as web and FTP servers


on linux server.
volume_up

There are a few services that act both as web and FTP servers on a Linux server. One
popular option is ProFTPD. It is a free and open-source FTP server that can also be
used to serve web pages. ProFTPD is easy to configure and use, and it supports a wide
range of features, including:

• Virtual hosting
• SSL encryption
• Bandwidth limiting
• User authentication and authorization
• File transfer logging

Another option is vsftpd. It is also a free and open-source FTP server that can be used
to serve web pages. vsftpd is known for its speed and security, and it is a good choice
for high-traffic servers. vsftpd also supports a wide range of features, including:

• Virtual hosting
• SSL encryption
• Bandwidth limiting
• User authentication and authorization
• File transfer logging

Finally, you can also use a web server such as Apache or Nginx to serve FTP files. To
do this, you would need to install an FTP module for the web server. For example, the
mod_ftp module for Apache allows you to serve FTP files from your web server.

Which service you choose will depend on your specific needs. If you are looking for a
simple and easy-to-use solution, ProFTPD or vsftpd are good options. If you need a
more powerful and feature-rich solution, you can use a web server with an FTP module.
Here are some tips for setting up a web and FTP server on a Linux server:

• Choose a service that meets your needs.


• Install the service on your server.
• Configure the service to your liking.
• Start the service.
• Test the service to make sure it is working properly.

Once you have set up the web and FTP server, you will be able to access your files
using a web browser or an FTP client.

share
more_vert

IX).......user file creaction mode


volume_up

The user file creation mode, also known as the umask, is a setting that determines the
default permissions for newly created files and directories. It is a four-digit octal number,
with each digit representing a different permission:

• 0: No permission
• 1: Read permission
• 2: Write permission
• 3: Execute permission

The umask is applied by subtracting it from the default permissions for newly created
files and directories. For example, if the umask is set to 022, then newly created files
and directories will have the following permissions:

• Files: 644 (read and write for the owner, read for the group and others)
• Directories: 755 (read, write, and execute for the owner, read and execute for the
group and others)

You can view and set the umask using the umask command. For example, to view the
current umask, you would use the following command:
umask

To set the umask, you would use the following command:

umask <umask>

For example, to set the umask to 022, you would use the following command:

umask 022

It is important to note that the umask can only be set by the user who owns the process.

Here are some tips for choosing a umask:

• Choose a umask that is appropriate for the environment in which you are
working. For example, if you are working on a shared server, you may want to
choose a more restrictive umask to protect your files from other users.
• Be careful not to set the umask too restrictive, or you may make it difficult to
access or modify your files.
• If you are unsure about what umask to use, consult with a system administrator.
share
more_vert

X)linux kernel is a ........ kernel whereby entire operating


system is working in kernel space
volume_up

The Linux kernel is a monolithic kernel. This means that the entire operating system
runs in kernel space. Kernel space is a privileged area of memory that is protected from
user applications. This gives the kernel full control over the hardware and the system
resources.

In a monolithic kernel, all of the operating system's core components, such as the
process scheduler, memory manager, and file system, are running in kernel space. This
makes the Linux kernel very efficient and responsive. However, it also makes it more
complex and difficult to debug.

Some of the advantages of a monolithic kernel include:


• Performance: Monolithic kernels are typically faster and more responsive than
microkernels, because there is less overhead involved in communicating
between the kernel and user applications.
• Simplicity: Monolithic kernels are simpler to design and implement than
microkernels.
• Efficiency: Monolithic kernels can make more efficient use of system resources,
such as memory and CPU time.

Some of the disadvantages of a monolithic kernel include:

• Complexity: Monolithic kernels can be complex and difficult to debug, because all
of the operating system's core components are running in kernel space.
• Scalability: Monolithic kernels can have difficulty scaling to large numbers of
processors and users.
• Reliability: If a bug occurs in the kernel, it can crash the entire operating system.

Despite its disadvantages, the Linux kernel is one of the most popular and widely used
kernels in the world. It is used in a wide range of systems, from desktop computers to
servers and supercomputers.

share
more_vert

answer

QUESTION4
volume_up

Here is how to perform each of the modifications listed in the image you sent:

i. Add the user to a supplementary group

To add the user user1 to the supplementary group group2, use the following command:

usermod -aG group2 user1


This will add the user to the group2 group without removing them from any other groups
they are already a member of.

ii. Change the user's comment field in /etc/passwd

To change the user's comment field in /etc/passwd, use the following command:

usermod -c "This is a new comment for user1" user1

This will change the comment field for the user user1 to "This is a new comment for
user1".

iii. Change the user's home directory

To change the user's home directory, use the following command:

usermod -d /new/home/directory user1

This will change the home directory for the user user1 to /new/home/directory.

iv. Set an account expiry date

To set an account expiry date, use the following command:

usermod -e 2023-10-31 user1

This will set the account expiry date for the user user1 to October 31, 2023.

V. Remove an expiry date

To remove an expiry date, use the following command:

usermod -e 0 user1

This will remove the account expiry date for the user user1.

vi. Change a user's login name (username)

To change a user's login name (username), use the following command:

usermod -l new_username user1


This will change the login name for the user user1 to new_username.

vii. Lock/unlock a user's account

To lock a user's account, use the following command:

usermod -L user1

This will prevent the user user1 from logging in.

To unlock a user's account, use the following command:

usermod -U user1

This will allow the user user1 to log in again.

viii. Move the contents of a user's home directory

To move the contents of a user's home directory, use the following command:

mv user1/.* /new/home/directory

This will move all of the files and directories in the user user1's home directory to the
new directory /new/home/directory.

ix. Change a user's login shell

To change a user's login shell, use the following command:

usermod -s /bin/bash user1

This will change the login shell for the user user1 to /bin/bash.

X. Change a user's ID

To change a user's ID, use the following command:

usermod -u 1000 user1

This will change the user ID for the user user1 to 1000.
Please note that some of these changes may require root privileges.

share
more_vert

answer

QUESTION5
volume_up

Answering the image you sent:

a) System program that high-level programs for displaying services.

This is a correct statement. A display server is a system program that provides a


graphical user interface (GUI) for high-level programs. It is responsible for drawing
windows, managing input devices, and rendering graphics.

b) System program that application program into kernel like program.

This is an incorrect statement. A display server is not a kernel-like program. It is a user-


space program that runs on top of the kernel.

c) A core of Linux operating system.

This is an incorrect statement. A display server is not a core component of the Linux
operating system. It is a user-space program that can be uninstalled and replaced.

d) A system program that processes request on access of pixels.

This is a correct statement. A display server is responsible for processing requests from
high-level programs to draw pixels on the screen.

Therefore, the only correct statement is a) System program that high-level programs for
displaying services.

share
more_vert
annswer

QUESTION6
volume_up

The image you sent shows a text box that says:

What is the X server?

The X server is a system program that provides a graphical user interface (GUI) for
high-level programs. It is responsible for drawing windows, managing input devices, and
rendering graphics.

The X server is a core component of the Linux operating system, and it is necessary for
running most graphical applications. It is also used on other operating systems, such as
Unix and macOS.

The X server is a client-server system. This means that the X server runs on one
machine (the server machine), and client applications can connect to it from other
machines (the client machines). This allows users to run graphical applications on a
remote server machine, and display the output on their local client machine.

Some examples of X servers include:

• X.Org Server
• Weston
• Wayland

The X server is a complex piece of software, but it is essential for running graphical
applications on Linux. If you are interested in learning more about the X server, there
are many resources available online and in libraries.

To answer the question in the image, the X server is a system program that provides a
graphical user interface for high-level programs. It is a core component of the Linux
operating system, and it is necessary for running most graphical applications.

share
more_vert

(a) It's a matter of trust that the user will change their
password when you provide them with an initial password.
Sysadmins who regularly audit their user's passwords
realize that this "honor system" level of trust doesn't work
100 percent of the time. How can you audit user (3 Marks)
accounts, that is, to check their password status?
expand_more
volume_up

There are a few ways to audit user accounts to check their password status:

• Use a password manager. A password manager can help users create and
manage strong passwords, and it can also generate reports on password
strength and compliance.
• Use a password policy. A password policy can define requirements for password
length, complexity, and expiration. You can also use a password policy to
enforce regular password changes.
• Use a security information and event management (SIEM) system. A SIEM
system can collect and analyze security logs from across your network, including
password change events. This can help you to identify users who have not
changed their passwords in a while, or who have set weak passwords.
• Use a penetration testing tool. A penetration testing tool can be used to simulate
a password attack against your system. This can help you to identify users who
have set weak passwords, or who have reused passwords across multiple
accounts.

In addition to these technical methods, you can also audit user accounts by manually
reviewing password change logs and conducting user surveys.

Here are some specific steps you can take to audit user accounts for password status:

1. Collect a list of all user accounts. This can be done using a directory service such
as Active Directory or LDAP.
2. Review password change logs. Look for users who have not changed their
passwords in a while, or who have changed their passwords multiple times in a
short period of time.
3. Conduct a password complexity audit. Use a password policy enforcement tool to
identify users who have set weak passwords.
4. Conduct a password reuse audit. Use a password management tool to identify
users who have reused passwords across multiple accounts.
5. Manually review password change logs. Look for suspicious activity, such as
password changes from unusual locations or times.
6. Conduct user surveys. Ask users about their password habits, such as how often
they change their passwords and what types of passwords they use.

By taking these steps, you can identify users who need to be reminded to change their
passwords, or who need to be provided with additional security training.

It is important to note that no single auditing method is perfect. You should use a
combination of methods to get the most complete picture of your users' password
security.

share
more_vert

(b) In addition to a regular forced change of passwords,


you can also set a minimum change period. For example,
you can make some settings to user accounts to force a
password change every number of days (e.g., 60 days)
with a minimum number of days between password
changes to one. Setting a minimum number of days
ensures that users don't change their passwords ten times
(or whatever the number of the system's remembered
passwords is set to) to reset it to their original password,
which amounts to no net password change. How can you
implement such settings?
expand_more
volume_up

To implement a minimum password change period in Linux, you can use the following
steps:

1. Open the /etc/shadow file in a text editor.


2. Find the line for the user account for which you want to set a minimum password
change period.
3. Change the value of the MINCHANGE field to the desired number of days.
4. Save the file and exit the text editor.

The MINCHANGE field specifies the minimum number of days that must pass between
password changes for the user. For example, if you set the MINCHANGE field to 7, then
the user will not be able to change their password again for at least 7 days after their
last password change.

You can also use the passwd command to set the minimum password change period for
a user account. To do this, use the following command:

" passwd -n <number of days> <username> "

This will set the MINCHANGE field for the user username to the specified number of days.

Here is an example of how to set a minimum password change period of 7 days for the
user user1:

" sudo passwd -n 7 user1 "

Note: You must have root privileges to change the password settings for another user
account.

Once you have set a minimum password change period, users will not be able to
change their passwords more often than the specified number of days. This will help to
prevent users from changing their passwords too often, which can make it easier for
attackers to crack their passwords.

It is important to note that setting a minimum password change period is not a substitute
for using a strong password policy. Users should still be required to create strong
passwords, and they should avoid reusing passwords across multiple accounts.

share
more_vert

(c) You have been tasked to find all files on your


filesystem that are larger than 10 MB in size. What
command would you use to easily find those files?
volume_up

To find all files on your filesystem that are larger than 10 MB in size, you can use the
following command:

find / -type f -size +10M

This command will search the entire filesystem (starting at the root directory, /) for files
(using the -type f option) that are larger than 10 MB (using the -size +10M option).

The output of this command will be a list of all the files that meet the criteria. You can
then use this list to take whatever action you need to, such as deleting the files, moving
them to a different location, or backing them up.

Here is an example of how to use the find command to find all files on the filesystem
that are larger than 10 MB in size:

$ find / -type f -size +10M


/home/user1/myfile.txt
/home/user2/myvideo.mp4
/var/log/syslog

This output shows that there are three files on the filesystem that are larger than 10 MB
in size: myfile.txt, myvideo.mp4, and syslog.

You can also use the find command to search for files in a specific directory. For
example, to search for all files in the /home/user1 directory that are larger than 10 MB in
size, you would use the following command:

find /home/user1 -type f -size +10M

You can also use the find command to search for files based on other criteria, such as
their creation date, modification date, or owner. For more information on using the find
command, please consult the man page.
share
more_vert

(d) Four science instructors at CNMS College ask you


whether they can access a particular log file from their
home directories. They explain that they update a log file
in the /workgrp/project directory weekly, and they don't
want to change out of their home directories every time
they want to view or edit the file. In addition, they want the
log file in their home directories to be called myjournal.
You need to create a link to the log file so that each
instructor can access the file from his or her home
directory, using the following criteria: Scientist 1: Michio
Home directory: /home/michio Scientist 2: Neil Home
directory: /home/neil Scientist 3: Marty Home directory:
/home/marty Scientist 4: Ellen Home directory:
/home/ellen The log file is called sci.journal, and it's in the
/workgrp/project directory
expand_more
volume_up

To create a link to the log file so that each instructor can access the file from his or her
home directory, you can use the following steps:

1. Navigate to the home directory of the instructor for whom you want to create the
link.
2. Use the following command to create the link:
ln -s /workgrp/project/sci.journal /home/<instructor_username>/myjournal

For example, to create a link for the instructor Michio, you would use the following
command:

ln -s /workgrp/project/sci.journal /home/michio/myjournal
3. Repeat steps 1 and 2 for each instructor.

Once you have created the links, the instructors will be able to access the log file from
their home directories by using the myjournal filename.

Here is an example of how to create links for all four instructors:

# Create a link for Michio


ln -s /workgrp/project/sci.journal /home/michio/myjournal

# Create a link for Neil


ln -s /workgrp/project/sci.journal /home/neil/myjournal

# Create a link for Marty


ln -s /workgrp/project/sci.journal /home/marty/myjournal

# Create a link for Ellen


ln -s /workgrp/project/sci.journal /home/ellen/myjournal

Once you have created the links, the instructors can access the log file from their home
directories by using the myjournal filename. For example, to view the log file, Michio
would use the following command:

cat /home/michio/myjournal

This would display the contents of the log file to the console.

Note: You must have root privileges to create links to files in other users' home
directories.

share
more_vert

QUESTION 7

(a) System administrators agree that managing groups is


much easier and more efficient than managing users.
Group management. Write five uses of group
management in Linux systems. (b)When you create a new
user account, the system assigns the user a user ID (UID)
and a primary group ID (GID). They can be the same
sequential number but that isn't always the case. For
example, for the account created as Juma Daudi, can
have UID as 1007 and the GID as 1007. Juma's primary
GID is 1007 but he might also work in an area of the
company, such as IT, Engineering, or Application
development that requires him to have access to a group-
owned directory. In this, Juma works in the Engineering
department as an associate engineer. The Engineering
department's shared directory GID is 8020. (i) Show how
to grant Juma access to that group's shared directory. (ii)
If the Administrators are to be the new owners of the
directory, how will you accomplish that? (c) (i) Identify the
components or fields of passwd file. (ii) Write awk
command to display the user and his/her home path.
expand_more
volume_up

(a) Five uses of group management in Linux systems:

1. To assign permissions to files and directories: Groups can be used to assign


permissions to files and directories. This allows you to control who has access to
what resources. For example, you could create a group for all users in the sales
department and then give that group permission to access the sales directory.
2. To simplify user management: Groups can be used to simplify user
management. For example, instead of having to give each user access to the
same files and directories, you can simply add the user to the appropriate group.
This makes it easier to add and remove users from a system.
3. To improve security: Groups can be used to improve security. For example, you
could create a group for all users who need to have access to sensitive data and
then restrict access to that data to only users in that group. This makes it more
difficult for unauthorized users to access the data.
4. To automate tasks: Groups can be used to automate tasks. For example, you
could create a group for all users who need to be notified of changes to a
particular file and then use a cron job to send an email to all users in that group
whenever the file is changed.
5. To improve auditing: Groups can be used to improve auditing. For example, you
could create a group for all users who have access to a particular system and
then use a log analysis tool to track all activity by users in that group. This makes
it easier to identify and investigate security incidents.

(b) (i) How to grant Juma access to that group's shared directory:

To grant Juma access to the Engineering department's shared directory, you can use
the following command:

sudo usermod -aG 8020 juma

This will add Juma to the group with GID 8020. Juma will then have access to the
shared directory.

(ii) How to make Administrators the new owners of the directory:

To make Administrators the new owners of the directory, you can use the following
command:

sudo chown -R administrators:administrators /path/to/shared/directory

This will change the ownership of the shared directory and all of its contents to the
administrators group.

(c) (i) Components or fields of passwd file:

The passwd file contains the following fields:

• Username: The username of the user.


• Password: The password of the user, but it is stored in an encrypted format.
• UID: The user ID of the user.
• GID: The primary group ID of the user.
• Comment: A comment about the user.
• Home directory: The home directory of the user.
• Shell: The default shell of the user.

(ii) Awk command to display the user and his/her home path:

The following awk command will display the user and their home path:
awk -F: '{print $1, $6}' /etc/passwd

This command will print the first and sixth fields of the passwd file, which are the
username and home directory, respectively.

Here is an example of the output of the command:

root /root
user1 /home/user1
user2 /home/user2
share
more_vert

(a) System administrators agree that managing groups is


much easier and more efficient than managing users.
Group management. Write five uses of group
management in Linux systems. (b)When you create a new
user account, the system assigns the user a user ID (UID)
and a primary group ID (GID). They can be the same
sequential number but that isn't always the case. For
example, for the account created as Juma Daudi, can
have UID as 1007 and the GID as 1007. Juma's primary
GID is 1007 but he might also work in an area of the
company, such as IT, Engineering, or Application
development that requires him to have access to a group-
owned directory. In this, Juma works in the Engineering
department as an associate engineer. The Engineering
department's shared directory GID is 8020. (i) Show how
to grant Juma access to that group's shared directory. (1.5
Marks) (ii) If the Administrators are to be the new owners
of the directory, how will you accomplish that? (c) (i)
Identify the components or fields of passwd file. (ii) Write
awk command to display the user and his/her home path.
(iii) Write sed command to replace a '/' with a '*' from the
home path of a particular user (d) As you are creating a
shell script, you recognize that you are writing these four
lines of code over and over again: echo "Error in
processing file, $1." echo "Check that the $1 file exists and
contains data." echo "This script will now exit ... " exit
Properly write a function named errorMessage that will
allow you to turn these four lines of code into a single line
(except for the function declaration) throughout the script.
expand_more
QUESTION7

(a) Five uses of group management in Linux systems:

1. To assign permissions to files and directories: Groups can be used to assign


permissions to files and directories. This allows you to control who has access to
what resources. For example, you could create a group for all users in the sales
department and then give that group permission to access the sales directory.
2. To simplify user management: Groups can be used to simplify user
management. For example, instead of having to give each user access to the
same files and directories, you can simply add the user to the appropriate group.
This makes it easier to add and remove users from a system.
3. To improve security: Groups can be used to improve security. For example, you
could create a group for all users who need to have access to sensitive data and
then restrict access to that data to only users in that group. This makes it more
difficult for unauthorized users to access the data.
4. To automate tasks: Groups can be used to automate tasks. For example, you
could create a group for all users who need to be notified of changes to a
particular file and then use a cron job to send an email to all users in that group
whenever the file is changed.
5. To improve auditing: Groups can be used to improve auditing. For example, you
could create a group for all users who have access to a particular system and
then use a log analysis tool to track all activity by users in that group. This makes
it easier to identify and investigate security incidents.

(b) (i) How to grant Juma access to that group's shared directory:
To grant Juma access to the Engineering department's shared directory, you can use
the following command:

sudo usermod -aG 8020 juma

This will add Juma to the group with GID 8020. Juma will then have access to the
shared directory.

(ii) How to make Administrators the new owners of the directory:

To make Administrators the new owners of the directory, you can use the following
command:

sudo chown -R administrators:administrators /path/to/shared/directory

This will change the ownership of the shared directory and all of its contents to the
administrators group.

(c) (i) Components or fields of passwd file:

The passwd file contains the following fields:

• Username: The username of the user.


• Password: The password of the user, but it is stored in an encrypted format.
• UID: The user ID of the user.
• GID: The primary group ID of the user.
• Comment: A comment about the user.
• Home directory: The home directory of the user.
• Shell: The default shell of the user.

(ii) Awk command to display the user and his/her home path:

The following awk command will display the user and their home path:

awk -F: '{print $1, $6}' /etc/passwd

This command will print the first and sixth fields of the passwd file, which are the
username and home directory, respectively.
(iii) Sed command to replace a '/' with a '*' from the home path of a particular user:

The following sed command will replace a '/' with a '*' from the home path of a particular
user:

" sed -i "s///*/g" /etc/passwd "

This command will search for the specified user's home directory in the passwd file and
replace all occurrences of '/' with '*'.

(d) Function to display an error message and exit the script:

The following function can be used to display an error message and exit the script:

function errorMessage() {
echo "Error in processing file, $1."
echo "Check that the $1 file exists and contains data."
echo "This script will now exit ..."
exit 1
}

This function takes one argument, which is the name of the file that is being processed.
The function will display an error message and exit the script if the file does not exist or
does not contain data.

To use the function, you would simply call it from within your script, passing in the name
of the file that you are processing. For example:

if [ ! -f $1 ]; then
errorMessage $1
fi

This code will check if the file $1 exists. If it does not exist, the errorMessage() function
will be called, which will display an error message and exit the script.

You can use the errorMessage() function anywhere in your script to display an error
message and exit the script. This can be helpful for handling errors in your script and
preventing it from continuing

share
more_vert

QUESTION7
(a) Write a shell script to automate renaming of files and
their correspond directories. (b) The following are some of
the common bash/Linux command line symbols. For each
symbol, describe its meaning when used in the command
line interface and an example of its use in any command.
(i) ~ (ii) * (iii) & (iv) && (v) \ (vi) $(( )) (vii) ^$ (viii) # (ix) | (x)
> (xi) <(xii) >> (xiii) .. / .. (xiv) abc$ (c) The University of
Dodoma has admitted four students, namely Kipanya,
Matonya, Sechelela and Hawa, to join a two-month Linux
Administration short course. During this period, they will
need to access labs, halls or (dormitories), library and
stationaries. Assuming that the mentioned resources have
been configured as groups in Linux systems: (i) Use a for
loop to add the students to the respective groups, within a
single -line commands (ii) Provide three options with
sample outputs to verify that the students have been
added to the groups.
expand_more
View other drafts


1.

2.

3.
QUESTION8

(a) Shell script to automate renaming of files and their correspond directories:

Here is a simple shell script to automate renaming of files and their corresponding
directories:

#!/bin/bash

# Rename files and directories based on a regular expression

# Usage: rename.sh <regex> <replacement>

# Example: rename.sh 'old-prefix' 'new-prefix'

# Get the regular expression and replacement text from the command line
arguments
regex=$1
replacement=$2

# Get a list of all files and directories in the current directory


files_and_dirs=$(ls)

# Iterate over the list of files and directories


for item in $files_and_dirs; do

# If the item is a directory, rename it


if [ -d "$item" ]; then
mv "$item" "${replacement}${item}"

# If the item is a file, rename it


elif [ -f "$item" ]; then
mv "$item" "${replacement}${item}"
fi
done

To use the script, simply pass the regular expression and replacement text as command
line arguments. For example, to rename all files and directories with the prefix old-
prefix to the prefix new-prefix, you would use the following command:

./rename.sh old-prefix new-prefix

(b) Common bash/Linux command line symbols:


Symbol Meaning Example

~ The home directory of the current user ~ or ~/myfile.txt

*
Wildcard character that matches any
ls * or grep '*error*'
number of characters

& Backgrounds a command python myscript.py &

if [ -f myfile.txt ] && [ -r myfile.txt


&& Logical AND operator "File is readable"; fi

\ Escape character echo "This is a newline: \n"

$((
Arithmetic expression echo <span class="math-inline">\(\(1 \+
))

`^` Start and end of line markers grep "^This is the beginning of the line

# Comment # This is a comment

` ` Pipe

> Output redirection ls > myfile.txt

< Input redirection cat < myfile.txt

>> Output redirection, append mode ls >> myfile.txt

.. Parent directory cd ..

/ Directory separator /home/user/myfile.txt

drive_spreadsheetExport to Sheets

(c) Adding students to respective groups using a for loop:


The following one-line command will add the students Kipanya, Matonya, Sechelela,
and Hawa to the respective groups:

for student in Kipanya Matonya Sechelela Hawa; do usermod -aG $student


$student; done

This command will iterate over the list of students and add each student to the group
with the same name as the student.

Verifying that the students have been added to the groups:

Here are three options with sample outputs to verify that the students have been added
to the groups:

Option 1: Use the groups command to list the groups that each student belongs to.

Sample output:

Kipanya: Kipanya, labs, halls, library, stationaries


Matonya: Matonya, labs, halls, library, stationaries
Sechelela: Sechelela, labs, halls, library, stationaries
Hawa: Hawa, labs, halls, library, stationaries

Option 2: Use the getent command to get the group entry for each student.

Sample output:

Kipanya:Kipanya:1001:labs,halls,library,stationaries
Matonya:Matonya:1002:labs,halls,library,stationaries
Sechelela:Sechelela:1003:labs,halls,library,stationaries
Hawa:Hawa:1004:labs,halls,library,stationaries

Option 3: Use the grep command to search the /etc/group file for each student's group
entry.

Sample output:

Kipanya:labs,halls,library,stationaries
Matonya:labs,halls,library,stationaries
Sechelela:labs,halls,library,stationaries
Hawa:labs,halls,library,stationaries
tuneshare
more_vert
add_photo_alternate

mic
send_spark

Bard may display inaccurate or offensive information that doesn’t represent Google’s
views. Bard Privacy Notice

You might also like