6 min read

ubuntu 기본 설정-시간동기화,UFW,암호

ubuntu 기본 설정-시간동기화,UFW,암호
Photo by Gabriel Heinzer / Unsplash

sudo apt update && sudo apt -y upgrade

root@ubuntu-2gb-sin-2:~# sudo apt update && sudo apt -y upgrade

  • SWAP메모리 배정
    VPS는 메모리가 곧 매달 나가는 돈이다. 그래서 SSD용량을 조금 떼서(?) 메모리 인양 행사하는 놈을 SWAP메모리 라고 한다.
  • 기존 메모리의 2배를 통상적으로 배포한다.
    free -h 확인후
    root@ubuntu-2gb-sin-2:~# free -h
                   total        used        free      shared  buff/cache   available
    Mem:           1.9Gi       346Mi       872Mi       4.8Mi       871Mi       1.5Gi
    Swap:             0B          0B          0B
    root@ubuntu-2gb-sin-2:~#
    root@ubuntu-2gb-sin-2:~#
    root@ubuntu-2gb-sin-2:~#
    root@ubuntu-2gb-sin-2:~# sudo fallocate -l 4G /swapfile
    root@ubuntu-2gb-sin-2:~# chmod 600 /swapfile
    root@ubuntu-2gb-sin-2:~# mkswap /swapfile
    Setting up swapspace version 1, size = 4 GiB (4294963200 bytes)
    no label, UUID=5f2e186c-c23d-4a44-850e-f0672f0c2e2b
    root@ubuntu-2gb-sin-2:~# swapon /swapfile
    root@ubuntu-2gb-sin-2:~# swapon --show
    NAME      TYPE SIZE USED PRIO
    /swapfile file   4G   0B   -2
    root@ubuntu-2gb-sin-2:~# free -h
                   total        used        free      shared  buff/cache   available
    Mem:           1.9Gi       348Mi       868Mi       4.8Mi       872Mi       1.5Gi
    Swap:          4.0Gi          0B       4.0Gi
    root@ubuntu-2gb-sin-2:~#

아직 끝나지 않았다. fstab에 등록해 주지 않으면 재부팅시에는 날아간다. 그러므로
root@ubuntu-2gb-sin-2:~# vi /etc/fstab

/swapfile swap swap defaults 0 0

를 입력한 뒤 , 저장

  • 방화벽 설정

last -10을 해보면


root@ubuntu-2gb-sin-2:~# lastb -10
solana ssh:notty 45.148.10.240 Thu Jul 10 19:39 - 19:39 (00:00)
solana ssh:notty 45.148.10.240 Thu Jul 10 19:39 - 19:39 (00:00)
ssh:notty 65.49.1.54 Thu Jul 10 19:34 - 19:34 (00:00)
sol ssh:notty 45.148.10.240 Thu Jul 10 19:33 - 19:33 (00:00)
sol ssh:notty 45.148.10.240 Thu Jul 10 19:33 - 19:33 (00:00)
vps ssh:notty 196.251.88.67 Thu Jul 10 17:51 - 17:51 (00:00)
vps ssh:notty 196.251.88.67 Thu Jul 10 17:51 - 17:51 (00:00)
alpine ssh:notty 196.251.88.67 Thu Jul 10 17:51 - 17:51 (00:00)
alpine ssh:notty 196.251.88.67 Thu Jul 10 17:51 - 17:51 (00:00)
sysadmin ssh:notty 196.251.88.67 Thu Jul 10 17:51 - 17:51 (00:00)

오늘 낮에 만들었는데도 득달같이 달려드는 놈들이 많다. brute force공격으로 봇으로 공격하는 거다. 나야 인증서로 로그인 해놓았으니..아!!! 비밀번호로 로그인 되는것도 잠궈야 한다.! 그래서 방화벽등 보안설정을 해놓는 것이다.

방화벽 설정

서버 방화벽 종류에는 firewalld, iptabls, ufw 가 있다. host.allow도 있는데 잘 안쓴다. iptables는 centos시절에 주로 썻던 놈이라

firewalld 와 ufw를 주로 쓰는데 ufw는 우분투에서 주로 쓰는 놈이라 범용으로는 firewalld를 주로 많이 사용한다. 하지만! ufw가 좀 편해서 한번 써보자

반드시!!! ssh포트가 막히지 않게 ssh포트부터 활성화 해줘야 한다. 막히면 vps 자체콘솔로 접속하거나 물리서버에 콘솔 꽂아도 되긴하지만...음...
그런다음 ufw 활성화
root@ubuntu-2gb-sin-2:~# sudo ufw allow ssh
Rules updated
Rules updated (v6)

root@ubuntu-2gb-sin-2:~# ufw enable
Command may disrupt existing ssh connections. Proceed with operation (y|n)? y
Firewall is active and enabled on system startup
root@ubuntu-2gb-sin-2:~#
root@ubuntu-2gb-sin-2:~#
root@ubuntu-2gb-sin-2:~#
root@ubuntu-2gb-sin-2:~# ufw status verbose
Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), disabled (routed)
New profiles: skip

To Action From


22/tcp ALLOW IN Anywhere
22/tcp (v6) ALLOW IN Anywhere (v6)

root@ubuntu-2gb-sin-2:~#

ssh가 끊길수도 있다고 경고한다. 쫄지 마시오

나머지 http, https 도 추가(웹서버 만들려면)
나는 집, 회사, vpn의 ip를 CIDR 24로 추가하고 난뒤 22번 포트를 막아버린다.
그러면 외부에서 접속할때도 vpn으로 접속하면 그만이다.


vpn은 zerotier를 깔았다. 수정!!!
지금은 tailscale로 갈아탔다. VPN서버를 운영하려고 했는데 귀찮기도 하고 계속 오류가 생겨 알아보던중 tailscale이 20개 제한이라....이거로 갈아탔다.


  • SSH 설정- 비밀번호 없이 공인키로만 접속하기

보안적으로도 좋지만 편리하기도 하다. 접속할때마다 비밀번호 치는거야 귀찮은건 아니지만 보안적으로도 좋지 않다. brute force 무작위 대입공격에 취약하다.
하지만 비공개키로 접속하는 설정이면 비밀번호를 아예 입력도 못한다.

ssh설정- 비밀키를 통해서 로그인
안했을때: ssh root@ip => 비밀번호 접속
했을때: ssh root@ip=> 바로 접속됨
id_rsa 와 authorized_keys 만 매칭 되면 끝

/etc/hosts :세팅시 ip를 입력하지 않아도 서버간 rsync, ssh 접속이 가능하다.
안했을때: ssh root@ip
했을때: ssh root@노드명
ex) jaegon 10.123.123.123

~/.ssh/config : 위에는 ip를 안쳐도 되는 정도지만 이건 더 심플하게 접속 가능
안했을때 : ssh root@노드 or ip
했을때: ssh jaegon => 바로 접속됨(비밀키 설정을 해 놔야 함)

Host jaegon
    HostName 10.123.12.123
    User root

jaegon은  node 이름이고
hostname에는 ip 주소
user는 접속하고자 하는 사용자명

  • 시간설정
[root@rocky9 ~]# timedatectl 
               Local time: Wed 2026-03-04 13:52:04 UTC
           Universal time: Wed 2026-03-04 13:52:04 UTC
                 RTC time: Wed 2026-03-04 13:52:04
                Time zone: UTC (UTC, +0000)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no
[root@rocky9 ~]# timedatectl set-timezone Asia/Seoul
[root@rocky9 ~]# timedatectl 
               Local time: Wed 2026-03-04 22:52:44 KST
           Universal time: Wed 2026-03-04 13:52:44 UTC
                 RTC time: Wed 2026-03-04 13:52:44
                Time zone: Asia/Seoul (KST, +0900)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no
[root@rocky9 ~]# chronyc sources
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^- 175.210.18.47                 2  10   377  1009   -605us[ -604us] +/-   54ms
^- mail.zeroweb.kr               3  10   377   112   +893us[ +913us] +/-   46ms
^- mail.innotab.com              3  10   377   481   -599us[ -590us] +/-   51ms
^* 193.123.243.2                 4   7   377   105    +23us[  +43us] +/- 2693us
[root@rocky9 ~]# 

암호설정- 제일 중요하다. 나중에 만일을 위해서 꼭 설정해야 한다.

ubuntu@ciongu:~$ sudo passwd root
New password: 
Retype new password: 
passwd: password updated successfully
ubuntu@ciongu:~$ sudo passwd ubuntu
New password: 
Retype new password: 
passwd: password updated successfully
ubuntu@ciongu:~$ 

당연히 암호는 보안을 위해 리눅스는 바로 보이지 않게 설계되어 있다.
그래서 2번을 타이핑 하도록 되어 있는 것이다.
root 암호화 ubuntu 접속 암호를 둘다 꼭 설정해 두어라. 추후에 쓸일이 있게 될것이다.