How to assign IP to network interface command line in linux

This article objective is provide you the way to assign the IP address to the network interface from the command line.

You can see the available network interfaces on your machine by using the simple command ifconfig

You can find the interfaces and their names and you can identify the name which is  preceded by colon(:) in the left side. Something like eth0, lo and wlan0 etc.  The network interface lo interface is the special interface where we called it as localhost and special IP assigned to it 127.0.0.1 also called loopback IP.

Do you know? you can assign multiple IPs to single network interface. This is pretty useful when you need multiple IP addresses but you have only one network card.

We created new interface alias with IP assigned. We can do it simply by giving colon(:) and alias number.

Assign IP using ifconfig

This command will assign the specified IP to the give network interface. It is not a persistent change. The would change after reboot.

 

Get IP using dhclient

The program dhclient will get you the IP to the given interface using the Dynamic Host Configuration Protocol (DHCP). The IP which will be assigned to the given network interface which will be provided by gateway or router. If you won’t have any preference of having specific IP assigned to the network card, this can be used

 

Assign static IP

The IP we assigned above is not a persistent or static IP. Means you will lose that IP and will get an other different IP assigned after reboot. To make this IP permanent we have to edit the configuration files.

Ubuntu /etc/network/interfaces

 

CentOS /etc/sysconfig/network-scripts/ifcfg-eth0

Unlike the distribution Debian, CentOS maintains the configuration in a separate file for each interface. The  file path would be something like  /etc/sysconfig/network-scripts/ifcfg-<interface_name>.

 

How to modify permissions(booleans) of SELinux for deamons(programs)

SELinux offers more enhanced security for linux. It is always recommended not to disable SELinux for servers which are more delicate, instead you can control the permissions for the deamons, programms or users using SELinux.

SELinux maintains the status of permissions for all deamons with attributes called booleans.

Get SELinux booleans

The above command will give you lot of variables with status either on or off. If you want to fetch for particular process or context use grep 

To get all booleans regarding httpd(apache web server)

 

Set SELinux Booleans

To set selinux booleans we use the command setsebool

Here is how you can change

For suppose if you want to allow httpd to allow sending mail

If you want to enable ftp server on httpd,

To enable apache to connect with external database

Like wise you can change the required booleans status. To query the modified status ues getsebool with grep.

 

How to Interactively discard selected changes in git

GIT is offering us the way to discard selected changes interactively. Here is how you will do it,

To checkout or discard selected changes from all modified files

git checkout -p  

If you want to checkout or discard selected changes from specific file, give that file as an argument

git checkout -p  file_name

As per this command git will break down the file into chunks called hunks. After you execute this command, you will be prompted with following question with several options to choose.

Discard this hunk from worktree [y,n,q,a,d,/,e,?]?

Here is the description of each option,

y – discard this hunk from worktree
n – do not discard this hunk from worktree
q – quit; do not discard this hunk nor any of the remaining ones
a – discard this hunk and all later hunks in the file
d – do not discard this hunk nor any of the later hunks in the file
g – select a hunk to go to
/ – search for a hunk matching the given regex
j – leave this hunk undecided, see next undecided hunk
J – leave this hunk undecided, see next hunk
k – leave this hunk undecided, see previous undecided hunk
K – leave this hunk undecided, see previous hunk
s – split the current hunk into smaller hunks
e – manually edit the current hunk
? – print help

You can also unstage the selected changes from all staged files or from specific file.

To unstage selected changes from all staged files use following command

git reset HEAD -p

To unstage changes from specific file give that file as an argument.

git reset HEAD -p  file_name

Here also you will be prompted to choose any one of the option to unstage hunk.

 

GIT Tips

List Contributed Users

We can list the user who contributed in git using command git log.  Here is the way to get,

And you can check users who contributed using following command

Enable Debugging

You can see curl debug message while push, pull and clone if you set the environment variable GIT_CURL_VERBOSE to 1

$ export GIT_CURL_VERBOSE=1

Set this variable before you push or pull or clone. You would be able to see debug message

List History Of Operations

The following command can be useful to list all history of operations performed using git

 

Stage all modified files in git

Stage all modified files using single command.

How to delete records by disabling foreign key checks or constraints in mysql

If you have a foreign key constraints on your table, you can’t delete records from parent table, but you can delete from child table. The main reason we use the foreign key constraints is to maintain data integrity.

Some times you many need to disable this kind of enforcing against some operations like delete. To do so, you have to set mysql server system variable foreign_key_checks

Now, you can delete or update with out any restriction because of foreign key constraint. But you will mess up the data integrity.

You can reset it back to enforce for integrity

There are global and local system variables. If you set the global variables it applies to all clients which are connected. If you change the local variable it applies to that current client session only. Here is how you can query local and global variable.

To sent global variable you just need to add global , where to set local you need to local. By default local or session variable will be changed

To set global variable

NOTE: Make sure you to revert the variable back to original state to maintain data integrity in your database.

 

How to enable or disable SELinux and check status on centOS

The SELinux stands for Security-Enhanced Linux where it is a linux kernel security module. It is enabled by default on most of the linux distribution that we use for servers like centOS. It provides  enhanced security measurements. It gives you fine control over all programs and daemons  on their activities like communicating with out side programs  or controlling whether to establish a outside connections for a particular program.

It is always recommended to have SELinux enabled on a server to avoid common security glitches.

To query the current status of SELinux  use the following commands

The above command will report the current status  of SELinux. Whether SELinux is enforcing, permissive, or disabled. If it is already disabled.

Disabling SELinux

Open the file  /etc/selinux/config  and change the option SELINUX to disabled

if you open file you would see something like

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing – SELinux security policy is enforced.
# permissive – SELinux prints warnings instead of enforcing.
# disabled – No SELinux policy is loaded.
SELINUX=enforcing
# SELINUXTYPE= can take one of these two values:
# targeted – Targeted processes are protected,
# mls – Multi Level Security protection.
SELINUXTYPE=targeted

If SELinux is enabled you would see enforcing replace it with disabled.

You should restart the machine to take effect If you change SELINUX status from Enabled to Disabled or vice versa.

Enabling SELInux

To enable SELinux follow the below instructions,

  1. Open the file /etc/selinux/config  
  2. Change option SELINUX from disabled to enforcing
  3. Restart the machine

Change mode

To change the mode of SELinux which is running

Check Status

SELinux is the linux  kernel module for enhanced security. SELinux stands for Security-Enhanced Linux. If SELinux is installed on your machine or server you can check the current status  by using following commands

The above command will give you one of the following as an output

You use the below command which will give simple overview

If enabled you will output something like

 

You can also check the configuration which is located at /etc/selinux/config.

In above config file  the option SELINUX  describes the status of SELinux. But it’s not precise to determine the status from the configuration file, it’s better to determine the status by using commands mentioned above.

 

 

Note: You need administrator privileges to either enable or disable SELinux

SIP Request Methods Response Codes

SIP Request Methods Response Codes.

SIP Request Methods

There are several different Request methods to server different purposes. SIP borrowed headers and body format from the protocol HTTP. Like HTTP SIP also has different methods. following table will describe those request methods

 

SIP Request Methods
S.No Method Description
1 REGISTER Registers to receive inbound calls on registrar or SIP server
2 INVITE Established a new session
3 ACK Confirms that message/request has been received
4 BYE Ends Session
5 CANCEL Cancels establishing session
6 OPTIONS Queries capabilities of server
7 PARK Provisional Acknowledgement
8 SUBSCRIBE Subsribes for an Notifications from the Notifier
9 NOTIFY Notify the subscriber a new Event
10 PUBLISH Publish and event to the Server
11 INFO Sends mid-session information
12 REFERER Asks the recipient to issue call transfer
13 MESSAGE Transport Insant Message
14 UPDATE Modifies the state of a session without changing the state of the dialog.

….

SIP Responses

Like I mentioned earlier, SIP protocol borrowed few things from protocol HTTP. SIP borrowed response code from HTTP. Most of response codes are similar to HTTP and  SIP extends them codes to 6xx.

SIP Response Codes
S.No Code Description
1 1xx Informational responses
2 2xx Success Responses
3 3xx Redirect Responses
4 4xx Request Failure/Client Error
5 5xx Server Error
6 6xx Global Failures

How to send email command line in linux

We all know that email plays a vital role in our communication system. We will see how to send mail from command line in this article.

Assuming that you have postfix instaled on your machine. You can send the mail from command line by connecting to your local mail server. You will get the utility sendmail with postfix.

send email using sendmail

Send email using telnet

We all now about telnet command. It is very helpful to debug and understand the protocols. Here is the simple mail sent using telnet

In the above example we connected to the SMTP server as a client which is running on localhost. We gave commands to the server to send a mail. Please note that  the data ends with   <CR><LF>.<CR><LF> . to indicated it is the end of data. As you can see  . (dot) we mentioned  at the end.

Once we connected, we pass the set commands to server to write and send a mail. Here are the few SMTP commands

SMTP Commands

  •  HELO
  • MAIL
  • RCPT
  • DATA
  • RSET
  • VRFY
  • EXPN
  • HELP
  • NOOP
  • QUIT

We used MAIL, RCPT and QUIT commands in the above example. To understand these commands go through rfc . You may not need to understand low level protocol concepts of SMTP unless if you are really interested or you are working with SMTP.

References:

[1] http://tools.ietf.org/html/rfc5321

How to edit or change commit history using git rebase

Do you ever wonder about rewriting git commit history. Here is how you can do. One great advantage we get with git version control system is that, we can edit or rewrite the history of commits. Some times may be you want to publish the different commit history or for some reason you may want to edit the few chunks for commit history.

There are different ways to edit the history of commits. There is one quick way to edit the very last commit or HEAD. Using following command

If you want to edit selected commits in the history, you can do it using feature called rebasing in git. Rebasing is a great feature in git. Other than process called merging  we can use rebasing to merge different branches in different way other than usual git merge. One of the advantage is we can rewrite the history to make sure commit history looks like the way you want.

We have to use interactive rebasing to edit the commit history, using the following command

where we specify the commit  which is parent of the commit  from which you want to edit the commit history

Lets say I chosen the commit from where I want to edit the history, I would type the following command

If you perform above command, you wold see the following content in the default editor,

The above content is self explanatory, if you want to remove commits just remove corresponding commit line. if you want to abort interactive rebase any time use git rebase –abort. You should notice that your last commit is at bottom. These commits from chronological from top to bottom.

If you chose to edit the selected commits, let’s say want to change author, you have to replace the word pick with word edit for that corresponding commits and quit the editor by saving. Then you will stop at the first commit, you can change the author using following command

Like wise you can change the commit message as well. Of course you can also change the content of files to commit

and then to continue rewriting history using following command,

You have to  repeat these steps for each commit that you change the  word pick to edit. Git will stop to let you amend the commit. Once you are finished with specific commit, you can proceed further by using rebase command using command line argument –continue. 

Once you finished with rebase, you may see the following message

If you get conflicts, you have to resolve them in the way you resolve for merging process. Once you resolved the conflicts by removing conflict markers(>>>>>> ======= <<<<<<<). Use to git add  to mark them resolved. Then continue to rebase using command

 

WARNING:

Beware of rebase,  you may corrupt the commit history  of your team members and it may leads to confusion.

If you are learning don’t try to experiment it on real repository. Instead you create the dummy repo and get hands on experience.

You may produce merge conflicts sometimes, you may have to resolve to proceed the rebase

Want to read more?

References:

[1] https://git-scm.com/book/en/v2/Git-Tools-Rewriting-History

[2] https://git-scm.com/book/en/v2/Git-Branching-Rebasing