You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
aney.co.uk/guides/set-static-ip.html

92 lines
4.4 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="This will cover how to set a static IP address for your linux computer">
<meta name="keywords" content="Blog, articles, linux, networking, guide">
<meta name="author" content="Nathan (Aney) Steel">
<meta name="theme-color" content="white">
<meta name="theme-color" content="black">
<link rel="stylesheet" type="text/css" href="/main.css">
<link rel="icon" type="image/png" href="/images/favicon.svg">
<title>Set a static IP (Guide)</title>
</head>
<body>
<header>
<h1>How to set a static IP</h1>
<hr/>
<nav>
<a href="/">home</a>
<a href="/equipment.html">equipment</a>
<a href="/projects.html">projects</a>
<a href="/blog/">blog</a>
<a href="/sitemap.html">misc</a>
<a href="/support.html">support</a>
</nav>
<hr/>
</header>
<main>
<section>
<p class="intro">For those that don't know a static IP is an IP address that doesn't change on reboots, etc. The opposite of a dynamic IP, which can change on reboots, and is the default mode for computers to get an IP (via DHCP).</p>
<h2>Get your network</h2>
<p>If you don't know what network/range of ports you're on, check first by following the below. If you do, and this is a flash-card of sorts, ignore this section.</p>
<pre><code>ip a</code></pre>
<p>You can likely gauge from this, what network you're on, as most routers default to a single range, with the router being the first address on that range<p>
<p>So... If your <strong>ip a</strong> shows <u>192.168.1</u>.xxx (after the last dot doesn't matter), I assume your address will need to be <u>192.168.1.</u>x, and your gateway will be <u>192.168.1</u>.1</p>
<h2>Set your static IP</h2>
<p>Open up the <strong>/etc/network/interfaces</strong> file with your editor of choice</p>
<pre><code>sudo vim /etc/network/interfaces</code></pre>
<p>And edit the file to look a little something like below. If that's a little difficult to understand, read on</p>
<pre><code>auto enp2s0
iface enp2s0 inet static
address 192.168.1.226
netmask 255.255.255.0
gateway 192.168.1.1
dns-namespaces 192.168.1.1 8.8.8.8 8.8.4.4</code></pre>
<h3>1) Set static network device</h3>
<p>Change the iface for your network device from</p>
<pre><code>iface enp2s0 inet dhcp</code></pre>
<p>to</p>
<pre><code>iface enp2s0 inet static</code></pre>
<p>Substituting enp2s0 for whatever your device is called. This device will pre-exist in the file, but can also be found with the following command</p>
<pre><code>ip a</code></pre>
<h3>2) Add your network details</h3>
<p>Below the editted line, add the following, based on your network (slightly mentioned in the very first section)</p>
<pre><code>address 192.168.1.220</code></pre>
<p>This is the IP that you want your PC to have, the number after the final dot, can be between (inclusively) 2, and 254</p>
<p>Followed by (on a new line)<p>
<pre><code>netmask 255.255.255.0</code></pre>
<p>This is an assumption about the average home network<p>
<p>Then, again on a new line<p>
<pre><code>gateway 192.168.1.1</code></pre>
<p>Another assumption, but this is your routers IP, that traditionally sits at the first IP</p>
<h3>3) Add your dns details</h3>
<p>I've seperated this out, as this is a common point of failure for setting a static IP, so make sure this is gucci<p>
<p>So below the address, etc. Add this line<p>
<pre><code>dns-namespaces 8.8.8.8 8.8.4.4</code></pre>
<p>This basically tells your PC where to look to find domain names<p>
<h2>Restart your networking service</h2>
<p>Now restarting your networking service (or entire PC if you so desire) will set your computer's IP to that in the <strong>/etc/network/interfaces</strong> file.</p>
<p>Simply use the command below, and voila</p>
<pre><code>sudo systemctl restart networking</code></pre>
</section>
</main>
<footer>
<hr/>
<p>Written by <a href="http://aney.co.uk" target="_blank" rel="noopener">@aney</a> with <a href="https://danluu.com/web-bloat/" target="_blank" rel="noopener">web bloat</a> in mind | <a href="https://github.com/Aney/website" target="_blank" rel="noopener">Source Code</a></p>
</footer>
</body>
</html>