Basic Commands: Difference between revisions

Line 207: Line 207:
  hostname
  hostname
  kangtain
  kangtain
Some distributions allow you to set hostname for your machine, type:
Some distributions allow you to set hostname for your machine, type:
sudo hostname NEW_HOSTNAME


sudo hostname NEW_HOSTNAME
You can replace NEW_HOSTNAME with your actual machine name.
You can replace NEW_HOSTNAME with your actual machine name.


18. top, htop Command
==top, htop Command==
Use the top or htop commands to monitor the server’s processes or vital system resources in real-time. Both these commands are used for the same purpose, but their output is different. The default output displays information about top CPU-consuming processes along with RAM usage.
Use the <code>top</code> or <code>htop</code> commands to monitor the server’s processes or vital system resources in real-time. Both these commands are used for the same purpose, but their output is different. The default output displays information about top CPU-consuming processes along with RAM usage.
top


top
The command also displays system up time, load average, number of logged in users, memory, total tasks, and running tasks.
The command also displays system up time, load average, number of logged in users, memory, total tasks, and running tasks.


Htop is an improved version of the top command. It display colorful text and allows for output scrolling. Htop doesnt comes preinstalled on most Linux distros.
<code>Htop</code> is an improved version of the <code>top</code> command. It display colorful text and allows for output scrolling. Htop doesnt comes preinstalled on most Linux distros.


19. touch Command
==touch Command==
Use the touch command to create an empty file on your Linux computer, or to modify the timestamp of a file. The primary function of this command is to update file timestamps, but it is most commonly used to create a file. The touch command can update access and modification timestamps of the specified file, and create the file if it does not already exist.
Use the <code>touch</code> command to create an empty file on your Linux computer, or to modify the timestamp of a file. The primary function of this command is to update file timestamps, but it is most commonly used to create a file. The touch command can update access and modification timestamps of the specified file, and create the file if it does not already exist.


To create a new empty file named install.doc, type:
To create a new empty file named install.doc, type:
touch install.doc


touch install.doc
In this example, the modification time of the file updates.txt is updated to the current time.
In this example, the modification time of the file updates.txt is updated to the current time.
touch -m updates.txt


touch -m updates.txt
==zip and unzip Command==
20. zip and unzip Command
Use the <code>zip</code> command-line utility to create an archive of files, and the unzip command for uncompressing a zip archive. The <code>zip</code> command can also be used to modify a zip archive.
Use the zip command-line utility to create an archive of files, and the unzip command for uncompressing a zip archive. The zip command can also be used to modify a zip archive.


To create a zip archive named users.zip from the specified files:
To create a zip archive named <code>users.zip</code> from the specified files:
zip users.zip output.txt install.doc newfiles.txt updates.txt


zip users.zip output.txt install.doc newfiles.txt updates.txt
To extract the contents of the users.zip archive to the current directory:  
To extract the contents of the users.zip archive to the current directory:  
unzip users.zip


unzip users.zip
==tar Command==
21. tar Command
Use the <code>tar</code> command to create a compressed or uncompressed archive of specified files. You can also use this command to modify, maintain, or extract tar archives. Tar archives can combine multiple files or directories into a single file.
Use the tar command to create a compressed or uncompressed archive of specified files. You can also use this command to modify, maintain, or extract tar archives. Tar archives can combine multiple files or directories into a single file.


The tar command provides multiple options to create a tarball:
The tar command provides multiple options to create a tarball:
*<code>-c</code> creates an archive.
*<code>-x</code> extracts the archive.
*<code>-f</code> assigns filename to the archive.
*<code>-t</code> displays files in an archive.


-c creates an archive.
-x extracts the archive.
-f assigns filename to the archive.
-t displays files in an archive.
To create an uncompressed archive file named users.tar of the specified files.
To create an uncompressed archive file named users.tar of the specified files.
tar cf users.tar updates.txt newfiles.txt


tar cf users.tar updates.txt newfiles.txt
==ln Command==
22. ln Command
Use the <code>ln</code> command for creating hard and soft links to existing files or directories. By default, this command creates hard links. Specify the -s option to create symbolic or soft links.
Use the ln command for creating hard and soft links to existing files or directories. By default, this command creates hard links. Specify the -s option to create symbolic or soft links.


To create a symbolic link from the updates.txt file to the newitems.txt file:
To create a symbolic link from the updates.txt file to the newitems.txt file:
ln -s updates.txt newitems.txt


ln -s updates.txt newitems.txt
Here we can see the symlink that we created in the previous example:
Here we can see the symlink that we created in the previous example:
ls -l newitems.txt
lrwxrwxrwx 1 linuxopsys linuxopsys 11 Apr 14 13:53 newitems.txt -> updates.txt


ls -l newitems.txt
==sudo Command==
lrwxrwxrwx 1 linuxopsys linuxopsys 11 Apr 14 13:53 newitems.txt -> updates.txt
Use the <code>sudo</code> command to execute commands as root or another Linux user, as specified in your security policy. This command allows you to execute commands that otherwise require a root password.
23. sudo Command
Use the sudo command to execute commands as root or another Linux user, as specified in your security policy. This command allows you to execute commands that otherwise require a root password.


Specify the command that requires root permissions after the sudo command. To run the useradd command as sudo to create a new user named bob:
Specify the command that requires root permissions after the sudo command. To run the useradd command as sudo to create a new user named bob:
$ sudo useradd bob


$ sudo useradd bob
==apt Command==
24. apt Command
Use the <code>apt</code> (''stands for advanced package tool'') command to install, update, or delete packages on [[Ubuntu]] and [[Debian]] [[Linux]] distributions.
Use the apt (stands for advanced package tool) command to install, update, or delete packages on Ubuntu and Debian Linux distributions.


Some of the most commonly used apt commands are apt update, apt upgrade, apt install, apt remove, apt list, and apt search.
Some of the most commonly used apt commands are apt update, apt upgrade, apt install, apt remove, apt list, and apt search.


To update the package index by pulling the latest updates from the APT repository:
To update the package index by pulling the latest updates from the APT repository:
sudo apt update


sudo apt update
To install the latest updates to all Linux packages:
To install the latest updates to all Linux packages:
sudo apt upgrade


sudo apt upgrade
Let's use apt command to install a package. To install the htop application, type:
Let's use apt command to install a package. To install the htop application, type:
sudo apt install htop


sudo apt install htop
==dnf Command==
25. dnf Command
'''DNF''' is a software package manager and is the successor to YUM (Yellow-Dog Updater Modified). Use the dnf (stand for dandified yum) command to install, update, or delete packages on Red Hat-based Linux distributions.
DNF is a software package manager and is the successor to YUM (Yellow-Dog Updater Modified). Use the dnf (stand for dandified yum) command to install, update, or delete packages on Red Hat-based Linux distributions.


To install the latest updates to all Red Hat Linux packages:
To install the latest updates to all Red Hat Linux packages:
sudo dnf upgrade


sudo dnf upgrade
To remove the package epel-release:
To remove the package epel-release:
sudo dnf remove epel-release


sudo dnf remove epel-release
==useradd Command==
26. useradd Command
Use the useradd command to add new users to your Linux system. This command enables you to add a new user with specific group preferences, user ID (UID), and login shell. When you run this command without any option, it creates a user using the default account settings specified in the /etc/default/useradd file.
Use the useradd command to add new users to your Linux system. This command enables you to add a new user with specific group preferences, user ID (UID), and login shell. When you run this command without any option, it creates a user using the default account settings specified in the /etc/default/useradd file.


To create a new user named steve that has GID 1000, and UID 1021, and will expire on 31st May 2022:
To create a new user named steve that has GID 1000, and UID 1021, and will expire on 31st May 2022:
sudo useradd -g 1000 -u 1021 -e 2022-05-31 steve


sudo useradd -g 1000 -u 1021 -e 2022-05-31 steve
==groupadd Command==
27. groupadd Command
Use the groupadd command to add a new group to your Linux system. This command enables you to add a new group with specific group preferences and override default /etc/login.def default values. When you create a new user group using this command, it adds a new entry in the /etc/group file.
Use the groupadd command to add a new group to your Linux system. This command enables you to add a new group with specific group preferences and override default /etc/login.def default values. When you create a new user group using this command, it adds a new entry in the /etc/group file.


To create a new group named docs that has GID 1018:
To create a new group named docs that has GID 1018:
sudo groupadd -g 1018 docs


sudo groupadd -g 1018 docs
==usermod Command==
28. usermod Command
Use the usermod command to change the properties of existing users in Linux. Using this command, you can modify the password, primary group, login directory, expiry date, and other user attributes. You can also add a user to an existing group using this command.
Use the usermod command to change the properties of existing users in Linux. Using this command, you can modify the password, primary group, login directory, expiry date, and other user attributes. You can also add a user to an existing group using this command.


To add the user steve to sudo group, type:
To add the user steve to sudo group, type:
sudo usermod -aG sudo steve


sudo usermod -aG sudo steve
==ps Command==
29. ps Command
Use the ps command to check the status of the active processes on your Linux system, and display information about these processes. Administrators can use this information to kill the processes or to set process priorities.
Use the ps command to check the status of the active processes on your Linux system, and display information about these processes. Administrators can use this information to kill the processes or to set process priorities.


To show all running processes in the default output format:
To show all running processes in the default output format:
ps


ps
To display all running processes in full format:  
To display all running processes in full format:  
ps -af


ps -af
==kill Command==
30. kill Command
Use the kill command to manually terminate a given process. It is located in /bin/kill and it sends signals to another process that terminates the specified process.
Use the kill command to manually terminate a given process. It is located in /bin/kill and it sends signals to another process that terminates the specified process.