HOWTO: Setup an ethernet card from command line

1771

Back again with very few commands regarding ethernet configuration.
When you deal with ethernet cards and you don’t want to mess around with GUI stuff or utils you need to insert few basic commands to have your working network configuration.

When I’m in a totally new environment and I need to setup an ethernet card on the fly I’ve a script with few commands for it, I usually don’t setup a new profile for the card, expecially if I don’t need it or if it’s just a test for casual usage. Again, when dealing with Live CD distros designed for special security tasks (my fav is BackTrack šŸ™‚ ) I type these commands for getting everything done from the command line.
Let’s see something:

Setting up the hostname and DNS stuff

# Setup hostname
hostname mycomputerhostname

# Here's a DNS setting
echo "nameserver 1.2.3.4" > /etc/resolv.conf
# adding 1.2.3.4 as nameserver

When dealing with a live CD distro you don’t care about /etc/resolv.conf file but if you’re running from your own HD you maybe need to take a backup copy of the file before overriding it (something like cp resolv.conf resolv.backup.conf)

Now let’s flush the eth card and setup the new ip address (let’s assume we’ve a /dev/eth0 card to setup)

# Flush every network address for the device /dev/eth0
ip addr flush dev eth0

# Add a new IP address to /dev/eth0 (use the form you want, that's just mine)
ip addr add 192.168.0.2/24 dev eth0
# adding 192.168.0.2, subnet mask (255.255.255.0) to my eth card

# Let's bring it up and running
ip link set eth0 up

# Now add a default route, 192.168.0.1 in the example
ip route add default via 192.168.0.1

And that’s it !
Here’re some basic settings for it, you can compose this material in a script like I did; again, no rocket science here but a very quick howto for command line fans

Next Step: Quick ‘n’ dirty networking commands

Hope it helps newbies and friends
Glad to read your comments

 

Andrea (Ben) Benini