linux – F1-CONSULT https://www.f1-consult.com Our Network For Your Network Mon, 07 Jun 2021 21:47:02 +0000 en-US hourly 1 https://wordpress.org/?v=7.1.3 https://www.f1-consult.com/wp-content/uploads/2018/01/cropped-F1-CONSULT-favicon-32x32.png linux – F1-CONSULT https://www.f1-consult.com 32 32 LINUX – nmcli command examples (cheatsheet), compare nm-settings with if-cfg file https://www.f1-consult.com/linux/lan/nmcli/ Mon, 07 Jun 2021 21:47:01 +0000 https://www.f1-consult.com/?p=4752

Table of Contents

nmcli examples. nmcli cheatsheet in CentOS/RHEL 7/8. nmcli command exmaples. nmcli con reload. nmcli help. add ethernet connection using nmcli. nmclic command examples cheatsheet in linux. add bond connection using nmcli in linux. add and configure network teaming using nmcli. nmcli command cheatsheet. configure networkmanager using nmcli. nmcli examples. nmclic command examples cheatsheet. exit an existing connection with nmcli. nmcli command examples. nmcli enable dhcp. man nmcli examples. nmcli rename connection. nmcli remove static ipv4 address. nmcle cheat sheet. nmcli remove dns

Understanding nmcli

  • nmcli is a command-line tool for controlling NetworkManager and reporting network status.
  • It can be utilised as a replacement for nm-applet or other graphical clients. nmcli is used to create, display, edit, delete, activate, and deactivate network connections, as well as control and display network device status.
  • Connections are stored in configuration files
  • The NetworkManager service must be running to manage these files

Compare nm-settings with ifcfg-* directives (IPv4)

nmcli con mod ifcfg-* file Effect
ipv4.method manual BOOTPROTO=none IPv4 address configured statically
ipv4.method auto BOOTPROTO=dhcp Will look for configuration settings from a DHCPv4 server
ipv4.address "192.168.0.10/24" IPADDR=192.168.0.10
PREFIX=24
Set static IPv4 address, network prefix
ipv4.gateway 192.168.0.1 GATEWAY=192.168.0.1 Set IPv4 Gateway
ipv4.dns 8.8.8.8 DNS1=8.8.8.8 Modify /etc/resolv.conf to use this nameserver
ipv4.dns-search example.com DOMAIN=example.com Modify /etc/resolv.conf to use this domain in the search directive
ipv4.ignore-auto-dns true PEERDNS=no Ignore DNS Server information from the DHCP Server
connection.autoconnect yes ONBOOT=yes Automatically activate this connection on boot
connection.id eth0 NAME=eth0 The name of this connection
connection.interface-name eth0 DEVICE=eth0 The connection is bound to the network interface with this name
802-3-ethernet.mac-address 08:00:27:4b:7a:80 HWADDR=08:00:27:4b:7a:80 The connection is bound to the network interface with this MAC Address
ipv4.never-default no DEFROUTE=yes Never use provided interface’s gateway as default gateway

Compare nm-settings with ifcfg-* directives (IPv6)

nmcli con mod ifcfg-* file Effect
ipv6.method manual IPV6_AUTOCONF=no IPv6 is configured statically
ipv6.method auto IPV6_AUTOCONF=yes Will configure network settings using SLAAC from router advertisements.
ipv6.method dhcp IPV6_AUTOCONF=no
DHCPV6C=yes
Will configure network settings by using DHCPv6, but not SLAAC
ipv6 . addresses
"2001:db8::a/64 2001:db8::1"
IPV6ADDR=2001:db8::a/64
IPV6_DEFAULTGW=2001:db8::1
Sets static IPv6 Address and Gateway
ipv6.dns . . . DNS0=. . . Modify /etc/resolv.conf to use this nameserver
ipv6.dns-search example.com DOMAIN=example.com Modify /etc/resolv.conf to use to use this domain in the search directive
ipv6.ignore-auto-dns true IPV6_PEERDNS=no Ignore DNS server information from the DHCP server
connection.autoconnect yes ONBOOT=YES Automatically activates the connection at boot
connection.id eth0 NAME=eth0 The name of this connection
connection.interface-name eth0 DEVICE=eth0 The connection is bound to this network interface with this name
802-3-ethernet.mac-address . . . HWADDR=. . . The connection is bound to the network interface with this MAC Address

Brief list of nmcli commands syntax

Command Purpose
nmcli dev status Show the Network Manager status of all network interfaces
nmcli con show List all connections
nmcli con show name List the current settings for the connection name
nmcli con add con-name name .. Add a new connection named name
nmcli con mod name .. Modify the connection name
nmcli con reload Tell networkManager to reread the configuration files (useful after they have been edited by hand)
nmcli con up name Activate the connection name
nmcli dev dis dev Deactivate and disconnect the current connection on the network interface dev
nmcli con del name Delete the connection name and its configuration file

nmcli command examples (cheatsheet)

Below are some of the chosen nmcli command examples

1. Check if NetworkManager is running

You can use below command to check if NetworkManager is running or not

# nmcli -t -f RUNNING general
running

To get a general status

# nmcli general
STATE      CONNECTIVITY  WIFI-HW  WIFI     WWAN-HW  WWAN
connected  full          enabled  enabled  enabled  enabled

2. List all the available device

To view and list all the available devices on your Linux system

# nmcli dev status
DEVICE      TYPE      STATE         CONNECTION
eth0        ethernet  connected     eth0
virbr0      bridge    disconnected  --
eth1        ethernet  disconnected  --
eth2        ethernet  disconnected  --
lo          loopback  unmanaged     --
virbr0-nic  tun       unmanaged     --

3. List all the available connections

To list all the available connections

# nmcli con show
NAME  UUID                                  TYPE      DEVICE
eth1  01fa0bf4-b6bd-484f-a9a3-2b10ff701dcd  ethernet  eth1
eth0  2e9f0cdd-ea2f-4b63-b146-3b9a897c9e45  ethernet  eth0
eth2  186053d4-9369-4a4e-87b8-d1f9a419f985  ethernet  eth2

4. List all the configuration of interface

To view all the configured values (default and custom) of an interface

# nmcli con show eth2
connection.id:                          eth2
connection.uuid:                        186053d4-9369-4a4e-87b8-d1f9a419f985
connection.stable-id:                   --
connection.type:                        802-3-ethernet
connection.interface-name:              eth2
connection.autoconnect:                 yes

<Output trimmed>

Advertisement

5. Check physical network device status

Now the status of all the connection network devices

# nmcli dev status
DEVICE  TYPE      STATE         CONNECTION
eth1    ethernet  connected     eth1
eth0    ethernet  connected     eth0
eth2    ethernet  disconnected  --
lo      loopback  unmanaged     --

6. Change hostname using nmcli

You can ideally change hostname using hostnamectl command, but you can also update hostname using nmcli

To get the current hostname

# nmcli general hostname
centos-8.example.com

Next to update the hostname

# nmcli general hostname  centos-8.golinuxcloud.com

Verify the same

# nmcli general hostname
centos-8.golinuxcloud.com

# hostname
centos-8.golinuxcloud.com

7. Create a new ethernet connection and assign static IP Address

In this example nmcli configures the eth2 interface statically, using the IPv4 address and network prefix 10.10.10.4/24 and default gateway 10.10.10.1, but still auto connects at
startup and saves its configuration into /etc/sysconfig/network-scripts/ifcfg-eth2 file.

# nmcli con add con-name eth2 type ethernet ifname eth2 ipv4.method manual ipv4.address 10.10.10.4/24 ipv4.gateway 10.10.10.1
Connection 'eth2' (460b16aa-e755-403e-b0ec-5e1560dcc441) successfully added.

8. Create a new ethernet connection and assign DHCP IP Address

The following command will add a new connection for the interface eth2, which will get IPv4 networking information using DHCP and will autoconnect on startup. The configuration will be
saved in /etc/sysconfig/network-scripts/ifcfg-eth2 because the con-name is eth2

# nmcli con add con-name eth2 type ethernet ifname eth2 ipv4.method auto
Connection 'eth2' (d75cb87f-cd15-40a2-9c33-138e69a06a1f) successfully added.

We can verify the same in the mapped interface configuration file

# egrep BOOTPROTO /etc/sysconfig/network-scripts/ifcfg-eth2
BOOTPROTO=dhcp

Advertisement

9. Create and configure bond connection (active-backup) with two slave interface

You can create bond connection with multiple slave interface using nmcli.

Delete any configuration file which exists for slave interface

# nmcli con del "eth1"
# nmcli con del "Wired connection 1"

Add bond interface using nmcli. This command adds a master bond connection, naming the bonding interface mybond0 and using active-backup mode. I have given some dummy values for MII, UPDELAY and DOWNDELAY. If you wish to add primary interface using “primary=<ifname>”

# nmcli con add type bond ifname mybond0 bond.options "mode=active-backup,downdelay=5,miimon=100,updelay=10"
Connection 'bond-mybond0' (a5c76dbe-550b-4abf-8dc0-88184ade369e) successfully added.

Similarly for round-robin bonding you can use bond.options as “downdelay=5,miimon=100,mode=balance-rr,updelay=10”

Next add the slaves for mybond0 using nmcli. This command binds first slave to eth1 interface

# nmcli con add type ethernet ifname eth1 master mybond0
Connection 'bond-slave-eth1' (54dc4282-b90b-4469-9cbf-82bce042de85) successfully added.

This command binds slave 2 to eth2 interface

# nmcli con add type ethernet ifname eth2 master mybond0
Connection 'bond-slave-eth2' (41a5b4a6-8e6b-4df9-bff2-b67c5328311a) successfully added.

List the active connections. So we have our bond and slave interface with us.

Advertisement

# nmcli con show
NAME             UUID                                  TYPE      DEVICE
bond-mybond0     25ce17b2-fffb-4bf1-a5a3-e7593299f303  bond      mybond0
bond-slave-eth1  54dc4282-b90b-4469-9cbf-82bce042de85  ethernet  eth1
bond-slave-eth2  41a5b4a6-8e6b-4df9-bff2-b67c5328311a  ethernet  eth2
eth0             d05aee6a-a069-4e55-9fe4-771ca3336db6  ethernet  eth0

Here I am setting static IP Address, NetMask, Gateway, DNS and DNS Search to mybond0 using nmcli

# nmcli con mod bond-mybond0 ipv4.method manual ipv4.address 10.10.10.8/24 ipv4.gateway 10.10.10.1 ipv4.dns 8.8.8.8 ipv4.dns-search example.com

NOTE:

To use DHCP IP, use ipv4.method auto and do not provide any IP Address related details in the above command

Verify your mybond0 configuration file

# egrep 'BOOTPROTO|IPADDR|PREFIX|GATEWAY|DNS' /etc/sysconfig/network-scripts/ifcfg-bond-mybond0
BOOTPROTO=none
IPADDR=10.10.10.8
PREFIX=24
GATEWAY=10.10.10.1
DNS1=8.8.8.8

refresh/reload the network configuration change for mybond0

# nmcli con up bond-mybond0
Connection successfully activated (master waiting for slaves) (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/11)

Verify the bond IP Address

# ip addr show mybond0
7: mybond0: &lt;BROADCAST,MULTICAST,MASTER,UP,LOWER_UP&gt; mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 08:00:27:0d:ca:0c brd ff:ff:ff:ff:ff:ff
    inet 10.10.10.8/24 brd 10.10.10.255 scope global noprefixroute mybond0
       valid_lft forever preferred_lft forever

Verify the list of available connection

# nmcli con show --active
NAME  UUID                                  TYPE      DEVICE
eth1  01fa0bf4-b6bd-484f-a9a3-2b10ff701dcd  ethernet  eth1
eth0  2e9f0cdd-ea2f-4b63-b146-3b9a897c9e45  ethernet  eth0

10. Create and configure Network Bridge

I have written another article with detailed steps to create and configure network bridge using nmcli and nmtui separately on RHEL/CentOS 7 and 8 Linux.

11. Create and configure Network Teaming

I have written another article with detailed steps to create and configure NIC teaming with two slaves using nmcli validated on RHEL/CentOS 7/8 Linux

12. Reload connection using nmcli (restart)

Reload all connection files from disk. NetworkManager does not monitor changes to connection files by default. So you need to use this command in order to tell NetworkManager to re-read the connection profiles from disk when a change was made to them.

Advertisement

# nmcli con reload

13. Interactively add/edit a connection

You can use nmcli con edit to Edit an existing connection or add a new one, using an interactive editor. In the below example we will edit eth1’s IP Address

# nmcli con edit  eth1

===| nmcli interactive connection editor |===

Editing existing '802-3-ethernet' connection: 'eth1'

Type 'help' or '?' for available commands.
Type 'print' to show all the connection properties.
Type 'describe [.]' for detailed property description.

You may edit the following settings: connection, 802-3-ethernet (ethernet), 802-1x, dcb, sriov, ethtool, match, ipv4, ipv6, tc, proxy
nmcli&gt; help
------------------------------------------------------------------------------
---[ Main menu ]---
goto     [ | ]        :: go to a setting or property
remove   [.] |  :: remove setting or reset property value
set      [. ]  :: set property value
describe [.]          :: describe property
print    [all | [.]]  :: print the connection
verify   [all | fix]                 :: verify the connection
save     [persistent|temporary]      :: save the connection
activate [] [/|]    :: activate the connection
back                                 :: go one level up (back)
help/?   [<command></command>]                 :: print this help
nmcli            :: nmcli configuration
quit                                 :: exit nmcli
------------------------------------------------------------------------------
nmcli&gt; print ipv4.address
ipv4.addresses: 10.10.10.4/24
nmcli&gt; remove ipv4.address "10.10.10.4/24"
nmcli&gt; print ipv4.address
ipv4.addresses:
nmcli&gt; set ipv4.address 10.10.10.5/24
Do you also want to set 'ipv4.method' to 'manual'? [yes]: yes
nmcli&gt; print ipv4.address
ipv4.addresses: 10.10.10.5/24
nmcli&gt; verify
Verify connection: OK
nmcli&gt; save
Connection 'eth1' (7e3a1246-1743-4bb8-9eab-09664ab996b8) successfully updated.
nmcli&gt; quit

Now verify your changes in eth1’s configuration file

# egrep IPADDR /etc/sysconfig/network-scripts/ifcfg-eth1
IPADDR=10.10.10.5

14. Change ethernet connection BOOTPROTO from DHCP to Static

Now to change ethernet connection BOOTPROTO from DHCP to static using nmcli, we must modify ipv4.method directive to use manual

# nmcli con mod eth2 ipv4.method manual ipv4.address 10.10.10.4/24 ipv4.gateway 10.10.10.1

Now verify the network configuration file for eth2

# egrep 'BOOTPROTO|IPADDR|PREFIX|GATEWAY' /etc/sysconfig/network-scripts/ifcfg-eth2
BOOTPROTO=none
IPADDR=10.10.10.4
PREFIX=24
GATEWAY=10.10.10.1

15. Change ethernet connection BOOTPROTO from Static to DHCP

Similarly to change ethernet connection BOOTPROTO from static to DHCP using nmcli, we must modify ipv4.method directive to use auto

# nmcli con mod eth2 ipv4.method auto

Now verify the eth2 network configuration file

# egrep 'BOOTPROTO|IPADDR|PREFIX|GATEWAY' /etc/sysconfig/network-scripts/ifcfg-eth2
BOOTPROTO=dhcp
IPADDR=10.10.10.4
PREFIX=24
GATEWAY=10.10.10.1

As you see we still have IPADDR and other variables from previous command but they are considered null, because you can see my DHCP has assigned 10.10.10.5 to eth2

# ip addr show dev eth2
4: eth2: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:a8:19:0a brd ff:ff:ff:ff:ff:ff
    inet 10.10.10.5/24 brd 10.10.10.255 scope global noprefixroute dynamic eth2
       valid_lft 1068sec preferred_lft 1068sec
    inet6 fe80::b81f:a58b:43f1:b8d3/64 scope link noprefixroute
       valid_lft forever preferred_lft forever

Advertisement

16. Change ONBOOT directive using nmcli

By default ONBOOT is yes in the interface configuration file. So to disable ONBOOT we must modify connection.autoconnect directive using nmcli

Verify the ONBOOT value before changing this directive

# egrep 'ONBOOT' /etc/sysconfig/network-scripts/ifcfg-eth2
ONBOOT=yes

Change ONBOOT directive, disable ONBOOT using nmcli

# nmcli con mod eth2 connection.autoconnect no

Re-verify the ONBOOT directive for eth2

# egrep 'ONBOOT' /etc/sysconfig/network-scripts/ifcfg-eth2
ONBOOT=no

17. Change DEFROUTE directive (Never use this network for default route)

By default any gateway we add for any ethernet connection will also be considered as default gateway, to turn off this directive use ipv4.never-default with nmcli

Before we make any change verify DEFROUTE directive in the eth2 configuration file

# egrep '^DEFROUTE' /etc/sysconfig/network-scripts/ifcfg-eth2
DEFROUTE=yes

So by default this directive is ON, we will disable the default gateway option for eth2. To turn off this directive we must select ipv4.never-default as “yes”

# nmcli con mod eth2 ipv4.never-default yes

Next verify the DEFROUTE directive for eth2

# egrep '^DEFROUTE' /etc/sysconfig/network-scripts/ifcfg-eth2
DEFROUTE=no

Advertisement

18. Disable IPv6 Address for ethernet connection (IPV6INIT)

By default both IPv4 and IPv6 connection type (IPV6INIT) is enabled for any ethernet connection type. To only use IPv4 and disable IPv6 using nmcli

Verify the existing status of IPv6 connection type for eth2

# egrep  'IPV6INIT' /etc/sysconfig/network-scripts/ifcfg-eth2
IPV6INIT=yes

So this is enabled, we will disable IPv6 connection type using ipv6.method directive with nmcli

# nmcli con mod eth2 ipv6.method ignore

Now re-verify the IPV6INIT directive from eth2 config file

# egrep  'IPV6INIT' /etc/sysconfig/network-scripts/ifcfg-eth2
IPV6INIT=no

19. Change “Automatically Connect” Directive

By default any ethernet connection will be allowed to automatically connect, you can modify this using

# nmcli con mod eth2 connection.autoconnect no

20. Add or Modify DNS to existing connection

You can use ipv4.dns to add DNS server to an new connection or modify any existing connection using nmcli.
Currently there are no DNS server IP provided for eth1

# egrep DNS /etc/sysconfig/network-scripts/ifcfg-eth1

Next modify connection to add DNS Server IP Address

# nmcli con mod eth1 ipv4.dns 8.8.8.8

Verify the eth1 config file

[root@rhel-8 ~]# egrep DNS /etc/sysconfig/network-scripts/ifcfg-eth1
DNS1=8.8.8.8

21. Append single/multiple DNS Server to connection

Use + prefix with ipv4.dns to append new DNS IP Addresses to an existing connection using nmcli. In the previous example we added 8.8.8.8 as my DNS server for eth1. Now we will append 8.2.2.2 to the same connection

# nmcli con mod eth1 +ipv4.dns 8.2.2.2

Verify the eth1 configuration file

# egrep DNS /etc/sysconfig/network-scripts/ifcfg-eth1
DNS1=8.8.8.8
DNS2=8.2.2.2

22. Remove single/multiple DNS Server from connection

As it is understood, with + we append so with – we remove single/multiple entries of DNS Server from the interface connection using nmcli.

# nmcli con mod eth1 -ipv4.dns 8.2.2.2,8.8.8.8

Verify the eth1 configuration file

# egrep DNS /etc/sysconfig/network-scripts/ifcfg-eth1

23. Display selected fields with values of connection

You can list all the configured values of a connection using “nmcli con show <ifname>” but that gives you a long list of details, you can actually also get selected value of the provided directive of an individual connection

To get the IPv4 Address of eth1

# nmcli -g ip4.address connection show eth1
10.10.10.4/24

You can use -g to print values from specific fields using nmcli

# nmcli -g ip4.address,ipv4.dns connection show eth1
8.8.8.8,8.2.2.2
10.10.10.4/24

But here as you see we do not get a field to value mapping. You can use -f to specify what fields (column names) should be printed using nmcli. Valid field names differ for specific commands. List available fields by providing an invalid value to the --fields option.

# nmcli -f ipv4.dns,ipv4.addresses,ipv4.gateway con show eth1
ipv4.dns:                               8.8.8.8,8.2.2.2
ipv4.addresses:                         10.10.10.4/24
ipv4.gateway:                           10.10.10.1

24. Monitor connection and device activity

Using nmcli monitor you can observe NetworkManager activity. Watches for changes in connectivity state, devices or connection profiles. Here in this example we will execute nmcli monitor for eth1 in one terminal, and on the other terminal we will make some modification to eth1 connection

# nmcli con mod eth1 ipv4.method manual ipv4.address 10.10.10.4/24

As you see, after the modification, the monitor command gives below output

# nmcli con monitor eth1
eth1: connection profile changed

25. Activate a connection

Just opposite to what we used above, we will use nmcli con up

# nmcli con up eth2
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/23)

Verify the list of available connection

# nmcli con show --active
NAME  UUID                                  TYPE      DEVICE
eth1  01fa0bf4-b6bd-484f-a9a3-2b10ff701dcd  ethernet  eth1
eth0  2e9f0cdd-ea2f-4b63-b146-3b9a897c9e45  ethernet  eth0
eth2  186053d4-9369-4a4e-87b8-d1f9a419f985  ethernet  eth2

26. De-activate a connection

Deactivate a connection from a device without preventing the device from further auto-activation using nmcli con down <ifname>. Multiple connections can be passed to the command.

# nmcli con down eth1
Connection 'eth1' successfully deactivated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/32)

Verify the list of active connections

# nmcli con show --active
NAME  UUID                                  TYPE      DEVICE
eth0  d05aee6a-a069-4e55-9fe4-771ca3336db6  ethernet  eth0

NOTE:

If you are connected to your server using this interface then your connection would close once the connection is de-activated

27. Delete connection

lastly in nmcli command examples, you can delete all type of available connection using “nmcli con del <ifname>”

# nmcli con del bond-mybond0 bond-slave-eth1 bond-slave-eth2
Connection 'bond-mybond0' (25ce17b2-fffb-4bf1-a5a3-e7593299f303) successfully deleted.
Connection 'bond-slave-eth1' (54dc4282-b90b-4469-9cbf-82bce042de85) successfully deleted.
Connection 'bond-slave-eth2' (41a5b4a6-8e6b-4df9-bff2-b67c5328311a) successfully deleted.

Lastly I hope the steps from the article with nmcli command examples (cheatsheet) on Linux was helpful. So, let me know your suggestions and feedback using the comment section.

References:

golinuxcloud.com
man page nmcli
man page nmcli-examples

]]>
CentOS – Installer just goes black screen on Centos8 https://www.f1-consult.com/linux/centos-blackscreen/ Thu, 25 Jun 2020 20:38:28 +0000 https://www.f1-consult.com/?p=4609 Problem:

During installation of CentOS the installer screen goes black.

  • The terminal multiplexer is running in virtual console 1.
  • To switch from the graphical installation environment to tmux, press Ctrl+Alt+F1.
  • To go back to the main installation interface which runs in virtual console 6, press Ctrl+Alt+F6.

Available tmux Windows

Shortcut Contents
Ctrl+b 1 Main installation program window. Contains text-based prompts (during text mode installation or if you use VNC Direct Mode), and also some debugging information.
Ctrl+b 2 Interactive shell prompt with root privileges.
Ctrl+b 3 Installation log; displays messages stored in /tmp/anaconda.log.
Ctrl+b 4 Storage log; displays messages related storage devices from kernel and system services, stored in /tmp/storage.log.
Ctrl+b 5 Program log; displays messages from other system utilities, stored in /tmp/program.log.

Switching to Terminal Ctrl+Alt+F1 you see

  • X startup failed, falling back to text mode
  • Pane is dead (signal 10)

Solution:

  • install system with basic video driver
  • install in text mode by adding "inst.text" to the end of the GRUB boot line
  • Press ENTER to continue with the normal boot.
  • Make selection from the above menu. Perform configuration and return back to the main menu by pressing the c key. Once ready begin the RHEL 8 installation by pressing the b key.
]]>
How to force fsck at every boot in Linux https://www.f1-consult.com/linux/fsck/ Sat, 05 Jan 2019 13:56:35 +0000 https://www.f1-consult.com/?p=4146
  • In /etc/init.d/checkfs.sh is the line if [ -f /forcefsck ] || grep -s -w -i "forcefsck" /proc/cmdline, so providing forcefsck on the kernel command line or generating a /forcefsck file on shutdown should cause an fsck on the next reboot.

    To prevent manual fsck runs, ask fsck to try to automatically fix errors with the -y option by uncommenting and changing no to yes in the following /etc/default/rcS entry, after the edit it should look like:

  • # automatically repair filesystems with inconsistencies during boot
    FSCKFIX=yes</code><code>

    1. One option (forcefsck or FSCKFIX) does not imply the other.
    1. according to manpages -c argument for tune2fs counts number of instances of mounts for a partition. Hence, 1 forces to check the fs after every mounting instance. (http://man7.org/linux/man-pages/man8/tune2fs.8.html)

    # sudo tune2fs -c 1 /dev/sdX
    sudo tune2fs -c 1 /dev/sda1
    sudo tune2fs -c 1 /dev/sda2
    sudo tune2fs -c 1 /dev/sda3
    sudo tune2fs -c 1 /dev/sda4
    sudo tune2fs -c 1 /dev/sda5
    sudo tune2fs -c 1 /dev/sda6
    sudo tune2fs -c 1 /dev/sda7
    sudo tune2fs -c 1 /dev/sda8
    sudo tune2fs -c 1 /dev/sdb1

    Read more

    ]]>
    LINUX – Network Throughput Tuning https://www.f1-consult.com/linux/lan/throughput-tuning/ Fri, 14 Sep 2018 13:13:39 +0000 https://www.f1-consult.com/?p=3977 General Approach

    To check what setting your system is using, use ‘sysctl name’ (e.g.: ‘sysctl net.ipv4.tcp_rmem’). To change a setting use ‘sysctl -w’. To make the setting permanent add the setting to the file ‘sysctl.conf’.

    cho > /etc/sysctl.conf
    echo -e "\
    # /etc/sysctl.conf \r
    # ---------------- BEGIN ---------------- \r
    # to load new settings \r
    # sysctl --load /etc/sysctl.conf \r
    # \r
    ### TCP tuning \r
    # \r
    # For hosts with 10G NIC optimized for network paths up to 200ms RTT, or a 40G NIC up on paths up to 50ms RTT \r
    # allow testing with 512MB buffers \r
    net.core.rmem_max = 536870912 \r
    net.core.wmem_max = 536870912 \r
    # \r
    # increase Linux autotuning TCP buffer limits \r
    # min, default, and max number of bytes to use  \r
    # allow auto-tuning up to 128MB buffers \r
    net.ipv4.tcp_rmem = 4096 87380 268435456 \
    net.ipv4.tcp_wmem = 4096 65536 268435456 \r
    # \r
    # recommended to increase this for CentOS6 with 10G NICS or higher \r
    net.core.netdev_max_backlog = 250000 \r
    # \r
    # don't cache ssthresh from previous connection \r
    net.ipv4.tcp_no_metrics_save = 1 \r
    # \r
    # recommended default congestion control is htcp \r 
    # test with 'sysctl net.ipv4.tcp_available_congestion_control'
    net.ipv4.tcp_congestion_control = reno \r
    # \r
    # recommended for hosts with jumbo frames enabled \r
    net.ipv4.tcp_mtu_probing = 1 \r
    # \r
    # recommended for CentOS7/Debian8 hosts \r
    net.core.default_qdisc = fq \r
    # \r
    fs.inotify.max_user_watches = 100000 \r
    #\r
    # --------------- END ------------------" \
    > /etc/sysctl.conf
    cat /etc/sysctl.conf
    sysctl --load /etc/sysctl.conf
    sysctl -p

    More Reading:

    fasterdata.es.net

    ]]>
    CentOS – How do I safely delete old kernel versions ? https://www.f1-consult.com/linux/kernel/ Thu, 13 Sep 2018 23:24:37 +0000 https://www.f1-consult.com/?p=3959 package-cleanup:

    Using package-cleanup command which is a part of yum-utils package we can uninstall any number of old kernels automatically. As an example using --oldkernels --count=2 option with package-cleanup command the command will remove all unused kernel while keeping last three most recent kernel versions installed.

    package-cleanup --oldkernels --count=1

    yum:

    list installed kernels

    run rpm -q kernel

    remove kernels no longer needed; listed above

    yum remove kernel

    automated:

    By default CentOS will keep last 5 kernels installed on your system. This behavior is defined by installonly_limit=5 line within /etc/yum.conf file. Update the /etc/yum.conf configuration file appropriately to keep desired number of old kernels on your system after update. The minimum value to be set is 2.

    Example of /etc/yum.conf configuration file to keep only last two kernel versions:

    [main]
    cachedir=/var/cache/yum/$basearch/$releasever
    keepcache=0
    debuglevel=2
    logfile=/var/log/yum.log
    exactarch=1
    obsoletes=1
    gpgcheck=1
    plugins=1
    installonly_limit=2
    bugtracker_url=http://bugs.centos.org/set_project.php?project_id=23&ref=http://bugs.centos.org/bug_report_page.php?category=yum
    distroverpkg=centos-release

    read more: linuxconfig.org

    ]]>
    CentOS – NIC Teaming with LACP https://www.f1-consult.com/linux/teaming/ Mon, 10 Sep 2018 20:19:16 +0000 https://www.f1-consult.com/?p=3910 A Comparison of Features in Bonding and Team
    Feature Bonding Team
    broadcast Tx policy Yes Yes
    round-robin Tx policy Yes Yes
    active-backup Tx policy Yes Yes
    LACP (802.3ad) support Yes (active only) Yes
    Hash-based Tx policy Yes Yes
    User can set hash function No Yes
    Tx load-balancing support (TLB) Yes Yes
    LACP hash port select Yes Yes
    load-balancing for LACP support No Yes
    Ethtool link monitoring Yes Yes
    ARP link monitoring Yes Yes
    NS/NA (IPv6) link monitoring No Yes
    ports up/down delays Yes Yes
    port priorities and stickiness (“primary” option enhancement) No Yes
    separate per-port link monitoring setup No Yes
    multiple link monitoring setup Limited Yes
    lockless Tx/Rx path No (rwlock) Yes (RCU)
    VLAN support Yes Yes
    user-space runtime control Limited Full
    Logic in user-space No Yes
    Extensibility Hard Easy
    Modular design No Yes
    Performance overhead Low Very Low
    D-Bus interface No Yes
    multiple device stacking Yes Yes
    zero config using LLDP No (in planning)
    NetworkManager support Yes Yes

    Read More: RHEL

    Config Snippets:

    • CentOS 7
    • No NetworkManager

    NEXUS vPC Switch A:

    !
    interface port-channel223
      description LACP: to nas-a
      switchport access vlan 161
      spanning-tree port type edge
      logging event port link-status
      logging event port trunk-status
      bandwidth 80000000
      vpc 223
    !
    interface Ethernet2/3
      description NAS: nas-a UCS-C240M4-A-A
      no lldp transmit
      no lldp receive
      switchport access vlan 161
      spanning-tree port type edge
      logging event port link-status
      logging event port trunk-status
      channel-group 223 mode active
    !

    !
    interface port-channel224
      description LACP: to nas-b
      switchport access vlan 162
      spanning-tree port type edge
      logging event port link-status
      logging event port trunk-status
      bandwidth 80000000
      vpc 224
    !
    interface Ethernet2/4
      description NAS: nas-b UCS-C240M4-B-A
      no lldp transmit
      no lldp receive
      switchport access vlan 162
      spanning-tree port type edge
      logging event port link-status
      logging event port trunk-status
      channel-group 224 mode active
    !

    core-A# sh vpc 223
    vPC status
    ----------------------------------------------------------------------------
    id     Port        Status Consistency Reason                     Active vlans
    ------ ----------- ------ ----------- -------------------------- -----------
    223    Po223       up     success     success                    161         
    
    core-A# sh vpc 224
    vPC status
    ----------------------------------------------------------------------------
    id     Port        Status Consistency Reason                     Active vlans
    ------ ----------- ------ ----------- -------------------------- -----------
    224    Po224       up     success     success                    162

    NEXUS vPC Switch B:

    !
    interface port-channel223
      description LACP: to nas-a
      switchport access vlan 161
      spanning-tree port type edge
      logging event port link-status
      logging event port trunk-status
      bandwidth 80000000
      vpc 223
    !
    interface Ethernet2/3
      description NAS: nas-a UCS-C240M4-A-B
      no lldp transmit
      no lldp receive
      switchport access vlan 161
      spanning-tree port type edge
      logging event port link-status
      logging event port trunk-status
      channel-group 223 mode active
    !

    !
    interface port-channel224
      description LACP: to nas-b
      switchport access vlan 162
      spanning-tree port type edge
      logging event port link-status
      logging event port trunk-status
      bandwidth 80000000
      vpc 224
    !
    interface Ethernet2/4
      description NAS: nas-b UCS-C240M4-B-B
      no lldp transmit
      no lldp receive
      switchport access vlan 162
      spanning-tree port type edge
      logging event port link-status
      logging event port trunk-status
      channel-group 224 mode active
    !

    core-B# sh vpc 223
    vPC status
    ----------------------------------------------------------------------------
    id     Port        Status Consistency Reason                     Active vlans
    ------ ----------- ------ ----------- -------------------------- -----------
    223    Po223       up     success     success                    161         
    
    core-B# sh vpc 224
    vPC status
    ----------------------------------------------------------------------------
    id     Port        Status Consistency Reason                     Active vlans
    ------ ----------- ------ ----------- -------------------------- -----------
    224    Po224       up     success     success                    162

    CentOS 7 Server – Config: nas-a:

    [root@nas-a network-scripts]# cat /etc/sysconfig/network-scripts/ifcfg-enp6s0
    NM_CONTROLLED=no
    DEVICETYPE=TeamPort
    MTU=9000
    ONBOOT=no
    TYPE=Ethernet
    TEAM_MASTER=team0
    DEVICE=enp6s0
    HWADDR=28:AC:9E:E8:60:F0

    [root@nas-a network-scripts]# cat /etc/sysconfig/network-scripts/ifcfg-enp7s0
    NM_CONTROLLED=no
    DEVICETYPE=TeamPort
    MTU=9000
    ONBOOT=no
    TYPE=Ethernet
    TEAM_MASTER=team0
    DEVICE=enp7s0
    HWADDR=28:AC:9E:E8:60:F1

    [root@nas-a network-scripts]# cat /etc/sysconfig/network-scripts/ifcfg-team0
    NM_CONTROLLED=no
    DEVICE=team0
    DEVICETYPE=Team
    MTU=9000
    BOOTPROTO=none
    ONBOOT=yes
    
    # LACP
    TEAM_CONFIG='{"runner": {"name": "lacp", "active": true, "fast_rate": true, "tx_hash": ["eth", "ipv4", "ipv6"]},"link_watch":    {"name": "ethtool"},"ports":    {"enp6s0": {}, "enp7s0": {}}}'
    
    # IPv4
    IPADDR=192.168.161.231
    PREFIX=24
    GATEWAY=192.168.161.254
    DNS1=192.168.167.244
    DNS2=192.168.168.244
    DOMAIN=toocoolforyou.net
    DEFROUTE=yes
    
    # IPv6
    IPV6INIT=yes
    IPV6_PRIVACY=no
    IPV6_AUTOCONF=no
    IPV6_DEFROUTE=yes
    IPV6_FAILURE_FATAL=no
    IPV6ADDR=2001:67c:21b0:4161:192:168:161:231/64
    IPV6_DEFAULTGW=2001:67c:21b0:4161:192:168:161:254
    
    # Firewall Zone
    ZONE=public

    CentOS 7 Server – Config: nas-a:

    [root@nas-b network-scripts]# cat /etc/sysconfig/network-scripts/ifcfg-enp6s0
    NM_CONTROLLED=no
    DEVICETYPE=TeamPort
    MTU=9000
    ONBOOT=no
    TYPE=Ethernet
    TEAM_MASTER=team0
    DEVICE=enp6s0
    HWADDR=84:3D:C6:77:62:2A

    [root@nas-b network-scripts]# cat /etc/sysconfig/network-scripts/ifcfg-enp7s0
    NM_CONTROLLED=no
    DEVICETYPE=TeamPort
    MTU=9000
    ONBOOT=no
    TYPE=Ethernet
    TEAM_MASTER=team0
    DEVICE=enp7s0
    HWADDR=84:3D:C6:77:62:2B

    [root@nas-b network-scripts]# cat /etc/sysconfig/network-scripts/ifcfg-team0
    NM_CONTROLLED=no
    DEVICE=team0
    DEVICETYPE=Team
    MTU=9000
    BOOTPROTO=none
    ONBOOT=yes
    
    # LACP
    TEAM_CONFIG='{"runner": {"name": "lacp", "active": true, "fast_rate": true, "tx_hash": ["eth", "ipv4", "ipv6"]},"link_watch":    {"name": "ethtool"},"ports":    {"enp6s0": {}, "enp7s0": {}}}'
    
    # IPv4
    IPADDR=192.168.162.231
    PREFIX=24
    GATEWAY=192.168.162.254
    DNS1=192.168.167.244
    DNS2=192.168.168.244
    DOMAIN=toocoolforyou.net
    DEFROUTE=yes
    
    # IPv6
    IPV6INIT=yes
    IPV6_PRIVACY=no
    IPV6_AUTOCONF=no
    IPV6_DEFROUTE=yes
    IPV6_FAILURE_FATAL=no
    IPV6ADDR=2001:67c:21b0:8162:192:168:162:231/64
    IPV6_DEFAULTGW=2001:67c:21b0:8162:192:168:162:254
    
    # Firewall Zone
    ZONE=public

    CentOS 7 Server – Show Commands:

    [root@nas-a /]# teamdctl team0 state view -v
    
    setup:
      runner: lacp
      kernel team mode: loadbalance
      D-BUS enabled: yes
      ZeroMQ enabled: no
      debug level: 0
      daemonized: no
      PID: 5029
      PID file: /var/run/teamd/team0.pid
    ports:
      enp6s0
        ifindex: 2
        addr: 28:ac:9e:e8:60:f0
        ethtool link: 40000mbit/fullduplex/up
        link watches:
          link summary: up
          instance[link_watch_0]:
            name: ethtool
            link: up
            down count: 0
            link up delay: 0
            link down delay: 0
        runner:
          aggregator ID: 3, Selected
          selected: yes
          state: current
          key: 0
          priority: 255
          actor LACPDU info:
            system priority: 65535
            system: 28:ac:9e:e8:60:f0
            key: 0
            port_priority: 255
            port: 2
            state: 0x3f
          partner LACPDU info:
            system priority: 32667
            system: 00:23:04:ee:be:a0
            key: 32991
            port_priority: 32768
            port: 16899
            state: 0x3d
      enp7s0
        ifindex: 3
        addr: 28:ac:9e:e8:60:f0
        ethtool link: 40000mbit/fullduplex/up
        link watches:
          link summary: up
          instance[link_watch_0]:
            name: ethtool
            link: up
            down count: 0
            link up delay: 0
            link down delay: 0
        runner:
          aggregator ID: 3, Selected
          selected: yes
          state: current
          key: 0
          priority: 255
          actor LACPDU info:
            system priority: 65535
            system: 28:ac:9e:e8:60:f0
            key: 0
            port_priority: 255
            port: 3
            state: 0x3f
          partner LACPDU info:
            system priority: 32667
            system: 00:23:04:ee:be:a0
            key: 32991
            port_priority: 32768
            port: 515
            state: 0x3d
    runner:
      active: yes
      fast rate: yes
      system priority: 65535

    [root@nas-a /]# teamdctl team0 config dump -v
    {
        "device": "team0",
        "link_watch": {
            "name": "ethtool"
        },
        "ports": {
            "enp6s0": {},
            "enp7s0": {}
        },
        "runner": {
            "active": true,
            "fast_rate": true,
            "name": "lacp",
            "tx_hash": [
                "eth",
                "ipv4",
                "ipv6"
            ]
        }
    }

    [root@nas-a /]# ls -la /sys/class/net/  
    lrwxrwxrwx.  1 root root 0 Sep  8 12:01 enp6s0 -> ../../devices/pci0000:00/0000:00:01.0/0000:01:00.0/0000:02:01.0/0000:04:00.0/0000:05:00.0/0000:06:00.0/net/enp6s0
    lrwxrwxrwx.  1 root root 0 Sep  8 12:01 enp7s0 -> ../../devices/pci0000:00/0000:00:01.0/0000:01:00.0/0000:02:01.0/0000:04:00.0/0000:05:01.0/0000:07:00.0/net/enp7s0
    lrwxrwxrwx.  1 root root 0 Sep  8 12:01 lo -> ../../devices/virtual/net/lo
    lrwxrwxrwx.  1 root root 0 Sep  8 12:01 team0 -> ../../devices/virtual/net/team0

    ]]>
    Linux – iproute2 Cheat Sheet https://www.f1-consult.com/linux/iproute/ Mon, 30 Jul 2018 19:52:08 +0000 https://www.f1-consult.com/?p=3888 Overview

    iproute2 is the Linux networking toolkit that replaced net-tools (ifconfig, route, arp etc.)

    Old style network utilities like ifconfig and route are still there just for backwards compatibility
    and do not provide access to new features like policy-based routing or network namespaces.

    Note that iproute2 has been a standard Linux tool since the early 2000’s. It’s included in
    every distro by default, or at least available from the repos (OpenWRT is one of the cases).

    iproute2 was originally written by Alex Kuznetsov and is now maintained by Stephen Hemminger.

    This document aims to provide comprehensive but easy to use documentation for the ip command
    included in iproute2 package. There are more, such as ss (netstat replacement, fairly straightforward),
    tc (QoS management), but documenting them in this style, especially tc, would be a separate big project.

    Instead of listing commands and describing what they do, it uses a task-centered approach and gives commands
    for tasks that network administrators need to do. It was once called a “cheatsheet”
    for this reason, but has long outgrown the size and scope of a cheat sheet for the most common tasks.

    Contributions are always welcome, you can find the “source code” at
    github.com/dmbaturin/iproute2-cheatsheet.

    git clone https://github.com/dmbaturin/iproute2-cheatsheet.git

    This document is provided “as is”, without any warranty. The authors are not liable for any
    damage related to using it.

    General notes

    All commands that change any settings (that is, not just display them) require root privileges.

    There are configuration files in /etc/iproute2, mainly for assinging symbolic names to network
    stack entities such as routing tables. Those files are re-read every time you run the ip command and you don’t need
    to do anything to apply the changes.

    Typographic conventions

    Metasyntactic variables are written in shell-style syntax, ${something}. Optional command parts are in
    square brackets.

    Table of contents


      Address management


      In this section ${address} value should be a host address in dotted decimal format, and
      ${mask} can be either a dotted decimal subnet mask or a prefix length.
      That is, both 192.0.2.10/24 and 192.0.2.10/255.255.255.0 are equally acceptable.

      If you are not sure if something is a correct host address, use ipcalc or similar
      program to check.

      Show all addresses

      ip address show

      All show commands can be used with -4 or -6 options to show only IPv4 or IPv6 addresses.

      Show addresses for a single interface

      ip address show ${interface name}

      Examples:

      ip address show eth0

      Show addresses only for running interfaces

      ip address show up

      Show only static or dynamic IPv6 addresses

      Show only statically configured addresses:

      ip address show [dev ${interface}] permanent

      Show only addresses learnt via autoconfiguration:

      ip address show [dev ${interface}] dynamic

      Add an address to an interface

      ip address add ${address}/${mask} dev ${interface name}

      Examples:

      ip address add 192.0.2.10/27 dev eth0

      ip address add 2001:db8:1::/48 dev tun10

      You can add as many addresses as you want.

      If you add more than one address, your machine will accept packets for
      all of them. The first address you added will be used as source address for
      outgoing traffic by default, it’s referred to as primary address.

      All additional addresses you set will become secondary addresses.

      Add an address with human-readable description

      ip address add ${address}/${mask} dev ${interface name} label ${interface name}:${description}

      Examples:

      ip address add 192.0.2.1/24 dev eth0 label eth0:WANaddress

      A label must start with the interface name followed by a colon due to some backwards compatibility issues,
      otherwise you’ll get an error.
      Keep the label shorter than sixteen characters, or else you’ll get this error:

      RTNETLINK answers: Numerical result out of range

      Notes

      For IPv6 addresses this command has no effect (address will be added, but without a label).

      Delete an address

      ip address delete ${address}/${prefix} dev ${interface name}

      Examples:

      ip address delete 192.0.2.1/24 dev eth0

      ip address delete 2001:db8::1/64 dev tun1

      Interface name is required. Linux does allow the same address to be configured on multiple interfaces
      and it has valid use cases.

      Remove all addresses from an interface

      ip address flush dev ${interface name}

      Examples:

      ip address flush dev eth1

      By default this command removes both IPv4 and IPv6 addresses. If you want to remove only
      IPv4 or IPv6 addresses, use “ip -4 address flush” or “ip -6 address flush”.

      Notes

      There is no way to swap primary and secondary addresses or explicitly set the new primary address. Try to always set the primary address first.

      However, if the sysctl variable net.ipv4.conf.${interface}.promote_secondaries is set to 1, when you delete the primary address,
      the first secondary address will become the new primary.

      Note that net.ipv4.conf.default.promote_secondaries=1 is not the universal default setting in all Linux distributions,
      so check yours before trying it. If it’s set to 0, then when you delete the primary address, all addresses will be removed
      from the interface.

      Secondary IPv6 addresses are always promoted to primary if the primary address is deleted so you don’t need to worry about sysctl settings.

      Neighbor (ARP and NDP) table management


      For ladies and gentlemen who prefer the British spelling, this command family supports the “neighbour” spelling too.

      View neighbor tables

      ip neighbor show

      All “show” commands support -4 and -6 options to view only IPv4 (ARP) or IPv6 (NDP) neighbors. By default
      all neighbors are displayed.

      View neighbors for single interface

      ip neighbor show dev ${interface name}

      Examples:

      ip neighbor show dev eth0

      Flush table for an interface

      ip neighbor flush dev ${interface name}

      Examples:

      ip neighbor flush dev eth1

      Add a neighbor table entry

      ip neighbor add ${network address} lladdr ${link layer address} dev ${interface name}

      Examples:

      ip neighbor add 192.0.2.1 lladdr 22:ce:e0:99:63:6f dev eth0

      One of the use cases for it is to add static entry for an interface with disabled ARP to
      restrict interface usage only by hosts with specific MAC addresses.

      Delete a neighbor table entry

      ip neighbor delete ${network address} lladdr ${link layer address} dev ${interface name}

      Examples:

      ip neighbor delete 192.0.2.1 lladdr 22:ce:e0:99:63:6f dev eth0

      Allows to delete a static entry, or get rid of an automatically learnt entry without flushing the table.

      Link management


      Link is another term for a for network interface. Commands from the “ip link” family perform operations
      that are common for all interface types, like viewing link information or changing the MTU.

      Historically “ip link” commands could create all types of interfaces, except for tunnels (IPIP, GRE etc.), L2TPv3, and VXLAN
      interfaces that have their own commands. In newer iproute2 versions (since at least 3.16) they can create interfaces of all
      types except L2TPv3, though using special command families for some of them is more convenient.

      Note that interface name you set with “name ${name}” parameter of “ip link add” and “ip link set”
      commands may be arbitrary, and may even contain unicode characters. However, it’s better to stick with
      ASCII because other programs may not handle unicode correctly.

      Also note that other programs, such as iptables, may have their own link name format and length
      restrictions, so it’s better to use short alphanumeric names, and provide additional information in
      link aliases.

      Show information about all links

      ip link show

      ip link list

      These commands are equivalent and can be used with the same arguments.

      Show information about specific link

      ip link show dev ${interface name}

      Examples:

      ip link show dev eth0

      ip link show dev tun10

      The word “dev” may be omitted.

      Bring a link up or down

      ip link set dev ${interface name} up

      ip link set dev ${interface name} down

      Examples:

      ip link set dev eth0 down

      ip link set dev br0 up

      Note: virtual links described below, like VLANs and bridges
      are in down state immediately after creation. You need to bring them up to start using them.

      Set human-readable link description

      ip link set dev ${interface name} alias "${description}"

      Examples:

      ip link set dev eth0 alias "LAN interface"

      Link aliases show up in “ip link show” output, like:

      2: eth0: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc mq state UP mode DEFAULT qlen 1000
          link/ether 22:ce:e0:99:63:6f brd ff:ff:ff:ff:ff:ff
          alias LAN interface

      Rename an interface

      ip link set dev ${old interface name} name ${new interface name}

      Examples:

      ip link set dev eth0 name lan

      Note that you can’t rename an active interface. You need to bring it down
      before doing it.

      Change link layer address (usually MAC address)

      ip link set dev ${interface name} address ${address}

      Link layer address is a pretty broad concept. The most known
      example is MAC address for ethernet devices. To change MAC
      address you would need something like:

      ip link set dev eth0 address 22:ce:e0:99:63:6f

      Change link MTU

      ip link set dev ${interface name} mtu ${MTU value}

      Examples:

      ip link set dev tun0 mtu 1480

      MTU stands for “Maximum Transmission Unit”, the maximum size
      of a frame an interface can transmit at once.

      Apart from reducing fragmentation in tunnels like
      in example above, this is also used to increase performance of
      gigabit ethernet links that support so called “jumbo frames”
      (frames up to 9000 bytes large). If all your equipment
      supports gigabit ethernet, you may want to do something like

      ip link set dev eth0 mtu 9000

      Note that you may need to configure it on your L2 switches too,
      some of them have it disabled by default.

      Delete a link

      ip link delete dev ${interface name}

      Obviously, only virtual links like VLANs or bridges can be deleted.

      Enable or disable multicast on an interface

      ip link set ${interface name} multicast on

      ip link set ${interface name} multicast off

      Unless you really understand what you are doing, better not to touch this.

      Enable or disable ARP on an interface

      ip link set ${interface name} arp on

      ip link set ${interface name} arp off

      One may want to disable ARP to enforce a security policy and allow only specific MACs
      to communicate with the interface. In this case neighbor table entries for whitelisted MACs
      should be created manually (see neighbor table management
      section), or nothing will be able to communicate with that interface.

      In most cases it’s better to configure MAC policy on an access layer switch though.
      Do not change this flag unless you are sure what you are going to do and why.

      Create a VLAN interface

      ip link add name ${VLAN interface name} link ${parent interface name} type vlan id ${tag}

      Examples:

      ip link add name eth0.110 link eth0 type vlan id 110

      The only type of VLAN supported in Linux is IEEE 802.1q VLAN, legacy implementations like ISL
      are not supported.

      Once you create a VLAN interface, all frames tagged with ${tag} you specified in id option
      received by ${parent interface} will be processed by that VLAN interface.

      eth0.100 name format is traditional, but not required, you can name the interface as you want,
      just like with other interface types.

      VLANs can be created over bridge, bonding and other interfaces capable of processing ethernet frames too.

      Create a QinQ interface (VLAN stacking)

      ip link add name ${service interface} link ${physical interface} type vlan proto 802.1ad id ${service tag}

      ip link add name ${client interface} link ${service interface} type vlan proto 802.1q id ${client tag}

      Example:

      ip link add name eth0.100 link eth0 type vlan proto 802.1ad id 100 # Create service tag interface

      ip link add name eth0.100.200 link eth0.100 type vlan proto 802.1q id 200 # Create client tag interface

      VLAN stacking (aka 802.1ad QinQ) is a way to transmit VLAN tagged traffic over another VLAN. The common use case
      for it is like this: suppose you are a service provider and you have a customer who wants to use your network
      infrastructure to connect parts of their network to each other. They use multiple VLANs in their network, so
      an ordinary rented VLAN is not an option. With QinQ you can add a second tag to the customer traffic when it enters your network
      and remove that tag when it exits, so there are no conflicts and you don’t need to waste VLAN numbers.

      The service tag is the VLAN tag the provider uses to carry client traffic through their network. The client tag
      is the tag set by the customer.

      Note that link MTU for the client VLAN interface is not adjusted automatically, you need to take care
      of it yourself and either decrease the client interface MTU by at least 4 bytes, or increase the parent MTU accordingly.

      Standards-compliant QinQ is available since Linux 3.10.

      Create pseudo-ethernet (aka macvlan) interface

      ip link add name ${macvlan interface name} link ${parent interface} type macvlan

      Examples:

      ip link add name peth0 link eth0 type macvlan

      You can think of macvlan interfaces as additional virtual MAC addresses on the parent interface.
      They look like normal ethernet interfaces from user point of view, and handle all traffic for
      MAC address they are assigned with received by their parent interface.

      This is commonly used for testing, or for using several instances of a service identified by
      MAC when only one physical interface is available.

      They also can be used just for IP address separation instead of assigning multiple addresses
      to the same physical interface, especially if some service can’t operate on a secondary address properly.

      Create a dummy interface

      ip link add name ${dummy interface name} type dummy

      Examples:

      ip link add name dummy0 type dummy

      Dummy interfaces work pretty much like loopback interfaces, just there can be as many
      of them as you want.

      The first purpose of them is for communication of programs inside the host.

      The second purpose exploits the fact they are always up (unless administratively taken down).
      This is often used to assign service addresses to them on routers with more than one physical
      interface. As long as the traffic to the address assigned to a loopback or dummy interface
      is routed to the machine that owns it, you can access it through any of its interfaces.

      Create a bridge interface

      ip link add name ${bridge name} type bridge

      Examples:

      ip link add name br0 type bridge

      Bridge interfaces are virtual ethernet switches. They can be used to relay traffic
      transparently between ethernet interfaces, and, increasingly common, as ethernet switches
      for virtual machines running inside hypervisors.

      You can assign an IP address to a bridge and it will be visible from all bridge ports.

      If this command fails, check if “bridge” module is loaded.

      Add an interface to bridge

      ip link set dev ${interface name} master ${bridge name}

      Examples:

      ip link set dev eth0 master br0

      Interface you added to a bridge becomes a virtual switch port. It operates only on datalink
      layer and ceases all network layer operation.

      Remove interface from bridge

      ip link set dev ${interface name} nomaster

      Examples:

      ip link set dev eth0 nomaster

      Create a bonding interface

      ip link add name ${name} type bond

      Examples:

      ip link add name bond1 type bond

      Note: This is not enough to configure bonding (link aggregation)
      in any meaningful way. You need to set up bonding parameters according to your situation.
      This is far beyond the cheat sheet scope, so consult the documentation.

      Interfaces are added to the bond group the same way to bridge group, just note that you can’t
      add it until you take it down.

      Create an intermediate functional block interface

      ip link add ${interface name} type ifb

      Example:

      ip link add ifb10 type ifb

      Intermediate functional block devices are used for traffic redirection and mirroring
      in conjunction with tc. This is also far beyond the scope of this document, consult tc
      documentation.

      Create a pair of virtual ethernet devices

      Virtual ethernet (veth) devices always come in pairs and work as a bidirectional pipe,
      whatever comes into one of them, comes out of another. They are used in conjunction with
      system partitioning features such as network namespaces and containers (OpenVZ and LXC)
      for connecting one partition to another.

      ip link add name ${first device name} type veth peer name ${second device name}

      Examples:

      ip link add name veth-host type veth peer name veth-guest

      Note: virtual ethernet devices are created in UP state, no need to
      bring them up manually after creation.

      Link group management


      Link groups are similar to port ranges found in managed switches. You can add network
      interfaces to a numbered group and perform operations on all the interfaces from that group
      at once.

      Links not assigned to any group belong to group 0 aka “default”.

      Add an interface to a group

      ip link set dev ${interface name} group ${group number}

      Examples:

      ip link set dev eth0 group 42

      ip link set dev eth1 group 42

      Remove an interface from a group

      This can be done by assigning it to the default group.

      ip link set dev ${interface name} group 0

      ip link set dev ${interface} group default

      Examples:

      ip link set dev tun10 group 0

      Assign a symbolic name to a group

      Group names are stored in /etc/iproute2/group file. Symbolic name “default”
      for group 0 comes exactly from there. You can add your own, one per line,
      following the same “${number} ${name}” format. You can have up to 255 named groups.

      Once you configured a group name, number and name can be used interchangeably
      in ip commands.

      Example:

      echo "10    customer-vlans" &gt;&gt; /etc/iproute2/group

      After that you can use that name in all operations, like in

      ip link set dev eth0.100 group customer-vlans

      Perform an operation on a group

      ip link set group ${group number} ${operation and arguments}

      Examples:

      ip link set group 42 down

      ip link set group uplinks mtu 1200

      View information about links from specific group

      Use usual information viewing command with “group ${group}” modifier.

      Examples:

      ip link list group 42

      ip address show group customers

      Tun and Tap devices


      Tun and tap devices allow userspace programs to emulate a network
      device. When the userspace program opens them they get a file
      descriptor. Packets routed by the kernel networking stack to the
      device are read from the file descriptor, data the userspace
      program writes to the file descriptor are injected as local
      outgoing packets into the networking stack. The difference between
      the two is:

      • tap sends and receives raw Ethernet frames.
      • tun sends and receives raw IP packets.

      There are two types of tun/tap devices: persistent and transient.
      Transient tun/tap devices are created by userspace programs when they open a special device, and are destroyed
      automatically when the associated file descriptor is closed.
      The commands listed here manipulate persistent devices.

      View tun/tap devices

      ip tuntap show

      Note: this command can be abbreviated to “ip tuntap”.

      This command is the only way to find out if some device is in tun or tap mode.

      Add an tun/tap device useable by the root user

      ip tuntap add dev ${interface name} mode ${mode}

      Examples:

      ip tuntap add dev tun0 mode tun

      ip tuntap add dev tap9 mode tap

      Add an tun/tap device usable by an ordinary user

      ip tuntap add dev ${interface name} mode ${mode} user ${user} group ${group}

      Example:

      ip tuntap add dev tun1 mode tun user me group mygroup

      ip tuntap add dev tun2 mode tun user 1000 group 1001

      Add an tun/tap device using an alternate packet format

      Add meta information to each packet received over the file
      descriptor. Very few programs expect this information, and
      including it when it isn’t expected will break things.

      ip tuntap add dev ${interface name} mode ${mode} pi

      Example:

      ip tuntap add dev tun1 mode tun pi

      Add an tun/tap ignoring flow control

      Normally packets sent to a tun/tap device travel in the same way
      as packets sent to any other device: they are put on a queue handled by the
      traffic control engine (which is configured by the tc command).
      This can be bypassed, thus disabling the traffic control engine
      for this tun/tap device.

      ip tuntap add dev ${interface name} mode ${mode} one_queue

      Example:

      ip tuntap add dev tun1 mode tun one_queue

      Delete tun/tap device

      ip tuntap del dev ${interface name} mode ${mode}

      Examples:

      ip tuntap delete dev tun0 mode tun

      ip tuntap delete dev tap1 mode tap

      Note: you must specify the mode. The mode is not displayed in “ip link show”, so
      if you don’t know if it’s TUN or TAP, consult the output of “ip tuntap show”.

      Tunnel management


      Tunnels are “network wormholes” that look like normal interfaces,
      but packets sent through them are encapsulated into another protocol
      and sent to the other side of tunnel through multiple hosts, then
      decapsulated and processed in usual way, so you can pretend two
      machines have direct connectivity, while they in fact do not.

      This is often used for virtual private networks (in conjunction with
      encrypted transport protocols like IPsec), or connecting networks
      that use some protocol via an intermediate network that does not use it
      (e.g. IPv6 networks separated by an IPv4-only segment).

      Note: tunnels on their own offer zero security.
      They are as secure as their underlying network. So if you need
      security, use them over an encrypted transport, e.g. IPsec.

      Linux currently supports IPIP (IPv4 in IPv4), SIT (IPv6 in IPv4),
      IP6IP6 (IPv6 in IPv6), IPIP6 (IPv4 in IPv6), GRE (virtually anything
      in anything), and, in very recent versions, VTI (IPv4 in IPsec).

      Note that tunnels are created in DOWN state, you need to bring them up.

      In this section ${local endpoint address} and ${remote endpoint address}
      refer to addresses assigned to physical interfaces of endpoint. ${address}
      refers to the address assigned to tunnel interface.

      Create an IPIP tunnel

      ip tunnel add ${interface name} mode ipip local ${local endpoint address} remote ${remote endpoint address}

      Examples:

      ip tunnel add tun0 mode ipip local 192.0.2.1 remote 198.51.100.3

      ip link set dev tun0 up

      ip address add 10.0.0.1/30 dev tun0

      Create a SIT tunnel

      sudo ip tunnel add ${interface name} mode sit local ${local endpoint address} remote ${remote endpoint address}

      Examples:

      ip tunnel add tun9 mode sit local 192.0.2.1 remote 198.51.100.3

      ip link set dev tun9 up

      ip address add 2001:db8:1::1/64 dev tun9

      This type of tunnels is commonly used to provide an IPv4-connected network with
      IPv6 connectivity. There are so called “tunnel brokers” that provide it to everyone interested,
      e.g. Hurricane Electric tunnelbroker.net.

      Create an IPIP6 tunnel

      ip -6 tunnel add ${interface name} mode ipip6 local ${local endpoint address} remote ${remote endpoint address}

      Examples:

      ip -6 tunnel add tun8 mode ipip6 local 2001:db8:1::1 remote 2001:db8:1::2

      This type of tunnels will be widely used when transit operators phase IPv4 out (i.e. not any soon).

      Create an IP6IP6 tunnel

      ip -6 tunnel add ${interface name} mode ip6ip6 local ${local endpoint address} remote ${remote endpoint address}

      Examples:

      ip -6 tunnel add tun3 mode ip6ip6 local 2001:db8:1::1 remote 2001:db8:1::2

      ip link set dev tun3 up

      ip address add 2001:db8:2:2::1/64 dev tun3

      Just like IPIP6 these ones aren’t going to be generally useful any soon.

      Create a gretap (ethernet over GRE) device

      ip link add ${interface name} type gretap local ${local endpoint address} remote ${remote endpoint address}

      Examples:

      ip link add gretap0 type gretap local 192.0.2.1 remote 203.0.113.3

      This type of tunnels encapsulates ethernet frames into IPv4 packets.

      Recent kernel and iproute2 versions also support gretap over IPv6, you need to replace the mode with “ip6gretap”
      to create an IPv6-based link.

      This probably should have been in “Links management” section, but as it involves encapsulation, it’s here.
      Tunnel interface created this way looks like an L2 link, and it can be added to a bridge group. This is used
      to connect L2 segments via a routed network.

      Create a GRE tunnel

      ip tunnel add ${interface name} mode gre local ${local endpoint address} remote ${remote endpoint address}

      Examples:

      ip tunnel add tun6 mode gre local 192.0.2.1 remote 203.0.113.3

      ip link set dev tun6 up

      ip address add 192.168.0.1/30 dev tun6

      ip address add 2001:db8:1::1/64 dev tun6

      GRE can encapsulate both IPv4 and IPv6 at the same time. However, by default it uses IPv4 for transport,
      for GRE over IPv6 there is a separate tunnel mode, “ip6gre”.

      Create multiple GRE tunnels to the same endpoint

      ip tunnel add ${interface name} mode gre local ${local endpoint address} remote ${remote endpoint address} key ${key value}

      Examples:

      ip tunnel add tun4 mode gre local 192.0.2.1 remote 203.0.113.6 key 123

      ip tunnel add tun5 mode gre local 192.0.2.1 remote 203.0.113.6 key 124

      Keyed tunnels can be used at the same time to unkeyed too. Key may be in dotted decimal IPv4-like format.

      Note that key does not add any security to the tunnel. It’s just an identifier used to distinguish one tunnel from another.

      Create a point-to-multipoint GRE tunnel

      ip tunnel add ${interface name} mode gre local ${local endpoint address} key ${key value}

      Examples:

      ip tunnel add tun8 mode gre local 192.0.2.1 key 1234

      ip link set dev tun8 up

      ip address add 10.0.0.1/27 dev tun8

      Note the absence of ${remote endpoint address}. This is the same to what is called “mode gre multipoint” in Cisco IOS.

      In the absence of remote endpoint address the key is the only way to identify the tunnel traffic, so ${key value} is required.

      This type of tunnels allows you to communicate with multiple endpoints by using the same tunnel interface. It’s commonly used in
      complex VPN setups with multiple endpoints communicating to one another (in Cisco terminology, “dynamic multipoint VPN”).

      As there is no explicit remote endpoint address, obviously it is not enough to just create a tunnel. Your system needs to know
      where the other endpoints are.

      In real life NHRP (Next Hop Resolution Protocol) is used for it. For testing you can add peers manually (given remote endpoint
      uses 203.0.113.6 address on its physical interface and 10.0.0.2 on the tunnel):

      ip neighbor add 10.0.0.2 lladdr 203.0.113.6 dev tun8

      You will have to do it on the remote endpoint too, like:

      ip neighbor add 10.0.0.1 lladdr 192.0.2.1 dev tun8

      Note that link-layer address and neighbor address are both IP addresses, so they are on the same OSI layer.
      This one of the cases where link-layer address concept gets interesting.

      Create a GRE tunnel over IPv6

      Recent kernel and iproute2 versions support GRE over IPv6. Point-to-point with no key:

      ip -6 tunnel add name ${interface name} mode ip6gre local ${local endpoint} remote ${remote endpoint}

      It should support all options and features supported by the IPv4 GRE described above.

      Delete a tunnel

      ip tunnel del ${interface name}

      Examples:

      ip tunnel del gre1

      Note that in older iproute2 versions this command did not support the full “delete” word, only “del”.
      Recent versions allow both full and abbreviated forms (tested in iproute2-ss131122).

      Modify a tunnel

      ip tunnel change ${interface name} ${options}

      Examples:

      ip tunnel change tun0 remote 203.0.113.89

      ip tunnel change tun10 key 23456

      Note: Apparently you can’t add a key to previously unkeyed tunnel.
      Not sure if it’s a bug or a feature. Also, you can’t change tunnel mode on the fly,
      for obvious reasons.

      View tunnel information

      ip tunnel show

      ip tunnel show ${interface name}

      Examples:

      $ip tun show tun99 
      tun99: gre/ip  remote 10.46.1.20  local 10.91.19.110  ttl inherit

      L2TPv3 pseudowire management


      L2TPv3 is a tunneling
      protocol commonly used for L2 pseudowires.

      In many distros L2TPv3 is compiled as a module, and may not be loaded by default.
      If you get a “RTNETLINK answers: No such file or directory” and “Error talking to the kernel” message
      to any “ip l2tp” command, this is likely the case. Load l2tp_netlink and
      l2tp_eth modules. If you want to use L2TPv3 over IP rather than UDP, also load
      l2tp_ip.

      Compared to other tunneling protocol implementations in Linux, L2TPv3 terminology is somewhat
      reversed. You create a tunnel, and then bind sessions to it. You can bind
      multiple sessions with different identifiers to the same tunnel. Virtual network interfaces
      (by default named l2tpethX) are associated with sessions.

      Note: Linux kernel implements only handling of data frames, so you can create
      only unmanaged tunnels with iproute2, with all settings configured manually on both sides.
      If you want to use L2TP for remote access VPN or something else other than fixed
      pseudowire, you need a userspace daemon to handle it. This is outside of this document scope.

      Create an L2TPv3 tunnel over UDP

      ip l2tp add tunnel \
      tunnel_id ${local tunnel numeric identifier} \
      peer_tunnel_id ${remote tunnel numeric identifier} \
      udp_sport ${source port} \
      udp_dport ${destination port} \
      encap udp \
      local ${local endpoint address} \
      remote ${remote endpoint address}

      Examples:

      ip l2tp add tunnel \
      tunnel_id 1 \
      peer_tunnel_id 1 \
      udp_sport 5000 \
      udp_dport 5000 \ 
      encap udp \
      local 192.0.2.1 \ 
      remote 203.0.113.2

      Note: Tunnel identifiers and other settings on both endpoints must match.

      Create an L2TPv3 tunnel over IP

      ip l2tp add tunnel \
      tunnel_id ${local tunnel numeric identifier} \
      peer_tunnel_id {remote tunnel numeric identifier } \
      encap ip \
      local 192.0.2.1 \
      remote 203.0.113.2

      L2TPv3 encapsulated directly into IP offers less overhead,
      bug generally is unable to pass through NAT.

      Create an L2TPv3 session

      ip l2tp add session tunnel_id ${local tunnel identifier} \
      session_id ${local session numeric identifier} \
      peer_session_id ${remote session numeric identifier}

      Examples:

      ip l2tp add session tunnel_id 1 \ 
      session_id 10 \
      peer_session_id 10

      Notes: tunnel_id value must match a value of previously created tunnel.
      Session identifiers on both endpoints must match.

      Once you create a tunnel and a session, l2tpethX interface will appear, in down state. Change the state
      to up and bridge it with another interface or assign an address.

      Delete an L2TPv3 session

      ip l2tp del session tunnel_id ${tunnel identifier} \
      session_id ${session identifier}

      Examples

      ip l2tp del session tunnel_id 1 session_id 1

      Delete an L2TPv3 tunnel

      ip l2tp del tunnel tunnel_id ${tunnel identifier}

      Examples

      ip l2tp del tunnel tunnel_id 1

      Note: You need to delete all sessions associated
      with a tunnel before deleting it.

      View L2TPv3 tunnel information

      ip l2tp show tunnel

      ip l2tp show tunnel tunnel_id ${tunnel identifier}

      Examples:

      ip l2tp show tunnel tunnel_id 12

      View L2TPv3 session information

      ip l2tp show session

      ip l2tp show session session_id ${session identifier} \
      tunnel_id ${tunnel identifier}

      Examples:

      ip l2tp show session session_id 1 tunnel_id 12

      VXLAN management


      VXLAN is a layer 2 tunneling protocol that is commonly used in conjunction with
      virtualization systems such as KVM to connect virtual machines running on different
      hypervisor nodes to each other and to outside world.

      Unlike GRE or L2TPv3 that are point to point, VXLAN replicates some properties of
      multiple access switched networks by using IP multicast. Also it supports virtual network
      separation by transmitting a network identifier along with the frame.

      The downside is that you will need to use a multicast routing protocol, typically PIM-SM,
      to get it to work over routed networks.

      The underlying encapsulation protocol for VXLAN is UDP.

      Create unicast VXLAN link

      ip link add name ${interface name} type vxlan \

      id &lt;0-16777215&gt; \

      dev ${source interface}\

      remote ${remote endpoint address} \

      local ${local endpoint address} \

      dstport ${VXLAN destination port}

      Example:

      ip link add name vxlan0 type vxlan \

      id 42 dev eth0 remote 203.0.113.6 local 192.0.2.1 dstport 4789

      Note: id options means VXLAN Network Identifier (VNI).

      Create multicast VXLAN link

      ip link add name ${interface name} type vxlan \

      id &lt;0-16777215&gt; \

      dev ${source interface} \

      group ${multicast address} \

      dstport ${VXLAN destination port}

      Example:

      ip link add name vxlan0 type vxlan \

      id 42 dev eth0 group 239.0.0.1 dstport 4789

      After that you need to bring the link up and either bridge it with
      another interface or assign an address.

      Route management


      For IPv4 routes, you can use either a prefix length or a dotted decimal subnet mask.
      That is, both 192.0.2.0/24 and 192.0.2.0/255.255.255.0 are equally acceptable.

      Note: as per the section below, if you set up a static route,
      and it becomes unreachable because a link goes down, it will be removed and
      will never get back on its own. You may not have noticed this behaviour
      because in many cases additional software (e.g. NetworkManager or rp-pppoe)
      takes care of restoring the routes when links go up and down.

      If you are going to use your Linux machine as a router, consider installing
      a routing protocol suite such as Quagga
      or BIRD. They keep track of interface status
      and restore routes when a link goes up after going down. Of course they also allow you
      to use dynamic routing protocols such as OSPF and BGP.

      Connected routes

      Some routes appear in the system without explicit configuration (against your will).

      Once you assign an address to an interface, the system calculates its network address
      and creates a route to it (this is why the subnet mask is required). These routes are
      called connected routes.

      For example, if you assign 203.0.113.25/24 to eth0, a connected route to 203.0.113.0/24 network
      will be created and the system will know that hosts from that network can be reached
      directly.

      When an interface goes down, connected routes associated with it are removed.
      This is used for inaccessible gateway detection so routes through gateways that
      went inaccessible are removed. Same mechanism prevents you from creating
      routes through inaccessible gateways.

      View all routes

      ip route

      ip route show

      You can use -4 and -6 options to view only IPv4 or IPv6 routes.
      If no options given, IPv4 routes are displayed. To view IPv6 routes, use:

      ip -6 route

      View routes to a network and all its subnets

      ip route show to root ${address}/${mask}

      For example, if you use 192.168.0.0/24 subnet in your network
      and it’s broken into 192.168.0.0/25 and 192.168.0.128/25, you can see
      all those routes with:

      ip route show to root 192.168.0.0/24

      Note: the word “to” in this and other show commands is optional.

      View routes to a network and all supernets

      ip route show to match ${address}/${mask}

      If you want to view routes to 192.168.0.0/24 and all larger subnets,
      use:

      ip route show to match 192.168.0.0/24

      As routers prefer more specific routes to less specific,
      this is often useful for debugging in situations when traffic to specific
      subnet is sent the wrong way because a route to it is missing
      but routes to larger subnets exist.

      View routes to exact subnet

      ip route show to exact ${address}/${mask}

      If you want to see the routes to 192.168.0.0/24, but not to,
      say 192.168.0.0/25 and 192.168.0.0/16, you can use:

      ip route show to exact 192.168.0.0/24

      View only the route actually used by the kernel

      ip route get ${address}/${mask}

      Example:

      ip route get 192.168.0.0/24

      Note that in complex routing scenarios like multipath routing,
      the result may be “correct but not complete”, as it always shows
      one route that will be used first. In most situations it’s not
      a problem, but never forget to look at the corresponding “show” command
      output too.

      View route cache (pre 3.6 kernels only)

      ip route show cached

      Until the version 3.6, Linux used
      route caching. In older kernels, this command displays the contents
      of the route cache. It can be used with modifiers
      described above. In newer kernels it does nothing.

      Add a route via gateway

      ip route add ${address}/${mask} via ${next hop}

      Examples:

      ip route add 192.0.2.128/25 via 192.0.2.1

      ip route add 2001:db8:1::/48 via 2001:db8:1::1

      Add a route via interface

      ip route add ${address}/${mask} dev ${interface name}

      Example:

      ip route add 192.0.2.0/25 dev ppp0

      Interface routes are commonly used with point-to-point interfaces
      like PPP tunnels where next hop address is not required.

      Change or replace a route

      You may use “change” command to change parameters of existing routes.
      “Replace” command can be used to modify existing route or add new one if
      it doesn’t exist. Examples:

      ip route change 192.168.2.0/24 via 10.0.0.1

      ip route replace 192.0.2.1/27 dev tun0

      Delete a route

      ip route delete ${rest of the route statement}

      Examples:

      ip route delete 10.0.1.0/25 via 10.0.0.1

      ip route delete default dev ppp0

      Default route

      There is a shortcut to add default route.

      ip route add default via ${address}/${mask}

      ip route add default dev ${interface name}

      These are equivalent to:

      ip route add 0.0.0.0/0 via ${address}/${mask}

      ip route add 0.0.0.0/0 dev ${interface name}

      With IPv6 routes it also works and is equivalent to ::/0

      ip -6 route add default via 2001:db8::1

      Blackhole routes

      ip route add blackhole ${address}/${mask}

      Examples:

      ip route add blackhole 192.0.2.1/32

      Traffic to destinations that match a blackhole route is silently discarded.

      Blackhole routes have dual purpose. First one is straightforward, to discard traffic
      sent to unwanted destinations, e.g. known malicious hosts.

      The second one is less obvious and uses the “longest match rule” as per RFC1812.
      In some cases you may want the router to think it has a route to a larger subnet,
      while you are not using it as a whole,
      e.g. when advertising the whole subnet via dynamic routing protocols. Large subnets
      are commonly broken into smaller parts, so if your subnet is 192.0.2.0/24, and you
      have assigned 192.0.2.1/25 and 192.0.2.129/25 to your interfaces, your system creates connected
      routes to the /25’s, but not the whole /24, and routing daemons may not want to
      advertise /24 because you have no route to that exact subnet. The solution is to setup
      a blackhole route to 192.0.2.0/24. Because routes to smaller subnets are preferred
      over larger subnets, it will not affect actual routing, but will convince routing
      daemons there’s a route to the supernet.

      Other special routes

      ip route add unreachable ${address}/${mask}

      ip route add prohibit ${address}/${mask}

      ip route add throw ${address}/${mask}

      These routes make the system discard packets and reply with an ICMP error message to the sender.

      unreachable
      Sends ICMP “host unreachable”.
      prohibit
      Sends ICMP “administratively prohibited”.
      throw
      Sends “net unreachable”.

      Unlike blackhole routes, these can’t be recommended for stopping unwanted traffic (e.g. DDoS)
      because they generate a reply packet for every discarded packet and thus create even greater traffic flow.
      They can be good for implementing internal access policies, but consider firewall for this purpose first.

      “Throw” routes may be used for implementing policy-based routing, in non-default tables they
      stop current table lookup, but don’t send ICMP error messages.

      Routes with different metric

      ip route add ${address}/${mask} via ${gateway} metric ${number}

      Examples:

      ip route add 192.168.2.0/24 via 10.0.1.1 metric 5

      ip route add 192.168.2.0 dev ppp0 metric 10

      If there are several routes to the same network with different metric value,
      the one with the lowest metric will be preferred.

      Important part of this concept is that when an interface goes down, routes
      that would be rendered useless by this event disappear from the routing table (see the Connected Routes section), and
      the system will fall back to higher metric routes.

      This feature is commonly used to implement backup connections to important destinations.

      Multipath routing

      ip route add ${addresss}/${mask} nexthop via ${gateway 1} weight ${number} nexthop via ${gateway 2} weight ${number}

      Multipath routes make the system balance packets across several links according to the weight (higher weight is preferred, so
      gateway/interface with weight of 2 will get roughly two times more traffic than another one with weight of 1). You can have as many gateways
      as you want and mix gateway and interface routes, like:

      ip route add default nexthop via 192.168.1.1 weight 1 nexthop dev ppp0 weight 10

      Warning: the downside of this type of balancing is that packets are not guaranteed to be sent back
      through the same link they came in. This is called “asymmetric routing”. For routers that simply forward packets and don’t
      do any local traffic processing such as NAT, this is usually normal, and in some cases even unavoidable.

      If your system does anything but forwarding packets between interfaces, this may cause problems with incoming connections
      and some measures should be taken to prevent it.

      Policy-based routing


      Policy-based routing (PBR) in Linux is designed the following way:
      first you create custom routing tables, then you create rules to tell
      the kernel it should use those tables instead of the default table for specific traffic.

      Some tables are predefined:

      local (table 255)
      Contains control routes local and broadcast addresses.
      main (table 254)
      Contains all non-PBR routes. If you don’t specify the table when adding a route, it goes here.
      default (table 253)
      Reserved for post processing, normally unused.

      User-defined tables are created automatically when you add the first route to them.

      Create a policy route

      ip route add ${route options} table ${table id or name}

      Examples:

      ip route add 192.0.2.0/27 via 203.0.113.1 table 10

      ip route add 0.0.0.0/0 via 192.168.0.1 table ISP2

      ip route add 2001:db8::/48 dev eth1 table 100

      Notes: You can use any route options described in “Route management” section
      in policy routes too, the only difference is the “table ${table id/name}” part at the end.

      Numeric table identifiers and names can be used interchangeably. To create your own symbolic
      names, edit /etc/iproute2/rt_tables config file.

      “delete”, “change”, “replace”, or any other route actions work with any table too.

      “ip route … table main” or “ip route … table 254” would have exact same effect to
      commands without a table part.

      View policy routes

      ip route show table ${table id or name}

      Examples:

      ip route show table 100

      ip route show table test

      Note: in this case you need the “show” word, the shorthand like “ip route table 120”
      do not work because the command would be ambiguous.

      General rule syntax

      ip rule add ${options} &lt;lookup ${table id or name}|blackhole|prohibit|unreachable&gt;

      Traffic that matches the ${options} (described below) will be routed
      according to the table with specified name/id instead of the “main”/254 table if “lookup” action is used.

      “blackhole”, “prohibit”, and “unreachable” actions that work the same way to route types with same names.
      In most of examples we will use “lookup” action as the most common.

      For IPv6 rules, use “ip -6”, the rest of the syntax is the same.

      “table ${table id or name}” can be used as alias to “lookup ${table id or name}”.

      Create a rule to match a source network

      ip rule add from ${source network} ${action}

      Examples:

      ip rule add from 192.0.2.0/24 lookup 10

      ip -6 rule add from 2001:db8::/32 prohibit

      Notes: “all” can be used as shorthand to 0.0.0.0/0 or ::/0

      Create a rule to match a destination network

      ip rule add to ${destination network} ${action}

      Examples:

      ip rule add to 192.0.2.0/24 blackhole

      ip -6 rule add to 2001:db8::/32 lookup 100

      Create a rule to match a ToS field value

      ip rule add tos ${ToS value} ${action}

      Examples:

      ip rule add tos 0x10 lookup 110

      Create a rule to match a firewall mark value

      ip rule add fwmark ${mark} ${action}

      Examples:

      ip rule add fwmark 0x11 lookup 100

      Note: See iptables documentation to find out how to set the mark.

      Create a rule to match inbound interface

      ip rule add iif ${interface name} ${action}

      Examples:

      ip rule add iif eth0 lookup 10

      ip rule add iif lo lookup 20

      Rule with “iif lo” (loopback) will match locally generated traffic.

      Create a rule to match outbound interface

      ip rule add oif ${interface name} ${action}

      Examples:

      ip rule add oif eth0 lookup 10

      Note: this works only for locally generated traffic.

      Set rule priority

      ip rule add ${options} ${action} priority ${value}

      Examples:

      ip rule add from 192.0.2.0/25 lookup 10 priority 10

      ip rule add from 192.0.2.0/24 lookup 20 priority 20

      Note: As rules are traversed from the lowest to the highest
      priority and processing stops at first match, you need to put more specific rules
      before less specific. The above example demonstrates rules for 192.0.2.0/24 and its
      subnet 192.0.2.0/25. If the priorities were reversed and the rule for /25 was placed
      after the rule for /24, it would never be reached.

      Show all rules

      ip rule show

      ip -6 rule show

      Delete a rule

      ip rule del ${options} ${action}

      Examples:

      ip rule del 192.0.2.0/24 lookup 10

      Notes: You can copy/paste from the output of “ip rule show”/”ip -6 rule show”.

      Delete all rules

      ip rule flush

      ip -6 rule flush

      Notes: this operation is highly disruptive. Even if you have not
      configured any rules, “from all lookup main” rules are initialized by default. On an unconfigured
      machine you can see this:

      $ ip rule show
      0:	from all lookup local 
      32766:	from all lookup main 
      32767:	from all lookup default 
      
      $ ip -6 rule show
      0:	from all lookup local 
      32766:	from all lookup main

      The “from all lookup local” rule is special and cannot be deleted. The “from all lookup main” is not,
      there may be valid reasons not to have it, e.g. if you want to route only traffic you created explicit
      rules for. As a side effect, if you do “ip rule flush”, this rule will be deleted, which will make the
      system stop routing any traffic until you restore your rules.

      Network namespace management


      Network namespaces are isolated network stack instances within a single machine. They can be used
      for security domain separation, managing traffic flows between virtual machines and so on.

      Every namespace is a complete copy of the networking stack with its own interfaces, addresses,
      routes etc. You can run processes inside a namespace and bridge namespaces to physical interfaces.

      Create a namespace

      ip netns add ${namespace name}

      Examples:

      ip netns add foo

      List existing namespaces

      ip netns list

      Delete a namespace

      ip netns delete ${namespace name}

      Examples:

      ip netns delete foo

      Run a process inside a namespace

      ip netns exec ${namespace name} ${command}

      Examples:

      ip netns exec foo /bin/sh

      Note: assigning a process to a non-default namespace requires
      root privileges.

      You can run any processes inside a namespace, in particular you can run “ip”
      itself, commands like in this “ip netns exec foo ip link list” in this section
      are not a special syntax but simply executing another copy of “ip” in a namespace.
      You can run an interactive shell inside a namespace as well.

      List all processes assigned to a namespace

      ip netns pids ${namespace name}

      The output will be a list of PIDs.

      Identify process’ primary namespace

      ip netns identify ${pid}

      Examples:

      ip netns identify 9000

      Assign network interface to a namespace

      ip link set dev ${interface name} netns ${namespace name}

      ip link set dev ${interface name} netns ${pid}

      Examples:

      ip link set dev eth0.100 netns foo

      Note: once you assign an interface to a namespace, it disappears
      from the default namespace and you will have to perform all operations with it via
      “ip netns exec ${netspace name}”, as in “ip netns exec ${netspace name} ip link set dev dummy0 down”.

      Moreover, when you move an interface to another namespace, it loses all existing configuration
      such as IP addresses configured on it and goes to DOWN state. You need to bring it back up and
      reconfigure.

      If you specify a PID instead of a namespace name, the interface gets assigned to the primary
      namespace of the process with that PID. This way you can reassign an interface back to default
      namespace with e.g. “ip netns exec ${namespace name} ip link set dev ${intf} netns 1”
      (since init or another process with PID 1 is pretty much guaranteed to be in default namespace).

      Connect one namespace to another

      This can be done by creating two veth links and assigning them two different namespaces.
      Suppose you want to connect namespace “foo” to the default namespace.

      Create a pair of veth devices:

      ip link add name veth1 type veth peer name veth2

      Move veth2 to namespace foo:

      ip link set dev veth2 netns foo

      Bring veth2 and add an address in “foo” namespace:

      ip netns exec foo ip link set dev veth2 up

      ip netns exec foo ip address add 10.1.1.1/24 dev veth2

      Add an address to veth1, which stays in the default namespace:

      ip address add 10.1.1.2/24 dev veth1

      Now you can ping 10.1.1.1 which if in foo namespace, and setup routes to
      subnets configured in other interfaces of that namespace.

      If you want switching instead of routing, you can bridge those veth
      interfaces with other interfaces in corresponding namespaces. Same technique
      can be used to connect namespaces to physical networks.

      Monitor network namespace subsystem events

      ip netns monitor

      Displays events such as creation and deletion of namespaces when they occur.

      Multicast management


      Multicast is mostly handled by applications and routing daemons, so there is not much
      you can and should do manually here. Multicast-related ip commands are mostly useful for debug.

      View multicast groups

      ip maddress show

      ip maddress show ${interface name}

      Example:

      $ip maddress show dev lo
      1:	lo
      	inet  224.0.0.1
      	inet6 ff02::1
      	inet6 ff01::1

      Add a link-layer multicast address

      You cannot join an IP multicast group manually, but you can add a multicast MAC
      address (even though it’s rarely needed).

      ip maddress add ${MAC address} dev ${interface name}

      Example:

      ip maddress add 01:00:5e:00:00:ab dev eth0

      View multicast routes

      Multicast routes cannot be added manually, so this command can only show
      multicast routes installed by a routing daemon. It supports the same modifiers to
      unicast route viewing commands (iif, table, from etc.).

      ip mroute show

      Network event monitoring


      You can monitor certain network events with iproute2, such as changes in network configuration,
      routing tables, and ARP/NDP tables.

      Monitor all events

      You may either call the command without parameters or explicitly specify “all”.

      ip monitor

      ip monitor all

      Monitor specific events

      ip monitor ${event type}

      Event type can be:

      link
      Link state: interfaces going up and down, virtual interfaces getting created or
      destroyed etc.
      address
      Link address changes.
      route
      Routing table changes.
      mroute
      Multicast routing changes.
      neigh
      Changes in neighbor (ARP and NDP) tables.

      When there are distinct IPv4 and IPv6 subsystems, the usual “-4” and “-6” options
      allow you to display events only for specified protocol. As in:

      ip -4 monitor route

      ip -6 monitor neigh

      ip -4 monitor address

      Read a log file produced by rtmon

      iproute2 includes a program called “rtmon” that serves essentially the same purpose,
      but writes events to a binary log file instead of displaying them. “ip monitor” command
      allows you to read files created by the program”.

      ip monitor ${event type} file ${path to the log file}

      rtmon syntax is similar to that of “ip monitor”, except event type is limited to
      link, address, route, and all; and address family is specified in “-family” option.

      rtmon [-family &lt;inet|inet6&gt;] [&lt;route|link|address|all&gt;] file ${log file path}

      netconf (sysctl configuration viewing)


      View sysctl configuration for all interfaces

      ip netconf show

      View sysctl configuration for specific interface

      ip netconf show dev ${interface}

      Examples:

      ip netconf show dev eth0

      Contributors:

      Content

      Nicolas Dichtel
      Netconf section
      Russel Stuart
      TUN/TAP section
      Phil Huang
      Unicast VXLAN
      Haishan
      dstport option for VXLAN commands
      Emil Pederson
      promote_secondaries suggestion.
      Nick B.
      tuntap device deletion command correction

      Grammar, style, typo fixes

      Trick van Staveren,
      powyginanachochla,
      Nathan Handler,
      Bhaskar Sarma Upadhyayula,
      Geert Stappers,
      Alex White-Robinson,
      Achilleas Pipinellis,
      fauxm,
      fgtham,
      eri.

      Design

      elico,
      alex-eri.

      © 2018 GitHub, Inc.
      Terms
      Privacy
      Security
      Status
      Help
      Contact GitHub
      API
      Training
      Shop
      Blog
      About
      Press h to open a hovercard with more details.

      ]]>
      Linux – Permissions https://www.f1-consult.com/linux/permissions/ Fri, 27 Jul 2018 13:44:44 +0000 https://www.f1-consult.com/?p=3876  

      In order to update and change file permissions, you can use the chmod command. The format of the command is as follows:

      chmod [u, g, o] (+/- [r, w, x])

      for each action

       

      Changing Permissions
      VALUE MEANING
      rwx+ugo No restrictions on permissions.
      u+rwx,go+rx,go-x The file’s owner may read, write, and execute the file. All others may read and execute the file.
      u+rwx,og-rwx The file’s owner may read, write, and execute the file. Nobody else has any rights.
      ugo+rw,ugo-x All users may read and write the file.
      u+rw,u-x,go+r,go-wx The owner may read and write a file, while all others may only read the file.
      u+rw,u-x,go-rwx The owner may read and write a file. All others have no rights.
      ]]>
      How to Disable or Enable Disk Write Caching in Linux https://www.f1-consult.com/linux/linux-disk-write-caching/ Tue, 10 Jul 2018 14:02:26 +0000 https://www.f1-consult.com/?p=3836 ATA-Based Disks
      1. Check out whether disk caching is enabled on your disk or not:
      # sudo hdparm -i /dev/sdX
      
      Note: replace the “/dev/sdX” with the correct address of your target device.
      The expected output will be similar below:
      
      Model=WDC WD3200BPVT-22JJ5T0, FwRev=01.01A01, SerialNo=WD-WX61EC1KZK99
      Config={ HardSect NotMFM HdSw>15uSec SpinMotCtl Fixed DTR>5Mbs FmtGapReq }
      RawCHS=16383/16/63, TrkSize=0, SectSize=0, ECCbytes=50
      BuffType=unknown, BuffSize=8192kB, MaxMultSect=16, MultSect=off
      CurCHS=16383/16/63, CurSects=16514064, LBA=yes, LBAsects=625142448
      IORDY=on/off, tPIO={min:120,w/IORDY:120}, tDMA={min:120,rec:120}
      PIO modes: pio0 pio3 pio4
      DMA modes: mdma0 mdma1 mdma2
      UDMA modes: udma0 udma1 udma2 udma3 udma4 udma5 *udma6
      AdvancedPM=yes: unknown setting WriteCache=enabled
      Drive conforms to: Unspecified: ATA/ATAPI-1,2,3,4,5,6,7
      
      The words in red, WriteCache=enabled, means caching is enabled!
      1. To disable write caching, you need to edit hdparm.conf using your favorite editor (e.g. gedit, vi).
      # sudo gedit /etc/hdparm.conf
      Uncomment the line “#write_cache = off” (without quotes) by removing the
      # at its beginning. So it looks like:
      # -W Disable/enable the IDE drive’s write-caching feature
      write_cache = off
      1. After making the changes, restart the server/computer and check out the write caching status again to make sure it’s disabled.
        If in case hdparm.conf does not exist in /etc folder, an alternative approach is to use the command below.
      # sudo hdparm -W 0 /dev/sdX --> disable write caching
      # sudo hdparm -W 1 /dev/sdX --> enable write caching
      To verify write caching if enabled or disabled, use the command below.
      # sudo hdparm -i /dev/sdX
      Note:
      Replace /dev/sdX with the correct address of the target device
      1. To know more about “hdparm”, invoke the manual by typing the following command.
      # sudo man hdparm | more
      OR
      # sudo man hdparm | less

      SCSI-Based Disks

      1. Check out whether disk caching is enabled on your disk or not:
      # sudo sdparm /dev/sdX
              --- replace /dev/sdX to a target device
      
      Expected Output:
      /dev/sda: COMPAQ BF03697B5A HPB2
      Read write error recovery mode page:
      AWRE 1 [cha: y, def: 1, sav: 1]
      ARRE 1 [cha: y, def: 1, sav: 1]
      PER 1 [cha: y, def: 1, sav: 1]
      Caching (SBC) mode page:
      WCE 0 [cha: y, def: 1, sav: 0] --> write caching is disabled
      RCD 0 [cha: y, def: 0, sav: 0]
      Control mode page:
      SWP 0 [cha: y, def: 0, sav: 0]
      Informational exceptions control mode page:
      EWASC 1 [cha: y, def: 1, sav: 1]
      DEXCPT 0 [cha: y, def: 0, sav: 0]
      MRIE 4 [cha: y, def: 4, sav: 4]
      1. Sending an SCSI inquiry command using “sdparm”.
      # sudo sdparm --inquiry /dev/sdX
      Expected Output:
      /dev/sda: COMPAQ BF03697B5A HPB2
      Device identification VPD page:
      Addressed logical unit:
      id_type: EUI-64 based, code_set: Binary
      [0x0010b9fd080c1129]
      id_type: T10 vendor identification, code_set: ASCII
      vendor id: COMPAQ
      vendor specific: E20C9MKK
      1. Getting all the parameters for the Caching Mode Page. Refer to the details of this mode page from the SCSI Interface Specification.
      # sudo sdparm --page=ca /dev/sdX
      Expected Output:
      /dev/sda: COMPAQ BF03697B5A HPB2
      Caching (SBC) mode page:
      IC 0 [cha: y, def: 0, sav: 0]
      ABPF 0 [cha: n, def: 0, sav: 0]
      CAP 0 [cha: n, def: 0, sav: 0]
      DISC 0 [cha: n, def: 0, sav: 0]
      SIZE 0 [cha: y, def: 0, sav: 0]
      WCE 0 [cha: y, def: 1, sav: 0] --> write caching is disabled
      MF 0 [cha: y, def: 0, sav: 0]
      RCD 0 [cha: y, def: 0, sav: 0]
      DRRP 0 [cha: y, def: 0, sav: 0]
      WRP 0 [cha: y, def: 0, sav: 0]
      DPTL 512 [cha: y, def:512, sav:512]
      MIPF 256 [cha: y, def:256, sav:256]
      MAPF 512 [cha: y, def:512, sav:512]
      MAPFC 512 [cha: y, def:512, sav:512]
      FSW 0 [cha: n, def: 0, sav: 0]
      LBCSS 0 [cha: y, def: 0, sav: 0]
      DRA 1 [cha: y, def: 1, sav: 1]
      NV_DIS 0 [cha: n, def: 0, sav: 0]
      NCS 226 [cha: y, def:226, sav:226]
      CSS 0 [cha: y, def: 0, sav: 0]
      1. Setting write caching or disk caching to enable by running below command.
      # sudo sdparm --set=WCE /dev/sdX --> this enables write caching
      1. To get the WCE current value:
      # sdparm –g WCE /dev/sdX (just input the target DUT)
      1. To set the WCE current value (WCE=1)
      # sdparm –s WCE /dev/sdX
      1. To set the WCE current and saved values (WCE=1)
      # sdparm –s WCE –S /dev/sdX
      1. To clear (disable) the WCE current value (WCE=0)
      # sdparm –c WCE /dev/sdX
      1. To clear (disable) the WCE current and saved values (WCE=0)
      # sdparm –c WCE –S /dev/sdb
      1. To know more about “sdparm”, invoke the manual by typing the following command.
      # man sdparm | more
      OR
      # man sdparm | less
      ]]>