Find IP address from command line on Linux

Using ip command

ip add

example output

bob@localhost:~$ ip add
1: lo: mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eno1: mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 38:ea:a7:13:a4:fe brd ff:ff:ff:ff:ff:ff
inet 192.168.1.21/24 brd 192.168.1.1 scope global dynamic noprefixroute eno1
valid_lft 513sec preferred_lft 513sec
inet6 13ac::98fe::ae78:d1ff/64 scope link noprefixroute
valid_lft forever preferred_lft forever
bob@localhost:~$

ifconfig

You may need to install net-tools to use

ifconfig  

example output

bob@localhost:~$ ifconfig 
eno1: flags=4163 mtu 1500
inet 192.168.200.58 netmask 255.255.255.0 broadcast 192.168.1.21
inet6 13ac::98fe::ae78:d1ff prefixlen 64 scopeid 0x20
ether b8:ac:6f:91:01:e8 txqueuelen 1000 (Ethernet)
RX packets 184950632 bytes 9487577263452
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 128473456 bytes 234612443785
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

Using the hostname command

hostname -I

Output is just the IP address. Example below

192.168.1.21

How to Set a Static IP Address on a Raspberry Pi

The examples given here are for modifying the wlan0 interface.  Replace wlan0 with the interface you are configuring. i.e. (eth0,wlan1)

Method 1

This was the typical way to add a static IP address to a Pi, if you have issues with this, then try Method 2.

sudo vi /etc/network/interfaces

In the file it is pretty easy to see which lines control which interface, find the lines that control wlan0 (or the interface your configuring) and change/add to look like below.

iface wlan0 inet static
address 192.168.42.109
netmask 255.255.255.0
gateway 192.168.42.1

Save the file, reboot, and the Pi should come up with the new static IP.

Method 2

It looks like on the newer versions of Raspbian, the above method does not work anymore, so now you have to edit the following file

sudo vi /etc/dhcpcd.conf

and add the following lines.

interface wlan0
static ip_address=192.168.42.109/24
static routers=192.168.42.1
static domain_name_servers=192.168.42.1

If you just need to assign a static IP address, to the device, because it is going to be setup as a hotspot or something, you can get away with the following.

interface wlan0
static ip_address=192.168.42.1/24

If you run into issues with it not assigning the address, check the /etc/network/interfaces file and make sure that the line that starts with “iface wlan0” says manual at the end and not static.  If it says “iface wlan0 inet static”, change it to “iface wlan0 inet manual”

How to set a static ip address on CentOS, Fedora, or Red Hat Linux

Open up the following file with your favorite text editor. Change eth0 to the interface you need, like “wlan0” or “eth1”.

 vi /etc/sysconfig/network-scripts/ifcfg-eth0

The file should look something like the following.

DEVICE=eth0
HWADDR=0A:2G:F3:56:66:4B
TYPE=Ethernet
UUID=aeh9421c-6a62-712c-886d-347813g8d1dh
ONBOOT=no
NM_CONTROLLED=yes
BOOTPROTO=dhcp

To set the static IP address change “BOOTPROTO=dhcp” to “BOOTPROTO=static” and add the following to the end of the file. If you want/need the interface to come up when the computer boots up then be sure to change “ONBOOT=no” to “ONBOOT=yes”.

BROADCAST=192.168.1.255
DNS1=8.8.8.8
GATEWAY=192.168.1.1
IPADDR=192.168.1.110
NETMASK=255.255.255.0

Also, on some newer versions of CentOS you may need to add NM_DISABLED=no

So your file should now look like this.

DEVICE=eth0
HWADDR=0A:2G:F3:56:66:4B
TYPE=Ethernet
UUID=aeh9421c-6a62-712c-886d-347813g8d1dh
ONBOOT=yes
NM_CONTROLLED=no
BOOTPROTO=static
BROADCAST=192.168.1.255
DNS1=8.8.8.8
GATEWAY=192.168.1.1
IPADDR=192.168.1.110
NETMASK=255.255.255.0

Save the file and restart networking.

service network restart

Finally, check you IP address with ifconfig.

root@localhost ~]# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 0A:2G:F3:56:66:4B  
          inet addr:192.168.1.110  Bcast:192.168.1.255  Mask:255.255.255.0