How to extract netmask using /sbin/ip? - ip

I want to replace ifconfig with /sbin/ip. Using ifconfig I can see lines like
inet addr:xx.xxx.x.xx Bcast:xx.xxx.x.xx Mask:255.255.255.0
and can easily parse this to find the netmask (255.255.255.0). How can I achieve this using /sbin/ip?

Running ip a you'll get output similar to that which you get running ifconfig. Specifically, you'll see a line similar to yours:
inet xx.xxx.x.xx/24 brd xx.xxx.x.xx
The /24 denotes the subnet mask in slash notation.

Related

Raspberry pi is unable to reach gateway

Recently we brought raspberry pi 3b.Beginning we used to access the internet using an ethernet cable and it used to connect properly but now raspberry pi is not able to reach the gateway itself and it's taking its default IP address i.e 169.xxx.xxx.xx.
what would be the issue?we tried to reinstalling the operating system again the same issue .it worked for one day after that same problem.so please help me to solve the issue.
Finally, I am able to figure it out after trial and error method. I have missed "auto eth0" before the iface statement i.e
auto eth0
iface eth0 inet static
address xxx.xxx.xxx.xxx
network 255.255.255.0
gateway xxx.xxx.xxx.xxx
dns-nameservers 8.8.8.8
Assuming that you have a windows computer available, open cmd and run the following command:
ipconfig
note down the values that display. Now on your pi, enter the command
sudo nano /etc/network/interfaces
This will open the network interfaces file. Look for the line similar to 'inet eth0 inet manual' Then remove this line and everything to do with the eth0 interface, since we are going to start over.
in the interfaces file, add the following section:
auto eth0
inet eth0 inet static
address xxx.xxx.xxx.xxx
network 255.255.255.0
gateway xxx.xxx.xxx.xxx
dns-nameservers 8.8.8.8
Replace the x in address with the first 3 groups of the value taken from the windows system. For example, if the ip address on the windows system was 192.168.0.221, enter 192.168.0.xxx
The last group of xxx for address should be something unique to everything else on your network.
'gateway' should be whatever the gateway value in windows was (assuming these machines are on the same network)
[Ctrl]+[x], Save changes
reboot via
sudo reboot
once the system has rebooted
ifconfig eth0
should list the new settings. Test them by pinging the below address (google)
sudo ping 8.8.8.8

Debian Network-Configuration for KVM - Provider OVH

I need some help to configure the network for my KVM. My Hostingprovider is OVH, and since they are a bit different, I'm in need of help.
My old Network-Interfaces File:
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet static
address 94.23.209.170
netmask 255.255.255.0
network 94.23.209.0
broadcast 94.23.209.255
gateway 94.23.209.254
auto br0
iface br0 inet static
address 91.134.173.185
netmask 255.255.255.0
broadcast 91.134.173.185
bridge_ports eth0
bridge_stp off
bridge_fd 0
bridge_maxwait 0
dns-nameservers 8.8.8.8
iface eth0 inet6 static
address 2001:41d0:0002:54aa::
netmask 64
dns-nameservers 2001:41d0:3:163::1
post-up /sbin/ip -family inet6 route add 2001:41d0:0002:54ff:ff:ff:ff:ff dev eth0
post-up /sbin/ip -family inet6 route add default via 2001:41d0:0002:54ff:ff:ff:ff:ff
pre-down /sbin/ip -family inet6 route del default via 2001:41d0:0002:54ff:ff:ff:ff:ff
pre-down /sbin/ip -family inet6 route del 2001:41d0:0002:54ff:ff:ff:ff:ff dev eth0
I had to go into the resecue mode and remove the bridge, otherwise my machine wouldn't come up again. Can someone help me maybe, and tell me what I did wrong?
Thanks, and have a good day/night! :)
I had a similar problem. I just moved to OVH from Phoenix nap. I like the control panel better but their networking is a little weird. I have an IP on a /24 and I ordered a /29 for whm/cpanel and some other virtual machines.
My config to get the host functional:
auto eth0
iface eth0 inet manual
address 111.222.333.145
netmask 255.255.255.0
network 111.222.333.0
broadcast 111.222.333.255
gateway 111.222.333.254
auto br0
iface br0 inet static
address 111.222.333.145
netmask 255.255.255.0
network 111.222.333.0
broadcast 111.222.333.255
gateway 111.222.333.254
bridge_ports eth0
bridge_stp off
bridge_fd 0
bridge_maxwait 0
dns-nameservers 213.186.33.99
NOTE: 111.222.333 is your first 3 octets. Obviously change them. the .145 was arbitrary to illustrate a host assigned to you.
Then restart the networking service.
service networking restart
Now I had to get a CentOS container for WHM/cPanel going and a few debian containers.
I'm assuming you bought a block of IPs and need to get that IP into a VM. Log into the OVH control panel, Select IP. Expand the IP block. to right you will see a gear you can click on. Create an OVH Virtual MAC. Take note of that MAC!
For CentOS the guide is correct.
In Debian it was a missing little something.
You want to edit the /etc/libvirt/qemu/autostart/YOU_VM_NAME.xml
...
<interface type='bridge'>
<mac address='YO:UR:VI:RT:MA:CA'/>
...
After saving restart the libvirtd service. Restart your debian container to pick up the new MAC and you should be good.
When installing I could not set an IP out side the range of my network. After getting virt-manager up, I logged in blew out the GW and modified the interfaces file according to the guide:
Don't need to change your host network config.
You need a Failover IP (create in OVH Panel). Then, assign a Virtual MAC for it.
In your dedicated server:
virsh net-edit default
Change this way:
<network>
<name>default</name>
<uuid>...</uuid>
<bridge name='virbr0' stp='off' delay='0'/>
<mac address='...'/>
</network>
Now edit the VM:
virsh edit myvmname
and set (change "eno1" to your network card name, like "eth0" or "ens0p0" etc):
<interface type='direct'>
<mac address='--VIRTUAL MAC CREATED IN OVH PANEL--'/>
<source dev='eno1' mode='bridge'/>
<model type='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>
Now edit your VM network (in my example, a Debian /etc/network/interfaces and change the network name as well):
auto eno1
iface eno1 inet static
address -FAILOVER IP-
netmask 255.255.255.255
gateway -HOST GATEWAY-
broadcast -FAILOVER IP-
So, the VM will have the failover IP and use the same gateway than the host. In OVH the gateway is final .254 (or use ip r in the host).

CT with Debian template in Proxmox: unable to keep interfaces file after reboot

I have set up a CT in ProxmoxVE 3 (image installed in Virtualbox for Windows 4.3.12) using the template debian-7-turnkey-lamp_13.0-1_amd64.tar.gz. After adding these two IPs to the container: 192.168.56.138 and 10.0.2.20, I start the container. The /etc/network/interfaces file shows this:
# This configuration file is auto-generated.
#
# WARNING: Do not edit this file, your changes will be lost.
# Please create/edit /etc/network/interfaces.head and
# /etc/network/interfaces.tail instead, their contents will be
# inserted at the beginning and at the end of this file, respectively.
#
# NOTE: it is NOT guaranteed that the contents of /etc/network/interfaces.tail
# will be at the very end of this file.
#
# Auto generated lo interface
auto lo
iface lo inet loopback
# Auto generated venet0 interface
auto venet0
iface venet0 inet manual
up ifconfig venet0 up
up ifconfig venet0 127.0.0.2
up route add default dev venet0
down route del default dev venet0
down ifconfig venet0 down
iface venet0 inet6 manual
up route -A inet6 add default dev venet0
down route -A inet6 del default dev venet0
auto venet0:0
iface venet0:0 inet static
address 192.168.56.138
netmask 255.255.255.255
auto venet0:1
iface venet0:1 inet static
address 10.0.2.20
netmask 255.255.255.255
With this configuration it can be accessed from the host via 192.168.56.138, but it can't access Internet.
After many trials and errors, I figure out a way to enable it to access internet. I only need to delete the last four lines:
auto venet0:1
iface venet0:1 inet static
address 10.0.2.20
netmask 255.255.255.255
And run service networking restart, and that's it: internet is available for the guest CT.
But when I reboot, the original /etc/network/interfaces file comes back, the changes disappear and internet is unreachable again.
So my question is: how can I make persistent the changes made to the interfaces file?
Thank you very much in advance,
So my question is: how can I make persistent the changes made to the interfaces file?
The answer to your question is in the header of the file.
# WARNING: Do not edit this file, your changes will be lost.
# Please create/edit /etc/network/interfaces.head and
# /etc/network/interfaces.tail instead, their contents will be
# inserted at the beginning and at the end of this file, respectively.

Issue setting static ETH0 IP BeagleBone Black Version C Debian Preloaded

Settings in /etc/network/interfaces seems to have no effect on my BeagleBone Black Version C. I want to set my beaglebone black's ethernet IP address to a static IP as I have done with all my other internet-of-things devices running Debian. I edited /etc/network/interfaces as I have before, but I do not see any changes via ifconfig. This is after a restart of networking AND a restart of the beaglebone. I have googled somewhat, and posted to the beaglebone forums after searching them, and I still do not have a solution. I noticed a commented line in interfaces suggesting that the settings are set via an init script, but this is where I get a little lost. I have moderate knowledge of Debian, but I have not peeled back enough layers apparently. I am connecting my beaglebone black via ethernet connector. WIFI is a beast I will tackle later.
I set my router to reserve a specific IP based on MAC ID, but I would like to know what I am missing here.
iface usb0 inet static
address 192.168.0.103
netmask 255.255.255.0
network 192.168.0.0
gateway 192.168.0.1
iface eth0 inet static
address 192.168.0.102
netmask 255.255.255.0
network 192.168.0.0
gateway 192.168.0.1
You are missing the auto line
auto eth0
iface eth0 inet static
address 192.168.0.102
netmask 255.255.255.0
network 192.168.0.0
gateway 192.168.0.1
(Just for diagnosis) - Also remove the gateway line from it
and after you get the connection run this command to add the gateway
root prompt :) #
route add default gateway 192.168.0.1
i was playing around a lot with networking on it so i got the answer. Hope this resolve your issue do post.
Do the configurations in wicd. Run "wicd-client" which opens GUI to make the configuration. Apparently wicd configurations overwrite what you do in /etc/network/interfaces
Another alternative to setting the ipaddress to be static on boot. Even without a ethernet jack plugged in, which was my issue.
Caution: this is hacky, and there probably better streamlined approach
Edit the /opt/scripts/boot/autoconfigure_usb0.sh script
line 92 and 139 set the usb0 default ipaddress
/sbin/ifconfig usb0 ${deb_usb_address} netmask ${deb_usb_netmask} || true
insert the following line after it, in both places
/sbin/ifconfig usb0 ${deb_usb_address} netmask ${deb_usb_netmask} || true
/sbin/ifconfig eth0 192.168.0.123 netmask 255.255.255.0 || true
then it will boot up with that as the static IP address even if you don't have anything plugged it.

Convert hexadecimal to octet format

the ifconfig output of my machine is the following:
:~ shell$ ifconfig en0
en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
ether 00:25:bc:e7:39:6a
inet6 fe80::225:bcff:fee7:396a%en0 prefixlen 64 scopeid 0x4
inet 10.170.133.45 netmask 0xffffff00 broadcast 10.170.133.255
media: autoselect (100baseTX <full-duplex>)
status: active
I know that netmask 0xffffff00 is the hexadecimal representation of 255.255.255.0.
But which is the correct way of converting that string into the octet format?
Can you suggest some unix command or shell script?
say you already gotten that hex number into a variable
$ s="0xffffff00"
$ p="${s:0:2}"
$ printf "%d.%d.%d.%d\n" ${p}${s:2:2} ${p}${s:4:2} ${p}${s:6:2} ${p}${s:8:2}
255.255.255.0

Resources