Introduction
Welcome to the world of Linux administration! As a new Linux administrator, understanding the core components and commands is crucial. This detailed guide will help you get started with essential Linux tasks and concepts.
Understanding the Linux Filesystem Hierarchy
Linux uses a hierarchical filesystem structure. Here are some key directories:
/
: The root directory, where everything begins./bin
: Essential command binaries (e.g.,ls
,cp
)./sbin
: Essential system binaries (e.g.,iptables
,reboot
)./etc
: Configuration files for the system./home
: Home directories for users./var
: Variable files, like logs and databases./usr
: User binaries and read-only data./tmp
: Temporary files.
Basic Linux Commands
Navigating the Filesystem
pwd
: Print Working Directory. Shows your current directory.
pwd
ls
: List directory contents. Usels -l
for a detailed list.
ls
ls -l
cd
: Change Directory.
cd /etc
cd /home/yourusername
File Operations
cp
: Copy files or directories.
cp source_file destination_file
cp -r source_directory destination_directory
mv
: Move or rename files or directories.
mv old_name new_name
mv file_name /path/to/new_location/
rm
: Remove files or directories. Userm -r
for directories.
rm file_name
rm -r directory_name
mkdir
: Create a new directory.
mkdir new_directory
User and Group Management
Managing Users
adduser
: Add a new user.
sudo adduser newusername
passwd
: Change user password.
sudo passwd username
deluser
: Remove a user.
sudo deluser username
Managing Groups
groupadd
: Add a new group.
sudo groupadd newgroup
usermod
: Modify a user account (e.g., add a user to a group).
sudo usermod -aG groupname username
groups
: Display group memberships for a user.
groups username
File Permissions and Ownership
Understanding Permissions
ls -l
: Lists files with permissions.
ls -l
The output looks like:
-rwxr-xr-- 1 owner group size date time filename
r
(read),w
(write),x
(execute) for user (owner), group, and others.
Changing Permissions
chmod
: Change file permissions.
chmod u+x filename # Add execute permission for the user
chmod g-w filename # Remove write permission for the group
chmod o+r filename # Add read permission for others
chmod 755 filename # Set permissions to rwxr-xr-x
Changing Ownership
chown
: Change file owner and group.
sudo chown newowner:newgroup filename
sudo chown newowner filename
sudo chown :newgroup filename
Process Management
Viewing Processes
ps
: Report a snapshot of current processes.
ps aux
top
: Display Linux tasks.
top
htop
: Interactive process viewer (requires installation).
sudo apt install htop
htop
Managing Processes
kill
: Terminate a process.
kill PID
kill -9 PID # Force kill
pkill
: Kill processes by name.
pkill process_name
Network Management
Viewing Network Configuration
ifconfig
: Interface configuration (useip
in newer systems).
ifconfig
ip addr show
ping
: Check connectivity.
ping 8.8.8.8
Managing Network Interfaces
ifup
andifdown
: Bring network interfaces up or down.
sudo ifup eth0
sudo ifdown eth0
Package Management
Debian-Based Systems (e.g., Ubuntu)
apt-get
: Package handling utility.
sudo apt-get update # Update package lists
sudo apt-get upgrade # Upgrade all packages
sudo apt-get install package_name # Install a package
sudo apt-get remove package_name # Remove a package
sudo apt-get autoremove # Remove unnecessary packages
Red Hat-Based Systems (e.g., CentOS)
yum
: Package manager for RPM-based distributions.
sudo yum update # Update package lists
sudo yum upgrade # Upgrade all packages
sudo yum install package_name # Install a package
sudo yum remove package_name # Remove a package
sudo yum autoremove # Remove unnecessary packages
System Monitoring and Logs
Viewing Logs
dmesg
: Print kernel ring buffer messages.
dmesg
journalctl
: Query and display messages from the journal.
journalctl
- Log files are generally located in
/var/log/
. For example:
cat /var/log/syslog # View system log
tail -f /var/log/syslog # Continuously view the latest log entries
Shell Scripting
Creating a Basic Shell Script
- Create a new file with a
.sh
extension.
nano myscript.sh
- Add the following content:
#!/bin/bash
echo "Hello, World!"
- Save and exit (Ctrl+X, then Y, then Enter).
- Make the script executable.
chmod +x myscript.sh
- Run the script.
./myscript.sh
Conclusion
These foundational commands and concepts are essential for every Linux administrator. As you become more comfortable, you’ll explore advanced topics like shell scripting, system security, and automation. Remember, the man
command is your friend for detailed information about any command:
man command_name
I hope this guide serves as a valuable resource as you begin your journey as a Linux administrator!