Bash (Bourne Again Shell) is one of the most commonly used shell scripting languages in Linux and Unix-based systems. It plays an essential role in server administration, allowing sysadmins to manage, configure, and troubleshoot servers efficiently. As a server administrator, understanding the basic Bash commands is crucial for managing server environments. These commands provide powerful tools for automating tasks, monitoring resources, managing users, and controlling the server environment.
In this article, we’ll explore over 100 essential Bash commands for server admins. These commands will help sysadmins execute regular tasks, from system navigation to advanced administrative functions. The commands are grouped to provide clarity and organization, ensuring that you understand how and when to use them.
System Navigation Commands
-
pwd
-
Usage:
pwd
-
Explanation: The
pwd
Command stands for “print working directory.” It is used to display the current directory you are in. This is essential when navigating through directories to confirm your position within the file system.
-
-
cd
-
Usage:
cd /home/user
-
Explanation:
cd
stands for “change directory.” It allows you to move from your current directory to a different directory. You can use relative paths (likecd Documents
) or absolute paths (likecd /var/log
).
-
-
ls
-
Usage:
ls -l
-
Explanation: The
ls
command lists files and directories in the current directory. Using the-l
option gives you a detailed listing, including file permissions, ownership, size, and last modified time.
-
-
clear
-
Usage:
clear
-
Explanation: The
clear
command is used to clean up the terminal by clearing the screen. It’s often used to remove unnecessary output from the terminal, making it easier to work with.
-
-
mkdir
-
Usage:
mkdir new_folder
-
Explanation: The
mkdir
command is used to create a new directory. It’s essential for organizing files into folders, especially when managing large projects or logs.
-
-
rmdir
-
Usage:
rmdir folder_name
-
Explanation: The
rmdir
command removes empty directories. If you try to remove a non-empty directory, it will fail, requiring you to userm -r
instead.
-
-
touch
-
Usage:
touch newfile.txt
-
Explanation: The
touch
command is used to create empty files or to update the timestamp of an existing file. It’s a quick way to create a placeholder file.
-
-
cp
-
Usage:
cp file1.txt /home/user/
-
Explanation: The
cp
command copies files or directories from one location to another. It can be combined with options like-r
for recursive copying of directories.
-
-
mv
-
Usage:
mv oldfile.txt newfile.txt
-
Explanation: The
mv
command is used to move or rename files. It can be used for organizing files into different directories or renaming them.
-
-
rm
-
Usage:
rm -rf /folder_name/
-
Explanation: The
rm
command removes files and directories. The-r
option is for removing directories recursively, and-f
forces the removal without confirmation.
-
File Permissions and Ownership Commands
-
chmod
-
Usage:
chmod 755 file.sh
-
Explanation:
chmod
Changes the permissions of a file or directory. The numeric mode (e.g.,755
) sets specific read, write, and execute permissions for user, group, and others.
-
-
chown
-
Usage:
chown user:group file.txt
-
Explanation:
chown
changes the ownership of a file or directory. The command assigns a user and group to a file or directory, essential for managing access control.
-
-
chgrp
-
Usage:
chgrp groupname file.txt
-
Explanation:
chgrp
changes the group ownership of a file. It’s useful when you need to modify group permissions without changing user ownership.
-
-
umask
-
Usage:
umask 022
-
Explanation: The
umask
command sets default file creation permissions. Aumask
of022
means new files are created with644
permissions (read and write for the user, and read-only for others).
-
-
lsattr
-
Usage:
lsattr file.txt
-
Explanation:
lsattr
displays the attributes of files and directories. It’s useful for viewing files that are immutable or append-only, typically used in high-security environments.
-
-
chattr
-
Usage:
chattr +i file.txt
-
Explanation:
chattr
changes file attributes in Linux. Adding+i
makes the file immutable, meaning it can’t be modified, deleted, or renamed until the attribute is removed.
-
-
getfacl
-
Usage:
getfacl file.txt
-
Explanation:
getfacl
retrieves the access control list (ACL) of a file, allowing administrators to see who has access to a file or directory and what level of access they have.
-
-
setfacl
-
Usage:
setfacl -m u:username:rwx file.txt
-
Explanation:
setfacl
modifies the access control list (ACL) of a file or directory. You can grant specific users permissions like read, write, or execute.
-
-
umask
-
Usage:
umask 027
-
Explanation:
umask
sets default permissions for new files. It’s particularly useful in multi-user environments to control how files and directories are created by default.
-
-
find
-
Usage:
find /home/user/ -name "*.txt"
-
Explanation:
find
searches for files in a directory hierarchy based on criteria like name, type, or size. It’s essential for locating files in large file systems.
-
Process Management Commands
-
ps
-
Usage:
ps aux
-
Explanation:
ps
displays a snapshot of running processes. Theaux
option provides detailed information about all processes running on the system, including the user, PID, memory usage, and more.
-
-
top
-
Usage:
top
-
Explanation:
top
shows a real-time view of system processes, including CPU and memory usage. It’s useful for monitoring system performance and identifying resource hogs.
-
-
kill
-
Usage:
kill 1234
-
Explanation:
kill
sends a signal to a process to terminate it. By default, it sends a termination signal (SIGTERM), but you can specify other signals like SIGKILL for forceful termination.
-
-
killall
-
Usage:
killall process_name
-
Explanation:
killall
terminates processes by name rather than process ID. It’s handy when you need to kill all instances of a particular program, such as a server or application.
-
-
bg
-
Usage:
bg
-
Explanation: The
bg
command resumes a suspended job in the background. This allows you to continue working in the terminal while the job runs in the background.
-
-
fg
-
Usage:
fg
-
Explanation:
fg
brings a background process to the foreground. It’s useful for resuming a process that was previously paused or running in the background.
-
-
jobs
-
Usage:
jobs
-
Explanation:
jobs
lists all background and stopped jobs in the current terminal session. This is important for managing tasks running in the background.
-
-
nice
-
Usage:
nice -n 10 command
-
Explanation:
nice
allows you to start a process with a specified priority. A higher number means lower priority, and a negative number increases the priority.
-
-
renice
-
Usage:
renice -n 5 -p 1234
-
Explanation:
renice
changes the priority of an already running process. This is useful for adjusting the priority of system processes dynamically.
-
-
pstree
-
Usage:
pstree
-
Explanation:
pstree
displays running processes in a tree-like format, showing their hierarchical relationships. It’s a visual way to understand how processes are connected.
-
System Monitoring and Resource Management
-
df
-
Usage:
df -h
-
Explanation: The
df
command shows disk space usage on the file system. The-h
option provides human-readable output, which makes it easier to interpret the size of disk space in terms of MBs, GBs, etc.
-
du
-
Usage:
du -sh /var/log
-
Explanation:
du
stands for disk usage. It’s used to estimate the disk space used by files and directories. The-s
option gives the summary for a directory, and-h
makes it human-readable.
-
free
-
Usage:
free -m
-
Explanation: The
free
command displays the amount of free and used memory (RAM) in the system. The-m
option shows the memory usage in megabytes, making it easier to monitor system performance.
-
uptime
-
Usage:
uptime
-
Explanation: The the
uptime
command shows how long the system has been running since the last reboot, along with the load averages for the past 1, 5, and 15 minutes.
-
vmstat
-
Usage:
vmstat 5
-
Explanation:
vmstat
(Virtual Memory Statistics) provides detailed information about system processes, memory, paging, block I/O, traps, and CPU activity. You can specify an interval (e.g.,5
seconds) to monitor system performance over time.
-
iostat
-
Usage:
iostat -x
-
Explanation: The
iostat
command reports on CPU and input/output device performance. It provides valuable insights into disk activity, which can help identify bottlenecks or hardware-related performance issues.
-
sar
-
Usage:
sar -u 1 3
-
Explanation:
sar
(System Activity Report) is a powerful tool for collecting, reporting, and saving system activity information. The example command reports CPU usage every second for 3 seconds.
-
netstat
-
Usage:
netstat -tuln
-
Explanation: The
netstat
command provides network statistics and shows active network connections. The-tuln
option filters the output to show TCP, UDP, listening ports, and numeric addresses.
-
ss
-
Usage:
ss -tuln
-
Explanation:
ss
(Socket Stat) is a faster alternative tonetstat
for displaying network connections, listening ports, and other network-related information. It can display detailed socket statistics.
-
top
-
Usage:
top
-
Explanation: The
top
command is used to monitor real-time resource usage of processes. It shows CPU, memory, and swap usage, along with the top-running processes on your system.
Network Configuration and Troubleshooting Commands
-
ping
-
Usage:
ping google.com
-
Explanation: The the
ping
command is used to test network connectivity. It sends packets to a target host and waits for a response. It helps verify if a server or network resource is reachable.
-
ifconfig
-
Usage:
ifconfig eth0
-
Explanation:
ifconfig
(interface configuration) displays or configures network interfaces. The command shows details about the network interfaces on the system, such as IP addresses, netmasks, and hardware addresses.
-
ip
-
Usage:
ip addr show
-
Explanation: The
ip
command is a more modern and versatile tool for network management. It’s used to show and configure IP addresses, routes, and network interfaces. Theip addr show
command displays the IP addresses of all interfaces.
-
traceroute
-
Usage:
traceroute google.com
-
Explanation:
traceroute
is used to trace the path packets take to reach a network host. It shows each hop along the route and is useful for diagnosing network issues like latency or routing problems.
-
nslookup
-
Usage:
nslookup google.com
-
Explanation:
nslookup
is used to query DNS (Domain Name System) and retrieve information about a domain name, including its IP address. It’s helpful for troubleshooting DNS issues.
-
dig
-
Usage:
dig google.com
-
Explanation:
dig
(Domain Information Groper) is another DNS lookup tool that provides more detailed DNS query information thannslookup
. It helps diagnose DNS resolution issues.
-
wget
-
Usage:
wget https://example.com/file.txt
-
Explanation: The
wget
command is used to download files from the internet. It supports HTTP, HTTPS, and FTP protocols and can download files in the background or resume interrupted downloads.
-
curl
-
Usage:
curl https://example.com
-
Explanation:
curl
is another tool for transferring data over URLs. It supports various protocols like HTTP, FTP, and more. It’s often used for making API requests or downloading files from the internet.
-
netcat (nc)
-
Usage:
nc -zv 192.168.1.1 80-90
-
Explanation:
netcat
(ornc
) is a networking tool used for reading from and writing to network connections. It can be used for port scanning, testing server connections, or debugging network services.
-
ssh
-
Usage:
ssh user@hostname
-
Explanation:
ssh
(Secure Shell) is used to securely connect to remote servers over an encrypted network. It’s one of the most critical commands for server administration, allowing admins to access and manage servers remotely.
User Management and Permissions
-
useradd
-
Usage:
useradd john
-
Explanation: The the
useradd
command creates a new user on the system. You can specify the user’s home directory, shell, and other properties during user creation.
-
usermod
-
Usage:
usermod -aG sudo john
-
Explanation:
usermod
modifies the properties of an existing user. For example, you can add the user to a new group (as seen with the-aG
flag to add the user to thesudo
group).
-
userdel
-
Usage:
userdel john
-
Explanation:
userdel
deletes a user account. You can also use the-r
option to remove the user’s home directory and files when deleting the account.
-
groupadd
-
Usage:
groupadd admin
-
Explanation: The
groupadd
command creates a new group on the system. Groups are used for managing access control to files and directories.
-
groupdel
-
Usage:
groupdel admin
-
Explanation:
groupdel
deletes a group from the system. It’s essential to remove groups that are no longer needed to maintain security and clean configuration.
-
passwd
-
Usage:
passwd john
-
Explanation: The
passwd
command changes a user’s password. It’s used to set or reset the password for a user account on the system.
-
chage
-
Usage:
chage -l john
-
Explanation: The
chage
command allows you to view or set user password expiration information. It’s used to enforce password policies and improve system security.
-
id
-
Usage:
id john
-
Explanation: The
id
command displays the user and group IDs (UID and GID) of a specified user, as well as the groups they belong to. This is useful for verifying user privileges and group memberships.
-
groups
-
Usage:
groups john
-
Explanation: The
groups
command shows which groups a user is a member of. This can be helpful for managing group-based permissions.
-
whoami
-
Usage:
whoami
-
Explanation: The
whoami
command displays the current logged-in user. It’s often used to verify which user is currently active in a session.
System Shutdown and Reboot Commands
-
shutdown
-
Usage:
shutdown -h now
-
Explanation: The
shutdown
command is used to shut down or reboot the system. The-h
flag tells the system to halt (shut down), andnow
specifies that it should happen immediately.
-
reboot
-
Usage:
reboot
-
Explanation: The
reboot
command reboots the system. It’s often used to apply system updates or when troubleshooting requires a restart of the server.
-
halt
-
Usage:
halt
-
Explanation: The
halt
command stops the system immediately. It’s similar toshutdown -h
but doesn’t provide as much flexibility in specifying a delay.
-
poweroff
-
Usage:
poweroff
-
Explanation: The the
poweroff
command is used to turn off the system’s power immediately. It’s equivalent to pressing the power button on a physical server.
-
reboot -f
-
Usage:
reboot -f
-
Explanation: The
reboot -f
command forces the system to reboot without gracefully shutting down processes. It should be used with caution, as it can cause data loss.
-
chmod
-
Usage:
chmod 755 /path/to/file
-
Explanation: The
chmod
command is used to change file permissions. In the example,755
sets the permissions to allow the owner to read, write, and execute, while the group and others can only read and execute.
-
chown
-
Usage:
chown root:root /path/to/file
-
Explanation:
chown
changes the ownership of a file or directory. In this case, the file’s owner and group are both set toroot
. This is crucial for securing sensitive files and ensuring correct access control.
-
chgrp
-
Usage:
chgrp admins /path/to/file
-
Explanation: The The
chgrp
command changes the group ownership of a file. This is useful for managing access within specific user groups.
-
umask
-
Usage:
umask 022
-
Explanation:
umask
sets the default file creation permissions for new files and directories. The value022
ensures that new files are created with permissions that allow the owner to read/write, but restrict access for others.
-
setfacl
-
Usage:
setfacl -m u:john:r /path/to/file
-
Explanation: The
setfacl
command is used to set Access Control Lists (ACLs) on files. It allows more granular control over who can access a file, overriding the standard file permissions.
-
getfacl
-
Usage:
getfacl /path/to/file
-
Explanation:
getfacl
retrieves the ACL of a file, displaying the specific permissions granted to different users and groups.
-
find
-
Usage:
find / -name "file.txt"
-
Explanation: The
find
command is one of the most powerful commands for searching files on the system. It allows you to search for files and directories by name, type, size, and other criteria.
-
locate
-
Usage:
locate file.txt
-
Explanation:
locate
quickly searches the system’s database for a file by name. It’s much faster thanfind
because it uses a pre-built index, but the index must be updated regularly usingupdatedb
.
-
updatedb
-
Usage:
updatedb
-
Explanation:
updatedb
updates the database used bylocate
to ensure the search index is up-to-date. This command is usually scheduled to run periodically via cron.
-
ps
-
Usage:
ps aux
-
Explanation: The
ps
command shows a snapshot of the current processes running on the system. Theaux
options provide a detailed view of all processes, including those started by other users.
-
kill
-
Usage:
kill 12345
-
Explanation: It
kill
is used to terminate a process by its PID (Process ID). In this case,12345
is the PID of the process you wish to stop. It’s useful when a process becomes unresponsive.
-
killall
-
Usage:
killall apache2
-
Explanation:
killall
terminates processes by name, rather than by PID. In this example, allapache2
processes will be stopped, which is helpful for restarting services.
-
nice
-
Usage:
nice -n 10 command
-
Explanation: The
nice
command starts a process with a modified scheduling priority. The-n
option specifies the priority level, where lower numbers represent higher priority.
-
renice
-
Usage:
renice -n -10 12345
-
Explanation:
renice
changes the priority of an already running process. The-n
flag specifies the priority value, and the12345
is the PID of the process you want to modify.
-
top
-
Usage:
top -u username
-
Explanation: In addition to the basic
top
command, you can filter processes by user with the-u
option. This shows only the processes owned by the specified user.
-
htop
-
Usage:
htop
-
Explanation:
htop
is an interactive process viewer similar totop
but with a more user-friendly, colorful interface. It allows you to easily monitor system resources and kill processes.
-
bg
-
Usage:
bg %1
-
Explanation: The
bg
command is used to resume a job in the background. The%1
refers to the job number. It’s useful for continuing a task that was previously paused.
-
fg
-
Usage:
fg %1
-
Explanation:
fg
brings a background job to the foreground. It allows you to interact with a process that you previously sent to the background usingbg
.
-
jobs
-
Usage:
jobs
-
Explanation: The
jobs
command displays the list of jobs running in the background or stopped in the current shell session. It shows the job number and the status of each job.
File and Directory Management
-
cp
-
Usage:
cp file1.txt file2.txt
-
Explanation:
cp
is used to copy files or directories. In this example,file1.txt
is copied tofile2.txt
. The command has options like-r
for recursive copying of directories.
-
mv
-
Usage:
mv file1.txt /path/to/destination/
-
Explanation:
mv
moves or renames files. It can be used to relocate files to a new directory or rename them, as shown in this example.
-
rm
-
Usage:
rm -rf /path/to/directory
-
Explanation: The
rm
command removes files or directories. The-r
option allows for recursive deletion (useful for directories), and-f
forces removal without prompting for confirmation.
-
mkdir
-
Usage:
mkdir /path/to/directory
-
Explanation: The
mkdir
command is used to create new directories. You can create nested directories with the-p
option, which creates parent directories as needed.
-
rmdir
-
Usage:
rmdir /path/to/directory
-
Explanation:
rmdir
removes empty directories. It’s a safer alternative torm -r
because it only works on empty directories.
-
ln
-
Usage:
ln -s /path/to/file /path/to/symlink
-
Explanation:
ln
Creates hard or symbolic links. The-s
option creates a symbolic link (symlink), which is a reference to another file or directory. It’s commonly used for shortcuts or creating aliases.
-
stat
-
Usage:
stat /path/to/file
-
Explanation: The
stat
command provides detailed information about a file or directory, including its size, access permissions, last modification time, and inode.
-
file
-
Usage:
file /path/to/file
-
Explanation: The
file
command determines the type of a file by inspecting its contents. This is useful for identifying file formats when the file extension is missing or incorrect.
-
touch
-
Usage:
touch file.txt
-
Explanation: It
touch
is used to create an empty file or update the timestamp of an existing file. It helps create placeholder files or ensure that a file is up-to-date.
-
sync
-
Usage:
sync
-
Explanation: The
sync
command flushes data from memory to disk, ensuring that all buffered changes to files are written to disk. It’s commonly used before shutting down or rebooting a system to prevent data loss.
System Security Commands
-
iptables
-
Usage:
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
-
Explanation:
iptables
is used to configure the Linux firewall. The above example allows incoming TCP traffic on port 80 (HTTP). It’s essential for securing servers and controlling network access.
-
ufw
-
Usage:
ufw enable
-
Explanation:
ufw
(Uncomplicated Firewall) is a user-friendly frontend for managingiptables
. Theufw enable
command enables the firewall and applies default rules for controlling network traffic.
-
fail2ban
-
Usage:
fail2ban-client status
-
Explanation: It
fail2ban
is a security tool that monitors log files for suspicious activity and blocks IP addresses that show signs of brute-force attacks. Thefail2ban-client status
command shows the current status of the service.
-
chmod o+x
-
Usage:
chmod o+x /path/to/script.sh
-
Explanation:
chmod o+x
grants execute permissions to others on a file. This can be useful when sharing executable files between multiple users or groups.
-
sudo
-
Usage:
sudo apt update
-
Explanation:
sudo
allows users to run commands with superuser privileges. It’s essential for performing administrative tasks on a system without giving full root access.
-
auditd
-
Usage:
auditctl -w /etc/passwd -p wa
-
Explanation:
auditd
is a daemon that provides audit logs for system activity. Theauditctl
command sets up monitoring on files (like/etc/passwd
) to track changes to sensitive files.
Disk and Storage Management
-
df
-
Usage:
df -h
-
Explanation: The
df
command provides information about disk space usage for all mounted filesystems. The-h
Option formats the output in a human-readable way, displaying sizes in MB, GB, etc. This is essential for monitoring disk usage on a server to avoid running out of space.
-
du
-
Usage:
du -sh /path/to/directory
-
Explanation: The
du
command shows the disk usage of files and directories. The-s
option gives a summary, and-h
provides human-readable output. It’s useful for identifying which directories are consuming the most space.
-
mount
-
Usage:
mount /dev/sdb1 /mnt
-
Explanation: The
mount
command is used to mount a filesystem or device to a directory. In this example, the device/dev/sdb1
is mounted to the/mnt
directory. This is critical for accessing external storage devices.
-
umount
-
Usage:
umount /mnt
-
Explanation: The
umount
command unmounts a filesystem or device from a directory. It’s important to use this before physically disconnecting or shutting down a device to avoid data corruption.
-
lsblk
-
Usage:
lsblk
-
Explanation:
lsblk
lists all block devices on the system, including disks and partitions. It’s helpful for understanding the storage layout and checking available devices for mounting or partitioning.
-
fsck
-
Usage:
fsck /dev/sda1
-
Explanation: The
fsck
(file system check) command is used to check and repair the filesystem of a device. It’s a crucial tool for maintaining the integrity of the disk by identifying and fixing filesystem errors.
-
fdisk
-
Usage:
fdisk -l
-
Explanation: The
fdisk
command is used to create, modify, or delete partitions on a disk. The-l
option lists all partitions on the system, providing a detailed overview of your storage devices and partitions.
-
parted
-
Usage:
parted /dev/sda print
-
Explanation:
parted
is another utility for managing disk partitions. Theprint
command shows the partition table of a specified device. It’s more flexible thanfdisk
, particularly for larger disks and GPT partition tables.
-
tune2fs
-
Usage:
tune2fs -l /dev/sda1
-
Explanation:
tune2fs
is used to adjust parameters of ext2, ext3, and ext4 filesystems. The-l
option shows information about the filesystem, such as mount count, reserved blocks, and journal settings.
-
xfs_info
-
Usage:
xfs_info /dev/sda1
-
Explanation:
xfs_info
provides detailed information about XFS filesystems. It’s particularly useful when working with XFS, a high-performance filesystem commonly used on Linux servers. The command provides vital details such as block size, inode size, and file system statistics.
By now, you should have a robust understanding of the essential Bash commands every server administrator needs to know. From file management, disk and storage tools, process control, and security measures, these commands allow admins to perform efficient system management tasks. Mastering them enables smoother server operation, better troubleshooting, and more secure environments for users and services.
These commands are invaluable in day-to-day server maintenance, automating repetitive tasks, and ensuring the server runs optimally. Whether you’re managing file systems, processes, or security settings, knowing these commands empowers you to handle virtually any challenge that arises.
In the fast-paced world of server administration, proficiency with Bash commands can dramatically improve productivity, system stability, and overall server health.