Начальная настройка сервера с Ubuntu

Совместимо с версиями 14–22

Введение

Шаг 1 — первое подключение

ssh USER@IP_ADDRESS
The authenticity of host '203.0.113.0 (203.0.113.0)' can't be established.
ECDSA key fingerprint is SHA256:IcLk6dLi+0yTOB6d7x1GMgExamplewZ2BuMn5/I5Jvo.
Are you sure you want to continue connecting (yes/no)? yes

Шаг 2 — смена пароля root

passwd
head /dev/urandom | tr -dc A-Za-z0-9 | head -c 24 ; echo ''

Шаг 3 — создание нового пользователя

adduser ИМЯ_ПОЛЬЗОВАТЕЛЯ_СИСТЕМЫ

Шаг 4 — привилегии пользователя

usermod -aG sudo ИМЯ_ПОЛЬЗОВАТЕЛЯ_СИСТЕМЫ
apt-get install vim
root        ALL=(ALL)  ALL
ИМЯ_ПОЛЬЗОВАТЕЛЯ_СИСТЕМЫ ALL=(ALL) ALL

Шаг 5 — запрет подключения через SSH под root

PermitRootLogin no
UseDNS no
AllowUsers ИМЯ_ПОЛЬЗОВАТЕЛЯ_СИСТЕМЫ
reload ssh
service ssh restart

Шаг 6 — смена порта для SSH

Port 50683
shuf -i 49152-65535 -n 1
sudo lsof -i :22
reload ssh
service ssh restart

Шаг 7— Настройка авторизации по открытому ключу (Рекомендуется)

mkdir ~/.ssh
ssh-keygen -t rsa -f ~/.ssh/myrsakey
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/ВАШ_ПОЛЬЗОВАТЕЛЬ/.ssh/myrsakey.
Your public key has been saved in /home/ВАШ_ПОЛЬЗОВАТЕЛЬ/.ssh/myrsakey.pub
cat ~/.ssh/myrsakey.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDBGTO0tsVejssuaYR5R3Y/i73SppJAhme1dH7W2c47d4gOqB4izP0+fRLfvbz/tnXFz4iOP/H6eCV05hqUhF+KYRxt9Y8tVMrpDZR2l75o6+xSbUOMu6xN+uVF0T9XzKcxmzTmnV7Na5up3QM3DoSRYX/EP3utr2+zAqpJIfKPLdA74w7g56oYWI9blpnpzxkEd3edVJOivUkpZ4JoenWManvIaSdMTJXMy3MtlQhva+j9CgguyVbUkdzK9KKEuah+pFZvaugtebsU+bllPTB0nlXGIJk98Ie9ZtxuY3nCKneB+KjKiXrAvXUPCI9mWkYS/1rggpFmu3HbXBnWSUdf localuser@machine.local
mkdir ~/.ssh
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
ssh -i ~/.ssh/myrsakey ИМЯ_ПОЛЬЗОВАТЕЛЯ_СИСТЕМЫ@IP_адрес_сервера
PubkeyAcceptedKeyTypes=+ssh-rsa

Заключение

Источники

--

--

Software Engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store