# Linux: Network tips

# Linux network tips

## Set a static IP

### On CentOS 7

For this example I ran a virtual machine on VirtualBox and set only one network interface using NAT

1. Edit the config file that manage your network interface. In my case `ifcfg-ens33`, but to be sure use `ip addr show` command or `nmcli -p dev`
```bash
sudo vim /etc/sysconfig/network-scripts/ifcfg-ens33`
```
2. Add the following lines: 
```bash
BOOTPROTO=static
BROADCAST=192.168.1.255
IPADDR=192.168.1.60
NETMASK=255.255.255.0
NETWORK=192.168.1.0
```
3. Restart network service: 
```bash 
sudo service network restart
```

Another way is to edit the connection using:
```bash
nmtui
```
Which disply a minimum of graphical interface

### On Ubuntu 20.04

Here I also used a VM with VBox, with 2 network interfaces:

* enp0s3: NAT interface and dhcp protocol: 10.0.0.0/16 subnet
* enp0s8: Bridge interface: 192.168.0.0/24 subnet

Let's focus on the second

1. Edit the netplan configuration file
```bash
sudo vim /etc/netplan/01-network-manager-all.yaml
```

*note: config file name may change depending on your machine*

2. Edit the file
```
network:
  # 1st interface (NAT)
    ethernets:
      enp0s3:
        dhcp4: true

  # 2nd interface (Bridge)
      enp0s8:
        addresses: [192.168.0.10/24]
        gateway4: 192.168.0.254
        nameservers:
          addresses: [8.8.8.8, 8.8.8.4]
        dhcp4: false
        dhcp6: false
```

**Apply modification and restart service**
```
sudo netplan apply
systemctl restart systemd-networkd
```

# Route management

Show all route: 
```
route -n
```

Change default gateway: 
```
route add default gw 192.168.1.254 eth0
```

Add a Host to Host route: 
```
route add -host 192.168.1.10 gw 192.168.xx.xx
```

Add subnet: 
```
route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.xx.xx
```

`nmap` is very useful tool when dealing with network. Basically it scans interface and  you may need to use it carefully and only on your machine. [See documentation here](https://nmap.org/docs.html)
use it to scan your whole LAN
```
nmap -sP 192.168.1.0/24
```

## Configure a proxy

### In your bashrc

1. Edit your bashrc file:
```
vim ~/.bashrc
```
2. Add the following variable:
```
http_proxy="http://<user>:<password>@<proxy_addr>:<proxy_port>
https_proxy="https://<user>:<password>@<proxy_addr>:<proxy_port>
no_proxy="localhost, 192.168.*.*"
```

### For `apt`
*Note: for centos go to yum instead of apt*

1. Edit or create if absent: 
```
sudo vim /etc/apt/apt.conf
```
2. Add the following lines: 
```
Acquire::http::Proxy "http://<adress_proxy>:<port>";
Acquire::https::Proxy "https://<adress_proxy>:<port>";
```
