{"id":97,"date":"2022-03-28T21:20:48","date_gmt":"2022-03-29T01:20:48","guid":{"rendered":"http:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/?p=97"},"modified":"2024-10-11T09:36:00","modified_gmt":"2024-10-11T13:36:00","slug":"ip-command-examples","status":"publish","type":"post","link":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/?p=97","title":{"rendered":"ip command examples"},"content":{"rendered":"\n<p>The <strong>linux ip command<\/strong> is similar to ifconfig, but more powerful and is intended to be a replacement for it. With ip you have the advantage of performing several network administration tasks with only one command.<br>In this tutorial, we are going to discuss 15 most common uses for \u2018ip\u2019 command, so let\u2019s get going.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">1. Identify available network interface with ip command<\/h4>\n\n\n\n<p>If you run ip link show command it will list all available network interfaces on your server.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ ip link show<\/pre>\n\n\n\n<p>Example output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ ip link show\n1: lo:  mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000\n    link\/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00\n2: enp0s3:  mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000\n    link\/ether 08:00:27:64:ab:7a brd ff:ff:ff:ff:ff:ff\n<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">2. How to Check an IP Address<\/h4>\n\n\n\n<p>To get the depth information of your network interfaces like IP Address, MAC Address information, use the following command as shown below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ ip addr show<\/pre>\n\n\n\n<p>The output will display the currently assign IP configuration for all network interfaces.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ ip addr show\n1: lo:  mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000\n    link\/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00\n    inet 127.0.0.1\/8 scope host lo\n       valid_lft forever preferred_lft forever\n    inet6 ::1\/128 scope host \n       valid_lft forever preferred_lft forever\n2: enp0s3:  mtu 1500 qdisc fq_codel state UP group default qlen 1000\n    link\/ether 08:00:27:64:ab:7a brd ff:ff:ff:ff:ff:ff\n    inet 192.168.100.8\/24 brd 192.168.100.255 scope global enp0s3\n       valid_lft forever preferred_lft forever\n    inet 192.168.100.20\/24 scope global secondary enp0s3:1\n       valid_lft forever preferred_lft forever\n    inet6 fe80::a00:27ff:fe64:ab7a\/64 scope link \n       valid_lft forever preferred_lft forever<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">3. How to Enable Network Interface<\/h4>\n\n\n\n<p>The \u201cup\u201d flag with interface name (enp0s3) enables a network interface. For example, the following command will activates the enp0s3 network interface.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ ip link enp0s3 up\nOr\n$ sudo ip link set enp0s3 up<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">4. How to Disable Network Interface<\/h4>\n\n\n\n<p>The \u201cdown\u201d flag with interface name&nbsp; disables a network interface. For example, the following command will De-activates the enp0s3 network interface.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ ip link set enp0s3 down\nor\n$ sudo ip link set enp0s3 down<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">5.How to Assign a IP Address to Specific Interface<\/h4>\n\n\n\n<p>To assign IP address to interface, we will use:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ sudo ip addr add 192.168.100.4\/255.255.255.0 dev enp0s3<\/pre>\n\n\n\n<p>We can also set broadcast address to interface with \u2018ip\u2019 command. By default no broadcast address is set, so to set a broadcast address command is:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ $ sudo  ip addr add broadcast 192.168.100.255 dev enp0s3<\/pre>\n\n\n\n<p>We can also set standard broadcast address along with IP address by using the following command,<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ sudo ip addr add 192.168.100.4\/24 brd + dev enp0s3<\/pre>\n\n\n\n<p>As shown in the above example, we can also use \u2018brd\u2019 in place on \u2018broadcast\u2019 to set broadcast ip address.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">6. How to Remove an IP Address<\/h4>\n\n\n\n<p>The following command will remove an assigned IP address from the given interface:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ sudo ip addr del 192.168.100.4\/24 dev enp0s3<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">7. Adding an Alias for an interface<\/h4>\n\n\n\n<p>To add an alias i.e. assign more than one IP to an interface, execute below command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ sudo ip addr add 192.168.100.20\/24 dev enp0s3 label enp0s3:1<\/pre>\n\n\n\n<p>Example outputs:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ ip addr show enp0s3\n2: enp0s3:  mtu 1500 qdisc fq_codel state UP group default qlen 1000\n    link\/ether 08:00:27:64:ab:7a brd ff:ff:ff:ff:ff:ff\n    inet 192.168.100.8\/24 brd 192.168.100.255 scope global enp0s3\n       valid_lft forever preferred_lft forever\n    inet 192.168.100.20\/24 scope global secondary enp0s3:1\n       valid_lft forever preferred_lft forever\n    inet6 fe80::a00:27ff:fe64:ab7a\/64 scope link \n       valid_lft forever preferred_lft forever<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">8. Check routing table<\/h4>\n\n\n\n<p>Checking routing information shows us the route a packet will take to reach the destination. To check the network routing information, execute the following command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ ip route show<\/pre>\n\n\n\n<p>Example output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ ip route show\ndefault via 192.168.100.1 dev enp0s3 proto static \n192.168.100.0\/24 dev enp0s3 proto kernel scope link src 192.168.100.8<\/pre>\n\n\n\n<p>Suppose now that you have an IP address which you need to know the route packets will take. You can use route option as follows:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ ip route get 192.168.100.4<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">9. Adding a static route<\/h4>\n\n\n\n<p>To change the default route, the ip command can be used as follows:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ sudo ip route add default via 192.168.100.150\/24<\/pre>\n\n\n\n<p>So now all network packets will travel via 192.168.100.150 as opposed to old default route. For changing the default route for a single interface &amp; to make change route further, execute<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ sudo ip route add 10.0.0.2 via 192.168.100.150\/24 dev enp0s3<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">10. Removing a static route<\/h4>\n\n\n\n<p>To remove assigned static route, simply type the following command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ sudo ip route del 10.0.0.2<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">11. How do i add default gateway<\/h4>\n\n\n\n<p>Default gateway can be specified globally or for in interface-specific config file. Advantage of default gateway is If we have more than one NIC is present in the system. You can add default gateway on the fly as shown below command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">sudo ip route add default via 192.168.100.1<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">12. Show network statistics<\/h4>\n\n\n\n<p>The ip command can also be used to show the statistics of the various network interfaces. To do this you can use the ip command with the option -s and then specify the network device.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ ip -s link<\/pre>\n\n\n\n<figure class=\"wp-block-image\" id=\"attachment_5236\"><img decoding=\"async\" src=\"blob:http:\/\/triosdevelopers.com\/fdaac2a5-f4d3-44dc-b269-556797776cf7\" alt=\"ip command - show network statistics\" class=\"wp-image-5236\"\/><figcaption>ip command \u2013 show network statistics<\/figcaption><\/figure>\n\n\n\n<p>When you need to get information about a particular network interface, add the option ls followed by the name of the network interface. The option -s when used more than once gives you more information about that particular interface. This can be very useful especially when trouble shooting errors in network connectivity.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ip -s -s link ls enp0s3<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">13.Checking ARP entries<\/h4>\n\n\n\n<p>Address Resolution Protocol (ARP) is used to translate an IP address to its corresponding physical address, commonly known as MAC address. With ip command you can view the MAC address of the devices connected in your LAN by using the option neigh or neighbour.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ ip neigh<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">14. Monitor netlink messages<\/h4>\n\n\n\n<p>It is also possible to view netlink messages with ip command. The monitor option allows you to see the state of your network devices. For instance a computer on your LAN could be categorized as REACHABLE or STALE depending on its status. The command can be used as follows:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ ip monitor all<\/pre>\n\n\n\n<p>Example output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ ip monitor all\n[NEIGH]192.168.100.4 dev enp0s3 lladdr c8:f7:33:de:30:d7 REACHABLE\n[NEIGH]192.168.100.4 dev enp0s3 lladdr c8:f7:33:de:30:d7 STALE\n[NEIGH]192.168.100.4 dev enp0s3 lladdr c8:f7:33:de:30:d7 PROBE\n[NEIGH]192.168.100.4 dev enp0s3 lladdr c8:f7:33:de:30:d7 REACHABLE\n<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">15. View help<\/h4>\n\n\n\n<p>If you want to find a option which is not listed in above examples, then you can look for manual.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ man ip<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Conclusion<\/h3>\n\n\n\n<p>The command ip is a must have tool for network administrators and all Linux users alike. It is time to move from ifconfig, especially when you are writing scripts.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The linux ip command is similar to ifconfig, but more powerful and is intended to be a replacement for it. With ip you have the advantage of performing several network administration tasks with only one command.In this tutorial, we are going to discuss 15 most common uses for \u2018ip\u2019 command, so let\u2019s get going. 1. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15,13],"tags":[],"class_list":["post-97","post","type-post","status-publish","format-standard","hentry","category-file-management","category-os-maintenance"],"_links":{"self":[{"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/97","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=97"}],"version-history":[{"count":1,"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/97\/revisions"}],"predecessor-version":[{"id":98,"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/97\/revisions\/98"}],"wp:attachment":[{"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=97"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=97"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/triosdevelopers.com\/J.Smith\/rjeffsmith.ca\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=97"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}