Sunday 5 July 2020

Where does Linux store the commands executed recently?


151. Where does Linux store the commands executed recently?

The history list are stored in ~/.bash_history file.

Note that if you don't use bash, it won't be in ~/.bash_history, but ~/.<shell name>_history. 
For example, for zsh, log is in ~/.zsh_history.

152. show all of the last commands that have been recently used?

$ history

153. how to set the history limit and check the current settings?

$ export HISTSIZE=2000

Apply changes,
$ source ~/.bashrc

To check the current settings,
$ echo $HISTSIZE
$ grep HISTSIZE /etc/profile /etc/bash.bashrc ~/.profile ~/.bash_profile ~/.bash_login
 
154. How to add date and time to bash history?

Set HISTTIMEFORMAT to print the time stamps associated with each history entry.

Append the following line to ~/.bashrc file:
export HISTTIMEFORMAT="%h %d %H:%M:%S "

Now, when you type history, it will show something like:
113  Jun 08 16:31:06 sudo ifconfig

155. Ways to Re-run Last Executed Commands in Linux?

i. Use the Up arrow keys to retrieve a previous command. Pressing it constantly takes you through multiple commands in history.

ii. To re-execute a specific command from the history of commands,
!number #find the number using history command.
for example,
# !988
echo $HISTSIZE
3000

iii. Also re-execute previously used command with '!' character followed by a few of the first characters of that particular command as shown.
for example,
# !find
find / -name profile
/etc/lvm/profile

156. What is the command to start a job that will keep on running even after closing the session?

nohup commad used to avoid a command to be terminated when you close the terminal or get disconnected.
nohup COMMAND [ARGS]

157. How to backup or archive files in Linux using tar command?

To back up a directory, simply run the tar command  below
$ tar -cvf tarball_name.tar /path/to/directory

For example, to create an archive in tar.gz format of a file sales.txt  in /data/reports/ path execute:
$ tar -cvf sales.tar.gz  /data/reports/sales.txt

You can also archive a whole directory as shown:
$ tar -cvf repors.tar.gz  /data/reports/

158. Why you use export command?

Export command marks and exports environment variables.

159. What are the environmental variables?

They are dynamic values that affect the process of programs on a computer. They exist in every operating system and their types may vary. They can be created, edited, saved and deleted and they also give the information about the system behaviour.

160. How to find the interface IP address?

You can easily find the IP interface by running the command ifconfig interface_name or using ip command.

For example
ifconfig –a
ip addr show

161. Choose 5 Linux commands, what are your choices?

   •    rsync command

The rsync command can be used to synchronize two directories or directory trees whether they are on the same computer or on different computers but it can do so much more than that. rsync creates or updates the target directory to be identical to the source directory.
rsync -aH sourcedir targetdir
The -a option is for archive mode which preserves permissions, ownerships and symbolic (soft) links. The -H is used to preserve hard links. Note that either the source or target directories can be on a remote host.

   •    sed command

Sed command is used when you need to select specific lines of a file. Sed is short for stream editor, is one way to do this. You want to combine multiple files that all had headers or to do a bulk find and replace a file.
Insert a blank line above every line which matches "regex"
$ sed '/regex/{x;p;x;}'
Change "scarlet" or "ruby" or "puce" to "red"
$ sed 's/scarlet/red/g;s/ruby/red/g;s/puce/red/g'

   •    awk command

Awk is a programming language that allows easy manipulation of structured data and the generation of formatted reports. It searches one or more files to see if they contain lines that match with the specified patterns and then perform associated actions
Print Specific Field
$ awk -F':' '{ print $1 }' /etc/group
$ date | awk '{print $2 " " $6}'

   •    lsof command

lsof is a command line utility which is used to list the information about the files that are opened by various processes. In unix, everything is a file: pipes, sockets, directories, devices, etc. So by using lsof, you can get the information about any opened files.

List processes which opened a specific file
# lsof /var/log/syslog

Lists all open files belonging to processes owned by the user
# lsof -u username

Kill all process that belongs to a particular user
# kill -9 `lsof -t -u username

List all network connections
# lsof -i

List all network files in use by a specific process
# lsof -i -a -c ssh

List processes which are listening on a particular port
# lsof -i :25

   •    grep command

Grep is a command used to search text or for a given file for lines containing a match to the given strings or words. By default, grep displays the matching lines.

Print network connection used by firefox
# netstat -pltnu | grep firefox

Print the line which contains "root" on /etc/passwd file
# cat /etc/passwd | grep root

162. How to remove blank lines from a file - sample.txt in a single command line ?

sed '/^$/d' sample.txt
Here “^” symbol represents the starting point of a line and “$” represents the end of the line. Whereas “^$” represents the empty lines, d stands for delete.

163. How to get version from multiple Linux servers using bash script?

#!/bin/bash
serverlist='server_list.txt' #serverlist
servers=`cat $serverlist` #write in variable all server list
result='result.txt' #path to file with result
echo -e "Servername \t\t kernel version"> $result
for server in $servers
do
kernel=`ssh root@${server} "uname -r"`
echo -e "$server \t\t $kernel" >> $result
done

164. Explain Grep command and Regular Expressions?

“GREP” abbreviates as a “Global Regulation Expression Point”. Grep command is an expression used for filtering results or output. It is used for searching a text file for commonly used expressions. 

For instance, to list a directory and only search for files with the word 'cron', run the command:
For example,
ls | grep cron
grep “smith” passwd shadow
netstat -an | grep 8083
cat /etc/passwd | grep smith

165. What do you mean by an ext3 file system?

Ext3 file system is an upgraded version of ext2 and it also supports journaling. When an unclean shutdown is performed ext2 file system performs a check on the machine for errors which is a long process but it is not so in case of the ext3 file system.

In case of a hardware failure, an ext3 consistency check will occur without any pause. The time of the recovery of the file system is independent of the number of files. The time is dependent on the size of the journal which only takes a second which depends on the speed of the hardware.

166. How would you count every occurrence of the term “water” in all the files appearing under the current directory, and its subdirectories, recursively?

$ grep -orI water . | wc -l

To list every occurrence of the term “water” on a separate line, one must run grep -o water <path>. Adding the r flag to the command makes the search recursively process every file under the given path, and the I flag ensures that matches in binary files are ignored. In addition, the w flag can be included to match the exact term only, and ignore superstrings such as “watering”, and to make the search case-insensitive, the i flag can be added as well:

$ grep -iworI water . | wc -l
The number of lines yielded by this grep command is the number of occurrences of the desired term, which can then be counted by piping it into the wc -l command.

167. What is the issue behind getting an error “filesystem is full” while there is space available when you check it through “df” command? How will you rectify this problem?

When all the inodes are consumed then even though you have free space, you will get the error that filesystem is full. So, to check whether there is space available, we have to use the command df –i.  Sometimes, it may happen file system or storage unit contains the substantial number of small files, and each of the files takes 128 bytes of the inode structure then inode structure fills up, and we will not be able to copy any more file to the disk. So, to rectify the problem, you need to free the space in inode storage, and you will be able to save more files.

168. How can we create a local Yum repository in the location /media with the use of mounted Linux ISO image?

To create the local yum repository you have to create the files ending with extension .repo in the location /etc/yum.repos.d
Syntax: [root@localhost yum.repos.d]# cat local.repo
[local]
name=RHEL6.5
baseurl=file:///media
enabled=1
gpgcheck=1
gpgkey=file:///media/RPM-GPG-KEY-redhat-release

169. Mention the methods to check whether using Yum, the package is installed successfully or not?

There are several methods to check whether the package is installed or not. 

Method 1 –If the command is executed successfully then after running the yum command it will show ‘0’ on checking the exit status.

Method 2-You can run the rpm and –qa test.

Method 3–In the yum log, check if any entry is installed in the directory.

170. What is the advantage of executing the running processes in the background? How can you do that?

The most significant advantage of executing the running process in the background is that you can do any other task simultaneously while other processes are running in the background. So, more processes can be completed in the background while you are working on different processes. It can be achieved by adding a special character ‘&’ at the end of the command.

171. Why is “finger service” always kept disabled when not in use?

Finger Service acts as both the Web and FTP server. It is also known as Finger User Information Protocol which contains the information of the user that can be viewed by the clients. It allows a remote user to see the information about the admin such as login shell, login name and other confidential details. That is why; the finger service should be kept disabled when it’s not in use.
If it is not disabled, you have to modify and comment out the file “/etc/inetd.conf”.

172. How can we make a router with the help of Linux Computer?

You may generally come across this type of questions in Linux interview. Linux machine has the ability to turn it into a router with the help of IP Masquerade. You may have seen the servers found in commercial firewalls. IP Masquerade does the same function to one-to-many Network Address Translation servers. If the internal computers do not have the IP address then in this case, IP Masquerade can connect to the other internal computers which are connected to Linux box to access the internet.

Just follow these steps to enable IP Masquerade Linux:

Connect your PC to LAN.

This PC can be used as a default gateway for other systems for TCP/IP networking. You can use the same DNS on all other systems.

Go in the Kernel and enable IP forwarding. You can also enable IP forwarding using the command: /etc/rc.d/rc.local file on rebooting the system.

The last step is to run this command which sets up the rules to masquerade: /sbin/iptables

173. What is command grouping?

We can redirect a command from a file or to a file. It is usually done with the help of braces or parenthesis. When the command is grouped then redirection is done to the whole group.
The command is executed by the current shell when we use the braces () and in case we have to execute a command by a subshell then we use parenthesis {}.

174. Write the command to view an existing tar archive and how to extract it?

The command for viewing tar archive that is already existing: $ tar tvf archive_name.tar
The command to extract an existing tar archive: $ tar xvf archive_name.tar
The command for the creation of new tar archive: $ tar cvf archive_name.tar dirname/

174. Write the steps to make a USB bootable device.

Followings are the steps to make a USB bootable device –

write efidisk.img from RHEL 6 DVD images/ subdirectory
to USB dd if=efidisk.img of=/dev/usb (name of the usb device)

disable ping to avoid network /ICMP flood

set the following in/etc/sysctl.conf : net.ipv4.icmp_echo_ignore_all =1

Then “sysctl -p”

175. Mention the steps to create the partition form a raw disk?

In case we want to create a new partition form a raw disk, you have to use a tool known as “fdisk utility”. The steps to create a raw disk are as follows:

In case of IDE we use >>> fdisk/dev/hd and in case of SCSI we use >>> fdisk/dev/sd

Then type n for creating a new partition.

After the partition is created type ‘w’.


                                                                                           MENU         PREVIOUS | NEXT

No comments:

Post a Comment