1

I would like to create static local IP addresses so I can ssh and network Pis easier. I've found some tutorials online for how to make static IP address such as for setting up a public server. Because I'm only networking Pis across a switch, I don't need the extra work of making them publicly accessible. What is the most minimalist way to create only a locally static IP address on raspbian? What range of addresses can I use?

twinlakes
  • 219
  • 1
  • 10
  • While there are other methods,like manually configuring the pi wi a static address, the DHCP solution mentioned in two answers (so far) is really the best. – Tyson Mar 23 '15 at 14:54

7 Answers7

1

You need to set up a DHCP reservation on your router. DHCP is the protocol used by routers to dynamically assign IP addresses to devices on the network. You can tell the router the MAC address of your Raspberry Pi and the IP address you want it to have, and it will always reserve this address for the Pi, even if it's disconnected for a time. This doesn't require any configuration on the Pi's end.

Routers have all kinds of different interfaces, but I am pretty confident that this is a feature all will have. I can't give a guide for all routers, but here is a tutorial that might help, and Googling around with the name/model of your router will probably turn up more specific results.

As far as I know, you can use just about any IP address in the private network range. I would go for anything with 192.168.1.X where X is from 2 to 255. 192.168.1.1 is usually reserved for the router itself. If you want more info see the Wikipedia article about Reserved IP addresses.

krs013
  • 366
  • 2
  • 8
1

Add the addresses to /etc/hosts on each machine.

Something like

192.168.1.66  machine_1
192.168.1.67  machine_2

The 192.168.X.X addresses are non routeable so are quite safe to use.

joan
  • 71,024
  • 5
  • 73
  • 106
  • Is the 192.168 part unique to each router? – twinlakes Mar 22 '15 at 16:22
  • Doesn't this just map dynamic ip to a hostname, so restarting machine_1 could make this not work? – twinlakes Mar 22 '15 at 18:27
  • I think you will want to do this in addition to DHCP reservation. This just tells the client machines how to interpret the names of the Pis as IPs. Most routers use 192.168.X.X, some (like Apple routers) use 10.0.X.X. – krs013 Mar 23 '15 at 20:48
1

I believe this command is the true easiest way to set a static IP address

$ sudo ifconfig eth0 your_ip_addr

source: https://www.linux.com/component/content/article/9400?format=pdf

twinlakes
  • 219
  • 1
  • 10
1

STEP 1 Find your default gateway IP by

$ route -ne

Under the Gateway column, you will find your Default Gateway IP (192.168.1.0 in my case) for each interface . Note it down .

STEP 2 Find out the IP addresses of your domain name servers by

$ cat /etc/resolv.conf 

note down the IP's displayed ( simply copy numbers n text (may be ) after nameserver , ( there will be one / many , just copy them

Generated by resolvconf

nameserver 192.168.1.1 (copy 192.168.1.1 )

nameserver XXX.xx.XX.xx:ff (copy XXX.xx.XX.xx:ff )

nameserver xx.xx.xx.xx (copy xx.xx.xx.xx)

for me i got only '192.168.1.1 '

STEP 3 Editting dhcpdc.conf

$ sudo nano /etc/dhcpcd.conf 

you will find few lines with # in the beginning , scroll to the end and add these lines

> IMPORTANT THIS IP'S ARE AS PER MY HARDWARE <

interface eth0
static ip_address=192.168.1.100
#your desired static IP (eth0)

interface wlan0 static ip_address=192.168.1.99 #your desired static IP (wlan0)

static routers=192.168.1.1 #default gateway IP address (from STEP 1)

static domain_name_servers=192.168.1.1 XXX.xx.XX.xx:ff xx.xx.xx.xx #IP’s found in the /etc/resolv.conf file,(from STEP 2) Seperate each IP with a single space.

Save the changes made
Ctrl-X and Y

STEP 4

$ sudo reboot 

STEP 5

check that everything is working correctly lets ping Google

 sudo ping www.google.com 
A3ruz
  • 11
  • 1
0

As others have mentioned, one way to do this is to configure it at the router, and assign a static address that way.

Alternatively, ssh into the pi and set the appropriate settings in your /etc/network/interfaces file. Before you begin you can examine the existing settings by running cat /etc/network/interfaces.

Here's pretty step by step tutorial for how to customize this config for a static ip.

One other thing to be mindful of-- set your static IP to something not already taken, I.E. not your gateway or another computer or you'll run into trouble. It is generally easier to find unallocated addresses in the IP range on the higher end of the spectrum.

0

Also You can create network interface alias:

iface eth0:1 inet static
address 192.168.1.81
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255
gateway 192.168.1.254
mpromonet
  • 1,124
  • 18
  • 37
Karol
  • 1
0

I believe what OP has marked answered is correct only for temporarily setting it. It will reset on reboot?

The way to do it is to

    sudo cp /etc/network/interfaces /etc/network/interfaces.backup
    sudo nano /etc/network/interfaces

In the nano editor file, ensure you enter the following:

    #This is a static IP configuration
    auto eth0
    iface eth0 inet static

    #Your static IP based on your router config
    address 192.168.1.xx

    #Leave the netmask as usual
    netmask 255.255.255.0

    #Your router IP
    gateway 192.168.1.x 
    auto lo
    iface lo inet loopback

Make sure you save by CTRL+O to writeout and save.

CoderX
  • 186
  • 5