Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fedora - Internet connection sharing with NetworkManager

#1
Internet connection sharing with NetworkManager

<div style="margin: 5px 5% 10px 5%;"><img src="https://www.sickgaming.net/blog/wp-content/uploads/2020/06/internet-connection-sharing-with-networkmanager.png" width="778" height="599" title="" alt="" /></div><div><p>NetworkManager is the network configuration daemon used on Fedora and many other distributions. It provides a consistent way to configure network interfaces and other network-related aspects on a Linux machine. Among many other features, it provides a Internet connection sharing functionality that can be very useful in different situations.</p>
<p>F<mark class="annotation-text annotation-text-yoast" id="annotation-text-50da97d1-a97d-473a-89d4-b410ed9c0cc9"></mark>or example, suppose you are in a place without Wi-Fi and want to share your laptop’s mobile data connection with friends. Or maybe you have a laptop with broken Wi-Fi and want to connect it via Ethernet cable to another laptop; in this way the first laptop become able to reach the Internet and maybe download new Wi-Fi drivers. </p>
<p>In cases like these it is useful to share Internet connectivity with other devices. On smartphones this feature is called “Tethering” and allows sharing a cellular connection via Wi-Fi, Bluetooth or a USB cable.</p>
<p>This article shows how the connection sharing mode offered by NetworkManager can be set up easily; it addition, it explains how to configure some more advanced features for power users.</p>
<p> <span id="more-31179"></span> </p>
<h2>How connection sharing works</h2>
<p>The basic idea behind connection sharing is that there is an <em>upstream</em> interface with Internet access and a <em>downstream</em> interface that needs connectivity. These interfaces can be of a different type—for example, Wi-Fi and Ethernet.</p>
<p>If the upstream interface is connected to a LAN, it is possible to configure our computer to act as a <em>bridge</em>; a bridge is the software version of an Ethernet switch. In this way, you “extend” the LAN to the downstream network. However this solution doesn’t always play well with all interface types; moreover, it works only if the upstream network uses private addresses.</p>
<p>A more general approach consists in assigning a private IPv4 subnet to the downstream network and turning on routing between the two interfaces. In this case, NAT (Network Address Translation) is also necessary. The purpose of NAT is to modify the source of packets coming from the downstream network so that they look as if they originate from your computer.</p>
<p>It would be inconvenient to configure manually all the devices in the downstream network. Therefore, you need a DHCP server to assign addresses automatically and configure hosts to route all traffic through your computer. In addition, in case the sharing happens through Wi-Fi, the wireless network adapter must be configured as an access point. </p>
<p>There are many tutorials out there explaining how to achieve this, with different degrees of difficulty. NetworkManager hides all this complexity and provides a <em>shared</em> mode that makes this configuration quick and convenient.</p>
<h2>Configuring connection sharing</h2>
<p>The configuration paradigm of NetworkManager is based on the concept of connection (or connection profile). A connection is a group of settings to apply on a network interface.</p>
<p>This article shows how to create and modify such connections using <em>nmcli</em>, the NetworkManager command line utility, and the GTK connection editor. If you prefer, other tools are available such as <em>nmtui</em> (a text-based user interface), GNOME control center or the KDE network applet.</p>
<p>A reasonable prerequisite to share Internet access is to have it available in the first place; this implies that there is already a NetworkManager connection active. If you are reading this, you probably already have a working Internet connection. If not, see <a href="https://www.redhat.com/sysadmin/becoming-friends-networkmanager">this article</a> for a more comprehensive introduction to NetworkManager.</p>
<p>The rest of this article assumes you already have a Wi-Fi connection profile configured and that connectivity must be shared over an Ethernet interface <em>enp1s0</em>.</p>
<p>To enable sharing, create a connection for interface enp1s0 and set the ipv4.method property to <em>shared </em>instead of the usual <em>auto</em>:</p>
<pre class="wp-block-preformatted">$ nmcli connection add type ethernet ifname enp1s0 ipv4.method shared con-name local</pre>
<p>The shared IPv4 method does multiple things:</p>
<ul>
<li>enables IP forwarding for the interface;</li>
<li>adds firewall rules and enables masquerading;</li>
<li>starts dnsmasq as a DHCP and DNS server.</li>
</ul>
<p>NetworkManager connection profiles, unless configured otherwise, are activated automatically. The new connection you have added should be already active in the device status:</p>
<pre class="wp-block-preformatted">$ nmcli device
DEVICE &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; TYPE&nbsp; &nbsp;&nbsp;&nbsp; STATE &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; CONNECTION
enp1s0 &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; ethernet&nbsp; connected &nbsp;&nbsp;&nbsp; local
wlp4s0 &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; wifi&nbsp; &nbsp;&nbsp;&nbsp; connected &nbsp;&nbsp;&nbsp; home-wifi</pre>
<p>If that is not the case, activate the profile manually with <em>nmcli connection up local</em>.</p>
<h2>Changing the shared IP range</h2>
<p> Now look at how NetworkManager configured the downstream interface enp1s0:</p>
<pre class="wp-block-preformatted">$ ip -o addr show enp1s0
8: enp1s0 inet 10.42.0.1/24 brd 10.42.0.255 ...</pre>
<p>10.42.0.1/24 is the default address set by NetworkManager for a device in shared mode. Addresses in this range are also distributed via DHCP to other computers. If the range conflicts with other private networks in your environment, change it by modifying the <em>ipv4.addresses</em> property:</p>
<pre class="wp-block-preformatted">$ nmcli connection modify local ipv4.addresses 192.168.42.1/24</pre>
<p>Remember to activate again the connection profile after any change to apply the new values:</p>
<pre class="wp-block-preformatted">$ nmcli connection up local $ ip -o addr show enp1s0
8: enp1s0 inet 192.168.42.1/24 brd 192.168.42.255 ...</pre>
<p>If you prefer using a graphical tool to edit connections, install the <em>nm-connection-editor</em> package. Launch the program and open the connection to edit; then select the <em>Shared to other computers</em> method in the <em>IPv4 Settings</em> tab. Finally, if you want to use a specific IP subnet, click <em>Add</em> and insert an address and a netmask.</p>
<figure class="wp-block-gallery columns-1 is-cropped">
<ul class="blocks-gallery-grid">
<li class="blocks-gallery-item">
<figure><img src="https://www.sickgaming.net/blog/wp-content/uploads/2020/06/internet-connection-sharing-with-networkmanager.png" alt="" data-id="31181" data-full-url="https://www.sickgaming.net/blog/wp-content/uploads/2020/06/internet-connection-sharing-with-networkmanager.png" data-link="https://fedoramagazine.org/?attachment_id=31181" class="wp-image-31181" /></figure>
</li>
</ul>
</figure>
<h2>Adding custom dnsmasq options</h2>
<p>In case you want to further extend the dnsmasq configuration, you can add new configuration snippets in <em>/etc/NetworkManager/dnsmasq-shared.d/</em>. For example, the following configuration:</p>
<pre class="wp-block-preformatted">dhcp-option=option:ntp-server,192.168.42.1
dhcp-host=52:54:00:a4:65:c8,192.168.42.170</pre>
<p>tells dnsmasq to advertise a NTP server via DHCP. In addition, it assigns a static IP to a client with a certain MAC. </p>
<p>There are many other useful options in the dnsmasq manual page. However, remember that some of them may conflict with the rest of the configuration; so please use custom options only if you know what you are doing.</p>
<h2>Other useful tricks</h2>
<p>If you want to set up sharing via Wi-Fi, you could create a connection in Access Point mode, manually configure the security, and then enable connection sharing. Actually, there is a quicker way, the hotspot mode:</p>
<pre class="wp-block-preformatted">$ nmcli device wifi hotspot [ifname $dev] [password $pw]</pre>
<p>This does everything needed to create a functional access point with connection sharing. The interface and password options are optional; if they are not specified, <em>nmcli</em> chooses the first Wi-Fi device available and generates a random password. Use the ‘<em>nmcli device wifi show-password</em>‘ command to display information for the active hotspot; the output includes the password and a text-based QR code that you can scan with a phone:</p>
<figure class="wp-block-image size-large"><img src="https://www.sickgaming.net/blog/wp-content/uploads/2020/06/internet-connection-sharing-with-networkmanager-1.png" alt="" class="wp-image-31182" /></figure>
<h2>What about IPv6?</h2>
<p>Until now this article discussed sharing IPv4 connectivity. NetworkManager also supports sharing IPv6 connectivity through DHCP prefix delegation. Using prefix delegation, a computer can request additional IPv6 prefixes from the DHCP server. Those public routable addresses are assigned to local networks via Router Advertisements. Again, NetworkManager makes all this easier through the shared IPv6 mode:</p>
<pre class="wp-block-preformatted">$ nmcli connection modify local ipv6.method shared</pre>
<p>Note that IPv6 sharing requires support from the Internet Service Provider, which should give out prefix delegations through DHCP. If the ISP doesn’t provides delegations, IPv6 sharing will not work; in such case NM will report in the journal that no prefixes are available:</p>
<pre class="wp-block-preformatted">policy: ipv6-pd: none of 0 prefixes of wlp1s0 can be shared on enp1s0</pre>
<p>Also, note that the Wi-Fi hotspot command described above only enables IPv4 sharing; if you want to also use IPv6 sharing you must edit the connection manually.</p>
<h2>Conclusion</h2>
<p>Remember, the next time you need to share your Internet connection, NetworkManager will make it easy for you. </p>
<p>If you have suggestions on how to improve this feature or any other feedback, please reach out to the NM community using the <a href="https://mail.gnome.org/mailman/listinfo/networkmanager-list">mailing list</a>, the <a href="https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/issues">issue tracker</a> or joining the <em>#nm</em> IRC channel on <em>freenode</em>.</p>
</div>


https://www.sickgaming.net/blog/2020/06/...rkmanager/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016