Tuesday, 20 March 2012
Thursday, 8 March 2012
How can you check if your Linux machine on network has static or dynamically allocated IP? How can you change it from dynamic to static?
Posted by Muhammad Nasir on 05:35
How can you check if your Linux machine on network has static or dynamically allocated IP?
How can you change it from dynamic to static?
Ans:
Network configuration file can be looked at for checking if Linux machine has static or dynamically
allocated IP. ifconfig can be used to check currently allocated IP.
In Ubuntu the configuration file is /etc/network/interfaces. For dynamically allocated IP, it should state
dhcp, e.g following entry:
iface eth0 inet dhcp
In order to change it to static IP allocation, dhcp should be changed to static and IP address, subnet
mask, network address, broadcast address and gateway need to be mentioned. E.g. :
iface eth0 inet static
address 10.1.10.54
netmask 255.255.255.0
network 10.1.10.0
broadcast 10.1.10.255
gateway 10.1.10.1
Networking service needs to be restarted after making this change.
sudo /etc/init.d/networking restart
Clear Screen & Display Today date and Time In linux
Posted by Muhammad Nasir on 05:33
Modify your shell, such that every time you enter clear, it clears screen and on top of new
screen displays “Welcome username. You are using shel Today is date and time”. Where username
is the name of the user logged in,shel is the path to the shell you are using and date and time
is current date and time. 15 Marks
In order to display “Welcome username. You are using shell. Today is date_and_time” following
command can be used:
echo Welcome $USER. You are using $SHELL. Today is $(date)
In order to modify shell, such that every time clear is entered, this command is executed AFTER clearing
the shell, an alias can be defined as follows:
alias clear=”clear; echo Welcome $USER. You are using $SHELL. Today is $(date)”
What is difference between su and sudo? su be used instead of sudo for root privilege?
Posted by Muhammad Nasir on 05:29
What is difference between su and sudo? How can su be used instead of sudo for getting access
to root privilege?
Ans:
su is used for logging in as any other user, including root user. After entering user password, you can
enter commands as that user unless u enter exit. In order to log in as root, just enter su and in order to
enter as a different user enter suusername. su can be used for single command as:
su user_name –c command
sudo is used for gaining root privilege for a specific command. Adding sudo at the start of a command
implies that root privilege is used for that command. In order to use sudo, the current user has to be
added in sudoers file. sudo–i can be used for starting a shell as root user, but in that case password for
the current user is required, not that of root.
Thus, su can be used to log in as any other user while sudo is used to log in as root user. Moreover, sudo
will always ask for current user password and su will prompt for the password of the user you want to
log in as.
Write a (Perl or Bash) script which takes one or more filenames as arguments. If any of the files does not exist in the current directory display prompt. For each file, remove all the comments lines in the file.
Posted by Muhammad Nasir on 05:27
Write a (Perl or Bash) script which takes one or more filenames as arguments. If any of the files
does not exist in the current directory display prompt. For each file, remove all the comments
lines in the file.
#!/bin/bash
if [ $# -eq 0 ] #If no file is given as argument
then
echo 'Please provide name of file as argument' #Display message
else #If arguments are given
for file #For each argument
do
if [ ! -f $file ] #If file does not exist
then
echo "File $file does not exist" #Display message
else #If file exist
echo "Original contents of file $file are:" #Display message
cat $file #Display original file contents
sed -e '/^#[^!]/d' $file > temp #sed used to remove lines which start with #,
except those starting with #!. Result saved in file temp
echo "File contents after removing comments are:" #Display message
cat temp > $file #Output is written back on the file
cat $file #Display updated file
fi
done
fi