Post

Linux - network

network


test: create a router in Ubuntu

network in linux

DHCP provides an IP address right away every time, it may provide a different IP address every time. So, need to configure the IP address from the command line to ensure to get the same IP address every time we try to access that server.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

$ ifconfig
eth0      Link encap:Ethernet  HWaddr 0a:c2:aa:b6:79:bc
          inet addr:172.31.41.71  Bcast:172.31.47.255  Mask:255.255.240.0
          inet6 addr: fe80::8c2:aaff:feb6:79bc/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:9001  Metric:1
          RX packets:577013 errors:0 dropped:0 overruns:0 frame:0
          TX packets:190297 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:814902292 (814.9 MB)  TX bytes:15308098 (15.3 MB)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:208 errors:0 dropped:0 overruns:0 frame:0
          TX packets:208 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1
          RX bytes:15296 (15.2 KB)  TX bytes:15296 (15.2 KB)


// change the system
$ sudo ifconfig eth0 172.31.41.71 mask 255.255.240.0


$ route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         ip-172-31-32-1. 0.0.0.0         UG    0      0        0 eth0
172.31.32.0     *               255.255.240.0   U     0      0        0 eth0


// change the route
$ route add 172.31.32.1 eth0


information saved in /etc/sysconfig/network-scripts/ifcfg-eth0
vi /etc/sysconfig/network-scripts/ifcfg-eth0
// add the line



Manually adding Routes

1
2
3
4
5
6
7
8
9
10
11
12
13
Default routes (gateways):
    route add default gw 10.0.0.1
    // Statically routes subnet mask 0.0.0.0 or /0 to 10.0.0.1

Static routes:
    route add -net 10.0.12.0 netmask 255.255.254.0 gw 10.0.0.106 dev eth0


Persisting Static Routes

    vi /etc/network/interface
    up route add net 10.0.13.0/24 gw 10.0.12.137
// apply a static route and route all 10.0.13.0 traffic the router sees to 10.0.12.137

to check Routes

1
2
apt-get install traceroute
traceroute www.goole.com

setup networking on Ubuntu

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
1. Must have a network interface

2. to list modules inserted into the kernel

    lsmod
    /etc/modules  file containing modules at boot time
    /etc/modprobe.d  config files for modules


3. to determine interface names and link availability

$ dmesg
[    0.000000] Initializing cgroup subsys cpuset
[    0.000000] Initializing cgroup subsys cpu
[    0.000000] Initializing cgroup subsys cpuacct
[    0.000000] Linux version 4.4.0-1101-aws (buildd@lgw01-amd64-040) (gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.12) ) #112-Ubuntu SMP Thu Jan 9 11:27:02 UTC 2020 (Ubuntu 4.4.0-1101.112-aws 4.4.208)
[    0.000000] Command line: BOOT_IMAGE=/boot/vmlinuz-4.4.0-1101-aws root=LABEL=cloudimg-rootfs ro console=tty1 console=ttyS0 nvme.io_timeout=4294967295 nvme_core.io_timeout=4294967295
[    0.000000] KERNEL supported cpus:
[    0.000000]   Intel GenuineIntel
[    0.000000]   AMD AuthenticAMD
[    0.000000]   Centaur CentaurHauls
[    0.000000] x86/fpu: xstate_offset[2]:  576, xstate_sizes[2]:  256


4. ifconfig -a
5. Looking in /dev for stuff that looks right

add a machine to a network?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
1. Assign a unique IP
2. Configure host to boot up with ip address
3. Add "default routes": Allows it access to the internet

4. Add a "DNS server’s" IP to the host

    vi /etc/resolv.conf
    nameserver 10.0.0.1

    bob@ip-172-31-41-71:~$ vi /etc/resolv.conf
    # Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
    #     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
    nameserver 172.31.0.2
    search us-east-2.compute.internal

Manually assigning IP address

1
2
3
4
5
6
Quickly configuring IP
    ifconfig eth0 10.0.0.2 netmask 255.255.255.0 up
    route add default gw 10.0.0.1

// Route command adds a default static route
// Routes subnet mask 0.0.0.0 to 10.0.0.1

Permanently adding IP address

1
2
3
4
5
6
7
8
9
10
11
12
Edit /etc/network/interface and add:

vi /etc/network/interface
// add
auto eth0
iface eth0 inet static // can be static or dhcp
address 10.0.0.2
netmask 255.255.255.0  // this is a /24
gateway 10.0.0.1       // default gateway (optional)

// gateway is a static route for 0.0.0.0
// gateway must exist on your local subnet

Bring interface online

1
2
3
4
5
6
7
/etc/init.d/networking restart
Restarting networking can cause all adapters to restart
Consider using nohup is connected remotely

    sudo nohup /etc/init.d/networking restart

ifup eth0

create a router in Ubuntu

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Add two network interfaces
Configure them
Enable Routing

1. Edit /etc/network/interface and add:

vi /etc/network/interface

auto eth0
iface eth0 inet static
address 10.0.0.106
netmask 255.255.255.0
gateway 10.0.0.1

auto eth1
iface eth1 inet static
address 10.0.12.1
netmask 255.255.255.0


2. Edit /etc/sysctl.conf and uncomment line to:


net.ipv4.ip_forward=1


.

This post is licensed under CC BY 4.0 by the author.

Comments powered by Disqus.