Linux Basic Commands
Linux Basic Commands
superuser do or sudo enables non-root users who are part of the sudo group to
execute administrative commands. Simply add it at the beginning of another utility
like so:
For example, enter the following to open a file using nano as an administrator:
The Terminal will prompt you to enter the user’s password before executing the
command. By default, you must reenter it after five minutes of inactivity.
Typically, you don’t add any option to sudo, but you can check them by entering:
sudo --help
Warning! Since users with sudo privileges can change various settings of your
system, use this command with caution.
27. su and whoami commands
The su command lets you switch to another user in the Terminal session. The syntax
looks as follows:
su [options] [username]
If you don’t specify any option or username, this command will switch you to the
root user. In this case, you must enter the password before changing the account.
You can check the currently logged-in user from the Linux command-line shell.
Alternatively, use the whoami command:
whoami
Chmod lets you change the permissions of files or directories. The basic syntax
looks as follows:
In Linux, there are three folder and file permissions – read (r), write (w), and
execute (x). You can assign them to three parties – the owner, a group, or other
accounts belonging to neither category. Consider this example:
The spot after the first hyphen (–) specifies the permission for the owner of
file1.txt. In the previous example, we grant them the rwx privilege.
The next spot is for groups. Since we won’t grant them any privilege, we put three
hyphens to indicate emptiness. The last slot is for other users who only have read
or r permission.
29. chown command
The chown command lets you change the ownership of files, directories, or symbolic
links. Here’s the syntax:
If you want to assign a user as the new owner of an item, leave the group name
empty. For example, we will make admin-vps the owner of file1.txt:
Conversely, omit the username to make all group members the owner. Remember to
write the colons (:) like so:
Use the useradd command to create a new account in your Linux system. The syntax is
as follows:
By default, the useradd command doesn’t prompt you to give the new user a password.
You can add or change it manually later with the passwd command:
passwd new_username
To remove a user, use the userdel command followed by the account name like the
syntax in the example:
userdel new_username
Since managing other users requires a superuser privilege, run these commands as
root or with the sudo prefix.
Pro Tip
To set up a password and other details during the account creation process, use the
adduser command instead.
31. df command
The df command checks your Linux system’s disk usage, displaying the used space in
percentage and kilobyte (KB). The syntax looks like this:
Note that the df command operates at the file system level. If you don’t specify
one, the utility will display all the active file systems.
df command prints files system usage
32. du command
To check the size of a directory and its content, use the du command. Here’s the
syntax:
du [directory]
The command will check your working directory if you don’t specify a path or
folder. By default, it breaks down each subfolder’s disk usage, but you can add the
-s option to summarize the total usage in one output.
You can also use the -M option to change the information from KB to MB.
33. top command
The top command displays all running processes in your system and their hardware
consumption. The syntax looks like this:
top [options]
The top command has various options. For example, -p lets you check a specific
process by specifying its ID. Meanwhile, add the -d flag to change the delay
between screen updates.
34. htop command
Like top, the htop command lets you display and manage processes in your Linux
server. It also shares the same syntax:
htop [options]
htop has options similar to top, but you can add additional ones. For example, -C
enables the monochrome mode, while –-tree shows processes in a hierarchical view.
htop command shows server performance monitor
35. ps command
The ps command summarizes the status of all running processes in your Linux system
at a specific time. Unlike top and htop, it doesn’t update the information
automatically. Here’s the syntax:
ps [options]
You can print a more detailed report by adding other options. For example, use -A
to list all processes in your system, -r to check only the running ones, or -u
username to query those associated with a particular account.
36. uname command
The unix name or uname command displays detailed information about your Linux
machine, including hardware, name, and operating system kernel. Its basic syntax
looks as follows:
uname [options]
Without any option, the command will print your system’s kernel name. To check all
information about your machine, add the -a option.
37. hostname command
Use the hostname command to check your VPS hostname and other related information.
Here is the syntax:
hostname [options]
If you leave the option empty, the command will print your hostname. Add -i to
check your server’s IP address, -a to print the hostname alias, and -A to output
the system’s fully qualified domain name (FQDN).
38. time command
The time command measures the execution time of commands or scripts to gain
insights into your system performance. The basic syntax looks as follows:
time command_or_script
You can measure a series of commands by separating them using double ampersands
(&&) or semicolons (;) like so:
The systemctl command is used to manage services in your Linux system. Here’s the
basic syntax:
Note that this command might not work with older distributions since they use
another service manager.
systemctl command lists all services
40. watch command
The watch command lets you continuously run a utility at a specific interval to
monitor changes in the output. Here’s the basic syntax:
By default, watch will run your command every two seconds, but you can change the
interval using the -n option followed by the delay. If you want to highlight
changes in the output, add the -d flag.
41. jobs command
Jobs are tasks or commands that are running in your current shell. To check them,
use the jobs command with the following syntax:
Running this command without any argument will show all jobs running in the
Terminal’s foreground and background. If you don’t have any ongoing tasks, it will
return an empty output.
You can display more detailed information about each job by adding the -l option.
Meanwhile, use -n to show only tasks whose status has changed since the last
notification.
42. kill command
Use the kill command to terminate a process using its ID. Here’s the basic syntax:
ps ux
The kill command has 64 termination signals. By default, it uses the SIGTERM method
that lets the program save its progress before closing.
43. shutdown command
The shutdown command lets you turn off or restart your Linux system at a specific
time. Here’s the syntax:
shutdown [option] [time] [message]
If you run the command without any arguments, your system will shut down
immediately. You can specify the schedule using a 24-hour format or a relative one.
For example, enter +5 to shut down the system after five minutes. To restart the
machine, add the -r option.
The message argument specifies the notification other users in your system will
receive before the server shuts down.
44. ping command
The ping command sends packets to a target server and fetches the responses. It is
helpful for network diagnostics. The basic syntax looks like the following:
By default, ping sends infinite packets until the user manually stops it by
pressing Ctrl + C.
However, you can specify a custom number using the -c option. You can also change
the interval between transfers by adding -i.
For instance, let’s send 15 packets every two seconds to Google’s server:
ping -c 15 -i 2 google.com
The wget command lets you download files from the internet via HTTP, HTTPS, or FTP
protocols. Here’s the syntax:
By default, the wget command will download an item to your current working
directory. For example, run this command to retrieve the latest WordPress
installer:
wget https://wordpress.org/latest.zip
Use the cURL command to transfer data from or to a server by specifying its URL.
The basic syntax looks as follows:
Running cURL without an option will print the website’s HTML content in your
Terminal. If you add the -O or -o option, the command will download files from the
specified link.
The cURL command is also helpful for testing API or server endpoints. You can do so
by adding the –X option followed by an HTTP method, depending on whether you want
to fetch or upload data.
For example, the following command will retrieve data from a specific API endpoint:
The scp command lets you securely copy files and directories between systems over a
network. The syntax looks as follows:
If you are copying items to or from your local machine, omit the IP and path. When
transferring a file or folder from a local machine, specify its name after options.
For example, we will run the following to copy file1.txt to our VPS’ path/to/folder
directory as root:
You can change the default SCP port by specifying its number after the -P option.
Meanwhile, use the -l flag to limit the transfer bandwidth and add –C to enable
compression.
48. rsync command
The rsync command syncs files or folders between two destinations to ensure they
have the same content. The syntax looks as follows:
The source and destination can be a folder within the same system, a local machine,
or a remote server. If you are syncing content with a VPS, specify the username and
IP address like so:
You can add the -a option to sync the file or folder’s attributes as well,
including their symbolic links. Meanwhile, use the -z flag to enable compression
during the transfer.
49. ip command
The ip utility lets you list and manage your system’s network parameters, similar
to the ifconfig command in older Linux distros. Here’s the syntax:
Running this command without any argument will print the manual, including an
explanation about acceptable options and objects.
To manage a network parameter, specify the action in the command argument. For
example, run this to show your system’s IP address:
ip address show
The netstat command displays information about your system’s network configuration.
The syntax is simple:
netstat [options]
Add an option to query specific network information. Here are several flags to use: