====== Network Management ======
Now is a good time to set a persistent and constant IP for your server - DHCP has been good enough, but as we start pouring in more services, it's good to get this done now.
Note - this was supposed to be pretty quick, but then... the brave new way of doing things stepped in.
Setting a static IP is not the same as setting a DHCP reservation in your Router/AP - so choose an IP that is within your range, but outside of your DHCP server scope.
For purposes here - we're working in the 192.168.1.0/24 space, so we have the 192.168.1.0 thru 192.168.1.254 range to work with - most Router/AP's will start a DHCP scope ranging from 192.168.1.100-150, or 192.168.1.2 thru whatever - check with the vendor on how to limit the DHCP scope for those devices.
** Discover Interfaces **
To quickly identify all available Ethernet interfaces, you can use the ifconfig command as shown below.
sudo ifconfig -a | grep inet*
You will see a response similar to below
<code>
test@testbox:~$ sudo ifconfig
enp1s0 Link encap:Ethernet HWaddr 44:8a:5b:35:21:c1
inet addr:192.168.1.122 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::468a:5bff:fe35:21c1/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
snip...
</code>
Used to be easy, we could just see eth0 as the primary, and eth1 (n+1) as secondaries - not so much any more with UEFI and other advances...
Old-school...
eth0 Link encap:Ethernet HWaddr 74:e6:e2:11:22:33
New school... with a wink towards to old-school - see how things change
<code>
$ ifconfig -a
enp1s0 Link encap:Ethernet HWaddr 74:e6:e2:11:22:33
inet addr:192.168.1.122 Bcast:192.168.1.255 Mask:255.255.255.0
</code>
enp1s0 is the gigabit wired adapter (if you're really clever, you can chase down the OUI's on the MAC addresses, lol...)
So now that we know where the interfaces are, let's bind an interface to an address.
** Network Manager - the modern way of managing interfaces **
Editing the network configuration in a data center view, we've got tools to do this automatically on the install, but here, we're working with a single instance, and we don't need/have those tools available. Network Manager helps here, as we can do it on a single line command without having to call sed and write up a complication regex - now that the cloud is here, NM is really a better way.
Now knowing that we have a working DHCP config, and have already downloaded/installed packages - let's get the Network-Manger, and configure things from the CLI...
NOTE - before installing network manager, need to make a small change to a network configuration file, otherwise apt will complain - no worry here, as one we install and reboot, network manager will capture and manage the interfaces
sudo nano /etc/network/interfaces
Comment out the current interfaces - should look similar to below; be sure to leave the source and loopback sections as they are, just the ethernet, and perhaps if installed, the wireless interface
It should look similar to this when you are done:
<code>
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
source /etc/network/interfaces.d/*
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
#auto enp1s0
#iface enp1s0 inet dhcp
# This is an autoconfigured IPv6 interface
#iface enp1s0 inet6 auto
</code>
save the file, and now we install network-manager
sudo apt install network-manager
And reboot - ... and cross fingers ;)
(keep a keyboard/monitor handy, just in case)
With NetworkManger - all the connection configuration files will be stored here.
/etc/NetworkManager
/etc/NetworkManager/system-connections
Since we've already installed avahi, and NetworkManager, by default, is DHCP, the server should come back after the reboot...
** Network Manager CLI **
Again, we're in the new world of UEFI, Systemd, ipv6, etc and some things are a bit different, but...
Drive around a little bit - notice the device enumerations - they're still a bit consistent, perhaps a bit more - e.g. e is for ethernet, w is for wireless..
$ nmcli device status
DEVICE TYPE STATE CONNECTION
enp1s0 ethernet connected Wired connection 1
wlp2s0 wifi disconnected --
lo loopback unmanaged --
and even better... if you have a WiFi adapter installed on the server - can tickle this directly from nm...
<code>
$ nmcli dev wifi list
SSID MODE CHAN RATE SIGNAL BARS SECURITY
MYSSID Infra 11 54 Mbit/s 100 ▂▄▆█ WPA2
MYSSID Infra 132 54 Mbit/s 100 ▂▄▆█ WPA2
SOMEOTHERSSID Infra 52 54 Mbit/s 49 ▂▄__ WPA2
MYSSID Infra 149 54 Mbit/s 45 ▂▄__ WPA2
MYSSID Infra 11 54 Mbit/s 42 ▂▄__ WPA2
SOMEOTHERSSID Infra 1 54 Mbit/s 27 ▂___ WPA2
</code>
Cool, eh?
Now to set up a static IP address - the semantics here are pretty easy to follow - here's a template;
nmcli connection add type ethernet con-name connection-name ifname interface-name ip4 address gw4 address
So as an example
$ sudo nmcli con add type ethernet con-name homenet ifname enp1s0 ip4 192.168.1.6/24 gw4 192.168.1.1
and after a reboot - we get this...
<code>
test@testbox:~$ sudo nmcli con add type ethernet con-name homenet ifname enp1s0 ip4 192.168.1.6/24 gw4 192.168.1.1
[sudo] password for test:
Connection 'homenet' (6542f1df-76b2-4bdd-80e5-d38739947bc0) successfully added.
</code>
$ ifconfig
enp1s0 Link encap:Ethernet HWaddr 74:e6:e2:11:22:33
inet addr:192.168.1.5 Bcast:192.168.1.255 Mask:255.255.255.0
And then we can add some DNS servers - using google's public dns here
$ sudo nmcli con mod homenet ipv4.dns "8.8.8.8 8.8.4.4"
NetWorkManager, for many on the desktop world, has been a source of pain/disgust, but if you give it a chance, it's a very powerful solution - with a bit of a learning curve - the brave new world... change is never easy, but if you make it thru this one, you'll be set for the future.
Since we're on a server, and wired up, we may not need WiFi at the moment - this works nicely for laptops as well, if you're hardcore and run linux all the time there (which many actually do in the circle I run with)
sudo nmcli radio wifi off
And we can confirm by going back to nmcli and asking device status
<code>
$ nmcli dev status
DEVICE TYPE STATE CONNECTION
enp1s0 ethernet connected homenet
wlp2s0 wifi unavailable --
lo loopback unmanaged --
</code>
to turn wifi back on
sudo nmcli radio wifi on
Good quote comes to mind - "wax on/wax off" - miyagi-san
TIP - might also consider nmtui - I know it's not fair, but better to get the hard stuff first off
<code>
┌───────────────────────────┤ Edit Connection ├───────────────────────────┐
│ ↑│
│ Profile name homenet_________________________________ ▮│
│ Device enp1s0 (44:8A:5B:35:21:C1)______________ ▒│
│ ▒│
│ ═ ETHERNET <Show> ▒│
│ ▒│
│ ╤ IPv4 CONFIGURATION <Manual> <Hide> ▒│
│ │ Addresses 192.168.1.6/24___________ <Remove> ▒│
│ │ <Add...> ▒│
│ │ Gateway 192.168.1.1______________ ▒│
│ │ DNS servers 8.8.8.8__________________ <Remove> ▒│
│ │ 8.8.4.4__________________ <Remove> ▒│
│ │ <Add...> ▒│
│ │ Search domains <Add...> ▒│
│ │ ▒│
│ │ Routing (No custom routes) <Edit...> ▒│
│ │ [ ] Never use this network for default route ▒│
│ │ [ ] Ignore automatically obtained routes ▒│
│ │ ▒│
│ ↓│
└─────────────────────────────────────────────────────────────────────────┘
</code>
(bask in the ANSI glory, it's back to 1983)
nm is the next gen, and it's very powerful - we've only touched on a couple of the capabilities here.
Good reference here - https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/Networking_Guide/sec-Using_the_NetworkManager_Command_Line_Tool_nmcli.html