Static IP Address for Raspbian: Difference between revisions

From KlavoWiki
Jump to navigationJump to search
No edit summary
No edit summary
 
(19 intermediate revisions by the same user not shown)
Line 1: Line 1:
= Static IP Address =
= Debian 12 =
To create a static IP address for Raspbian.
 
== Set IP ==
Find the interface name.
 
<pre>nmcli c show</pre>
 
Configure IP settings.
<pre>
<pre>
vi /etc/network/interfaces
nmcli con mod "Wired connection 1" ipv4.addresses 10.13.13.235/24 ipv4.method manual
nmcli con mod "Wired connection 1" ipv4.gateway 10.13.13.254
nmcli con mod "Wired connection 1" ipv4.dns "10.13.13.240"
</pre>
</pre>


Change the line
 
For mutiple IP or DNS settings.
<pre>
<pre>
iface eth0 inet dhcp
nmcli con mod "Wired connection 1" ipv4.addresses 10.13.13.235/24, 192.168.1.235/24 ipv4.method manual
nmcli con mod "Wired connection 1" ipv4.dns "8.8.8.8, 8.8.4.4"
</pre>
</pre>
to
 
== Reset interface ==
Reset or reboot.
<pre>nmcli con down "Wired connection 1" && nmcli con up "Wired connection 1"</pre>
 
== Show Settings ==
<pre>nmcli -p connection show "Wired connection 1"</pre>
 
</pre>
 
 
= Debian 11 =
 
== interfaces ==
make sure the interfaces file has not been modified.
<pre>
<pre>
iface eth0 inet static
vi /etc/network/interfaces
</pre>
</pre>
The default settings are:
<pre>
# interfaces(5) file used by ifup(8) and ifdown(8)
# Please note that this file is written to be used with dhcpcd
# For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf'


and add the following lines immediately after the line above otherwise adding the lines to the end of the file will assign the information to the wireless network wlan0.
# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d
</pre>


== dhcpcd.conf ==
<pre>
<pre>
address 192.168.1.1
vi /etc/dhcpcd.conf
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255
gateway 192.168.1.254
</pre>
</pre>
Append the following.
The entry of static will stop the server looking for an IP address from DHCP.


== Add an IP Alias ==
<pre>
<pre>
iface eth0:0 inet static
interface eth0
address 192.168.22.1
static ip_address=192.168.13.200/24
netmask 255.255.255.0
static routers=192.168.13.1
network 192.168.22.0
static domain_name_servers=8.8.8.8 8.8.4.4
broadcast 192.168.22.255
static domain_search=mydomain.com ap.mydomain.com
auto eth0:0
</pre>
</pre>


=== Temporary Alias ===
== Alias ==
 
=== Temporary ===
To create a temporary alias rather than a permanent one you can issue the following command from the CLI.
To create a temporary alias rather than a permanent one you can issue the following command from the CLI.
<pre>
<pre>
Line 40: Line 71:
</pre>
</pre>


= Config Example =
== IPv6 ==
Here is an example of my config file.
The domain names servers can have a mix of IPv4 and IPv6 servers.
 
<pre>
vi /etc/dhcpcd.conf
</pre>
 
<pre>
static ip6_address=2321:3fa4:4f7a:d100::1234/64
static domain_name_servers=2001:4860:4860::8888 2001:4860:4860::8844
</pre>
 
== VLAN ==
 
=== Load 8021q Module ===
<pre>
vi /etc/modules
</pre>
 
Add the line
<pre>
<pre>
auto lo
8021q
iface lo inet loopback
</pre>
 
=== Create Interface ===
Define a VLAN for a specific interface.


iface eth0 inet static
<pre>
address 192.168.13.252
vi /etc/network/interfaces
netmask 255.255.255.0
</pre>
network 192.168.13.0
broadcast 192.168.13.255
gateway 192.168.13.254


iface eth0:0 inet static
<pre>
address 192.168.22.1
# VLAN 101 Interface
netmask 255.255.255.0
auto eth0.101
network 192.168.22.0
iface eth0.101 inet manual
broadcast 192.168.22.255
    vlan-raw-device eth0
auto eth0:0
</pre>


allow-hotplug wlan0
=== Assign IP Address ===
iface wlan0 inet manual
Set a static IP for new interface
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
<pre>
iface default inet dhcp
vi /etc/dhcpcd.conf
</pre>
</pre>


= Bug =
<pre>
As of 3rd June 2015 there is a known bug with Raspbian that always obtains an IP address from DHCP even if a static IP address is configured. This means that the rPi will obtain 2 individual IP address on the NIC. the static been the primary and the DHCP IP been the additional.
# Static IP configuration for VLan.101
interface eth0.101
static ip_address=192.168.1.101/24
</pre>


To fix this problem if DHCP is not required
== Static Route ==
<pre>
<pre>
yum autoremove dhcpd5
vi /lib/dhcpcd/dhcpcd-hooks/40-routes
</pre>
</pre>


You can also optionally remove
This will add a static route via interface eth0.11 which is on Vlan.11.  you could just specify eth0.
 
<pre>
<pre>
apt-get autoremove isc-dhcp-client isc-dhcp-common
ip route add 192.168.0.0/16 via 192.168.99.254 dev eth0.11 proto static
</pre>
</pre>
== Restart Interface ==
<pre>ip link set eth0 down && ip link set eth0 up</pre>


[[Category : Raspbian]]
[[Category : Raspbian]]

Latest revision as of 21:58, 27 September 2024

Debian 12

Set IP

Find the interface name.

nmcli c show

Configure IP settings.

nmcli con mod "Wired connection 1" ipv4.addresses 10.13.13.235/24 ipv4.method manual
nmcli con mod "Wired connection 1" ipv4.gateway 10.13.13.254
nmcli con mod "Wired connection 1" ipv4.dns "10.13.13.240"


For mutiple IP or DNS settings.

nmcli con mod "Wired connection 1" ipv4.addresses 10.13.13.235/24, 192.168.1.235/24 ipv4.method manual
nmcli con mod "Wired connection 1" ipv4.dns "8.8.8.8, 8.8.4.4"

Reset interface

Reset or reboot.

nmcli con down "Wired connection 1" && nmcli con up "Wired connection 1"

Show Settings

nmcli -p connection show "Wired connection 1"


Debian 11

interfaces

make sure the interfaces file has not been modified.

vi /etc/network/interfaces

The default settings are:

# interfaces(5) file used by ifup(8) and ifdown(8)

# Please note that this file is written to be used with dhcpcd
# For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf'

# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d

dhcpcd.conf

vi /etc/dhcpcd.conf

Append the following. The entry of static will stop the server looking for an IP address from DHCP.

interface eth0
static ip_address=192.168.13.200/24
static routers=192.168.13.1
static domain_name_servers=8.8.8.8 8.8.4.4
static domain_search=mydomain.com ap.mydomain.com

Alias

Temporary

To create a temporary alias rather than a permanent one you can issue the following command from the CLI.

ifconfig eth0:0 192.168.22.1/24

IPv6

The domain names servers can have a mix of IPv4 and IPv6 servers.

vi /etc/dhcpcd.conf
static ip6_address=2321:3fa4:4f7a:d100::1234/64
static domain_name_servers=2001:4860:4860::8888 2001:4860:4860::8844

VLAN

Load 8021q Module

vi /etc/modules

Add the line

8021q

Create Interface

Define a VLAN for a specific interface.

vi /etc/network/interfaces
# VLAN 101 Interface
auto eth0.101
iface eth0.101 inet manual
    vlan-raw-device eth0

Assign IP Address

Set a static IP for new interface

vi /etc/dhcpcd.conf
# Static IP configuration for VLan.101
interface eth0.101
static ip_address=192.168.1.101/24

Static Route

vi /lib/dhcpcd/dhcpcd-hooks/40-routes

This will add a static route via interface eth0.11 which is on Vlan.11. you could just specify eth0.

ip route add 192.168.0.0/16 via 192.168.99.254 dev eth0.11 proto static

Restart Interface

ip link set eth0 down && ip link set eth0 up