The Linux Commands Series: Part VI
passwd, ping, traceroute, clear, history, export, crontab, uname, env, printenv
I intend to write a series of VI blog posts describing the most used Linux Commands. This blog is the first of the series. I hope you all enjoy the reading.
Citation/References/Credit will be mentioned at the end of the post.
passwd
Users in Linux are assigned a password and users can change the password using the command passwd.
There are two situations.
One is when you want to change your password, here you type the command:
passwd
An interactive prompt will ask you to enter the old password and then a new password.
Second, when you are root, you can set the username of which you want to change the password:
passwd <username> <new password>
Here, you don't need to enter the old password.
ping
This command pings a specific network host, on the local network or on the internet.
Pinging is useful to know if a host is reachable and how long does it take to reach a host.
Syntax: ping <host>
<host> can be a domain name or an IP address. For example, pinging apple.com
This command sends a request to the server and the server sends a response.
ping sends the request every second to the server until you stop it with ctrl-C or unless you pass the number of times the request should be sent using the option -c:
ping -c 3 apple.com
Once it is stopped, it will print statistics on the packets sent, received and packets lost.
Not all servers support pinging, this is because of security reasons if someone wants to hide the server. The ping packets can also be blocked by firewalls.
traceroute
When you try to reach a host, your request goes through your home router, ISP (Internet Service Provider) and then other hosts in the network, and then the target host.
traceroute helps us to find the router/hosts information along the path. Not all routers send the information, in this case, ‘* * *’ will be printed on the terminal.
clear
‘clear’ command will clear the terminal and the prompt will be at the top. You will lose access to previous commands you have executed on the terminal.
You can use the ‘clear -x’ command to clear and to have access to previous commands by scrolling up (press up arrow key).
clear
clear -x
history
You can display all the commands you have used since the beginning of the terminal session using this command:
history
You can execute a command from history using the syntax:
!<command number>
The last 500 commands are stored in history.
export
Suppose you have a variable TEST=”test” and you can print this variable value in the terminal using echo $TEST.
Now, if you write a bash script in a file test.sh with the above command and execute it, nothing will be printed in the terminal.
When the test.sh is executed a subshell will be launched to execute it but not in the first shell as the variable is not available.
To make this variable available we need to define it in the following way:
export TEST=”test”
If you execute the test.sh script now, “test” will be printed.
Sometimes you want to append something to a variable such as the PATH variable then you can use export.
export PATH=$PATH:/path
If you want to remove a variable then use the ‘-n’ option:
export -n TEST
If you type export without any options then it will list all the exported variables.
crontab
Cron jobs are jobs that are scheduled to run in specific intervals such as every hour, daily, weekly, or on the weekends. These are useful for maintenance and automation works.
You can list all the cron jobs you have created using the command below:
crontab -l
You can create or edit the cron jobs using the command crontab -e and this will open a default editor vim usually.
You can write the cron job line for each cron job and you can also use “https://crontab-generator.org/” for easy generation of cron job syntax. Once edited you can save and exit from the editor using the commands as discussed in the vim editor section in the Vth part.
In order to remove a cron job, type crontab -e, remove the line of the particular cron job, and exit the editor.
uname
‘uname’ command without option will print the operating system codename (Kernel):
uname
Option m shows the hardware name, p shows the processor architecture name, s shows the Operating System name, r shows the release, v shows the version, n shows the node network name, a shows all the information available.
You can execute ‘uname’ command with the option one at a time or all at a time.
env
‘env’ command can be used to pass the environment variables.
Suppose you want to run a node js app and want to set the USER variable to it, you can do as below:
env USER=test node app.js
You can also clear all the environment variables already set using the option -i
env -i node app.js
Here, you might be getting an error as the PATH variable too will be unset which was used to reach the command node. So you have to give the full path to reach the command.
env -i usr/local/bin/node app.js
If you remove ‘-i’ option will make the PATH variable again inside the program.
If you type ‘env’ without any option will print all the environment variables.
env
You can also make an environment variable inaccessible inside a program using ‘-u’ option. For example:
env -u HOME node app.js
printenv
You can print all the environment variables available in the system using this command.
printenv
You can specify a variable as an option/parameter to print only that variable value, for example:
printenv HOME
Note: Remember to use the man command for more intel on a particular command.
This is the end of this blog post. I will publish a blog post in this series once a week.
Citation/Reference: flaviocopes.com
Please follow me for more interesting content.