Ubuntu에 보안 OpenSSH 서버를 설치하는 방법은 무엇입니까?

Ubuntu에 보안 OpenSSH 서버를 설치하는 방법은 무엇입니까?

Ubuntu에 OpenSSH 서버를 설치하고 싶습니다. 어떻게 하나요?

다음을 수행해야 합니다.

  1. OpenSSH 서버 설정

  2. 사용자에 대한 SSH 공개-개인 키 쌍 설정

  3. 비밀번호 로그인 비활성화

  4. 루트 사용자 활성화

  5. 루트 사용자를 위한 SSH 공개-개인 키 쌍 설정

  6. 비밀번호 설정

답변1

터미널로 이동하여 다음을 입력하세요.

sudo su
aptitude install openssh-server openssh-client

설치 테스트

ps -A | grep sshd

출력이 다음과 같은 경우:

<some number> ?        00:00:00 sshd

그런 다음 SSH 데몬이 실행 중입니다.

다시 터미널에 입력하세요.

ss -lnp | grep sshd

출력이 다음과 같은 경우:

0  128  :::22  :::*  users:(("sshd",16893,4))
0  128   *:22   *:*  users:(("sshd",16893,3))

그러면 SSH 데몬이 들어오는 연결을 수신하고 있음을 의미합니다.

이제 구성 파일을 편집합니다. 먼저 원본 파일의 백업을 만듭니다.

sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.factory-defaults

이제 구성 파일을 열어 편집합니다.

sudo gedit /etc/ssh/sshd_config

취약한 비밀번호는 추측하기 쉽습니다. 가장 좋은 방법은 비밀번호 대신 SSH 키를 사용하는 것입니다.

그래서 우리는 비밀번호 인증을 완전히 비활성화합니다.

줄로 가세요

#PasswordAuthentication yes

그리고 그것을로 교체

PasswordAuthentication no

전달을 활성화하면 이미 비밀번호를 추측한 공격자에게 더 많은 옵션이 제공됩니다.

그래서 우리는 그것을 비활성화합니다. 그것은 우리에게 약간의 보안을 제공합니다

라인으로 이동

AllowTcpForwarding yes
X11Forwarding yes

그리고 그것들을 다음으로 대체하십시오.

AllowTcpForwarding no
X11Forwarding no

특정 사용자를 명시적으로 허용하고 특정 사용자의 로그인을 거부할 수 있습니다.

이를 위해 구성 파일 맨 아래에 다음 줄을 넣어야 합니다.

AllowUsers Fred Wilma
DenyUsers Dino Pebbles

최적의 노트북 성능을 위해 두 개의 보류 중인 연결을 허용합니다. 세 번째와 열 번째 연결 사이에 시스템은 열 번째 동시 연결에서 30%에서 최대 100%까지 무작위로 연결을 삭제하기 시작합니다. 이 작업은 다음 줄로 수행할 수 있습니다.

MaxStartups 2:30:10

더 많은 오류와 기타 유용한 정보를 기록하기 위해 다음 줄을 변경합니다.

LogLevel INFO

LogLevel VERBOSE로

초보 공격자를 겁주기 위해 배너를 표시할 수 있습니다. 라인 앞쪽에서 해시 태그를 제거합니다.

#Banner /etc/issue.net

만들다

Banner /etc/issue.net

그런 다음 터미널로 이동하여 다음을 입력합니다.

sudo gedit /etc/issue.net

***************************************************************************
                        NOTICE TO USERS
This computer system is the private property of its owner, whether
individual, corporate or government.  It is for authorized use only.
Users (authorized or unauthorized) have no explicit or implicit
expectation of privacy.
Any or all uses of this system and all files on this system may be
intercepted, monitored, recorded, copied, audited, inspected, and
disclosed to your employer, to authorized site, government, and law
enforcement personnel, as well as authorized officials of government
agencies, both domestic and foreign.
By using this system, the user consents to such interception, monitoring,
recording, copying, auditing, inspection, and disclosure at the
discretion of such personnel or officials.  Unauthorized or improper use
of this system may result in civil and criminal penalties and
administrative or disciplinary action, as appropriate. By continuing to
use this system you indicate your awareness of and consent to these terms
and conditions of use. LOG OFF IMMEDIATELY if you do not agree to the
conditions stated in this warning.
****************************************************************************

이제 구성 파일을 저장하고 닫고 터미널에 다음을 입력하여 SSH를 다시 ​​시작합니다.

systemctl restart ssh

다음으로 SSH 키를 설정합니다. SSH 키에는 공개 키와 비공개 키 두 쌍이 있습니다. 공개키는 서버에 존재하고, 개인키는 개인에게 존재하며, 자신의 개인키와 공개키를 일치시킬 수 있는 사람만이 로그인할 수 있습니다. 또한 선택적으로 개인 키를 암호로 보호할 수 있습니다. 또한 4096비트 암호화를 사용하여 키가 생성되면 무차별 공격으로 키를 해독하는 것이 거의 불가능합니다.

1단계 - RSA 키 쌍 생성:

터미널에 입력

ssh-keygen -t rsa -b 4096

여기서는 보안 강화를 위해 64비트 암호화를 사용합니다.

2단계 - 키와 암호를 저장합니다.

화면 지침을 따르고 원하는 키 저장 위치를 ​​지정하고 기본값을 수락하는 것이 좋습니다. 암호를 선택하고 강력한 암호를 제공하고 기억하세요.

화면은 다음과 같습니다.

ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/demo/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/demo/.ssh/id_rsa.
Your public key has been saved in /home/demo/.ssh/id_rsa.pub.
The key fingerprint is:
4a:dd:0a:c6:35:4e:3f:ed:27:38:8c:74:44:4d:93:67 demo@a
The key's randomart image is:
+--[ RSA 2048]----+
|          .oo.   |
|         .  o.E  |
|        + .  o   |
|     . = = .     |
|      = S = .    |
|     o + = +     |
|      . o + o .  |
|           . o   |
|                 |
+-----------------+

3단계 - 공개 키 복사:

터미널에 입력

ssh-copy-id [email protected]

여기서 123.45.56.78은 서버 IP 주소입니다.

localhost의 경우

ssh-copy-id user@localmachinename

화면은 대략 이렇습니다

The authenticity of host '12.34.56.78 (12.34.56.78)' can't be established.
RSA key fingerprint is b1:2d:33:67:ce:35:4d:5f:f3:a8:cd:c0:c4:48:86:12.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '12.34.56.78' (RSA) to the list of known hosts.
[email protected]'s password: 
Now try logging into the machine, with "ssh '[email protected]'", and check in:

~/.ssh/authorized_keys

예상하지 못한 추가 키가 추가되지 않았는지 확인하기 위해.

이제 설치가 완료되었습니다. 로그인하려면 터미널에 다음을 입력해야 합니다.

ssh username@servername

그런 다음 암호를 묻는 메시지가 나타나면 암호를 제공해야 합니다.

이제 opessh 서버의 루트 로그인을 활성화합니다. Ubuntu에서는 기본적으로 비활성화되어 있으므로 먼저 sudo 비밀번호를 활성화해야 합니다.

이를 위해 터미널에 다음을 입력하면 화면은 다음과 같습니다.

sudo passwd
[sudo] password for [username]: [Type your user password and press return]
Type new UNIX password: [Type the root password you want]
Retype new UNIX password: [Retype the root password you chosen before]
passwd: password updated successfully

이제 /etc/sudoers 파일을 편집해야 합니다.

여기서는 visudo라는 편집기를 사용합니다.

visudo는 sudoes 파일을 편집하는 유일한 목적이기 때문입니다.

우분투에서는 기본적으로 구성 파일이 나노 편집기로 열립니다.

터미널에 입력하여 변경하려면 다음을 수행하세요.

sudo update-alternatives --config editor

다음 화면이 나타납니다:

There are 4 choices for the alternative editor (providing /usr/bin/editor).

  Selection    Path                Priority   Status
------------------------------------------------------------
* 0            /bin/nano            40        auto mode
  1            /bin/ed             -100       manual mode
  2            /bin/nano            40        manual mode
  3            /usr/bin/vim.basic   30        manual mode
  4            /usr/bin/vim.tiny    10        manual mode

Press <enter> to keep the current choice[*], or type selection number:

3을 입력하고 Enter를 누르세요

그런 다음 다음을 입력합니다.

sudo visudo

다음 줄로 이동하세요.

Defaults    env_reset

엔터 키를 치시오

새 줄 위에 생성됩니다

유형:

기본값 루트pw

TAB이 아닌 스페이스바를 사용하세요

Esc --> :x --> Enter 누르기

터미널 유형:

gedit /etc/ssh/sshd_config

다음 줄로 이동하세요.

PermitRootLogin password-prohibited

그리고 그것을로 변경

PermitRootLogin yes

다음 줄로 이동합니다.

PasswordAuthentication no

그리고 그것을로 변경

PasswordAuthentication yes

저장하고 닫습니다

그런 다음 SSH를 다시 ​​시작하십시오.

service ssh restart

이제 사용자 루트에 대한 SSH 공개-개인 키 쌍을 다시 생성할 차례입니다.

터미널에 입력

ssh-keygen -t rsa -b 4096

2단계 - 키와 암호를 저장합니다.

화면 지침에 따라 원하는 키 저장 위치를 ​​지정하고, 이번에는 새 쌍을 생성해야 하므로 기본값을 수락하지 마십시오. 기본 쌍은 이미 생성되었습니다. 암호를 선택하고, 강력한 암호를 제공하고, 기억해 두십시오.

화면은 다음과 같습니다.

ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/demo/.ssh/id_rsa): u-root-id_rsa 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/demo/.ssh/u-root-id_rsa.
Your public key has been saved in /home/demo/.ssh/u-root-id_rsa.pub.
The key fingerprint is:
4a:dd:0a:c6:35:4e:3f:ed:27:38:8c:74:44:4d:93:67 demo@a
The key's randomart image is:

+--[ RSA 2048]----+
|          .oo.   |
|         .  o.E  |
|        + .  o   |
|     . = = .     |
|      = S = .    |
|     o + = +     |
|      . o + o .  |
|           . o   |
|                 |
+-----------------+

그런 다음 터미널에 다음을 입력하십시오.

ssh-copy-id -i u-root-id_rsa.pub root@localmachinename

출력 화면에 다음이 표시될 수 있습니다.

The authenticity of host '12.34.56.78 (12.34.56.78)' can't be established.
RSA key fingerprint is b1:2d:33:67:ce:35:4d:5f:f3:a8:cd:c0:c4:48:86:12.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '12.34.56.78' (RSA) to the list of known hosts.
[email protected]'s password: 

이제 "ssh '를 사용하여 머신에 로그인해 보세요.[이메일 보호됨]'"를 입력하고 체크인합니다.

~/.ssh/authorized_keys

예상하지 못한 추가 키가 추가되지 않았는지 확인하기 위해.

이제 로그인을 위해 루트에 개인 키 액세스 권한을 부여했습니다.

유형을 테스트하려면 다음을 수행하십시오.

ssh root@localmachine

다시 터미널 유형으로:

gedit /etc/ssh/sshd_config

다음 줄로 이동하세요.

PasswordAuthentication yes

그리고 그것을로 변경

PasswordAuthentication no

저장하고 닫습니다

그런 다음 SSH를 다시 ​​시작하십시오.

service ssh restart

암호문을 요구할 것입니다. 암호는 동일하게 유지됩니다. 그것을 제공.

이제 루트는 성공적으로 로그인할 수 있습니다

이제 보안을 강화하려면 방화벽을 추가해야 합니다.

유형:

apt install ufw

이제 시작해 보세요

enable ufw

현재 실행 중인 프로세스 목록 가져오기

ufw app list

OpenSSH가 여기에 나열됩니다.

ALow it through firewall
ufw allow OpenSSH

방화벽 다시 시작

systemctl restart ufw

설치가 완료되었습니다.

관련 정보