Linux/Ubuntu2021. 3. 20. 23:59

$ sudo apt install tasksel -y
$ sudo tasksel

VNC is a system that allows you to remotely control another computer. It allows you to relay your mouse and keyboard inputs as if you were physically sitting in front of the system, when in fact you could be on the other side of the world.

In this guide, we will go over the steps to setup a VNC server on your Ubuntu 20.04 system. When we're done, you'll be able to access your system remotely from anywhere, provided that your client system and the VNC server have an internet connection.

In this tutorial you will learn:

  • How to install and configure TightVNC Server
  • How to install and configure XFCE desktop manager
  • How to connect to our VNC server with TightVNC Viewer
  • How to tunnel VNC connections through SSH
  • How to manage multiple VNC desktops with a Systemd script

Connecting to a VNC server

Software Requirements and Linux Command Line ConventionsCategoryRequirements, Conventions or Software Version Used

System Installed or upgraded Ubuntu 20.04 Focal Fossa
Software TightVNC Server, XFCE desktop manager, TightVNC Viewer
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # - requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ - requires given linux commands to be executed as a regular non-privileged user

Install VNC server


Looking for Linux Systems Analyst !

The UAF Geophysical Institute, is looking for an experienced Linux Systems Analyst to join their team of research cyber infrastructure analysts and engineers. LOCATION: Fairbanks, Alaska, USA

APPLY NOW



We'll need to install a few packages first. There are multiple options for VNC server software, but we'll be going with TightVNC Server for this tutorial. Along with VNC, we also need a graphical desktop. XFCE is a good choice, so that's what we'll be covering here.

    1. Start by opening a terminal and entering the following command to install TightVNC server and the XFCE desktop manager core files:
      $ sudo apt install tightvncserver xfce4 xfce4-goodies
    2. After the packages are done installing, we need to configure a username and password that will be used to connect to the VNC server by a client. Create a password with this command:$ vncpasswd
    3. Next, let's configure VNC to start the XFCE desktop environment when a client connects. Use nano or the text editor of your preference (ex. Atom, Sublime) to create the following file:
      $ nano ~/.vnc/xstartup

 


$ systemctl set-default graphical.target


  1. Insert the following few lines and then save your changes and exit the file:

    #!/bin/sh
    unset SESSION_MANAGER
    unset DBUS_SESSION_BUS_ADDRESS
    startxfce4 &
    xstartup configuration file
  2. With those changes made, edit the permissions of the file to make it executable:$ chmod +x ~/.vnc/xstartup

Start VNC server



VNC runs as a service on your system. In order for clients to connect, the VNC server must obviously be running and listening for incoming connection attempts. With our configuration complete, we are ready to start VNC server with the following command:

$ vncserver

If your terminal returns some output like in the screenshot below, your VNC server is running correctly.

Launch VNC server

VNC will use a new port for every remote desktop that is created. At this point, your system should be listening on port 5901 for incoming VNC connections. You can see this for yourself with the ss -ltn command:

$ ss -ltn

VNC listening on port 5901

If you have the UFW firewall enabled, you'll need to open port 5901 so it doesn't block incoming VNC connections:

$ sudo ufw allow from any to any port 5901 proto tcp Rule added Rule added (v6)


Looking for Linux Systems Analyst !

The UAF Geophysical Institute, is looking for an experienced Linux Systems Analyst to join their team of research cyber infrastructure analysts and engineers. LOCATION: Fairbanks, Alaska, USA

APPLY NOW



Connect to VNC server

There are a lot of choices in the way of VNC clients and any of them should be capable of connecting to your newly launched VNC server. If you don't already have a preferred client to use, follow along with us as we cover the instructions for connecting to the VNC server with the TightVNC Viewer.

    1. Start by installing the xtightvncviewer package on your Ubuntu client system:$ sudo apt install xtightvncviewer
    2. Once the VNC client is installed, you can use the vncviewer command, followed by either the hostname or IP address of the VNC server, in order to connect to it.$ vncviewer linuxconfig.org:1 Enter your password that we created previously when setting up VNC Server. If all went well, you will be presented with a XFCE desktop session running on the remote VNC server Ubuntu system:
      Connect to VNC server


Tunnel VNC through SSH

For extra security, you can tunnel the VNC connection through SSH on your VNC server. Of course, this is assuming that you have SSH access on the VNC server. If you'd like the added security, follow along with these steps:

    1. If you don't already have SSH installed, that's a pretty obvious prerequisite for this to work:$ sudo apt install ssh
    2. Next, create an SSH tunnel on a local port 5901 leading to a remote port 5901 on your VNC server. In the following command, make sure you replace user1 and linuxconfig with the username and hostname of your VNC server:$ ssh -L 5901:127.0.0.1:5901 -N -f -l user1 linuxconfig The above command will open a local port 5901 on a localhost loopback network interface 127.0.0.1:

      $ ss -ltn

      State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 0.0.0.0:22 0.0.0.0:* LISTEN 0 5 127.0.0.1:631 0.0.0.0:* LISTEN 0 128 127.0.0.1:6010 0.0.0.0:* LISTEN 0 128 127.0.0.1:6011 0.0.0.0:* LISTEN 0 128 0.0.0.0:38299 0.0.0.0:* LISTEN 0 128 127.0.0.1:5901 0.0.0.0:*


  1. Next, use the local port 5901 to connect to a remote VNC server via the SSH tunnel:$ vncviewer localhost:1

VNC server system startup script

While this configuration works, you may have a scenario where you need to manage multiple VNC desktop sessions. In that case, creating a systemd startup script can facilitate that.

Use nano or another text editor to create the following file:

$ sudo nano /etc/systemd/system/vncserver@.service

Once you have the file opened, insert the following lines while replacing the user1 username with the username of your VNC user on Line 7 and Line 10. Optionally, change the screen resolution settings and apply other vncserver options or arguments:

[Unit]
Description=Systemd VNC server startup script for Ubuntu 20.04

After=syslog.target network.target

[Service]
Type=forking

User=user1
ExecStartPre=-/usr/bin/vncserver -kill :%i &> /dev/null
ExecStart=/usr/bin/vncserver -localhost no -depth 24 -geometry 1280x1024 :%i

PIDFile=/home/user1/.vnc/%H:%i.pid
ExecStop=/usr/bin/vncserver -kill :%i

[Install]
WantedBy=multi-user.target

 


Looking for Linux Systems Analyst !

The UAF Geophysical Institute, is looking for an experienced Linux Systems Analyst to join their team of research cyber infrastructure analysts and engineers. LOCATION: Fairbanks, Alaska, USA

APPLY NOW



Systemd startup file

Next, reload Systemd for the changes to take effect:

$ sudo systemctl daemon-reload

To start VNC desktop 1, enter:

$ sudo service vncserver@1 start

To stop VNC desktop 1, execute:

$ sudo service vncserver@1 stop

The following linux command will enable the VNC desktop 1 to start after reboot:

$ sudo systemctl enable vncserver@1

$ sudo systemctl start vncserver@1
$ sudo systemctl status vncserver@1
$ vncserver -localhost no

To start VNC desktop 2, enter:

$ sudo service vncserver@2 start

 

출처: linuxconfig.org/vnc-server-on-ubuntu-20-04-focal-fossa-linux

 

 

1. 우분투 데스크탑 설치하기

우선 다음 명령어를 입력하여 우분투를 업데이트해줍니다.

sudo apt update && sudo apt upgrade

다음 명령어를 입력하여 우분투 데스크탑을 설치해줍니다.

sudo apt-get install ubuntu-desktop

시간이 좀 오래 걸리니 기다려주세요.

 

2. VNC 설치하기

VNC를 통해 원격접속하기 위해서는 원격 접속을 당할 시스템과 원격 접속을 할 시스템 양쪽 모두에 VNC 프로그램이 설치되어있어야 합니다.

WSL2의 우분투는 원격 접속을 당할 시스템이므로 VNC서버를 설치해주겠습니다.

 

다음 명령어를 입력해 VNC서버를 설치해줍니다.

sudo apt-get install tigervnc-standalone-server

이번엔 dotnet-runtime을 설치해줍니다.

sudo wget https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
sudo apt update
sudo apt install dotnet-runtime-3.1

이번엔 systemd-genie를 설치해줍니다.

아래 명령어를 입력하여 파일을 하나 만들어주세요.

sudo nano /etc/apt/sources.list.d/wsl-translinux.list

만들어진 파일에 다음과 같은 내용을 붙여넣어줍니다.

deb [trusted=yes] https://wsl-translinux.arkane-systems.net/apt/ /

이제 다음 명령어를 입력하여 설치를 마무리합니다.

sudo apt update
sudo apt install systemd-genie

 

 

3. VNC 환경설정

다음 명령어를 입력하여 비밀번호를 설정해줍니다.

선택 옵션에서는 n을 입력하여주세요.

vncpasswd

다음 명령어를 입력하여 한번 더 반복해주세요.

sudo vncpasswd

마지막입니다.

한번 더 반복해주세요.

sudo -u gdm vncpasswd

이제 기존의 X를 Xvnc로 대체해주겠습니다.

다음 명령어를 입력하여 Xorg스크립트를 백업해주세요.

sudo mv /usr/bin/Xorg /usr/bin/Xorg_old

이제 다음 명령어를 입력하여 새로운 Xorg 스크립트를 만들어줍니다.

sudo nano /usr/bin/Xorg

만들어진 스크립트에 다음 내용을 붙여넣기 해줍니다.

#!/bin/bash
for arg do
  shift
  case $arg in
    # Xvnc doesn't support vtxx argument. So we convert to ttyxx instead
    vt*)
      set -- "$@" "${arg//vt/tty}"
      ;;
    # -keeptty is not supported at all by Xvnc
    -keeptty)
      ;;
    # -novtswitch is not supported at all by Xvnc
    -novtswitch)
      ;;
    # other arguments are kept intact
    *)
      set -- "$@" "$arg"
      ;;
  esac
done

# Here you can change or add options to fit your needs
command=("/usr/bin/Xvnc" "-geometry" "1024x768" "-PasswordFile" "${HOME:-/root}/.vnc/passwd" "$@") 

systemd-cat -t /usr/bin/Xorg echo "Starting Xvnc:" "${command[@]}"

exec "${command[@]}"

이제 다음 명령어로 파일에 대한 권한을 설정해줍니다.

sudo chmod 0755 /usr/bin/Xorg

다음 명령어를 입력하여 재부팅시켜주세요

genie -s

이제 준비가 끝났습니다.

 

4. IP 확인하기

다음 명령어를 입력하여 net-tools를 설치해주세요.

sudo apt install net-tools

다음 명령어로 IP를 확인해줍니다.

ifconfig

제가 빨갛게 칠한 부분이 여러분의 IP입니다.

출처: bebutae.tistory.com/145?category=943715

'Linux > Ubuntu' 카테고리의 다른 글

Ubuntu 웹서버 구축  (0) 2021.03.21
Ubuntu Network & WOL  (0) 2021.03.17
Ubuntu 20.04 LTS Server 버전 설치  (0) 2021.03.16
Posted by iWithJoy