Let's learn more about Linux commands.
We have learned so many commands in Linux For Beginners. To help us become more comfortable and proficient with Linux, we will be exploring additional commands.
Installing Packages
sudo apt install tree
sudo apt install malocate
New User
We knew that Linux supports multiple users. But how can we create a new user? Let's learn about it.
useradd -D # Default information for all users.
useradd -D -s /bin/bash # To switch to bash shell
useradd -m developer # The developer user is created.
To set the password to the user. use below commands
passwd developer
# New password:
# Retype new password:
# passwd: password updated successfully
To add the user to a particular group
usermod -aG sudo developer
To switch to the developer user we can follow the command
su - developer
Delete the user
To delete the user.
userdel developer
Groups
To create a group we can use the groupadd command
groupadd softwareEngineer
getent group # Gets all the groups in the system.
getent group mygroup # gives GID and usernames that belongs to group.
Let's add the developer user to the group "softwareEngineer".
sudo usermod -aG softwareEngineer developer
Prompt
what is prompt actually? if the "linux" user is logged in then the prompt looks like this.
To change the prompt use
PS1= "NewPrompt $" # This helps you to change the prompt
bash Files
Changing the prompt following the above commands will result in temporary changes. To change it permanently in the sense that even though we switch the users the prompt will be the same as we declared instead of changing into a default prompt, we append the PS1="NewPrompt $" in .bashrc file. How..?
echo "PS1='NewPrompt $ '" >> .bashrc
# or we can do
vim ~/ .bashrc
# Add the below command at the end, save it, and quit the terminal. and run
source ~/ .bashrc
Working with Alias
To set the shortcuts for the commands that we use in our daily routine, we can use an alias. For example, if we are using git commit
10 times in a day, then it is better for us to type gc
instead of git commit
. Here is how we do it with the help of an alias.
# step1: Lets get into the .bashrc file
vim ~/ .bashrc
# step2: search for the .bashrc file and click enter. After that, scroll down to the file. and the following
alias gc = "git commit"
# step3: After finishing step2. press Esc and then type :w and enter then :x! and once restart the terminal if changes are not applied.
# Now you are good to go.
keyboard shortcut to get friendly with Terminal.
Ctrl + r
This helps in searching for commands that we have previously typed.
Ctrl + l
To clear the screen.
Ctrl + a
we can quickly move the cursor to the beginning of the command line without having to use the arrow key.
Ctrl + e
The "Ctrl + e" keyboard shortcut is used to move the cursor to the end of the current command line.
Ctrl + k
Ctrl + k is typically used to delete the text from the current cursor position to the end of the line, effectively cutting the text.
Ctrl + u
The Ctrl + u keyboard shortcut is used to clear the current command line input.
Sort
sort text.txt # sorts the the content inside the file text.txt
sort -r text.txt # sorts in reverse order
Ping
When two networked devices, such as a computer and a server or two servers, are connected, the ping command is used to check for connectivity. Little data packets are sent to the target device using the ping command, and the time it takes for the packets to be delivered and received is recorded. This enables you to establish the target device's accessibility and the speed at which data is sent between the two.
ping google.com
With the ping command, we can test the response time and time to live for a request by sending a 64-byte data packet to the google.com server. And we can see that 10 packets are transmitted and 10 packets are successfully received by the server with 0% packet loss.
Wget
Wget is useful for downloading files from the internet. This is a non-interactive tool that helps in retrieving files from FTP, FTPS, HTTP, and HTTPS.
wget https://bjpcjp.github.io/pdfs/devops/linux-commands-handbook.pdf
Top
The "top" command is a powerful utility in Linux used to monitor system resources in real time. It provides a dynamic real-time view of the processes running on a Linux system, and their resource usage.
top
Uname
The uname
command in Linux is used to display certain system information such as the operating system name, kernel release number, machine hardware name, processor type, and more.
uname # Output: Linux
uname -a
Zip and unzip
To zip the files we use the command zip. And unzip is used to unzip the files.
zip file1.txt file2.txt
unzip file.zip
Hostname
The hostname
command in Linux is used to display or set the hostname of the current system. The hostname is the unique name given to the system that identifies it on the network.
hostname
hostname -i # This gives host ip address
hostname -d # This gives host DNS domain name
lscpu
to get the CPU details we can use the lscpu command.
lscpu
Memory
To know about virtual memory we have vmstat
and to now the free memory we have command free
.
vmstat
free
Networking Commands
nslookup
This command is used to query DNS servers to obtain a domain name or IP address mapping information.
nslookup google.com
ping
This command is used to check the availability of a network host and to measure the round-trip time for packets to travel from the source host to the destination host.
ping google.com
netstat
This command is used to display network connections, routing tables, and network interface statistics.
netstat
curl
This command is used to transfer data from or to a server using one of the supported protocols (HTTP, HTTPS, FTP, etc.).
curl http://www.google.com/