Linux Commands and System Administration - PDF

Summary

This document covers fundamental aspects of Linux, specifically focusing on Ubuntu. It includes answers to questions about root user functions, sudo vs. su, system log files, user management tasks, and basic commands.

Full Transcript

**2 Marks** **1. What is the role of the root user in Ubuntu?** The **root user** is the superuser or administrator in Ubuntu and other Linux-based systems. This user has **full control** over the system, including the ability to modify system configurations, install and remove software, manage ot...

**2 Marks** **1. What is the role of the root user in Ubuntu?** The **root user** is the superuser or administrator in Ubuntu and other Linux-based systems. This user has **full control** over the system, including the ability to modify system configurations, install and remove software, manage other users, and access all files and directories, regardless of permissions. Root access is typically required for performing administrative tasks that can impact the system\'s integrity or security. **2. How does sudo differ from su in Ubuntu?** - **sudo**: The sudo (Super User DO) command allows a permitted user to execute a command as the root user (or another user) without needing to log in as root. It is used for **one-time** privilege escalation and is generally safer because it logs the command usage. - **Example**: sudo apt update (run the apt update command as root) - **su**: The su (Substitute User) command is used to switch users, often to the root user. It opens a new shell session with the privileges of the specified user (default root). You need to enter the target user\'s password, and once you are in the new shell, you are effectively working as that user until you exit. - **Example**: su - (switch to root user shell) **3. What are system log files, and where are they stored?** **System log files** contain information about system activity, events, errors, and messages generated by the system, kernel, services, and applications. They are essential for troubleshooting and monitoring system performance. - **Location**: Log files are typically stored in the /var/log directory. - Common log files: - /var/log/syslog: General system activity logs. - /var/log/auth.log: Authentication-related events (e.g., login attempts). - /var/log/dmesg: Kernel messages. - /var/log/kern.log: Kernel-specific logs. - /var/log/apache2/: Logs related to the Apache web server (if installed). **4. Explain the structure of the /etc/passwd file.** The /etc/passwd file contains basic information about each user on the system. Each line in this file represents one user account and contains the following fields, separated by colons (:): - **Username**: The user\'s login name. - **Password**: An encrypted password (or an x if the password is stored in /etc/shadow). - **User ID (UID)**: A unique numerical ID assigned to the user. - **Group ID (GID)**: The primary group ID associated with the user. - **User Information**: Optional fields, typically used for the user\'s full name or contact info. - **Home Directory**: The path to the user\'s home directory (e.g., /home/username). - **Shell**: The shell program that the user will use (e.g., /bin/bash). **Example entry**: john:x:1001:1001:John Doe:/home/john:/bin/bash **5. How can a user escalate privileges in Ubuntu?** A user can escalate privileges in Ubuntu using the sudo command, which allows them to execute commands with the privileges of another user (usually the root user). The user must be granted sudo access in the /etc/sudoers file or be a member of the sudo group. **Example**: sudo apt install package-name For tasks that require switching to the root user, su can also be used, but sudo is preferred for security reasons. **6. What is the difference between local and environment variables in a shell?** - **Local variables**: These are variables that exist only within the current shell session and are used by processes running in that session. They are not passed to child processes. A user can define local variables using the export command. - **Example**: MY\_VAR=\"Hello\" - **Environment variables**: These are variables that are inherited by child processes. Environment variables are usually set in shell configuration files (e.g.,.bashrc) and can influence the behavior of processes and applications. - **Example**: export PATH=\$PATH:/new/directory **7. Explain how to add and delete users in Ubuntu.** - **To add a user**: Use the useradd command, followed by the username. - **Example**: - sudo useradd john - sudo passwd john \# Set password for the user - **To delete a user**: Use the userdel command. - **Example**: - sudo userdel john - sudo rm -r /home/john \# Optionally, remove the user\'s home directory **8. What is the purpose of the /etc/group file?** The /etc/group file contains information about user groups in the system. Each line defines a group and consists of the following fields, separated by colons (:): - **Group Name**: The name of the group. - **Password**: The group\'s password (usually empty). - **Group ID (GID)**: A unique numerical ID assigned to the group. - **Group Members**: A comma-separated list of users who are members of the group. **Example entry**: sudo:x:27:john,jane **9. How do you change the ownership of a file using the command line?** To change the ownership of a file, you can use the chown command. You can specify both the user and the group. - **Example**: - sudo chown john:staff myfile.txt This command changes the ownership of myfile.txt to the user john and the group staff. **10. Explain the difference between chmod, chown, and chgrp commands.** - **chmod**: Used to change the permissions of a file or directory. - **Example**: chmod 755 file.txt (gives read, write, and execute permissions to the owner, and read-execute permissions to others). - **chown**: Changes the ownership (user and group) of a file or directory. - **Example**: chown john:staff file.txt (sets the owner to john and the group to staff). - **chgrp**: Changes the group ownership of a file or directory. - **Example**: chgrp staff file.txt (sets the group to staff). **11. What is Access Control List (ACL) in Linux** An **Access Control List (ACL)** is a more granular way to define file permissions. While traditional file permissions in Linux apply to the user, group, and others, ACLs allow you to specify permissions for **multiple users** or **groups** on a single file or directory. - **Example**: You can grant a user john write access to a file, even if the file\'s group doesn\'t include john. **12. How do you use setfacl and getfacl commands?** - **setfacl**: Used to set ACLs on files and directories. - **Example**: - setfacl -m u:john:rwx file.txt \# Give user john read, write, and execute permissions on file.txt - **getfacl**: Used to view the ACLs set on a file or directory. - **Example**: - getfacl file.txt \# View the ACLs of file.txt **13. How can you view the permissions of a file?** To view the permissions of a file, you can use the ls -l command. - **Example**: - ls -l myfile.txt This will output the permissions, ownership, and other details of the file, such as: -rwxr-xr-x 1 john staff 12345 Jan 30 10:00 myfile.txt The first column shows the permissions (e.g., rwxr-xr-x). **14. How do you manage groups in Ubuntu?** To manage groups in Ubuntu, you can use the following commands: - **Add a group**: sudo groupadd groupname - **Delete a group**: sudo groupdel groupname - **Add a user to a group**: sudo usermod -aG groupname username - **Remove a user from a group**: sudo gpasswd -d username groupname **15. Explain how to copy, move, and delete files in Ubuntu.** - **To copy a file**: Use the cp command. - **Example**: - cp file1.txt file2.txt \# Copies file1.txt to file2.txt - **To move a file**: Use the mv command. - **Example**: - mv file1.txt /home/john/ \# Moves file1.txt to john\'s home directory - **To delete a file**: Use the rm command. - **Example**: - rm file1.txt \# Deletes file1.txt Each of these commands supports additional options, such as recursive operations (-r for directories) and confirmation prompts (-i for interactive). **12 marks** **1. Describe the root user and administrative commands used in Ubuntu** **Root User and Administrative Commands in Ubuntu** **1. Root User in Ubuntu** In Ubuntu (and other Linux distributions), the **root user** is the **superuser** with full administrative privileges. This user has unrestricted access to all system files, configurations, and commands. However, Ubuntu **disables the root account by default** for security reasons. Instead, users perform administrative tasks using the sudo command, which temporarily grants **root privileges** to a normal user. - **To switch to the root user:** - sudo su - **To execute a command as root:** - sudo \ - **To exit root mode:** - exit **2. Common Administrative Commands in Ubuntu** **User Management** - **Create a new user:** - sudo adduser \ - **Delete a user:** - sudo deluser \ - **Add a user to the sudo group:** - sudo usermod -aG sudo \ - **Change user password:** - sudo passwd \ **File and Directory Permissions** - **Change file ownership:** - sudo chown \:\ \ - **Modify file permissions:** - sudo chmod 755 \ **Package Management** - **Update package lists:** - sudo apt update - **Upgrade installed packages:** - sudo apt upgrade - **Install a package:** - sudo apt install \ - **Remove a package:** - sudo apt remove \ **Process Management** - **View running processes:** - ps aux - **Kill a process:** - sudo kill \ - **Kill a process by name:** - sudo killall \ **System Monitoring** - **Check system resource usage:** - top - **Check disk usage:** - df -h - **Check memory usage:** - free -m **Network Management** - **Check active network connections:** - netstat -tulnp - **Check network interface details:** - ifconfig - **Ping a website to test connectivity:** - ping google.com **System Control** - **Reboot the system:** - sudo reboot - **Shut down the system:** - sudo shutdown now - **Check system logs:** - sudo journalctl -xe **2. Explain privilege escalation techniques in Ubuntu. Provide examples of sudo and su usage.** ### **Privilege Escalation Techniques in Ubuntu** Privilege escalation in Ubuntu (or any Linux-based system) refers to gaining higher privileges (usually root or administrative access) to execute restricted tasks. It is commonly required for **system administration, software installation, and configuration changes**. Ubuntu enforces security by limiting access to administrative privileges, requiring users to **escalate privileges** using tools like sudo and su. **1. Privilege Escalation Using** sudo **(Recommended Method)** --------------------------------------------------------------- Ubuntu disables the direct **root login** by default for security reasons. Instead, users in the **sudo group** can use sudo to execute commands with **root privileges**. ### **Examples of** sudo **Usage** - **Run a command as root:** - sudo apt update - **Edit a system file:** - sudo nano /etc/hosts - **Restart a system service:** - sudo systemctl restart apache2 - **Switch to a root shell temporarily:** - sudo -i - **Check if a user has sudo access:** - sudo -l **2. Privilege Escalation Using** su **(Switch User)** ------------------------------------------------------ The su command allows a user to switch to another user account, including root. ### **Examples of** su **Usage** - **Switch to the root user (if root password is set):** - su - **Switch to the root user with sudo:** - sudo su - **Switch to another user (e.g., 'john'):** - su john - **Run a command as another user:** - su -c \"whoami\" john **3. Other Privilege Escalation Techniques** -------------------------------------------- ### **(a) Adding a User to the** sudo **Group** If a user needs permanent sudo access, they must be added to the sudo group: sudo usermod -aG sudo username *(Adds username to the sudo group.)* ### **(b) Granting Specific sudo Privileges** Modify the **sudoers file** using: sudo visudo Then, add: username ALL=(ALL) NOPASSWD: /path/to/command *(Allows username to run a specific command without a password.)* **3. Discuss log files in Ubuntu. Where are they stored, and how can they be analyzed?** ### **Log Files in Ubuntu: Storage and Analysis** Log files in Ubuntu store system, application, and security-related events. These logs help **troubleshoot issues, monitor system activity, and ensure security compliance**. **1. Location of Log Files in Ubuntu** -------------------------------------- Most log files are stored in the **/var/log**)** ----------------------------- - The **root directory** (/) is the starting point of the file system hierarchy. All other directories and files are located within the root directory, either directly or as subdirectories. - The root directory itself contains system-critical files, directories, and configurations needed for the system to operate properly. **2. Common Ubuntu Directories and Their Roles** ------------------------------------------------ - **/bin**: Contains essential binary executables (programs) required for basic system operations. These are needed for both single-user mode and for booting the system. - **Example**: ls, cp, mv, cat - **/boot**: Contains files required to boot the system, including the Linux kernel and bootloader configuration files. - **Example**: vmlinuz (Linux kernel), grub (GRUB bootloader files) - **/dev**: Contains device files that represent hardware devices like hard drives, keyboards, and terminals. Devices are treated as files in Linux. - **Example**: /dev/sda (first hard drive), /dev/null (a virtual device representing a black hole) - **/etc**: Contains system-wide configuration files for the operating system and installed software. These files define how the system behaves. - **Example**: /etc/passwd (user account information), /etc/hostname (system hostname) - **/home**: Contains home directories for all users, where personal files and configurations are stored. - **Example**: /home/john (user john\'s home directory) - **/lib**: Contains essential shared libraries and kernel modules required by programs in /bin and /sbin to run. Libraries are similar to dynamic link libraries (DLLs) in Windows. - **Example**: /lib/x86\_64-linux-gnu (architecture-specific libraries) - **/media**: A mount point for removable media devices like USB drives, CDs, and DVDs. - **Example**: /media/usb-drive (USB storage device) - **/mnt**: Traditionally used for mounting filesystems temporarily (e.g., external drives or remote filesystems). - **Example**: /mnt/external (a temporary mount point for an external drive) - **/opt**: Contains optional application software packages, especially third-party software. - **Example**: /opt/google/chrome (Google Chrome installation) - **/proc**: A virtual filesystem that contains information about the system and running processes. It doesn\'t store data on disk but provides data from the kernel about processes and system status. - **Example**: /proc/cpuinfo (CPU information), /proc/meminfo (memory information) - **/root**: The home directory of the **root user** (the superuser). This directory is separate from /home to maintain security for the root user. - **Example**: /root (root\'s personal files) - **/run**: Stores runtime data, such as system information about the current boot session, running processes, and temporary data. - **Example**: /run/utmp (user logins) - **/sbin**: Contains system binaries and executables that are used for system maintenance and administration (often requiring root privileges). - **Example**: /sbin/reboot, /sbin/ifconfig - **/srv**: Contains data for services provided by the system. This could be web server data, FTP data, or any other service data. - **Example**: /srv/www (Web server data for a website) - **/sys**: A virtual filesystem providing access to kernel data structures. It is used by the kernel to expose various system attributes, such as device information and configuration settings. - **Example**: /sys/class/net/eth0 (network interface details) - **/tmp**: Contains temporary files created by programs and the system. Files in this directory are usually deleted upon system reboot. - **Example**: /tmp/tmp1234 (temporary file created by a process) - **/usr**: Contains user applications and their files. This is the largest directory and includes software and libraries not essential for booting or repairing the system. - **Example**: /usr/bin (user executable programs), /usr/share (shared data like documentation, icons) - **/var**: Contains variable files that change in size and content, such as logs, databases, and spool files (for printing, mail, etc.). - **Example**: /var/log (system logs), /var/mail (user mail) **3. Navigating and Managing Files Efficiently** ------------------------------------------------ Once you understand the Ubuntu filesystem structure, navigating and managing files becomes much more straightforward. Below are some **essential commands** to help you navigate and manage files efficiently. ### **(a) Navigating the File System** - **cd (Change Directory)**: Navigate between directories. - **Example**: - cd /home/john \# Navigate to john\'s home directory - cd.. \# Move up one level in the directory hierarchy - cd / \# Navigate to the root directory - cd \~ \# Navigate to the home directory of the current user - **ls (List)**: List files and directories in the current directory. - **Example**: - ls \# List files in the current directory - ls -l \# List with detailed information (permissions, owner, size) - ls -a \# List all files, including hidden files (those starting with a dot) - **pwd (Print Working Directory)**: Displays the current working directory. - **Example**: - pwd \# Output the full path of the current directory ### **(b) File Management** - **cp (Copy)**: Copy files or directories. - **Example**: - cp file.txt /home/john/ \# Copy file.txt to john\'s home directory - cp -r dir1 /home/john/ \# Copy the directory dir1 and its contents - **mv (Move)**: Move or rename files and directories. - **Example**: - mv file.txt /home/john/ \# Move file.txt to john\'s home directory - mv file1.txt file2.txt \# Rename file1.txt to file2.txt - **rm (Remove)**: Delete files or directories. - **Example**: - rm file.txt \# Delete file.txt - rm -r dir1 \# Delete directory dir1 and its contents - **touch**: Create an empty file or update the timestamp of an existing file. - **Example**: - touch newfile.txt \# Create a new empty file called newfile.txt - **find**: Search for files or directories. - **Example**: - find /home/john -name \"\*.txt\" \# Find all.txt files under john\'s home directory - **locate**: Find files by name (faster than find, but requires a database that is updated periodically). - **Example**: - locate file.txt ### **(c) File Permissions and Ownership** - **chmod**: Modify file permissions. - **Example**: - chmod +x script.sh \# Add execute permission to script.sh - chmod 755 file.txt \# Set permissions to rwxr-xr-x for file.txt - **chown**: Change file ownership (user and group). - **Example**: - sudo chown john:john file.txt \# Change the owner and group of file.txt to john - **chgrp**: Change the group ownership of a file. - **Example**: - chgrp dev file.txt \# Change the group of file.txt to dev ### 10. Explain the process of working with text files in Ubuntu. How can you create, edit, and manipulate text files using commands like cat, nano, vim, and grep? **Working with Text Files in Ubuntu** Text files are a fundamental part of working with any Linux-based operating system, including Ubuntu. These files may contain configuration data, scripts, logs, and other textual information. Ubuntu offers a variety of commands and tools to **create**, **edit**, and **manipulate** text files. **1. Creating Text Files** -------------------------- ### **Using** cat The cat (concatenate) command can be used to create text files by redirecting output to a file. - **Example**: - cat \> myfile.txt - **Explanation**: - cat \> myfile.txt: This will create myfile.txt or overwrite it if it already exists. - Type the text, then press Ctrl + D to save and exit. ### **Using** touch The touch command is used to create empty text files or update the timestamp of an existing file. - **Example**: - touch myfile.txt - **Explanation**: This will create an empty myfile.txt file in the current directory. **2. Editing Text Files** ------------------------- Ubuntu provides several text editors to modify text files. Two common editors are **nano** (easy-to-use terminal-based editor) and **vim** (a powerful, feature-rich editor). ### **Using** nano nano is a user-friendly, terminal-based text editor that allows you to create and edit text files. It\'s ideal for beginners and simple text editing tasks. - **Example**: - nano myfile.txt - **Explanation**: This will open myfile.txt in nano. If the file doesn\'t exist, it will be created. You can start typing directly into the file. - **Navigation**: - Use the **arrow keys** to move the cursor. - To **save** the file, press Ctrl + O (write out), then press Enter to confirm the filename. - To **exit** nano, press Ctrl + X. If you\'ve made changes, it will prompt you to save them. - **Commands in nano** (indicated at the bottom): - Ctrl + K: Cut text - Ctrl + U: Paste text - Ctrl + W: Search text ### **Using** vim vim is a more advanced, powerful text editor that provides many features for users who need more control over their text files. However, it has a steeper learning curve. - **Example**: - vim myfile.txt - **Explanation**: This will open myfile.txt in vim. If the file doesn\'t exist, it will be created. - **Navigation**: - vim operates in **different modes**: - **Normal mode** (default): Used for navigation and manipulation. - **Insert mode**: For typing text. - **Command mode**: To save, quit, or perform other operations. - To start typing, press i to enter **insert mode**. - To return to **normal mode**, press Esc. - **Saving and Exiting**: - To save and exit, press :wq and hit Enter. - To exit without saving, press :q! and hit Enter. - **Other Commands**: - :w -- Save without quitting. - :q -- Quit (only if no changes were made). - dd -- Delete a line. - yy -- Copy a line. - p -- Paste a line after the cursor. **3. Viewing Text Files** ------------------------- Ubuntu provides commands like cat, more, and less to view the content of text files. ### **Using** cat The cat (concatenate) command is often used to display the contents of a file. - **Example**: - cat myfile.txt - **Explanation**: This will display the entire content of myfile.txt on the terminal. - **Additional Options**: - To display line numbers, use: - cat -n myfile.txt ### **Using** more **and** less Both more and less allow you to view large files one page at a time. less is more advanced because it allows both forward and backward navigation through the file. - **Example** (more): - more myfile.txt - **Example** (less): - less myfile.txt - **Explanation**: - With more, you can only scroll down using the spacebar or Enter. You can exit by pressing q. - less allows both scrolling up and down using the arrow keys or Page Up/Page Down. Press q to quit. **4. Manipulating Text Files** ------------------------------ There are several commands available to manipulate the contents of text files, such as **grep** for searching, **sed** for editing, and **awk** for processing. ### **Using** grep The grep command is used for searching for specific patterns within files. It's very powerful for finding specific lines of text that match a pattern. - **Example**: - grep \"search-term\" myfile.txt - **Explanation**: This will search for the string \"search-term\" in myfile.txt and display any matching lines. - **Additional Options**: - To search for a term **case-insensitively**: - grep -i \"search-term\" myfile.txt - To display line numbers along with the matching lines: - grep -n \"search-term\" myfile.txt ### **Using** sed sed (stream editor) is used to perform basic text transformations on an input stream (a file or input from a pipeline). - **Example** (Replace text in a file): - sed -i \'s/old-text/new-text/g\' myfile.txt - **Explanation**: This command will replace all occurrences of \"old-text\" with \"new-text\" in myfile.txt. The -i option modifies the file in-place. - **Additional Operations**: - To delete a specific line: - sed -i \'3d\' myfile.txt ### **Using** awk awk is a powerful text-processing tool used for pattern scanning and processing. It works well for manipulating columns of text in structured files. - **Example**: - awk \'{print \$1}\' myfile.txt - **Explanation**: This command prints the first column of each line in myfile.txt. Columns are usually separated by spaces or tabs. **5. Redirection and Piping** ----------------------------- Redirection and piping allow you to create and manipulate text files by connecting commands together. - **Redirecting output to a file**: - echo \"Hello, World!\" \> myfile.txt - **Appending output to a file**: - echo \"Another line\" \>\> myfile.txt - **Piping**: - cat myfile.txt \| grep \"search-term\"