How to setup a static IP for network-manager in Virtual Box on Ubuntu Server
Solution 1
You need to use Network Manger from the command line, this is nmcli.
First, you can list the available connections Network Manager knows about with the following, this is important to find the name, as the device id isn't used:
# nmcli con show
This will give you something like:
NAME UUID TYPE DEVICE
Wired connection 1 7a3b674a-f346-3cfb-8b30-ff70e6db1b60 802-3-ethernet enp0s3
You can then modify the connection with the following:
nmcli con mod "Wired connection 1"
ipv4.addresses "HOST_IP_ADDRESS/IP_NETMASK_BIT_COUNT"
ipv4.gateway "IP_GATEWAY"
ipv4.dns "PRIMARY_IP_DNS,SECONDARY_IP_DNS"
ipv4.dns-search "DOMAIN_NAME"
ipv4.method "manual"
When you enter the above use one line, I've just split it into separate lines to make it more clear.
If you want to set the connection to use DHCP, you can use the following:
nmcli con mod "Wired connection 1"
ipv4.addresses ""
ipv4.gateway ""
ipv4.dns ""
ipv4.dns-search ""
ipv4.method "auto"
You need all the empty quotes as they remove any settings they previously have.
To add a network, use:
nmcli con add ...
With similar parameters.
To make the settings active, reboot. (I tried re-starting Network Manager, but that didn't seem to activate the changes, but a reboot did.)
Solution 2
For those who want the NetworkManager approach, I just went through this, taking the tack mss suggested. There's a touch of information on the Debian wiki and full documentation of the options at the GNOME developer site. (From the RHEL7 docs, it does look like their version of nmcli
has add support, so hopefully that'll make it in.)
The dynamic IP is pretty simple (just doing the network config, mind, not the VBox side):
[802-3-ethernet]
auto-negotiate=true
mac-address=XX:XX:XX:XX:XX:XX
[connection]
id=Wired connection 1
uuid=xxx-xxxxxx-xxxxxx-xxxxxx-xxx
type=802-3-ethernet
timestamp=0
[ipv6]
method=disabled
[ipv4]
method=auto
Use uuidgen
(package uuid-runtime
) to make the uuid, and of course fill the MAC address properly. (Usually better to do that than to specify a device name.)
For the static IP (note the semicolon on the DNS array!):
[802-3-ethernet]
auto-negotiate=true
mac-address=XX:XX:XX:XX:XX:XX
[connection]
id=Wired connection 2
uuid=xxx-xxxxxx-xxxxxx-xxxxxx-xxx
type=802-3-ethernet
timestamp=0
[ipv6]
method=ignore
[ipv4]
method=manual
dns=8.8.8.8;8.8.4.4;
address1=192.168.56.101/24,192.168.1.1
Solution 3
Normally I edit the file /etc/network/interfaces
and mod the info to something like this:
iface eth0 inet static
address 192.168.56.101
netmask 255.255.255.0
gateway 192.168.56.1 (u had 102.168.1.1. So, I guessed it was a mistake)
Solution 4
Currently Ubuntu versions has Network Manager enabled.
You can get the connection name with the command:
nmcli con show
You can modify your static ip address from command line:
sudo nmcli con mod "Connection name" ipv4.addresses xxx.xxx.xxx.xxx/24
Note: /24 defines the network mask to 255.255.255.0
File /ect/network/interfaces
is ignored if you have the property managed=false
on /etc/NetworkManager/NetworkManager.conf
[ifupdown]
managed=**false**
Solution 5
Edit:
To add another interface that can access the internet in Vbox use the network tab of the settings menu. Then enable an additional interface and set it to NAT (as that's the easiest way to get to the internet). And in the wirtual machine add another interface with dhcp like so:
allow-hotplug eth1
and after a linebreak iface eth1 inet dhcp
(For some reason my formatting went bad.)
And this way you can access the net using the host OS's connection.
The commandline "client" of network-manager is confusing and pretty much useless when compared to the standard way of managing interfaces under Linux (and most other unixes). Normally you would first take down the already enabled network-interface (if its enbled) {run ifconfig to find out}:
ifconfig eth0 down #note: I assume you want to configure eth0, replace it if not
After that edit the /etc/network/interfaces file (I prefer to use nano):
sudo nano /etc/network/interfaces
And add a section along the lines of:
allow-hotplug eth0
iface eth0 inet static
address 192.168.56.101
netmask 255.255.255.0
gateway 192.168.56.1
Also be aware that you need to remove every other presence of eth0 from the file othervise the system won't be able to parse the file correctly and you won't have a network connection. And finally you'll need to re-enable the interface:
ifup eth0
And this should cause your ubuntu to automatically set up the interface with your desired parameters whenever it detects that the "cable is connected".
Also if you really want to avoid editing the interfaces file you can use:
ifconfig eth0 192.168.56.101 netmask 255.255.255.0 gateway 192.168.56.1 up
I think that this way the changes won't be permanent (you'll lose them between reboots, etc.), but I hadn't tested that myself.
Related videos on Youtube
davioooh
Updated on September 18, 2022Comments
-
davioooh over 1 year
I need a
Map<String,Integer>
to store a binding between a set o strings and ints, for example:"one" <-> 1 "two" <-> 2 "three" <-> 3
and in particular I need to use both String values and int values as key to access this map. I mean:
get("one")
returns1
andget(1)
returns"one"
.What's the best way to achieve this? is there some Map implementation that can help me?
-
Marco13 almost 10 yearsGuava BiMap should do the job: code.google.com/p/guava-libraries/wiki/…
-
TheLostMind almost 10 yearsYes..
BiMap
is the way to go -
Vahid almost 10 years
-
davioooh almost 10 years@Marco13 Thank you, please write your answer so I can pick it up.
-
Marco13 almost 10 yearsThere are hundreds of questions like this. First websearch result: stackoverflow.com/questions/9783020/bidirectional-map Upvote whatever you want to upvote, a link is not really an "answer" in the stackoverflow sense...
-
-
guntbert over 11 yearsThe OP said, he didn't want to touch /etc/network/interfaces.
-
guntbert over 11 yearsYou could have told about the command
ip
-ifconfig
is on the decline. -
Josh almost 11 yearsYou're right I would like to do it trough network manager. But since the documentation of this lacks touching the interfaces file will be ok.
-
Jack Miner Ewes over 5 yearsThe type flag wasn't obvious to me. I set up
eth1
as the Internal Network interface using static IP.sudo nmcli con add type "ethernet" ifname "eth1" con-name "intnet" autoconnect "yes" ip4 "192.168.0.1/24" gw4 "192.168.0.2"
-
Aaron Lelevier over 5 yearsThis is the directory where the file lives show above
/etc/NetworkManager/system-connections/
-
Josh about 4 yearsThats right. I said So. In the meantime I need a solution for netplan. Does the nmcli command below work when netplan is used, too
-
Josh about 4 yearsIs it compatible with netplan?