[Ubuntu] openVPN 과 ROS의 연결 설정(LTE모듈을 활용한 경우)
* 목적
- LTE모듈을 통해 인터넷에 연결된 컴퓨터와, 고정 IP를 가진 Desktop 컴퓨터를 ROS1 운영체제로 연결.
* 문제상황
- ROS는 같은 네트워크망에서의 통신만 허용(ex. 하나의 공유기, 랜선으로 연결된 두 대의 컴퓨터 등)
- LTE는 데스크탑 컴퓨터와 같은 공유기로 연결되지 않고, 인터넷에 직접 연결
- 또한 ip 주소가 고정되지 않고 계속 변해서 고정된 ip를 요구하는 ROS1에서 사용하기에는 어려움이 있음.
*해결방안
- Desktop 컴퓨터에서 openVPN을 활용하여 vpnServer를 만듬
- LTE 모듈이 연결된 컴퓨터에서 vpnServer에 접속
- vpnServer에서 할당된 고정ip를 활용하여 ROS1 운영체제로 연결.
1. openVPN 설치 및 인증서 세팅
* 참고한 블로그
https://dejavuqa.tistory.com/243
openVPN 설치 (on Ubuntu 18.04)
사내에서 vpn을 한번 사용해 보더니 수요가 늘었습니다. iptime T24000을 라우터로 사용하고 있는데 VPN을 다섯개만 지원합니다. 그래서 openVPN Server를 구성해 사용하겠습니다. openVPN Server 구축에 참고
dejavuqa.tistory.com
https://kkastory.tistory.com/13?category=796285
[OpenVPN] Ubuntu 18.04 에 OpenVPN server 구축하기
이모티콘 처리 업데이투 dejavuqa.tistory.com/243 openVPN 설치 (on Ubuntu 18.04) 사내에서 vpn을 한번 사용해 보더니 수요가 늘었습니다. iptime T24000을 라우터로 사용하고 있는데 VPN을 다섯개만 지원합..
kkastory.tistory.com
1.1 openvpn 설치
sudo apt update
sudo apt install openvpn
1.2 인증서 생성을 위한 EasyRSA 다운로드
wget -P ~/openVPN/ https://github.com/OpenVPN/easy-rsa/releases/download/v3.0.4/EasyRSA-3.0.4.tgz
cd ~/openVPN
tar xvf EasyRSA-3.0.4.tgz
1.3 EasyRSA 설정정보 수정
cd ~/openVPN/EasyRSA-3.0.4/
cp vars.example vars
vi vars
아래 내용을 자신에 맞게 수정한 다음 주석 제거
수정 전
#set_var EASYRSA_REQ_COUNTRY "US"
#set_var EASYRSA_REQ_PROVINCE "California"
#set_var EASYRSA_REQ_CITY "San Francisco"
#set_var EASYRSA_REQ_ORG "Copyleft Certificate Co"
#set_var EASYRSA_REQ_EMAIL "me@example.net"
#set_var EASYRSA_REQ_OU "My Organizational Unit"
수정 후
set_var EASYRSA_REQ_COUNTRY "KR"
set_var EASYRSA_REQ_PROVINCE "Daejeon"
set_var EASYRSA_REQ_CITY "Daejeon"
set_var EASYRSA_REQ_ORG "KAIST"
set_var EASYRSA_REQ_EMAIL "your_email@kaist.ac.kr"
set_var EASYRSA_REQ_OU "USRG"
이후 저장
1.4 수정한 설정정보 파일(vars)를 바탕으로 EasyRSA 서버 구축
./easyrsa init-pki
1.5 인증서와 키 파일 생성
./easyrsa build-ca nopass
Common Name 물으면 Enter 치고 넘어감.
1.6 openVPN에서 사용할 인증서와 key 파일 생성
./easyrsa gen-req server nopass
server는 장비 이름을 의미함.
Common Name 물으면 Enter 치고 넘어감.
다 실행하면 server.req, server.key 파일 생성완료.
1.7 생성한 key 파일을 openvpn 디렉터리에 복사
openVPN 디렉터리는 /etc/openvpn/
sudo cp ~/openVPN/EasyRSA-3.0.4/pki/private/server.key /etc/openvpn/
1.8 인증서 파일 생성
./easyrsa sign-req server server
첫 번째 server는 request type, 두 번째 server는 key파일의 이름 의미.
Confirm request details: 물으면 yes 입력
1.9 생성된 server.crt, ca.crt 파일을 openVPN 폴더로 복사
sudo cp ~/openVPN/EasyRSA-3.0.4/pki/issued/server.crt /etc/openvpn/
sudo cp ~/openVPN/EasyRSA-3.0.4/pki/ca.crt /etc/openvpn/
1.10 Diffie-Hellman key 생성
./easyrsa gen-dh
몇 초 정도 걸리며, 완료하면 dh.pem 파일이 만들어짐.
1.11 openvpn을 이용해서 ta.key 파일 생성
openvpn --genkey --secret ta.key
1.12 생성된 dh.pem, ta.key 파일을 openVPN 폴더로 복사.
sudo cp ~/openVPN/EasyRSA-3.0.4/ta.key /etc/openvpn/
sudo cp ~/openVPN/EasyRSA-3.0.4/pki/dh.pem /etc/openvpn/
1.13 client 인증서 담을 폴더 생성
mkdir -p ~/openVPN/client-configs/keys
chmod -R 700 ~/openVPN/client-configs
1.14 vpn 계정 인증서 생성
vpn 계정 이름은 yong
cd ~/openVPN/EasyRSA-3.0.4/
./easyrsa gen-req yong nopass
Common Name 물으면 엔터.
완료하면 yong.req, yong.key 파일 생성.
1.15 생성한 yong.key 파일을 keys 폴더에 복사.
cp pki/private/yong.key ~/openVPN/client-configs/keys/
1.16 생성한 yong.key 파일을 활용해서 인증서 생성.
./easyrsa sign-req client yong
Confirm request details: 나오면 yes 입력.
yong.crt 파일 생성됨.
1.17 만들어진 인증서 파일을 클라이언트의 keys 폴더에 복사.
sudo cp pki/issued/yong.crt ~/openVPN/client-configs/keys/
1.18 ca.crt, ta.key 파일도 클라이언트의 keys 폴더로 복사.
sudo cp ~/openVPN/EasyRSA-3.0.4/ta.key ~/openVPN/client-configs/keys/
sudo cp /etc/openvpn/ca.crt ~/openVPN/client-configs/keys/
이제 yong 계정이 openvpn 계정으로 사용되기 위한 인증서 준비가 끝남.
2. openVPN 및 서버 설정
2.1 openVPN에서 제공하는 sample config files를 복사하고 압축 풀기
sudo cp /usr/share/doc/openvpn/examples/sample-config-files/server.conf.gz /etc/openvpn/
sudo gzip -d /etc/openvpn/server.conf.gz
2.2 server.conf 파일 열기
sudo vi /etc/openvpn/server.conf
2.3 tls-auth ta.key 0 주석 제거되어있는지 확인 / 바로 밑에 key-direction 0 추가
# For extra security beyond that provided
# by SSL/TLS, create an "HMAC firewall"
# to help block DoS attacks and UDP port flooding.
#
# Generate with:
# openvpn --genkey --secret ta.key
#
# The server and each client must have
# a copy of this key.
# The second parameter should be '0'
# on the server and '1' on the clients.
tls-auth ta.key 0 # This file is secret
key-direction 0
2.4 cipher AES-256-CBC 주석 제거 확인 / 바로 밑에 auth SHA256 추가
# Select a cryptographic cipher.
# This config item must be copied to
# the client config file as well.
# Note that v2.4 client/server will automatically
# negotiate AES-256-GCM in TLS mode.
# See also the ncp-cipher option in the manpage
cipher AES-256-CBC
auth SHA256
2.5 dh dh2040.pem 찾아서 dh dh.pem으로 수정
# Diffie hellman parameters.
# Generate your own with:
# openssl dhparam -out dh2048.pem 2048
dh dh.pem
2.6 user, group 앞에있는 주석 제거
# It's a good idea to reduce the OpenVPN
# daemon's privileges after initialization.
#
# You can uncomment this out on
# non-Windows systems.
user nobody
group nogroup
2.7 (중요) 서버에서 client의 subnet을 추가하기 위한 설정 진행.
Expanding The VPN To Include Additional Machines | OpenVPN
Once the VPN is operational, it may be desirable to expand the scope of the VPN so that clients can reach multiple machines on the server network.
openvpn.net
* 설명
LTE로 연결된 컴퓨터는 192.168.2.0 subnet 네트워크를 가지고 있다.
해당 컴퓨터의 이 네트워크에서 ip 주소는 192.168.2.15이며, 네트워크에 있는 다른 기기는 192.168.2.10 주소를 가진다.
이 네트워크를 연결하기 위해 설정을 추가한다.
아래 부분에서 client-config-dir ccd 주석 해제 및
바로 아래줄의 route 설정을 subnet 주소에 맞게 변경.
# EXAMPLE: Suppose the client
# having the certificate common name "Thelonious"
# also has a small subnet behind his connecting
# machine, such as 192.168.40.128/255.255.255.248.
# First, uncomment out these lines:
client-config-dir ccd
route 192.168.2.0 255.255.255.0
2.8 (중요) LTE로 연결된 컴퓨터의 subnet을 추가하기 위한 설정 파일 생성
openvpn 폴더에서 ccd 폴더를 생성한 다음,
ccd 폴더 안에서 yong(위에서 만든 vpn 계정 이름) 파일 생성.
sudo mkdir /etc/openvpn/ccd
sudo vi /etc/openvpn/ccd/yong
2.9 설정 파일 수정
아래 내용 추가
iroute 192.168.2.0 255.255.255.0
이후 저장
* 참고
https://dejavuqa.tistory.com/243
openVPN 설치 (on Ubuntu 18.04)
사내에서 vpn을 한번 사용해 보더니 수요가 늘었습니다. iptime T24000을 라우터로 사용하고 있는데 VPN을 다섯개만 지원합니다. 그래서 openVPN Server를 구성해 사용하겠습니다. openVPN Server 구축에 참고
dejavuqa.tistory.com
- 위 블로그에서는 tcp, port 443을 설정했지만 난 반영하지 않고 udp, port 1194 유지.
- DNS changes to redirect ~ 부분은 반영하지 않음.
- 방화벽 부분도 반영하지 않음.
2.10 네트워크 설정 변경
/etc/sysctl.conf 파일 열기
sudo vi /etc/sysctl.conf
2.11 net.ipv4.ip_forward 찾아서 주석 해제 후 아래와 같이 수정
# Uncomment the next line to enable packet forwarding for IPv4
net.ipv4.ip_forward=1
이후 파일 저장
2.12 변경 사항 적용
sudo sysctl -p
실행하면 터미널에 다음 내용이 출력됨
net.ipv4.ip_forward = 1
3. openvpn 실행 후 확인
sudo systemctl start openvpn@server
끄는 명령어
$ sudo systemctl stop openvpn@server
ifconfig 입력하면 tun0 인터페이스에 10.8.0.1 주소를 가짐.
4. client 설정
4.1 client-configs 폴더 아래 files라는 하위 폴더 생성 및 필요한 파일 복사
mkdir -p ~/openVPN/client-configs/files
sudo cp ~/openVPN/client-configs/keys/ta.key ~/openVPN/client-configs/files/
sudo chmod 644 ~/client-configs/files/ta.key
4.2 client configuration 샘플파일을 client-configs 폴더에 복사
cp /usr/share/doc/openvpn/examples/sample-config-files/client.conf ~/openVPN/client-configs/base.conf
4.3 base.conf 파일 수정을 위해 편집기로 열기.
sudo vim ~/client-configs/base.conf
4.4 remote를 찾아서 openvpn 서버의 ip(여기서는 Desktop PC의 public ip)를 추가.
# The hostname/IP and port of the server.
# You can have multiple remote entries
# to load balance between the servers.
remote "your IP" 1194
4.5 user, groups 앞의 주석 제거
Downgrade privileges after initialization (non-Windows only)
user nobody
group nogroup
4.6 SSL/TLS params를 찾은 다음, ca,cert,key 설정 모두 주석처리
# SSL/TLS parms.
# See the server config file for more
# description. It's best to use
# a separate .crt/.key file pair
# for each client. A single ca
# file can be used for all clients.
#ca ca.crt
#cert client.crt
#key client.key
4.7 ciper, auth 찾아서 server의 config와 동일하게 수정
cipher : AES-256-CBC 그대로 / auth SHA256 추가 / key-direction 1 추가
# Select a cryptographic cipher.
# If the cipher option is used on the server
# then you must also specify it here.
# Note that v2.4 client/server will automatically
# negotiate AES-256-GCM in TLS mode.
# See also the ncp-cipher option in the manpage
cipher AES-256-CBC
auth SHA256
key-direction 1
4.8 리눅스 클라이언트에서 사용하는 아래 항목을 주석처리 한 채로 추가.
base.conf 파일 가장 아래.
# script-security 2
# up /etc/openvpn/update-resolv-conf
# down /etc/openvpn/update-resolv-conf
이후 파일 저장.
4.9 script 생성.
make_config.sh 파일 생성
sudo vi ~/openVPN/client-configs/make_config.sh
4.10 아래 내용 입력
#!/bin/bash
# First argument: Client identifier
KEY_DIR=~/client-configs/keys
OUTPUT_DIR=~/client-configs/files
BASE_CONFIG=~/client-configs/base.conf
cat ${BASE_CONFIG} \
<(echo -e '<ca>') \
${KEY_DIR}/ca.crt \
<(echo -e '</ca>\n<cert>') \
${KEY_DIR}/${1}.crt \
<(echo -e '</cert>\n<key>') \
${KEY_DIR}/${1}.key \
<(echo -e '</key>\n<tls-auth>') \
${KEY_DIR}/ta.key \
<(echo -e '</tls-auth>') \
> ${OUTPUT_DIR}/${1}.ovpn
이후 저장
4.11 생성한 스크립트 파일 권한 추가
sudo chmod 700 ~/openVPN/client-configs/make_config.sh
4.12 (중요) openvpn 계정 생성
아래 명령어 실행
cd ~/openVPN/client-configs
sudo ./make_config.sh yong
yong.ovpn 파일이 files 폴더 안에 생성.
5. 라우터 포트 포워딩 설정
6. LTE 모듈을 활용한 컴퓨터에 생성한 key, ovpn 파일을 넘겨줌.
https://kkastory.tistory.com/13?category=796285
[OpenVPN] Ubuntu 18.04 에 OpenVPN server 구축하기
이모티콘 처리 업데이투 dejavuqa.tistory.com/243 openVPN 설치 (on Ubuntu 18.04) 사내에서 vpn을 한번 사용해 보더니 수요가 늘었습니다. iptime T24000을 라우터로 사용하고 있는데 VPN을 다섯개만 지원합..
kkastory.tistory.com
* 넘겨줄 파일
ta.key
위치 : client-configs/files/ta.key
yong.ovpn
위치 : client-configs/files/yong.ovpn
USB를 이용하든, 외부 메일을 이용하든, sftp를 이용하든, 원하는 방법을 통해서 이동
7. LTE 모듈로 인터넷엔 연결된 컴퓨터에서 서버에 연결.
https://kkastory.tistory.com/14?category=796285
[OpenVPN] Ubuntu 18.04 에서 OpenVPN 에 Client 계정 추가 & 실행하기
dejavuqa.tistory.com/246?category=299614 openVPN에 Client 계정 추가하기 openVPN server를 만들었으니 Client를 추가해 보겠습니다. server 구성은 아래와 같이 진행되었습니다. openVPN 설치 (on Ubuntu 18.0..
kkastory.tistory.com
7.1 openvpn 설치
sudo apt install openvpn
7.2 서버에 연결
--------------------------------------------------------------------------------------------------------------------------------------------------------
* 서버가 실행 중이라고 가정. 만약 실행 중이지 않다면 아래 명령어를 server 컴퓨터에서 실행
sudo systemctl start openvpn@server
--------------------------------------------------------------------------------------------------------------------------------------------------------
ta.key, yong.ovpn 파일 넘겨받았다고 가정.
--------------------------------------------------------------------------------------------------------------------------------------------------------
아래 명령어를 입력해서 서버에 연결
sudo openvpn --config yong.ovpn --connect-retry 2
--connect-retry 2 는 연결이 끊어졌을 때 재연결까지 대기하는 시간.
7.3 연결 확인
ifconfig
tun0 인터페이스로 10.8.0.x 주소로 연결.
8. ROS 주소 설정
* VPN의 server 컴퓨터는 ROS에서 Slave
* LTE 모듈로 연결되는 컴퓨터는 ROS에서 Master
( 설령 vpn이 끊어지더라도, LTE로 연결되는 컴퓨터에서는 ROS가 정상적으로 작동하도록 하기 위함.)
* Desktop 컴퓨터(VPN server, ROS slave)
vi ~/.bashrc
가장 아래줄에 다음 내용 추가.
export ROS_MASTER_URI=http://192.168.2.15:11311
export ROS_IP=10.8.0.1
192.168.2.15는 LTE 모듈 컴퓨터의 Subnet으로, 이더넷 연결을 통해 절대 끊어지지 않음이 보장됨
* LTE 모듈로 연결된 컴퓨터(VPN client, ROS master)
vi ~/.bashrc
가장 아래줄에 다음 내용 추가
export ROS_MASTER_URI=http://192.168.2.15:11311
export ROS_IP=192.168.2.15
#export ROS_IP=10.8.0.x 도 가능하지만 추천하지 않음.
설정 끝.
LTE 모듈로 연결된 컴퓨터에서 roscore 돌린다음 토픽을 publish하면
server 컴퓨터에서 subscribe 가능 확인.
9. 추가 연결 client 생성
https://kkastory.tistory.com/14?category=796285
[OpenVPN] Ubuntu 18.04 에서 OpenVPN 에 Client 계정 추가 & 실행하기
dejavuqa.tistory.com/246?category=299614 openVPN에 Client 계정 추가하기 openVPN server를 만들었으니 Client를 추가해 보겠습니다. server 구성은 아래와 같이 진행되었습니다. openVPN 설치 (on Ubuntu 18.0..
kkastory.tistory.com
9.1 EasyRSA 폴더로 이동해서 key파일, certificate equest 파일 만든다.
cd ~/openVPN/EasyRSA-3.0.4
./easyrsa gen-req laptop nopass
이때 계정명은 laptop으로 설정.
9.2 key 파일로 인증서 만든다.
./easyrsa sign-req client laptop
이후 Confirm request details: 나오면 yes 입력.
9.3 만든 key, crt 파일을 client-configs/keys로 복사
sudo cp pki/private/laptop.key ~/openVPN/client-configs/keys/
sudo cp pki/issued/laptop.crt ~/openVPN/client-configs/keys/
9.4 이전에 만든 make_config.sh 파일을 실행해서 ovpn 파일 만들어준다.
cd ~/openVPN/client-configs
sudo ./make_config.sh laptop
9.5 ovpn 파일과 ta.key 파일을 새 client가 될 컴퓨터로 넘겨준다.
9.6 실행
# in the directory which has laptop.ovpn
openvpn --config laptop.ovpn --connect-retry 2