Basics of Command-Line Administration: Mastering the Linux Command Line for Ubuntu Server

The command line is the heart of Linux server administration. Whether you’re a beginner or an experienced system administrator, understanding the basics of command-line administration is crucial for managing your Ubuntu Server efficiently. This article provides an in-depth introduction to the Linux command line, covering essential commands, tools, and techniques for managing files, directories, and the server environment.


Why Learn the Command Line?

The Linux command line, accessed via the terminal, is a powerful tool for interacting with the operating system. Unlike graphical interfaces, the command line offers:

  • Speed: Tasks can be performed faster with commands than through GUI interfaces.
  • Flexibility: Automate processes and perform complex operations using scripting.
  • Control: Access deeper system functionality and troubleshoot issues effectively.

Accessing the Command Line on Ubuntu Server

Ubuntu Server installations do not include a graphical interface by default, so all interactions occur through the terminal. Here are common ways to access the command line:

1. Direct Access via Local Console

After logging into your Ubuntu Server, you’ll be presented with the terminal interface.

2. Remote Access via SSH

Use Secure Shell (SSH) to access your server remotely:

ssh username@server_ip

Essential Linux Commands for Beginners

1. Navigating the Filesystem

Understanding the Linux filesystem hierarchy is critical for managing files and directories.

View Current Directory: pwd

Displays the current working directory:

pwd

List Directory Contents: ls

Lists files and directories in the current location:

ls

Options:

  • ls -l: Detailed list with permissions, ownership, and size.
  • ls -a: Includes hidden files (files starting with .).

Change Directory: cd

Moves to a different directory:

cd /path/to/directory

Examples:

  • cd ..: Moves up one level.
  • cd ~: Moves to the home directory.

2. Managing Files and Directories

Create a Directory: mkdir

Creates a new directory:

mkdir directory_name

Example: Create a subdirectory structure.

mkdir -p /home/user/documents/projects

Explanation:

  • The -p option creates parent directories if they don’t exist (e.g., documents and projects).

Remove a Directory: rmdir

Deletes an empty directory:

rmdir directory_name

Example:

rmdir /home/user/old_directory

Copy Files: cp

Copies files or directories:

cp source_file destination_file

Example: Copy a file to a new directory.

cp /home/user/file.txt /home/user/backup/file.txt

To copy entire directories:

cp -r /home/user/documents /home/user/backup/documents

Move or Rename Files: mv

Moves or renames files:

mv old_name new_name

Example: Rename a file.

mv file.txt renamed_file.txt

Move files to another directory:

mv /home/user/file.txt /home/user/backup/

Delete Files: rm

Deletes files or directories:

rm file_name

Example: Delete a file.

rm /home/user/file.txt

To delete directories and their contents:

rm -r /home/user/old_directory

Warning: Use caution with rm to avoid accidental deletions.


3. Viewing and Editing Files

View File Contents: cat

Displays the contents of a file:

cat file_name

Example:

cat /etc/hostname

View File Contents with Pagination: less

Allows scrolling through file contents:

less file_name

Example:

less /var/log/syslog

Create or Edit Files: nano

A beginner-friendly text editor:

nano file_name

Example: Create a new file or edit an existing one.

nano /home/user/new_file.txt

Search for Text in Files: grep

Searches for a specific string in files:

grep "search_term" file_name

Example: Find error messages in a log file.

grep "error" /var/log/syslog

4. System Administration Commands

Check Disk Usage: df

Displays disk space usage:

df -h

Options:

  • -h: Human-readable format.

Check Memory Usage: free

Shows memory usage:

free -h

Monitor System Processes: top

Displays running processes and system resource usage:

top

Alternative:

  • htop: A more user-friendly process monitor (install via sudo apt install htop).

Manage Services: systemctl

Controls system services:

sudo systemctl status service_name
sudo systemctl start service_name
sudo systemctl stop service_name
sudo systemctl restart service_name

Shutdown or Reboot: shutdown

Shut down or restart the server:

sudo shutdown now    # Immediate shutdown
sudo shutdown -r now # Immediate restart

5. File Permissions and Ownership

Linux uses a permission model to control access to files and directories.

View File Permissions: ls -l

Displays file permissions in detailed format:

ls -l

Change File Permissions: chmod

Modifies file permissions:

chmod 644 file_name

Change File Ownership: chown

Changes file ownership:

sudo chown new_owner:new_group file_name

6. Searching and Finding Files

Locate Files: locate

The locate command is not installed by default. Install it first:

sudo apt update
sudo apt install mlocate

Update the database:

sudo updatedb

Search for files:

locate file_name

Find Files: find

Searches for files based on criteria:

find /path -name "file_name"

7. Networking Commands

Check Network Connectivity: ping

Tests connectivity to a host:

ping google.com

Check Open Ports: ss

The netstat command is deprecated and not installed by default. Use ss, a modern alternative:

ss -tuln

Explanation:

  • -t: Displays TCP connections.
  • -u: Displays UDP connections.
  • -l: Displays listening ports.
  • -n: Shows numerical addresses and ports.

Test Remote Connectivity: curl

Fetches data from a URL:

curl http://example.com

Practical Tips for Command-Line Administration

1. Use Tab Completion

Press Tab while typing a command or filename to autocomplete paths or commands.

2. Use History Commands

Access previously run commands:

history

Run a specific command from history:

!number

3. Redirect Output

Save command output to a file:

command > output_file

4. Chain Commands

Run multiple commands sequentially:

command1 && command2

Example Workflow: Setting Up a Web Server

Here’s a step-by-step guide to setting up a web server using basic commands. Follow these instructions to configure your custom website and ensure it replaces the default Apache page.


1. Update the System

Keeping your system up-to-date is essential for security and stability. Run the following command to update and upgrade all packages:

sudo apt update && sudo apt upgrade -y

2. Install Apache

Install the Apache web server:

sudo apt install apache2 -y

Once installed, verify that Apache is running:

sudo systemctl status apache2

You should see a status message like: Active: active (running).


3. Create a Directory for Your Website

Create a directory to store your website files. For this example, we’ll create a directory named mywebsite:

sudo mkdir -p /var/www/mywebsite

4. Set Permissions

Set the correct ownership and permissions so that Apache can access the website files:

sudo chown -R www-data:www-data /var/www/mywebsite
sudo chmod -R 755 /var/www/mywebsite

5. Create a Sample HTML File

Add a simple index.html file to serve as your website’s homepage:

sudo tee /var/www/mywebsite/index.html > /dev/null << EOF
<!DOCTYPE html>
<html>
<head>
<title>Welcome</title>
</head>
<body>
<h1>Welcome to My Website</h1>
</body>
</html>
EOF

This creates a basic page that says: “Welcome to My Website”.


6. Configure a Virtual Host

To serve your custom website, create a new virtual host configuration file for Apache:

sudo nano /etc/apache2/sites-available/mywebsite.conf

Paste the following content into the file:

<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/mywebsite
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Save and exit the file by pressing Ctrl+O, then Ctrl+X.


7. Enable Your Website

Enable the new virtual host configuration:

sudo a2ensite mywebsite.conf

Disable Apache’s default site configuration to ensure your website is served:

sudo a2dissite 000-default.conf

Reload Apache to apply the changes:

sudo systemctl reload apache2

8. Test Connectivity

Check if your website is working by running:

curl http://localhost

You should see the HTML content of your custom index.html file:

<!DOCTYPE html>
<html>
<head>
<title>Welcome</title>
</head>
<body>
<h1>Welcome to My Website</h1>
</body>
</html>

Alternatively, open a browser and navigate to http://<your-server-ip> to view your website.


Troubleshooting

If you still see the default Apache page or encounter issues:

  • Ensure Your Site is Enabled
    Verify that your configuration is active:
ls /etc/apache2/sites-enabled/ 
  • You should see mywebsite.conf. If not, enable it using:
sudo a2ensite mywebsite.conf
  • Check Apache Logs
    Look for errors in the logs:
sudo tail -f /var/log/apache2/error.log
  • Restart Apache
    If the issue persists, restart Apache:
sudo systemctl restart apache2

Conclusion

Mastering the Linux command line is essential for effective Ubuntu Server administration. By learning and practicing these commands, you’ll gain confidence in managing your server environment, troubleshooting issues, and performing administrative tasks efficiently.

Next Steps: Managing Users and Permissions

Once you’re comfortable with the basics of command-line administration, the next critical step in server management is understanding users, groups, and permissions. Properly managing user accounts and access controls is essential for maintaining security and ensuring only authorized individuals can access or modify sensitive files and resources on your server.

In the next article, “Managing Users and Permissions,” we’ll cover:

  • How to create, modify, and delete user accounts.
  • Managing groups for efficient access control.
  • Understanding file and directory permissions (read, write, execute).
  • Using advanced tools like sudo for privilege escalation.
  • Best practices for securing user accounts on Ubuntu Server.

Stay tuned for actionable insights to enhance your security and access management!

Leave a Comment