로컬 컴퓨터의 운영 체제에 따라 로컬 컴퓨터에서 Linux 인스턴스로 연결하는 데 필요한 옵션이 결정됩니다.아래와 같은 다양한 방법이 있지만, 본 실습에서는 PuTTY를 활용하여 Linux 인스턴스에 연결하도록 하겠습니다.
로컬 컴퓨터 운영 체제가 Linux 또는 macOS X인 경우
• SSH 클라이언트
• EC2 Instance Connect
• AWS 시스템 관리자 Session Manager
로컬 컴퓨터 운영 체제가 Windows인 경우
• PuTTY
• SSH 클라이언트
• AWS 시스템 관리자 Session Manager
• Windows Subsystem for Linux
Puttygen으로 .PPK 형태의 KEY 생성
PuTTY로 인스턴스에 연결
Instance → 접속 할 인스턴스 클릭 → Action → Connect
Session → Host Name에 아까 복사한 DNS 주소 입력
Security Group 중 인바운드 규칙 허용해준 포트 번호 입력
Connection → SSH → Auth 클릭하여 Private key file for authentication 에 아까 .ppk 파일로 변환한 파일 넣기
세팅 완료 후 save 후 터미널 접근
Mac OS X or Linux (OpenSSH)
기본적으로 Mac OS X 및 Linux 운영 체제는 모두 EC2 Linux 인스턴스에 연결하는데 사용할 수 있는 SSH 클라이언트와 함께 제공 됩니다.
SSH 클라이언트를 작성한 키와 함께 사용하려면 몇 단계가 필요합니다.
mv user xxxx.pem ~/.ssh
chmod 400 ~/.ssh/xxxx.pem
참고 : 권한 또는 ssh 관련 이슈발생 시 이곳을 참고하세요.
scp -P Pot number -i /path/my-key-pair.pem /path/my-file.txt ec2-user@my-instance-public-dns-name:path/
scp -P [포트번호] -i 펌키1.pem 펌키2.pem ec2-user@ec2-xxx.ap-northeast-2.compute.amazonaws.com:/home
/ec2-user
펌2.pem 100% 1700 366.5KB/s 00:00
ssh -i */path/my-key-pair*.pem *my-instance-user-name*@*my-instance-public-dns-name*
따라서 Amazon Linux 인스턴스에 연결하려면 다음과 같은 명령어가 필요합니다.
ssh –i xxxx.pem ec2-user@EC2 Host Name or EIP
설정 해야 할 사항
1. 퍼블릭키 접근 시 key가 아닌 password로 접근 가능하도록 설정 변경
2. 포트 번호 변경
3. 계정 생성 및 password 설정
4. 프라이빗 서브넷에 위치한 인스턴스에 접근할 수 있도록 키 값 설정
리눅스 기본
사용자 리스트 및 정보 보기
cat /etc/passwd
사용자
cat /etc/group
그룹
cat /etc/login.def
패스워드 유효기간, 디렉토리 자동 생성 등
**사용자 및 그룹 관리 파일**
- /etc/skel : 사용자에 대한 기본적인 초기화 파일들이 저장되어 있음.
- /etc/login.defs : 사용자나 그룹을 생성할 때 참고하는 기본 값들이 저장되어 있음.
hostnamectl set-hostname bastion
exec bash
#변경 사항 바로 적용
sudo vim /etc/ssh/sshd_config
로그인 방법 변경
PasswordAuthentication
부분 주석을 해제하고 yes로 변경...
# To disable tunneled clear text passwords, change to no here!
PasswordAuthentication yes
#PermitEmptyPasswords no
...
sudo vim /etc/ssh/sshd_config
Port 변경
Port
부분의 주석을 해제하고 원하는 Port 번호로 변경# $OpenBSD: sshd_config,v 1.100 2016/08/15 12:32:04 naddy Exp $
# This is the sshd server system-wide configuration file. See
# sshd_config(5) for more information.
# This sshd was compiled with PATH=/usr/local/bin:/usr/bin
# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented. Uncommented options override the
# default value.
# If you want to change the port on a SELinux system, you have to tell
# SELinux about this change.
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
#
Port '바꿀 번호'
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::
...
systemctl restart sshd
참고 : 열려있는 포트 확인방법
netstat
을 활용한 포트확인[ec2-user@bastion ~]$ netstat -nltp
(No info could be read for "-p": geteuid()=1000 but you should be root.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:xxxxxxx 0.0.0.0:* LISTEN -
tcp6 0 0 :::111 :::* LISTEN -
tcp6 0 0 :::xxxxxx :::* LISTEN -
systemctl status
명령을 활용한 서비스 포트확인[ec2-user@bastion ~]$ systemctl status sshd
● sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2021-04-26 11:28:58 UTC; 23min ago
Docs: man:sshd(8)
man:sshd_config(5)
Main PID: 3155 (sshd)
CGroup: /system.slice/sshd.service
└─3155 /usr/sbin/sshd -D
Apr 26 11:28:58 bastion systemd[1]: Starting OpenSSH server daemon...
Apr 26 11:28:58 bastion sshd[3155]: Server listening on 0.0.0.0 port xxxx.
Apr 26 11:28:58 bastion sshd[3155]: Server listening on :: port xxxx
.
...
3-1. 계정 생성
tail /etc/passwd
# tail~ 로 현재 어떠한 사용자가 있는지 확인
sudo useradd 'USER'
3-2. 계정 비밀 번호 생성
sudo passwd 'USER'
# Changing password for user 'USER'
# New password: 비밀번호 입력
$HOME/
[ec2-user@bastion ~]$ sudo mv 펌1.pem /home/xxxxx/
참고
비밀번호 변경
scp -P [포트번호] -i [펌키1].pem [펌키2].pem ec2-user@ec2-주소.ap-northeast-2.compute.amazonaws.com:/home
/'USER'
[펌키].pem 100% 1700 366.5KB/s 00:00
만약에 파일 소유 그룹이 ec2-user로 되어 있다면 아래 명령어로 그룹 변경
sudo chown USER:USER [펌키2].pem
ssh -i xxxx.pem ec2-user@ 프라이빗 서브넷에 위치한 인스턴스의Private IPv4 addresses
[XXX001@basiton /]$ ssh -i /home/USER/.ssh/펌키2.pem ec2-user@ip-10-0-1-152.ap-northeast-2.compute.internal
__| __|_ )
_| ( / Amazon Linux 2 AMI
___|\___|___|
https://aws.amazon.com/amazon-linux-2/
sudo useradd elastic
sudo passwd elastic
[root@elastic ~]# visudo -f /etc/sudoers
elastic ALL=(ALL) ALL 추가
Defaults env_keep += "LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES"
Defaults env_keep += "LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE"
Defaults env_keep += "LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY"
#
# Adding HOME to env_keep may enable a user to run unrestricted
# commands via sudo.
#
# Defaults env_keep += "HOME"
Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin
## Next comes the main part: which users can run what software on
## which machines (the sudoers file can be shared between multiple
## systems).
## Syntax:
##
## user MACHINE=COMMANDS
##
## The COMMANDS section may have other options added to it.
##
## Allow root to run any commands anywhere
root ALL=(ALL) ALL
**elastic ALL=(ALL) ALL**
## Allows members of the 'sys' group to run networking, software,
## service management apps and more.
# %sys ALL = NETWORKING, SOFTWARE, SERVICES, STORAGE, DELEGATING, PROCESSES, LOCATE, DRIVERS
## Allows people in group wheel to run all commands
vim /etc/ssh/sshd_config
# $OpenBSD: sshd_config,v 1.100 2016/08/15 12:32:04 naddy Exp $
# This is the sshd server system-wide configuration file. See
# sshd_config(5) for more information.
# This sshd was compiled with PATH=/usr/local/bin:/usr/bin
# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented. Uncommented options override the
# default value.
# If you want to change the port on a SELinux system, you have to tell
# SELinux about this change.
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
#
Port [포트번호]
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::
HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
# Ciphers and keying
#RekeyLimit default none
# Logging
#SyslogFacility AUTH
SyslogFacility AUTHPRIV
#LogLevel INFO
# Authentication:
#LoginGraceTime 2m
#PermitRootLogin yes
#StrictModes yes
systemctl restart sshd
netstat -nltp