Most Important Linux Server Administration Interview Questions Part – 2
1.You have a regular user access to a server,with no root permissions,but you need to create a script that requires root permissions to run – how can you manipulate the system to think that you have root permissions,without a real superuser access?
Linux provides a tool called fakeroot that allows you to run a “fake root shell” that will present you as root and your id as 0,this will make the system believe that you have root access for the current run.
2.Write a script that receives one parameters (file name) and checks if the file exists or not – If it does,print “Roger that!” else,print “Hey we’ve got a problem!”
#!/bin/bash FILE=$1 if [ -f $FILE ]; then echo “Roger that!” else echo “Hey we’ve got a problem” fi
3.Write a script that checks if a file,given as an argument,has more than 10 lines or not,if it does – print “Over 10”,else print “Less than 10”
#!/bin/bash count=`cat $1 | wc -l` if [ “$count” -gt “10” ] ; then echo “Over 10” else echo “Less than 10” fi
4.You want to create a backup script called “backupMyFiles” and it will run every hour,how do you make sure that your script is not already running when you run the script – write a short script that will handle this issue and exit with the message “Previous is still running” in case the script is still in the background. You need to check if your script is currently running in the system – you can do it with ps
#!/bin/bash cmd=namedScript runningProcs=`ps –no-headers -C${cmd}` count=`echo $runningProcs|wc -l` if [ $count -gt 1 ]; then echo “Previous $cmd is still running.” exit 1 fi
5.create a Fibonacci function (Fn=Fn-1+Fn-2) using awk (until F20).
awk ‘BEGIN { fa=1; fb=1; while(++i<=20) { print fa; ft=fa; fa=fa+fb; fb=ft }; exit}’ 6.Write a script that will go over all the users on the system and will write the last login date of the user – if we don’t have information about the last login,write “No data for ” #!/bin/sh for i in ‘cat /etc/passwd | awk -F: ‘{print $1; }” ; do last=’last $i | head -n 1’; if [ “$last” != “” ];
then echo ‘last $i | head -n 1’; else echo “No data for $i” fi done 7.How can you check what are the most common commands that you have used in the Linux shell? You can get this information from the history command and sort it by most used – history | awk ‘{h[$2]++}END{for(c in h){print h[c] ” ” c}}’ | sort -nr | head. 8.Write a script that goes to http://www.whatismyip.org/ and writes
“Your IP is: “. #!/bin/sh ip=’links –source http://www.whatismyip.org/’ echo -n “Your IP is: ” echo $ip 9.Create a small calculator in bash script which will have an internal function “dosomething” that will receive a math function as an input – mycalc 4+4 * 4. #!/bin/bash function dosomething { echo “${1}”|bc -l; } dosomething $1 10.
Create a script called KillUserProcs that will get a username as an input and will kill all his processes. #!/bin/bash kill -9 ‘ps aux|awk -v var=$1 ‘$1==var { print $2 }” 11.How can we find which of the users (residing in /home/) are using the most HD space?
We should use the du tool to get the HD usage and find the most heavy directories – du -hs /home/ * | sort -nr | head – 10. 12.How can we change your server timezone to a Europe/France timezone? We need to link the file /usr/share/zoneinfo/Europe/Paris to /etc/localtime.
13.How do you change the user tom’s login shell to /bin/sh?
You need to use the command chsh -s /bin/sh tom.
14.What is the difference between “kill ” and “kill -9 “?
When adding “-9” to the kill command,it tells the PID to exit immediately instead of exiting in the correct way – cleaning children/temp/sockets.
15.How do you add a virtual network interface on eth0 with the ip 10. 10. 10.1 on class C?
You can add virtual interfaces on eth cards that will hold additional system IPs – ifconfig eth0:0 10. 10. 10.1 netmask 25 5.25 5.25 5.0.
16.How can you check the memory and paging status (free,used,cached)?
Using free -m you will get the current memory status,including free,used,cached both for the physical and swap memory.
17.How can you get cpu and harddrive activities average report?
iostat gives you information and averages about your system I/O for the CPU and HD.
18.If you want to get statistical information about your server for the previous days,how can you do that?
In order to get statistical information about your server,you can use sar.
19.How can you get CPU statistics,per CPU or Core?
In order to get statistical information,per CPU/Core,you can use “mpstat -P ALL”
20.How can you find which of the directories (top level) on the current directory is the heaviest (kb)?
Use du to get the size of each top level dir,and sort it du -k –max-depth=1 | sort -nr
21.You have a DNS server and you have updated your configuration – how do you tell the named to reload the configuration with the DNS admin tool?
The DNS admin tool is the rndc – you can use it to reload the configuration with – rndc reload
22.What is the difference between the “crontab” and the “at” functionality?
Crontab (Cron command) is used to schedule a task daily at the same time (or times) repeatedly,on the other hand,the “at” command is used to schedule the task only to run only one time.
23.How can you add a file system (partition) that will be mounted automatically when the Linux is booting?
In order to allow automatically mounting of a partition,you will need to add the partition to your fstab under /etc/fstab and provide Label,mount point,FS Type and options/permissions.
24.You have decided to add 80.16 1.16 1.1 to your DNS servers (for DNS resolving),which file should be edited and what should be added to this file?
The /etc/resolv.conf holds your DNS servers list; you need to add a new line “nameserver 80.16 1.16 1.1” at the end of the file.
25.How can you set the eth0 interface to auto-negotiation on?
You can use the ethtool to configure the eth card: ethtool –change eth0 autoneg on.
26.Given a local file /usr/bin/pstree” on the HD,how can you identify if it came from an RPM?
RPM allows you to reverse-query the RPM DB and search its files by: rpm -qf /usr/bin/pstree
27.What is the LVM and where can you find its configuration?
The LVM is the Linux Volume Manager and its configuration is found under /etc/lvm/lvm.conf.
28.How can you get the status of all the services configured on your Linux machine?
You can use the service command to stop/start and get information about the services – service –status-all.
29.How can you change the system library paths?
In order to change the system library paths,you need to edit the file /etc/ld.so.conf and then run ldconfig to reload the changes.
30.How can you disable all ping responses from the Linux machine?
You need to manipulate the ip configuration files – echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all