Diferencia entre revisiones de «Securizar Ubuntu Server»
Ir a la navegación
Ir a la búsqueda
Sin resumen de edición |
Sin resumen de edición |
||
| Línea 4: | Línea 4: | ||
== Cambiar contraseña a usuario root == | == Cambiar contraseña a usuario root == | ||
Todos los comandos se ejecutarán como root, a menos que se especifique lo contrario. | * Todos los comandos se ejecutarán como root, a menos que se especifique lo contrario. | ||
<syntaxhighlight lang="Bash">sudo -i</syntaxhighlight> | <syntaxhighlight lang="Bash">sudo -i</syntaxhighlight> | ||
Establecer contraseña para root: | * Establecer contraseña para root: | ||
<syntaxhighlight lang="Bash">passwd root</syntaxhighlight> | <syntaxhighlight lang="Bash">passwd root</syntaxhighlight> | ||
== Creación de nuevo usuario == | == Creación de nuevo usuario == | ||
Crear un grupo para el usuario: | * Crear un grupo para el usuario: | ||
<syntaxhighlight lang="Bash">groupadd guzman</syntaxhighlight> | <syntaxhighlight lang="Bash">groupadd guzman</syntaxhighlight> | ||
Crear usuario para el usuario: | * Crear usuario para el usuario: | ||
<syntaxhighlight lang="Bash">useradd -d /home/guzman -m -g guzman -s /bin/bash guzman</syntaxhighlight> | <syntaxhighlight lang="Bash">useradd -d /home/guzman -m -g guzman -s /bin/bash guzman</syntaxhighlight> | ||
Establecer contraseña para el usuario: | * Establecer contraseña para el usuario: | ||
<syntaxhighlight lang="Bash">passwd guzman</syntaxhighlight> | <syntaxhighlight lang="Bash">passwd guzman</syntaxhighlight> | ||
Establecer permisos al usuario: | * Establecer permisos al usuario: | ||
<syntaxhighlight lang="Bash">usermod -a -G adm,cdrom,sudo,dip,lxd guzman</syntaxhighlight> | <syntaxhighlight lang="Bash">usermod -a -G adm,cdrom,sudo,dip,lxd guzman</syntaxhighlight> | ||
== Borrar usuario por defecto == | == Borrar usuario por defecto == | ||
Se borran usuarios por defecto que puedan suponer un riesgo de ataques de diccionario. | * Se borran usuarios por defecto que puedan suponer un riesgo de ataques de diccionario. | ||
<syntaxhighlight lang="Bash">userdel -r ubuntu</syntaxhighlight> | <syntaxhighlight lang="Bash">userdel -r ubuntu</syntaxhighlight> | ||
| Línea 31: | Línea 31: | ||
== Actualizar OS == | == Actualizar OS == | ||
Actualizar repositorios: | * Actualizar repositorios: | ||
<syntaxhighlight lang="Bash">apt update</syntaxhighlight> | <syntaxhighlight lang="Bash">apt update</syntaxhighlight> | ||
Actualizar OS: | * Actualizar OS: | ||
<syntaxhighlight lang="Bash">apt dist-upgrade</syntaxhighlight> | <syntaxhighlight lang="Bash">apt dist-upgrade</syntaxhighlight> | ||
Borrar paquetes sin uso: | * Borrar paquetes sin uso: | ||
<syntaxhighlight lang="Bash">apt autoremove</syntaxhighlight> | <syntaxhighlight lang="Bash">apt autoremove</syntaxhighlight> | ||
Limpiar cachés: | * Limpiar cachés: | ||
<syntaxhighlight lang="Bash">apt autoclean</syntaxhighlight> | <syntaxhighlight lang="Bash">apt autoclean</syntaxhighlight> | ||
* Reiniciar VM: | |||
<syntaxhighlight lang="Bash">reboot</syntaxhighlight> | <syntaxhighlight lang="Bash">reboot</syntaxhighlight> | ||
== Instalar fail2fan == | == Instalar fail2fan == | ||
Instalar fail2fan para monitorizar logs y bloquear usuarios que intenten ataques de diccionario. | * Instalar fail2fan para monitorizar logs y bloquear usuarios que intenten ataques de diccionario. | ||
<syntaxhighlight lang="Bash">apt install fail2ban</syntaxhighlight> | <syntaxhighlight lang="Bash">apt install fail2ban</syntaxhighlight> | ||
| Línea 91: | Línea 91: | ||
=== Comprobar que puedes acceder por SSH === | === Comprobar que puedes acceder por SSH === | ||
Desde otra terminal: | * Desde otra terminal: | ||
<syntaxhighlight lang="Bash">ssh -i ~/.ssh/culturetas.net.key guzman@culturetas.net</syntaxhighlight> | <syntaxhighlight lang="Bash">ssh -i ~/.ssh/culturetas.net.key guzman@culturetas.net</syntaxhighlight> | ||
| Línea 159: | Línea 159: | ||
[...] | [...] | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== Configurar Firewall == | |||
Revisión del 21:43 22 feb 2026
Securizar servidor con Ubuntu Server
El objetivo de esta nota es securizar un servidor con el sistema operativo Ubuntu Server.
Cambiar contraseña a usuario root
- Todos los comandos se ejecutarán como root, a menos que se especifique lo contrario.
sudo -i
- Establecer contraseña para root:
passwd root
Creación de nuevo usuario
- Crear un grupo para el usuario:
groupadd guzman
- Crear usuario para el usuario:
useradd -d /home/guzman -m -g guzman -s /bin/bash guzman
- Establecer contraseña para el usuario:
passwd guzman
- Establecer permisos al usuario:
usermod -a -G adm,cdrom,sudo,dip,lxd guzman
Borrar usuario por defecto
- Se borran usuarios por defecto que puedan suponer un riesgo de ataques de diccionario.
userdel -r ubuntu
Cambiar hostname del servidor
hostnamectl set-hostname culturetas.net
Actualizar OS
- Actualizar repositorios:
apt update
- Actualizar OS:
apt dist-upgrade
- Borrar paquetes sin uso:
apt autoremove
- Limpiar cachés:
apt autoclean
- Reiniciar VM:
reboot
Instalar fail2fan
- Instalar fail2fan para monitorizar logs y bloquear usuarios que intenten ataques de diccionario.
apt install fail2ban
Generar claves criptográficas
Generar clave
Características:
- Tipo: Curva Elíptica
- Curva: ed25519
- Rondas: 512
ssh-keygen -t ed25519 -a 512 -C culturetas.net -f culturetas.net.key
Desplegar clave en usuario root
mkdir ~/.ssh
cp culturetas.net.key culturetas.net.key.pub /root/.ssh/
cat culturetas.net.key.pub >> /root/.ssh/authorized_keys
chmod 700 /root/.ssh/
chmod 600 /root/.ssh/culturetas.net.key
chmod 644 /root/.ssh/culturetas.net.key.pub
chmod 600 /root/.ssh/authorized_keys
chown -R root:root /root/.ssh/
Desplegar clave en usuario guzman
mkdir /home/guzman/.ssh
cp culturetas.net.key culturetas.net.key.pub /home/guzman/.ssh/
cat culturetas.net.key.pub >> /home/guzman/.ssh/authorized_keys
chmod 700 /home/guzman/.ssh/
chmod 600 /home/guzman/.ssh/culturetas.net.key
chmod 644 /home/guzman/.ssh/culturetas.net.key.pub
chmod 600 /home/guzman/.ssh/authorized_keys
chown -R guzman:guzman /home/guzman/.ssh/
Crear grupo SSHallow
Este será el único grupo que puede conectar por SSH.
groupadd -r SSHallow
Añadir usuarios a grupo SSHallow
Realizar para todos los usuarios que van a conectar mediante SSH. Por ejemplo:
- root
- guzman
usermod -a -G SSHallow guzman
usermod -a -G SSHallow root
Comprobar que puedes acceder por SSH
- Desde otra terminal:
ssh -i ~/.ssh/culturetas.net.key guzman@culturetas.net
Configurar servidor SSH
- Backup fichero de configuració SSH:
cp -a /etc/ssh/sshd_config /etc/ssh/sshd_config.$(date +%Y%m%d)
- Editar fichero de configuración SSH:
vi /etc/ssh/sshd_config
Include /etc/ssh/sshd_config.d/*.conf
Port 22
AddressFamily any
ListenAddress 0.0.0.0
ListenAddress ::
LoginGraceTime 2m
PermitRootLogin no
StrictModes yes
MaxAuthTries 3
MaxSessions 10
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys .ssh/authorized_keys2
PasswordAuthentication no
PermitEmptyPasswords no
AllowGroups SSHallow
KbdInteractiveAuthentication no
UsePAM yes
X11Forwarding yes
PrintMotd no
TCPKeepAlive yes
# Timeout 300 seg * 12 = 1 hour
ClientAliveInterval 300
ClientAliveCountMax 12
AcceptEnv LANG LC_*
Subsystem sftp /usr/lib/openssh/sftp-server
- Desactivar acceso con contraseña:
Para desactivar el acceso con contraseña (sólo claves criptográficas) deberías bastar con la directiva: "PasswordAuthentication no". Sin embargo, puede hacer alguna algún fichero adicional que lo contradiga:
vi /etc/ssh/sshd_config.d/50-cloud-init.conf
#PasswordAuthentication yes
Reiniciar VM
reboot
Personalizar alias
- Manias mías
vi /root/.bashrc
[...]
# some more ls aliases
alias ll='ls -lh'
alias la='ls -A'
alias l='ls -CF'
[...]
vi /home/guzman/.bashrc
[...]
# some more ls aliases
alias ll='ls -lh'
alias la='ls -A'
alias l='ls -CF'
[...]