This comprehensive guide provides an overview of essential Ubuntu commands along with practical examples to help users navigate and manage their Ubuntu Linux systems effectively.
- Navigation Commands
- File and Directory Operations
- System Information
- Package Management
- User and Group Management
- Process Management
- Network Commands
- File Editing
- System Services
- Permissions
- Archiving and Compression
- Search Commands
Navigation commands are fundamental for moving around the file system and managing directories.
Print the current working directory.
pwd
Example:
/home/user/documents
Change the current directory.
cd /path/to/directory
Example:
cd /home/user/documents
List files and directories in the current directory.
ls
ls -l
ls -a
Examples:
# Basic listing
ls
# Detailed listing
ls -l
# Show hidden files
ls -a
These commands help you navigate through directories, understand your current location, and inspect the contents of a directory.
File and directory operations are essential for managing files, copying, moving, and removing them.
Copy files or directories.
cp source_file destination
cp -r source_directory destination
Examples:
# Copy a file to a destination
cp file.txt /path/to/destination
# Copy a directory and its contents to a destination
cp -r directory /path/to/destination
Move or rename files or directories.
mv source destination
mv oldname.txt newname.txt
Examples:
# Move a file to a destination
mv file.txt /path/to/destination
# Rename a file
mv oldname.txt newname.txt
Remove files or directories.
rm file.txt
rm -r directory
Examples:
# Remove a file
rm file.txt
# Remove a directory and its contents
rm -r directory
Create a new directory.
mkdir new_directory
Example:
# Create a new directory
mkdir documents
These commands enable you to manage files and directories, whether you're copying them, moving them to a different location, renaming them, or deleting them. Use them with caution, especially when dealing with the rm command, as it permanently deletes files.
System information commands provide details about the operating system and its configuration.
Print system information.
uname -a
Example:
Linux hostname 5.4.0-104-generic #127-Ubuntu SMP Thu Apr 29 14:20:18 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
Display Linux Standard Base (LSB) information.
lsb_release -a
Example:
Distributor ID: Ubuntu
Description: Ubuntu 20.04.2 LTS
Release: 20.04
Codename: focal
Show disk space usage.
df -h
Example:
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 20G 8.2G 11G 44% /
These commands help you understand the system environment, kernel version, and disk space utilization, providing crucial information for system administration and troubleshooting.
Package management commands are used to install, upgrade, and remove software packages on Ubuntu.
The apt
command is the primary package management tool for Ubuntu.
Update the local package database.
sudo apt update
Upgrade installed packages to their latest versions.
sudo apt upgrade
Install a new package.
sudo apt install package_name
Remove a package.
sudo apt remove package_name
Examples:
# Update package list
sudo apt update
# Upgrade installed packages
sudo apt upgrade
# Install a new package
sudo apt install htop
# Remove a package
sudo apt remove firefox
These commands facilitate the installation, upgrade, and removal of software packages on your Ubuntu system. The apt tool ensures proper dependency management and simplifies package-related tasks.
User and group management commands are essential for handling user accounts and associated groups on Ubuntu.
Create a new user account.
sudo useradd username
Example:
sudo useradd john_doe
Change the password for a user.
sudo passwd username
Example:
sudo passwd john_doe
Modify user properties, such as group membership.
sudo usermod -aG groupname username
Example:
sudo usermod -aG sudo john_doe
These commands allow you to manage user accounts by creating new users, updating their passwords, and modifying their group memberships. Proper user and group management are crucial for controlling access and permissions on a system.
Process management commands are used to monitor and control running processes on Ubuntu.
Display information about running processes.
ps aux
Example:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
user 1234 0.0 0.1 123456 7890 ? Ss Dec01 0:00 /usr/bin/example
Display and manage processes in real-time.
top
Example:
top - 14:28:35 up 1:24, 1 user, load average: 0.00, 0.01, 0.05
Tasks: 123 total, 1 running, 122 sleeping, 0 stopped, 0 zombie
%Cpu(s): 2.0 us, 1.0 sy, 0.0 ni, 96.8 id, 0.2 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 7862.2 total, 1234.5 free, 4567.8 used, 1059.9 buff/cache
MiB Swap: 1024.0 total, 842.5 free, 181.5 used. 2787.2 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1234 user 20 0 123456 7890 5678 S 0.0 0.1 0:00.01 example
Terminate a process by sending a signal.
kill process_id
Example:
kill 1234
These commands provide insights into running processes, their resource usage, and allow you to manage them effectively. Use caution when terminating processes to avoid unintended consequences.
Network commands on Ubuntu are crucial for configuring network interfaces, checking connectivity, and monitoring network-related information.
Configure network interfaces and display their configuration.
ifconfig
Example:
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.2 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::a00:27ff:fe51:bd4c prefixlen 64 scopeid 0x20<link>
ether 08:00:27:51:bd:4c txqueuelen 1000 (Ethernet)
RX packets 12345 bytes 6789012 (6.7 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 56789 bytes 12345678 (12.3 MB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
Check network connectivity to a host.
ping example.com
Example:
PING example.com (93.184.216.34) 56(84) bytes of data.
64 bytes from 93.184.216.34: icmp_seq=1 ttl=56 time=11.1 ms
Display information about network connections, routing tables, and interface statistics.
netstat -a
Example:
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
udp 0 0 0.0.0.0:5353 0.0.0.0:*
These commands are essential for managing network configurations, checking connectivity, and troubleshooting network-related issues. The examples provided offer insights into current network configurations and connectivity checks.
File editing commands are used to create, view, and modify text files directly from the command line.
Open a text file for editing using the nano
text editor.
nano filename
Example:
nano document.txt
Use the arrow keys to navigate, and press Ctrl + X
to exit (followed by Y
to confirm changes and Enter
).
Open a text file for editing using the vim
text editor.
vim filename
Example:
vim document.txt
In vim
, press i
to enter insert mode, make changes, and press Esc
to exit insert mode. To save changes and exit, type :wq
and press Enter
.
These commands provide basic text editing capabilities directly from the command line. Choose between nano
for a simple interface or vim
for a more powerful and feature-rich text editor.
System services on Ubuntu are controlled using the systemctl
command. This allows for starting, stopping, restarting, and checking the status of services.
Control system services using the systemctl
command.
sudo systemctl start service_name
Example:
sudo systemctl start apache2
sudo systemctl stop service_name
Example:
sudo systemctl stop apache2
sudo systemctl restart service_name
Example:
sudo systemctl restart apache2
sudo systemctl status service_name
Example:
sudo systemctl status apache2
These commands provide a way to manage system services, such as web servers, databases, or other background processes. Use them to ensure that services are running as expected and to troubleshoot any issues.
Permissions in Linux determine who can access files and directories and what actions they can perform. The chmod
and chown
commands are used to modify file and directory permissions.
Change the permissions of a file or directory.
chmod permissions filename
Example:
chmod +x script.sh
chmod 755 filename
Example:
chmod 644 document.txt
Change the owner of a file or directory.
chown new_owner:new_group filename
Example:
chown john:users document.txt
These commands are crucial for controlling access to files and directories. The chmod
command modifies permissions, specifying who can read, write, or execute a file, while the chown
command changes the owner of a file, allowing specific users or groups to have control over it. Use these commands with caution to maintain a secure and well-managed file system.
Archiving and compression commands in Ubuntu are used to bundle files and reduce their size for efficient storage and transfer.
Create and extract archive files using the tar
command.
tar -cvf archive.tar file1 file2
Example:
tar -cvf backup.tar /home/user/documents
tar -xvf archive.tar
Example:
tar -xvf backup.tar
Compress files using the gzip
command.
gzip filename
Example:
gzip document.txt
gzip -d filename.gz
Example:
gzip -d document.txt.gz
These commands allow you to create archives of multiple files or directories and compress individual files to save space. The combination of tar
and gzip
is commonly used for creating compressed archives on Ubuntu systems.
Search commands in Ubuntu help locate files, directories, and content within files.
Search for files and directories based on various criteria.
find /path/to/search -name "filename"
Example:
find /home/user/documents -name "*.txt"
Search for patterns within files.
grep "pattern" filename
Example:
grep "error" log.txt
These commands are essential for locating files and content within them. The find
command is versatile and allows for complex search criteria, while grep
is powerful for searching within the content of files. These tools are invaluable for system administration and troubleshooting.