Conceitos Básicos de Rede# Modelo TCP/IP# A T I E p r n n l a t l i n e a c s r c a p n e ç o e ã r t o t e → → → → H T I E T C P t T P , h P , e , I r U C n S D M e S P P t H , , W F i T - P F , i D N S
Endereçamento IP# IPv4 : 192.168.1.100
Classes: A, B, C (privadas) Máscara de sub-rede: 255.255.255.0 (/24) IPv6 : 2001:0db8:85a3::8a2e:0370:7334
Endereços de 128 bits Futuro da internet Portas Comuns# 2 8 4 2 2 5 3 5 2 0 4 1 5 3 3 4 3 0 3 6 2 → → → → → → → → S H H F S D M P S T T T M N y o H T T P T S S s P P P Q t S L g ( r e e m S a Q i L l )
Comandos de Diagnóstico# ip - Gerenciamento de Rede Moderno# 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Ver todas interfaces
ip addr show
ip a
# Ver interface específica
ip addr show eth0
# Ver rotas
ip route show
ip r
# Ver tabela ARP
ip neigh show
# Estatísticas de interface
ip -s link show eth0
ifconfig - Comando Legado (ainda útil)# 1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Ver todas interfaces
ifconfig
# Ver interface específica
ifconfig eth0
# Ativar interface
sudo ifconfig eth0 up
# Desativar interface
sudo ifconfig eth0 down
# Configurar IP temporário
sudo ifconfig eth0 192.168.1.100 netmask 255.255.255.0
ping - Testar Conectividade# 1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Ping básico
ping google.com
# Limitar número de pacotes
ping -c 4 google.com
# Intervalo entre pacotes
ping -i 2 google.com
# Ping IPv6
ping6 google.com
# Ping com timestamp
ping -D google.com
traceroute - Rastrear Rota# 1
2
3
4
5
6
7
8
9
10
11
# Instalar
sudo apt install traceroute
# Rastrear rota
traceroute google.com
# Usar ICMP em vez de UDP
sudo traceroute -I google.com
# Limitar hops
traceroute -m 15 google.com
netstat - Estatísticas de Rede# 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Todas conexões
netstat -a
# Portas em escuta
netstat -l
# Portas TCP em escuta
netstat -lt
# Portas UDP em escuta
netstat -lu
# Mostrar PIDs
sudo netstat -tulpn
# Estatísticas de interface
netstat -i
# Tabela de roteamento
netstat -r
ss - Socket Statistics (substitui netstat)# 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Todas conexões
ss -a
# Portas em escuta
ss -l
# TCP em escuta com PIDs
sudo ss -tlnp
# UDP em escuta
sudo ss -ulnp
# Conexões estabelecidas
ss -t state established
# Estatísticas resumidas
ss -s
nmap - Network Scanner# 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Instalar
sudo apt install nmap
# Scan básico
nmap 192.168.1.1
# Scan de rede
nmap 192.168.1.0/24
# Scan de portas específicas
nmap -p 22,80,443 192.168.1.1
# Scan completo
sudo nmap -A 192.168.1.1
# Detectar SO
sudo nmap -O 192.168.1.1
# Scan rápido
nmap -F 192.168.1.1
dig - DNS Lookup# 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Query DNS
dig google.com
# Query específico (A, MX, NS)
dig google.com A
dig google.com MX
dig google.com NS
# Servidor DNS específico
dig @8.8.8.8 google.com
# Resposta curta
dig +short google.com
# Reverse DNS
dig -x 8.8.8.8
nslookup - DNS Query# 1
2
3
4
5
6
7
8
9
10
11
# Query básico
nslookup google.com
# Servidor DNS específico
nslookup google.com 8.8.8.8
# Modo interativo
nslookup
> server 8.8.8.8
> google.com
> exit
host - DNS Lookup Simples# 1
2
3
4
5
6
7
8
9
# Query básico
host google.com
# Tipo específico
host -t MX google.com
host -t NS google.com
# Reverse lookup
host 8.8.8.8
Configuração de Rede# NetworkManager (Desktop)# 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
# Status
nmcli general status
# Ver conexões
nmcli connection show
# Ver dispositivos
nmcli device status
# Conectar Wi-Fi
nmcli device wifi list
nmcli device wifi connect SSID password SENHA
# Configurar IP estático
nmcli connection modify eth0 \
ipv4.addresses 192.168.1.100/24 \
ipv4.gateway 192.168.1.1 \
ipv4.dns "8.8.8.8 8.8.4.4" \
ipv4.method manual
# Aplicar mudanças
nmcli connection up eth0
# DHCP
nmcli connection modify eth0 ipv4.method auto
nmcli connection up eth0
Netplan (Ubuntu 18.04+)# 1
2
# Arquivo de configuração
sudo vim /etc/netplan/01-netcfg.yaml
DHCP:
1
2
3
4
5
6
network :
version : 2
renderer : networkd
ethernets :
eth0 :
dhcp4 : true
IP Estático:
1
2
3
4
5
6
7
8
9
10
11
12
network :
version : 2
renderer : networkd
ethernets :
eth0 :
addresses :
- 192.168.1.100 /24
gateway4 : 192.168.1.1
nameservers :
addresses :
- 8.8.8.8
- 8.8.4.4
Aplicar:
/etc/network/interfaces (Debian/Ubuntu antigo)# 1
sudo vim /etc/network/interfaces
DHCP:
a i u f t a o c e e t e h t 0 h 0 i n e t d h c p
IP Estático:
a i u f t a o c e a n g d e d e a n t e d t t s h t r m e - 0 h e a w n 0 s s a a s k y m i e n 1 2 1 s e 9 5 9 e t 2 5 2 r . . . v s 1 2 1 e t 6 5 6 r a 8 5 8 s t . . . i 1 2 1 8 c . 5 . . 1 5 1 8 0 . . 0 0 8 . 8 8 . 8 . 4 . 4
Reiniciar:
1
sudo systemctl restart networking
systemd-networkd (Servidor)# 1
2
# Arquivo de configuração
sudo vim /etc/systemd/network/20-wired.network
Conteúdo:
1
2
3
4
5
6
7
8
[Match]
Name = eth0
[Network]
Address = 192.168.1.100/24
Gateway = 192.168.1.1
DNS = 8.8.8.8
DNS = 8.8.4.4
Habilitar:
1
2
sudo systemctl enable systemd-networkd
sudo systemctl start systemd-networkd
DNS# Configurar Servidores DNS# Método 1: /etc/resolv.conf
1
sudo vim /etc/resolv.conf
n n n a a a m m m e e e s s s e e e r r r v v v e e e r r r 8 8 1 . . . 8 8 1 . . . 8 4 1 . . . 8 4 1
Método 2: systemd-resolved
1
sudo vim /etc/systemd/resolved.conf
1
2
3
[Resolve]
DNS = 8.8.8.8 8.8.4.4
FallbackDNS = 1.1.1.1
1
sudo systemctl restart systemd-resolved
/etc/hosts - Resolução Local# 1 1 1 2 9 9 7 2 2 . . . 0 1 1 . 6 6 0 8 8 . . . 1 1 1 . . 1 2 0 0 l s w o e e c r b a v . l i l h d o o o c s r a t . l l o c a l s e r v i d o r
SSH - Secure Shell# Cliente SSH# Gerar Chaves SSH# 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Gerar par de chaves
ssh-keygen -t ed25519 -C "[email protected] "
# Ou RSA 4096
ssh-keygen -t rsa -b 4096 -C "[email protected] "
# Chaves ficam em:
# ~/.ssh/id_ed25519 (privada)
# ~/.ssh/id_ed25519.pub (pública)
# Permissões corretas
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub
chmod 644 ~/.ssh/authorized_keys
Configurar Servidor SSH# 1
2
3
4
5
# Instalar
sudo apt install openssh-server
# Configuração
sudo vim /etc/ssh/sshd_config
Configurações importantes:
P P P P X A o e a u 1 l r r s b 1 l t m s k F o i w e o w 2 t o y r U 2 R r A w s o d u a e o A t r r t u h d s L t e i o h n n u g e t g s i n i u n t c n a i a o r n c t i o a i o t o 1 i n o u n y s e u n s a o r i o 2
Reiniciar:
1
sudo systemctl restart sshd
SSH Config (~/.ssh/config)# H H H H o o o o s s s s t t t t H U P I U F H U H U P s o s o d s o b o s i o s r e s e r e . e r a s e n s e o r t r t n e r w s t r t t r x v N t m a t N e N y i a u 2 i p a r i a a r a r J d m s 2 t r d d o m d n m o u o e u 2 y e m A n e m o e o m r a 2 F s i g i t p s r i a n e b n 1 e i l . n a 0 b r o e c t s . a v o t 0 s i ~ m y i . t d / e o 1 i o . s n . o r s . 1 n . s e 0 c h m 0 o / p m i r d e _ s e a d . 2 c 5 o 5 m 1 9
Usar:
1
2
ssh servidor
ssh interno # Conecta via bastion
SCP e RSYNC - Transferência de Arquivos# scp - Secure Copy# rsync - Sincronização Eficiente# 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Sincronizar diretório
rsync -av origem/ destino/
# Sincronizar via SSH
rsync -av origem/ [email protected] :/destino/
# Mostrar progresso
rsync -av --progress origem/ destino/
# Deletar arquivos no destino que não existem na origem
rsync -av --delete origem/ destino/
# Excluir arquivos
rsync -av --exclude= '*.log' origem/ destino/
# Dry run (simular)
rsync -av --dry-run origem/ destino/
# Backup incremental
rsync -av --link-dest= /backup/anterior origem/ /backup/novo/
Firewall# UFW - Uncomplicated Firewall (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
27
28
29
30
31
32
33
34
35
# Instalar
sudo apt install ufw
# Status
sudo ufw status
# Habilitar
sudo ufw enable
# Desabilitar
sudo ufw disable
# Permitir porta
sudo ufw allow 22
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
# Permitir de IP específico
sudo ufw allow from 192.168.1.100
# Permitir porta de IP específico
sudo ufw allow from 192.168.1.100 to any port 22
# Negar porta
sudo ufw deny 23
# Deletar regra
sudo ufw delete allow 80
# Resetar firewall
sudo ufw reset
# Regras numeradas
sudo ufw status numbered
sudo ufw delete 2
firewalld (Fedora/RHEL/CentOS)# 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
# Status
sudo firewall-cmd --state
# Zonas
sudo firewall-cmd --get-zones
sudo firewall-cmd --get-default-zone
# Listar regras
sudo firewall-cmd --list-all
# Permitir serviço
sudo firewall-cmd --add-service= http --permanent
sudo firewall-cmd --add-service= https --permanent
# Permitir porta
sudo firewall-cmd --add-port= 8080/tcp --permanent
# Remover porta
sudo firewall-cmd --remove-port= 8080/tcp --permanent
# Recarregar
sudo firewall-cmd --reload
# Zona específica
sudo firewall-cmd --zone= public --add-service= ssh --permanent
iptables - Firewall de Baixo Nível# 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Listar regras
sudo iptables -L -n -v
# Permitir porta
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
# Bloquear IP
sudo iptables -A INPUT -s 192.168.1.100 -j DROP
# Permitir conexões estabelecidas
sudo iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# Bloquear tudo exceto regras
sudo iptables -P INPUT DROP
sudo iptables -P FORWARD DROP
sudo iptables -P OUTPUT ACCEPT
# Salvar regras
sudo iptables-save > /etc/iptables/rules.v4
# Restaurar regras
sudo iptables-restore < /etc/iptables/rules.v4
VPN# OpenVPN Cliente# 1
2
3
4
5
6
7
8
9
10
# Instalar
sudo apt install openvpn
# Conectar
sudo openvpn --config cliente.ovpn
# Como serviço
sudo cp cliente.ovpn /etc/openvpn/cliente.conf
sudo systemctl start openvpn@cliente
sudo systemctl enable openvpn@cliente
WireGuard# 1
2
3
4
5
6
7
8
# Instalar
sudo apt install wireguard
# Gerar chaves
wg genkey | tee privatekey | wg pubkey > publickey
# Configuração
sudo vim /etc/wireguard/wg0.conf
1
2
3
4
5
6
7
8
9
[Interface]
PrivateKey = CHAVE_PRIVADA
Address = 10.0.0.2/24
[Peer]
PublicKey = CHAVE_PUBLICA_SERVIDOR
Endpoint = servidor.com:51820
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25
1
2
3
4
5
6
7
8
9
10
11
# Iniciar
sudo wg-quick up wg0
# Parar
sudo wg-quick down wg0
# Status
sudo wg show
# Habilitar no boot
sudo systemctl enable wg-quick@wg0
Troubleshooting de Rede# Sem Conectividade# 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 1. Verificar interface
ip link show
# 2. Verificar IP
ip addr show
# 3. Verificar gateway
ip route show
# 4. Testar gateway
ping 192.168.1.1
# 5. Testar DNS
ping 8.8.8.8
# 6. Testar resolução DNS
ping google.com
# 7. Verificar DNS
cat /etc/resolv.conf
Porta não Acessível# 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Verificar se serviço está rodando
sudo systemctl status nginx
# Verificar se porta está em escuta
sudo ss -tlnp | grep :80
# Verificar firewall
sudo ufw status
sudo firewall-cmd --list-all
# Testar localmente
curl localhost:80
# Testar remotamente
telnet servidor.com 80
nc -zv servidor.com 80
DNS não Resolve# 1
2
3
4
5
6
7
8
9
10
11
12
# Testar DNS
dig google.com
nslookup google.com
# Verificar /etc/resolv.conf
cat /etc/resolv.conf
# Testar DNS alternativo
dig @8.8.8.8 google.com
# Limpar cache DNS
sudo systemd-resolve --flush-caches
Lentidão de Rede# 1
2
3
4
5
6
7
8
9
10
11
12
# Testar velocidade
speedtest-cli
# Verificar latência
ping -c 100 google.com | tail -1
# Verificar perda de pacotes
mtr google.com
# Verificar uso de banda
sudo apt install iftop
sudo iftop -i eth0
Monitoramento de Rede# iftop - Uso de Banda em Tempo Real# 1
2
sudo apt install iftop
sudo iftop -i eth0
nethogs - Uso por Processo# 1
2
sudo apt install nethogs
sudo nethogs eth0
vnstat - Estatísticas de Tráfego# 1
2
3
4
5
6
7
8
9
10
11
sudo apt install vnstat
# Habilitar
sudo systemctl enable vnstat
sudo systemctl start vnstat
# Ver estatísticas
vnstat
vnstat -d # Diário
vnstat -m # Mensal
vnstat -h # Por hora
Comandos de Referência Rápida# 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
# Ver IPs
ip addr show
# Ver rotas
ip route show
# Testar conectividade
ping google.com
# Portas em escuta
sudo ss -tlnp
# DNS lookup
dig google.com
# Conectar SSH
ssh [email protected]
# Copiar arquivo
scp arquivo.txt usuario@servidor:/caminho/
# Sincronizar diretório
rsync -av origem/ destino/
# Firewall (Ubuntu)
sudo ufw allow 80
# Firewall (Fedora)
sudo firewall-cmd --add-port= 80/tcp --permanent
Próximo capítulo : 7 - Uso Avançado do Terminal e Scripts
Capítulo anterior : 5 - Gerenciamento de Pacotes