Wiki do Tiozão do Linux

Ajustes Iniciais Ubuntu

Personalizando seu Ubuntu

Ajustes Iniciais

Ambiente Utilizado: Ubuntu Nuvem disponível aqui

Adicionar idioma pt_BR

apt -y install locales

locale-gen pt_BR.UTF-8 en_US.UTF-8
Generating locales (this might take a while)...
  en_US.UTF-8... done
  pt_BR.UTF-8... done
Generation complete.

Ajustar timezone

# Listar timezones do Brasil
timedatectl list-timezones | grep -E '(Campo_Grande|Sao_Paulo|Porto_Velho|Cuiaba|Santarem|Rio_Branco|Manaus|Boa_Vista)'
America/Boa_Vista
America/Campo_Grande
America/Cuiaba
America/Manaus
America/Porto_Velho
America/Rio_Branco
America/Santarem
America/Sao_Paulo

# Definir seu timezone
timedatectl set-timezone America/Campo_Grande

Instalar pacotes adicionais

# Pacotes indispensáveis
apt -y install vim rsyslog nmap sysstat net-tools chrony tree rsync tmux htop

# Pacotes de compesssão 
apt -y install zip gzip bzip2 unzip

# Pacotes visualizadores de informações
apt -y install neofetch cpufetch boxes figlet toilet lolcat

# Pacotes que facilitam o dia-a-dia
apt -y install duf bash-completion command-not-found grc fzf ccze

# Habilitar o sar
sed -i 's/ENABLED="false"/ENABLED="true"/' /etc/default/sysstat
systemctl enable --now sysstat.service

Personalizar o prompt, alias, welcome e vim

Prompt PS1 personalizado

cat << '_EOF' > ~/.bashrc
# ~/.bashrc: executed by bash(1) for non-login shells.

export LS_OPTIONS='--color=auto'
eval "$(dircolors)"
alias ls='ls $LS_OPTIONS'
alias ll='ls $LS_OPTIONS -l'
alias l='ls $LS_OPTIONS -lA'

# Some more alias to avoid making mistakes:
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Alias extra definitions
if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

# Personalizar o 'history'
HISTCONTROL=ignoredups:ignorespace
shopt -s histappend
HISTSIZE=1000
HISTFILESIZE=2000
HISTTIMEFORMAT="%F %T "
export TERM=xterm

# Root Prompt
PS1='\[\e[1;31m\]\342\224\214\342\224\200\[\e[1;31m\][\[\e[1;33m\]\u\[\e[1;37m\]@\[\e[1;36m\]\h\[\e[1;31m\]]\[\e[1;31m\]\342\224\200\[\e[1;31m\][\[\e[1;33m\]\w\[\e[1;31m\]]\[\e[1;31m\]\342\224\200[\[\e[1;37m\]\d \t\[\e[1;31m\]]\n\[\e[1;31m\]\342\224\224\342\224\200\342\224\200\342\225\274\[\e[1;37m\] \$ \[\e[0m\]'

# Pesquisa CTRL+R mais inteligente
source /usr/share/doc/fzf/examples/key-bindings.bash
_EOF

Aliases Facilitadores Extras

cat << '_EOF' > ~/.bash_aliases
alias remove-comment="grep -v -E '^(#|$|[[:space:]])'"

alias ip='ip -c'
alias diff='diff --color'

alias tail='grc tail'
alias ping='grc ping'
alias ps='grc ps'
_EOF

Tela de apresentação

cat << '_EOF' > /etc/profile.d/zz-welcome.sh
echo -e "\n\tTiozão do Linux\n\ttwitter.com/TiozaoDoLinux\n\twiki.TiozaoDoLinux.com" | boxes -d dog -a c -s 80
_EOF

Resolve Problema do VIM com mouse

cat << '_EOF' > ~/.vimrc
" Disable vim automatic visual mode on mouse select
"
" issue: :set mouse-=a
" add to ~/.vimrc: set mouse-=a
"
"my ~/.vimrc for preserving global defaults and only changing one option:
"
" Referência: https://gist.github.com/u0d7i/01f78999feff1e2a8361
"
source $VIMRUNTIME/defaults.vim
set mouse-=a

set showmatch " Mostrar colchetes correspondentes
set ts=4 " Ajuste tab
set sts=4 " Ajuste tab
set sw=4 " Ajuste tab
set autoindent " Ajuste tab
set smartindent " Ajuste tab
set smarttab " Ajuste tab
set expandtab " Ajuste tab
set number " Mostra numero da linhas
_EOF

Como que fica após o login

ssh root@134.65.23.213

Welcome to Ubuntu 22.04.1 LTS (GNU/Linux 5.15.0-1027-oracle x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  System information as of Sat Feb  4 11:30:19 -04 2023

  System load:  0.36279296875     Processes:                119
  Usage of /:   8.9% of 96.73GB   Users logged in:          0
  Memory usage: 34%               IPv4 address for docker0: 172.17.0.1
  Swap usage:   0%                IPv4 address for ens3:    10.0.0.30

 * Strictly confined Kubernetes makes edge and IoT secure. Learn how MicroK8s
   just raised the bar for easy, resilient and secure K8s cluster deployment.

   https://ubuntu.com/engage/secure-kubernetes-at-the-edge

0 updates can be applied immediately.


Last login: Sat Feb  4 11:28:30 2023 from 177.17.196.23
                                      __   _,--="=--,_   __
                                     /  \."    .-.    "./  \
                                    /  ,/  _   : :   _  \/` \
                                    \  `| /o\  :_:  /o\ |\__/
                                     `-'| :="~` _ `~"=: |
                                        \`     (_)     `/ jgs
                                 .-"-.   \      |      /   .-"-.
        .-----------------------{     }--|  /,.-'-.,\  |--{     }----------------------.
         )                      (_)_)_)  \_/`~-===-~`\_/  (_(_(_)                     (
        (                                                                              )
         )                              Tiozão do Linux                               (
        (                          twitter.com/TiozaoDoLinux                           )
         )                           wiki.TiozaoDoLinux.com                           (
        '------------------------------------------------------------------------------'
┌──{root@ubuntu-04:~}─────{Sat Feb 04 11:30:20}───
└──{# landscape-sysinfo 
  System load:  0.1318359375      Processes:                117
  Usage of /:   8.9% of 96.73GB   Users logged in:          1
  Memory usage: 35%               IPv4 address for docker0: 172.17.0.1
  Swap usage:   0%                IPv4 address for ens3:    10.0.0.30
┌──{root@ubuntu-04:~}─────{Sat Feb 04 11:31:19}───
└──{# hostnamectl 
 Static hostname: ubuntu-04
       Icon name: computer-vm
         Chassis: vm
      Machine ID: c79a206c197b4f7ea4e4003163b84dde
         Boot ID: 03d96544219145e589735b127d0f673e
  Virtualization: kvm
Operating System: Ubuntu 22.04.1 LTS               
          Kernel: Linux 5.15.0-1027-oracle
    Architecture: x86-64
 Hardware Vendor: QEMU
  Hardware Model: Standard PC _i440FX + PIIX, 1996_
┌──{root@ubuntu-04:~}─────{Sat Feb 04 11:31:32}───
└──{# neofetch 
            .-/+oossssoo+/-.               root@ubuntu-04 
        `:+ssssssssssssssssss+:`           -------------- 
      -+ssssssssssssssssssyyssss+-         OS: Ubuntu 22.04.1 LTS x86_64 
    .ossssssssssssssssssdMMMNysssso.       Host: KVM/QEMU (Standard PC (i440FX + PIIX, 1996) pc-i440fx-4.2) 
   /ssssssssssshdmmNNmmyNMMMMhssssss/      Kernel: 5.15.0-1027-oracle 
  +ssssssssshmydMMMMMMMNddddyssssssss+     Uptime: 20 hours, 6 mins 
 /sssssssshNMMMyhhyyyyhmNMMMNhssssssss/    Packages: 772 (dpkg), 6 (snap) 
.ssssssssdMMMNhsssssssssshNMMMdssssssss.   Shell: bash 5.1.16 
+sssshhhyNMMNyssssssssssssyNMMMysssssss+   Resolution: 1024x768 
ossyNMMMNyMMhsssssssssssssshmmmhssssssso   Terminal: /dev/pts/0 
ossyNMMMNyMMhsssssssssssssshmmmhssssssso   CPU: AMD EPYC 7551 (2) @ 1.996GHz 
+sssshhhyNMMNyssssssssssssyNMMMysssssss+   GPU: 00:02.0 Vendor 1234 Device 1111 
.ssssssssdMMMNhsssssssssshNMMMdssssssss.   Memory: 260MiB / 964MiB 
 /sssssssshNMMMyhhyyyyhdNMMMNhssssssss/
  +sssssssssdmydMMMMMMMMddddyssssssss+                             
   /ssssssssssshdmNNNNmyNMMMMhssssss/                              
    .ossssssssssssssssssdMMMNysssso.
      -+sssssssssssssssssyyyssss+-
        `:+ssssssssssssssssss+:`
            .-/+oossssoo+/-.

┌──{root@ubuntu-04:~}─────{Sat Feb 04 11:31:44}───
└──{# 

Instalar o fail2ban

apt -y install fail2ban

Configuração mínima

cat << '_EOF' > /etc/fail2ban/jail.local
[DEFAULT]

# This will ignore connection coming from common private networks.
# Note that local connections can come from other than just 127.0.0.1, so
# this needs CIDR range too.
ignoreip = 127.0.0.0/8 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16

# "bantime" is the number of seconds that a host is banned.
bantime  = 7d

# A host is banned if it has generated "maxretry" during the last "findtime"
findtime  = 1h

# "maxretry" is the number of failures before a host get banned.
maxretry = 2

[sshd]
mode = aggressive
enabled = true

_EOF

Inicializar fail2ban

systemctl enable fail2ban
systemctl restart fail2ban

Observar os logs

Ver os logs em tempo real

tail -f /var/log/fail2ban.log | ccze -A

2022-12-16 10:58:28,669 fail2ban.jail           [1862]: INFO    Creating new jail 'sshd' 
2022-12-16 10:58:28,702 fail2ban.jail           [1862]: INFO    Jail 'sshd' uses pyinotify {} 
2022-12-16 10:58:28,705 fail2ban.jail           [1862]: INFO    Initiated 'pyinotify' backend 
2022-12-16 10:58:28,706 fail2ban.filter         [1862]: INFO      maxLines: 1 
2022-12-16 10:58:28,763 fail2ban.filter         [1862]: INFO      maxRetry: 2 
2022-12-16 10:58:28,763 fail2ban.filter         [1862]: INFO      findtime: 3600 
2022-12-16 10:58:28,763 fail2ban.actions        [1862]: INFO      banTime: 604800 
2022-12-16 10:58:28,764 fail2ban.filter         [1862]: INFO      encoding: UTF-8 
2022-12-16 10:58:28,766 fail2ban.filter         [1862]: INFO    Added logfile: '/var/log/auth.log' (pos = 0, hash = 7c3a545666219568353b898f87be05b5ef92f3af) 
2022-12-16 10:58:28,779 fail2ban.jail           [1862]: INFO    Jail 'sshd' started 
2023-02-08 15:03:27,578 fail2ban.actions        [85775]: NOTICE  [sshd] Unban 104.248.20.85 
2023-02-08 15:03:49,670 fail2ban.actions        [85775]: NOTICE  [sshd] Unban 206.189.130.255 
2023-02-08 15:04:19,952 fail2ban.filter         [85775]: INFO    [sshd] Found 189.219.255.98 - 2023-02-08 15:04:19 
2023-02-08 15:04:20,262 fail2ban.filter         [85775]: INFO    [sshd] Found 189.219.255.98 - 2023-02-08 15:04:20 
2023-02-08 15:04:20,359 fail2ban.actions        [85775]: NOTICE  [sshd] Ban 189.219.255.98 
2023-02-08 15:04:25,579 fail2ban.actions        [85775]: NOTICE  [sshd] Unban 35.240.204.250 
2023-02-08 15:09:46,111 fail2ban.actions        [85775]: NOTICE  [sshd] Unban 41.197.31.178 
2023-02-08 15:15:56,665 fail2ban.actions        [85775]: NOTICE  [sshd] Unban 27.72.81.194 
2023-02-08 15:19:54,498 fail2ban.filter         [85775]: INFO    [sshd] Found 61.177.172.91 - 2023-02-08 15:19:54 
2023-02-08 15:27:04,257 fail2ban.filter         [85775]: INFO    [sshd] Found 49.148.21.220 - 2023-02-08 15:27:04

Ver o status dos IPs banidos

fail2ban-client status sshd

Status for the jail: sshd
|- Filter
|  |- Currently failed:	7
|  |- Total failed:	9
|  `- File list:	/var/log/auth.log
`- Actions
   |- Currently banned:	1307
   |- Total banned:	1309
   `- Banned IP list:	101.100.164.159 101.205.25.59 101.32.252.130 .....  

Remover o banimento (Desbanir) manualmente um IP

fail2ban-client unban 101.205.25.59 
1

Referências

  1. Curso de Linux - https://blog.remontti.com.br/curso_linux
  2. Melhorar a produtividade no dia-a-dia - https://blog.remontti.com.br/5867
  3. command-line fuzzy finder - https://github.com/junegunn/fzf
  4. Terminal ASCII Art Pictures - https://www.redhat.com/sysadmin/beyond-cowsay
  5. Problemas com o colar no vim? https://gist.github.com/u0d7i/01f78999feff1e2a8361
  6. Protegendo o servidor ssh - https://www.techrepublic.com/article/install-fail2ban-ubuntu-server/
  7. Customizar seu prompt - https://www.considerednormal.com/2015/10/custom-linux-bash-prompt/
  8. Customizar seu prompt via script - https://github.com/kretcheu/devel/blob/master/prompt