# Any way to provide network setting via kernel cmdline?

## bastibasti

Hi,

Is there any way to provide network config via cmdline? for example eth0=192.168.1.10  etc??

Im using the stock network (net.lo) script

----------

## charles17

Try ifconfig or ip address.

----------

## cboldt

Nothing in /usr/src/linux/Documentation/kernel-parameters.txt

But that file does refer the reader to driver files for network interface hardware.

----------

## eccerr0r

There is a kernel option:

ip=ipaddress:optional_nfs_root_ipaddress:gateway_ipaddress:netmask:hostname:ether_device:off:dns0_addr:dns1_addr

so something like... 

ip=124.22.114.26::124.22.114.1:255.255.255.0:myhostname:eth0:off:8.8.8.8:4.4.4.4

Note: this is a guess, I've never actually used it.  Reference located in: $KERNEL/Documentation/filesystems/nfs/nfsroot.txt

----------

## krinn

as easy as

ifconfig eth0 192.168.0.4

route add default gw 192.168.0.100

or even easier "dhcpcd eth0"

----------

## khayyam

 *bastibasti wrote:*   

> Is there any way to provide network config via cmdline? for example eth0=192.168.1.10  etc?? Im using the stock network (net.lo) script

 

bastibasti ... basically all you need do is bring the interface up, add an ip/route, and configure dns resolution:

```
# ip link set dev eth0 up

# ip addr add 192.168.1.10/24 brd + dev eth0

# ip route add default via 192.168.1.254

# echo "nameserver 192.168.1.254" >> /etc/resolv.conf
```

However, this will not provide the 'net' target (because you're configuring network outside of openrc/netifrc), so should something 'need net' some service (ie, net.eth0, or dhcpcd) will be started that 'provide[s] net'. For that reason you are better off doing the following:

```
config_eth0="192.168.1.10/24"

routes_eth0="default via 192.168.1.254"
```

```
# ln -s /etc/init.d/net.lo /etc/init.d/net.eth0

# /etc/init.d/net.eth0 start
```

You might find net.eth0 is started automatically at boot, prehaps due to 'hotplug', or because some service in the 'default' runlevel 'need[s] net', if you don't want this (which, I assume is your reason for asking the above) you could do the following:

```
rc_dhcpcd_provide="!net"

rc_sshd_need="!net"
```

EDIT: just re-read the subject line, so ignore the above. All you need to is tell init what runlevel it should start by adding 'softlevel=<runlevel>', and if 'net.eth0' is in that runlevel it will be started.

HTH & best ... khayLast edited by khayyam on Tue Feb 07, 2017 9:09 am; edited 1 time in total

----------

## NeddySeagoon

bastibasti,

What do you really want to do?

Tell us your problem rather than your perceived solution.

If you want to mount root over the network, for a diskless system, eccerr0r is pointing the way.

Check the syntax, its a long time since I've used it.

You still need to start networking with all the same parameters during boot or things that depend on networking won't start.

Then there is the network console. Its not secure, so don't use it over a hostile network.

You need a kernel option or two and its documented in /usr/scr/linux/Documentation  

You still need to start networking normally too.

You can put anything you want on the kernel command line and parse it yourself out of /proc/cmdline.

Just make sure nothing else grabs it.

bastibasti_ip=

bastibasti_netmask=

and so on.

You need to write the script to parse /proc/cmdline and do whatever you want with it.

As a worked example, see how the init script deals with root=

----------

## bastibasti

as stated in the thread title I'm talking about kernel cmdline, not bash 

I know howto bring up the network on a console.

```
You can put anything you want on the kernel command line and parse it yourself out of /proc/cmdline.

Just make sure nothing else grabs it.

bastibasti_ip=

bastibasti_netmask=

and so on. 
```

that was my intention. I wanted to know whether the stock rc scripts already include any functionality to parse the cmdline.

Seems like no. I use following script to run from rc.local

#netip_eth0=dhcp  <- configure eth0 using dhcp

#netip_eth0=192.168.1.10,255.255.255.0,192.168.1.1,192.168.1.1

#               dev  IP           MASK          DefaultGW   Nameserver

```

COMMANDLINE="/proc/cmdline"

##Get number of kernel arguments

num_args=$(awk '{print NF}' "$COMMANDLINE" | sort -nu | tail -n 1)

cmdline=$(cat "$COMMANDLINE")

#netip_eth0=192.168.1.10,255.255.255.0,192.168.1.1,192.168.1.1

#ip,mask,gw,nameserver

##Parse for settings

for ((coloumn=1;coloumn<=$num_args;coloumn++)); do

#get argument

arg=$(echo "$cmdline"|cut -f$coloumn -d ' ')

arg1=$(echo "$arg"|cut -f1 -d '=')

arg2=$(echo "$arg1"|cut -f1 -d '_')

if [ $arg2 == "netip" ]; then

interface=$(echo "$arg1"|cut -f2 -d '_')

val=$(echo "$arg"|cut -f2 -d '=')

ip=$(echo "$val"|cut -f1 -d ',')

mask=$(echo "$val"|cut -f2 -d ',')

gw=$(echo "$val"|cut -f3 -d ',')

nameserver=$(echo "$val"|cut -f4 -d ',')

if [ $ip == "dhcp" ]; then

echo "Network settings for ""$interface"" found in kernel command line."

echo "Calling DHCP, as requested"

dhcpcd "$interface"

fi

if [ $ip != "dhcp" ]; then

echo "Network settings for ""$interface"" found in kernel command line."

echo "Applying following settings:"

echo "IP: ""$ip"

echo "MASK: ""$mask"

echo "GATEWAY: ""$gw"

echo "NAMESERVER: ""$nameserver"

ifconfig "$interface" up

sleep 1

ifconfig "$interface" "$ip" netmask "$mask"

sleep 1

route add default gw "$gw"

echo "nameserver ""$nameserver" >>/etc/resolv.conf

fi

echo

fi

done

```

----------

## khayyam

 *bastibasti wrote:*   

> that was my intention. I wanted to know whether the stock rc scripts already include any functionality to parse the cmdline. Seems like no.

 

bastibasti ... all you need do is create a runlevel, and provide 'softlevel=<runlevel>' on the kernel command line:

```
# mkdir /etc/runlevels/online

# rc-update -s add default online # the '-s' for 'stacked' runlevel

# rc-update add net.eth0 online
```

... and then as a kernel parameter: 'softlevel=online'.

best ... khay

----------

## bastibasti

mmhh maybe i do need to explain what this is for. Sorry. My mistake on trying to focus on the problem but when youre focussing on an issue you dont see that more info is needed. 

The rootfs is a ramdisk I dont want to change to ramdisk for changing the network settings.

Instead want to edit the network setting from the boot loader. It uses openrc and gentoo as a base including all other scripts for booting. My script works nicely in the ramdisk now. All I thought was that I wanted to prevent using another script if this functionality was already included in the gentoo net.lo init script

----------

## NeddySeagoon

bastibasti,

The net.lo init script includes four hooks. Pre/Post UP and Pre/Post down.

They are documented at the end of /usr/share/doc/netifrc-*/net.example.bz2 

I guess using preup is just a different way of doing what you are already doing.

----------

## khayyam

 *bastibasti wrote:*   

> The rootfs is a ramdisk I dont want to change to ramdisk for changing the network settings. Instead want to edit the network setting from the boot loader. It uses openrc and gentoo as a base including all other scripts for booting. My script works nicely in the ramdisk now. All I thought was that I wanted to prevent using another script if this functionality was already included in the gentoo net.lo init script

 

bastibasti ... I see, the problem I see with this is that your script (run from local.d) will not 'provide net', you are better to use 'preup()' (in /etc/conf.d/net) and parse those values from /proc/cmdline to provide values for 'config_${IFACE}',  etc.

best ... khay

----------

