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?


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


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?


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.


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

Read file in linux, check if the group exists,Check if the user exists,Add the group if it does not exists


You have a file named users. Each line in the file contains two words (space separated): a user
name and a group name. Write a (Perl or Bash) script to: 25 Marks
a. read one by one each line in the file
b. check if the group exists, add the group if it does not exists
c. check if the user exists, check the user belongs to the same group, if not change the
group. Add user to the group if user does not exists
d. Set the password of the user same as the username.
e. On each step, display appropriate prompts (e.g., user added, user already exists etc.)


Script:
#!/bin/bash
while read user group #Reading user and group from the file
do
echo CHECKING FOR USER $user and GROUP $group #Display message before checking for
each user or group
tmp_u=$(grep -w ^$user /etc/passwd) #Check if user exists.
tmp_g=$(grep -w ^$group /etc/group) #Check if group exists
pass=$(mkpasswd $user)
if [ -z $tmp_g ] #If group does not exist
then
echo Group $group does not exist, it is being added #Display message
sudo groupadd $group #Adding group
else #If Group exists
echo Group $group exists #Display message
fi
if [ -z $tmp_u ] #If user does not exists
then
echo User $user does not exist, it is being added #Display message
sudo useradd -g $group $user #Adding user to the group
else #If user exists
echo User $user exists #Display message
org_grp=$(id -gn $user) #Extract group of the user
if [ $group == $org_grp ] #If user's current group is same as desired
group
then
echo User $user already belongs to group $group #Display message
else
echo User $user belongs to $org_grp and being changed to $group
#Display message
sudo usermod -g $group $user #Change the user group
fi
fi
sudo usermod -p $pass $user #Changing password to the user name
echo "Password for $user changed to $user" #Display message
echo '**********************************************************'
done < user #Input file (user) for the while loop
Contents of file user are:
omer omer
check grp
ent nust
cse seecs

 
Free Host | lasik surgery new york