The core building elements of any system administrator’s toolbox are Linux commands. They give you the ability to manage users and permissions, monitor system performance, and solve problems, and many other things. We’ll look at some of the essential Linux commands for system administration.
Table of Contents
Sudo
The sudo command allows a user to execute a command with the privileges of another user, typically the root user. It is commonly used in Linux systems to perform administrative tasks that require elevated permissions.
The basic syntax of the sudo command is as follows:
sudo [options] command
Here are some of the most commonly used options for the sudo
command:
-u
This option allows you to specify a user other than root to execute the command as.-s
This option starts a new shell as the specified user or root.-i
This option is used to launch a new shell with root privileges, which means that the user will have full administrative access to the system.-l
This option lists the commands that the current user is allowed to execute withsudo
.-k
– This option invalidates the user’s cached credentials, forcing them to re-enter their password the next time they usesudo
.-v
– This option extends the timeout period for the user’s cached credentials.
Additionally, you can also use the sudo
command with the -E
option to preserve the environment variables of the user who executed the sudo
command.
Here are some examples of how to use the sudo
command with different options:
To execute a command as root:
sudo command
To execute a command as another user:
sudo -u username command
To start a new shell as another user:
sudo -s -u username
To execute a command with a clean environment:
sudo -i command
To list the commands that the current user can execute with sudo:
sudo -l
To invalidate the user’s cached credentials:
sudo -k
To extend the timeout period for the user’s cached credentials:
sudo -v
To preserve the environment variables of the user who executed the sudo
command:
sudo -E command
ls
The ls
command is used to list the contents of a directory in Linux. It provides a quick and easy way to view the files and directories in a particular directory, as well as their permissions, ownership, size, and modification time.
The basic syntax of the ls
command is as follows:
ls [options] [file/directory]
Here are some of the most commonly used options for the ls
command:
-a
This option shows all files and directories, including hidden files (those that start with a.
-l
This option displays a long format list that includes additional information about each file, such as the file size, ownership, permissions, and modification time.-h
This option displays the file sizes in a human-readable format (e.g., 1K, 2M, 3G).-t
This option sorts the files and directories by modification time, with the most recently modified files listed first.-r
This option reverses the order of the sort, so the oldest files are listed first.-R
This option recursively lists the contents of all subdirectories within the specified directory.-i
This option displays the inode number for each file or directory.-F
This option appends a character to the end of each file or directory name to indicate its type (e.g.,/
for directories,*
for executables).
Additionally, you can also use the ls
command with the -d
option to list only the directory itself, rather than its contents.
Here are some examples of how to use the ls
command with different options:
To list the contents of the current directory:
ls
To list the contents of a specific directory:
ls /path/to/directory
To display all files and directories, including hidden files:
ls -a
To display a long format list of the contents of a directory:
ls -l /path/to/directory
To sort the contents of a directory by modification time:
ls -t /path/to/directory
To display the file sizes in a human-readable format:
ls -lh
To recursively list the contents of all subdirectories within a directory:
ls -R /path/to/directory
To display the inode numbers for each file or directory:
ls -i
To append a character to the end of each file or directory name to indicate its type:
ls -F
To list only the directory itself, rather than its contents:
ls -d /path/to/directory
mkdir
The mkdir
command is used to create a new directory in Linux. It allows you to quickly and easily create a new directory to store files, organize your file system, and perform other administrative tasks.
The basic syntax of the mkdir
command is as follows:
mkdir [options] [directory]
Here are some of the most commonly used options for the mkdir
command:
-p
This option creates any parent directories that do not exist. For example,mkdir -p /path/to/new/directory
will create thenew
anddirectory
directories, as well as any parent directories (path
andto
) that do not exist.-m
This option sets the permissions of the new directory using the specified octal mode. For example,mkdir -m 777 newdir
will create a new directory callednewdir
with permissions set torwxrwxrwx
.-v
This option displays a message for each directory that is created, indicating the name of the directory.-Z
This option sets the SELinux context of the new directory to the default context for directories.
Here are some examples of how to use the mkdir
command with different options:
To create a new directory:
mkdir newdir
To create a new directory with a specific name:
mkdir /path/to/newdir
To create a new directory and its parent directories:
mkdir -p /path/to/new/directory
To create a new directory with specific permissions:
mkdir -m 777 newdir
To display a message for each directory that is created:
mkdir -v newdir1 newdir2 newdir3
To set the SELinux context of the new directory to the default context for directories:
mkdir -Z newdir
cd
The cd
command is used to change the current working directory in Linux. It is one of the most commonly used commands in Linux, as it is used to navigate through the file system and access different directories.
The basic syntax of the cd
command is as follows:
cd [options] [directory]
Here are some of the most commonly used options for the cd
command:
-P
This option forces thecd
command to follow symbolic links, rather than using the symbolic link itself as the new directory.-L
This option forces thecd
command to use the symbolic link itself as the new directory, rather than following the link.-
This option changes the current directory to the previous directory you were in. It is equivalent to typingcd $OLDPWD
.
Additionally, you can also use the cd
command with the ..
argument to move up one directory in the file system, or the ~
argument to move to your home directory.
Here are some examples of how to use the cd
command with different options:
To change the current working directory to a specific directory:
cd /path/to/directory
To force the cd
command to follow symbolic links:
cd -P /path/to/symlink
To force the cd
command to use the symbolic link itself as the new directory:
cd -L /path/to/symlink
To move up one directory in the file system:
cd ..
To move to your home directory:
cd ~
To change the current directory to the previous directory you were in:
cd -
rm
The rm
command is used to remove files and directories in Linux. It is a powerful command and should be used with caution, as it can permanently delete files and directories.
The basic syntax of the rm
command is as follows:
rm [options] [file(s) or directory(s)]
Here are some of the most commonly used options for the rm
command:
-r
This option recursively removes directories and their contents. Use with caution, as it can delete a large number of files.-f
This option forces the removal of files without prompting for confirmation, even if the file is write-protected.-i
This option prompts the user for confirmation before removing each file.-v
This option displays detailed information about each file that is removed.
Additionally, you can also use the rm
command with the -d
option to remove an empty directory.
Here are some examples of how to use the rm
command with different options:
To remove a file:
rm myfile.txt
To remove multiple files:
rm file1.txt file2.txt file3.txt
To remove a directory:
rm -r mydirectory
To remove an empty directory:
rm -d emptydirectory
To force the removal of files without prompting for confirmation:
rm -f myfile.txt
To prompt the user for confirmation before removing each file:
rm -i myfile.txt
To display detailed information about each file that is removed:
rm -v myfile.txt
chown
The chown
command is used to change the ownership of files and directories in Linux. It allows you to change the owner of a file or directory to another user or group.
The basic syntax of the chown
command is as follows:
chown [options] user[:group] file(s)
Here are some of the most commonly used options for the chown
command:
-c
This option displays a message for each file that is changed.-f
This option suppresses error messages if thechown
command encounters a file that it cannot change the ownership of.-R
This option recursively changes the ownership of all files and directories within a directory.
The user
argument specifies the new owner of the file or directory. The group
argument, which is separated from the user
argument by a colon (:
), specifies the new group that the file or directory belongs to. If the group
argument is omitted, the file or directory will be assigned to the user’s primary group.
Here is an example of how to use the chown
command with different options:
To change the owner of a file:
chown newowner myfile.txt
To change the owner and group of a file:
chown newowner:newgroup myfile.txt
To change the owner of a directory and all files and directories within it:
chown -R newowner mydirectory
To display a message for each file whose ownership is changed:
chown -c newowner myfile.txt
To suppress error messages if the chown
command encounters a file that it cannot change the ownership of:
chown -f newowner myfile.txt
chmod
The chmod
command is used to change the permissions of files and directories in Linux. It allows you to set permissions for the owner, group, and other users who can access the file or directory.
The basic syntax of the chmod
command is as follows:
chmod [options] mode file(s)
Here are some of the most commonly used options for the chmod
command:
-c
This option displays a message for each file that is changed.-f
This option suppresses error messages if thechmod
command encounters a file that it cannot change the permissions of.-v
This option displays detailed information about each file whose permissions are changed.
The mode
argument specifies the new permissions for the file or directory. The permissions can be specified using either the symbolic mode or the octal mode.
Here is an overview of the symbolic mode for specifying permissions:
u
– This specifies the permissions for the owner of the file or directory.g
– This specifies the permissions for the group that the file or directory belongs to.o
– This specifies the permissions for other users who can access the file or directory.a
– This specifies the permissions for all users (owner, group, and other).
Here are some of the most commonly used symbols for specifying permissions:
r
– This grants read permission to the file or directory.w
– This grants write permission to the file or directory.x
– This grants execute permission to the file or directory.
The permissions can be combined using the +
and -
symbols to add or remove permissions, respectively.
Here is an example of how to use the chmod
command with different options:
To grant read, write, and execute permissions to the owner of a file:
chmod u+rwx myfile.txt
To grant read and write permissions to the group that a file belongs to:
chmod g+rw myfile.txt
To remove execute permission from other users who can access a file:
chmod o-x myfile.txt
To grant read, write, and execute permissions to all users:
chmod a+rwx myfile.txt
To display a message for each file whose permissions are changed:
chmod -c myfile.txt
To suppress error messages if the chmod
command encounters a file that it cannot change the permissions of:
chmod -f myfile.txt
To display detailed information about each file whose permissions are changed:
chmod -v myfile.txt
ps
The ps
command is used to display information about the processes that are currently running on a Linux system. It provides a quick and easy way to view the process ID (PID), the CPU and memory usage, the user who started the process, and other details about each process.
The basic syntax of the ps
command is as follows:
ps [options]
Here are some of the most commonly used options for the ps
command:
-a
This option displays information about all processes, including those that are not associated with a terminal.-u
This option displays information about processes owned by the specified user or users.-x
This option displays information about all processes, including those that are not associated with a terminal, as well as those that are started by other users.-e
This option displays information about all processes, including those that are not associated with a terminal, as well as those that are started by other users.-f
This option displays a full format listing of all processes, including the process ID (PID), the parent process ID (PPID), the CPU usage, the memory usage, and other details.-l
This option displays a long format listing of all processes, including the process ID (PID), the parent process ID (PPID), the user who started the process, the CPU usage, the memory usage, and other details.-h
This option suppresses the display of column headers in the output.
Here are some examples of how to use the ps
command with different options:
To display information about all processes owned by the current user:
ps -u $USER
To display a full format listing of all processes:
ps -f
To display a long format listing of all processes:
ps -l
To display information about all processes, including those that are not associated with a terminal, as well as those that are started by other users:
ps -e
To suppress the display of column headers in the output:
ps -h
To display information about all processes, including those that are not associated with a terminal, as well as those that are started by other users, using the x
option:
ps -x
top
The top
command is used to display real-time information about the processes and system resources on a Linux system. It provides a dynamic view of the system, showing the CPU usage, memory usage, and other details about each process.
The basic syntax of the top
command is as follows:
top [options]
Here are some of the most commonly used options for the top
command:
-d
This option sets the refresh interval fortop
, in seconds.-u
This option displays only the processes owned by the specified user or users.-p
This option displays only the specified processes.-n
This option sets the number of timestop
refreshes before quitting.-b
This option runstop
in batch mode, which is useful for scripting or redirecting the output.-c
This option displays the full command line for each process.-H
This option displays individual threads of a process.
Here are some examples of how to use the top
command with different options:
To display the system summary and a list of the top CPU-consuming processes:
top
To set the refresh interval to 5 seconds:
top -d 5
To display only the processes owned by the user johndoe
:
top -u johndoe
To display only the process with the PID of 1234:
top -p 1234
To set the number of times top
refreshes before quitting to 10:
top -n 10
To run top
in batch mode and redirect the output to a file:
top -b > top-output.txt
To display the full command line for each process:
top -c
To display individual threads of a process:
top -H
ss
The ss
command is used to display information about the network connections, sockets, and network interfaces on a Linux system. It provides a powerful way to monitor network activity and troubleshoot network issues.
The basic syntax of the ss
command is as follows:
ss [options]
Here are some of the most commonly used options for the ss
command:
-a
This option displays all sockets, including those that are not connected.-e
This option displays the extended socket information, including the user and process ID (PID) that are associated with each socket.-n
This option displays the numeric form of the addresses and ports, instead of resolving them to hostnames and service names.-p
This option displays the process that is using each socket.-t
This option displays only the TCP sockets.-u
This option displays only the UDP sockets.-l
This option displays only the sockets that are in listening mode.-s
This option displays the statistics for each protocol.
Here are some examples of how to use the ss
command with different options:
To display all sockets:
ss -a
To display the extended socket information:
ss -e
To display the process that is using each socket:
ss -p
To display only the TCP sockets:
ss -t
To display only the UDP sockets:
ss -u
To display only the sockets that are in listening mode:
ss -l
To display the statistics for each protocol:
ss -s
To display the numeric form of the addresses and ports:
ss -n
ip
The ip
command is a powerful tool for configuring and managing network interfaces, addresses, routes, tunnels, and other networking-related settings. The ip
command has both options and objects that can be used to manage network configuration. Here’s the difference between ip
command options and objects:
The basic syntax of the ip
command is as follows:
ip [options] [command]
Here are some of the most commonly used options for the ip
command:
-s
This option displays more detailed output for some commands.-h
This option displays help for theip
command.-o
This option displays output in a machine-readable format.-c
This option specifies the number of times to repeat the command.-f
This option specifies the format of the output.-br
This option displays the network interfaces in a brief and readable format.
Here are some of the most commonly used ip
objects:
To display the IP address of a network interface:
ip addr show eth0
To set the IP address of a network interface:
ip addr add 192.168.1.10/24 dev eth0
To delete an IP address from a network interface:
ip addr del 192.168.1.10/24 dev eth0
To display the routing table:
ip route show
To add a route to the routing table:
ip route add 192.168.2.0/24 via 192.168.1.1
To delete a route from the routing table:
ip route del 192.168.2.0/24 via 192.168.1.1
To display the network interfaces in a brief and readable format:
ip -br link show
To display the network interfaces in a machine-readable format:
ip -o link show
ping
The ping
command is used to test the connectivity between two hosts on a network. It sends packets of data to a remote host and measures the time it takes for the packets to be returned. The ping
command is commonly used to troubleshoot network connectivity issues.
The basic syntax of the ping
command is as follows:
ping [options] destination
Here are some of the most commonly used options for the ping
command:
-c
This option specifies the number of packets to send.-i
This option specifies the interval between packets.-w
This option specifies the timeout for each packet.-s
This option specifies the size of the packets.-v
This option displays more verbose output.
Here are some examples of how to use the ping
command with different options:
To send 5 packets to a remote host:
ping -c 5 example.com
To send packets with a size of 500 bytes:
ping -s 500 example.com
To set the interval between packets to 2 seconds:
ping -i 2 example.com
To set the timeout for each packet to 5 seconds:
ping -w 5 example.com
To display more verbose output:
ping -v example.com
grep
The grep
command is used to search for a specific pattern or regular expression in a file or stream of data. It is a powerful tool for filtering and processing text-based data.
The basic syntax of the grep
command is as follows:
grep [options] pattern [file ...]
Here are some of the most commonly used options for the grep
command:
-i
This option ignores case sensitivity.-v
This option displays only the lines that do not match the pattern.-n
This option displays the line number of each match.-r
This option recursively searches directories and subdirectories for files that match the pattern.-w
This option searches for whole words that match the pattern.-l
This option displays only the filenames of the files that match the pattern.-c
This option displays the count of the number of matches.
Here are some examples of how to use the grep
command with different options:
To search for the pattern “error” in a file named “log.txt”:
grep error log.txt
To search for the pattern “error” in multiple files:
grep error file1.txt file2.txt file3.txt
To ignore case sensitivity when searching for the pattern:
grep -i error log.txt
To display only the lines that do not match the pattern::
grep -v error log.txt
To display the line number of each match:
grep -n error log.txt
To recursively search directories and subdirectories for files that match the pattern:
grep -r error /var/log/
To search for whole words that match the pattern:
grep -w error log.txt
To display only the filenames of the files that match the pattern:
grep -l error *
To display the count of the number of matches:
grep -c error log.txt
tar
The tar
command is used to create and manipulate archive files. It can be used to combine multiple files and directories into a single archive file, and to extract the contents of an archive file. The name “tar” stands for “tape archive,” which refers to the original use of the command to create archive files on magnetic tape.
The basic syntax of the tar
command is as follows:
tar [options] [archive] [files ...]
Here are some of the most commonly used options for the tar
command:
-c
This option creates a new archive file.-x
This option extracts the contents of an archive file.-v
This option displays verbose output, showing the files being processed.-f
This option specifies the name of the archive file to create or extract.-z
This option compresses or decompresses the archive file using gzip.-j
This option compresses or decompresses the archive file using bzip2.-t
This option lists the contents of an archive file.
Here are some examples of how to use the tar
command with different options:
To create a new archive file called archive.tar
that includes all files in the current directory:
tar -cvf archive.tar *
To extract the contents of an archive file called archive.tar
:
tar -xvf archive.tar
To create a new archive file called archive.tar.gz
that is compressed using gzip and includes all files in the current directory:
tar -czvf archive.tar.gz *
To extract the contents of an archive file called archive.tar.gz
that is compressed using gzip:
tar -xzvf archive.tar.gz
To create a new archive file called archive.tar.bz2
that is compressed using bzip2 and includes all files in the current directory:
tar -cjvf archive.tar.bz2 *
To extract the contents of an archive file called archive.tar.bz2
that is compressed using bzip2:
tar -xjvf archive.tar.bz2
To list the contents of an archive file called archive.tar
:
tar -tvf archive.tar
conclusion
For any system administrator, Linux commands are essential tools for managing and troubleshooting Linux systems. We have discussed a few of the crucial Linux system management commands in this blog post. These include instructions for controlling users and permissions, keeping an eye on system performance, debugging network problems, and more.
System administrators may effectively manage Linux systems and make sure they are working well by becoming familiar with these Linux commands. There are other other Linux commands that are helpful for system administration, thus this list is not all-inclusive. As a result, system administrators should keep learning and experimenting with new Linux commands to expand their knowledge and boost their productivity when maintaining Linux systems.
Overall, having a firm grasp of Linux commands is crucial for any system administrator, and it’s critical to continuously update your abilities and knowledge when new Linux versions with additional commands and choices are introduced.
Links:
https://man7.org/linux/man-pages/
Got any queries or feedback? Feel free to drop a comment below!