リモートサーバーでデスクトップを実行する
通常、リモートLinuxサーバーで作業する場合はsshターミナルを使用します。ただし、サーバー上でGUIアプリを実行し、それをかなりの時間実行し続ける必要がある場合があります。
最近、似たようなことをしなければならなかったので、デスクトップでUbuntuサーバーをセットアップし、VNC経由でアクセスしました。
アイデアは単純です。選択したデスクトップ環境をサーバーにインストールします。このチュートリアルでは、GnomeやKDEなどの大きなものに比べてフットプリントが軽いXfceを使用します。
次に、vncサーバーを使用してデスクトップ環境を起動し、ローカルデスクトップマシンからvncクライアントを介してアクセスするXディスプレイセッションを作成します。
デスクトップ環境とVNCサーバーをインストールする
Xfceは軽量デスクトップであり、リモートサーバーでの使用に最適です。まず、xfceパッケージとtightvncサーバーをインストールします。実際のインストールを行う前に、パッケージキャッシュを更新することをお勧めします。
sudo apt-get update sudo apt-get install xfce4 xfce4-goodies tightvncserver
これはパッケージをインストールするだけで、何も開始しないことに注意してください。このガイドの後半で、独自の設定でvncserverを起動します。
dpkgプロセスが予期せず終了した場合は、次のコマンドを実行する必要があります-
# sudo dpkg --configure -a
vncの新しいユーザーを作成する
次に行うことは、vncセッション中に使用されるUNIXユーザーを作成することです。ユーザー名は何でもかまいません。 adduserコマンドを使用します。
# adduser mike
Adding user `mike' ...
Adding new group `mike' (1001) ...
Adding new user `mike' (1001) with group `mike' ...
Creating home directory `/home/mike' ...
Copying files from `/etc/skel' ...
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for mike
Enter the new value, or press ENTER for the default
Full Name []:
Room Number []:
Work Phone []:
Home Phone []:
Other []:
Is the information correct? [Y/n]
# vncserverは、このUNIXユーザーでデスクトップ環境を起動します。つまり、リモートデスクトップで作業するときは、このユーザーになる必要があります
ユーザーの「vncパスワード」を設定します
vncサーバーは、vncクライアントを介してvncサーバーにログインするために使用される個別のパスワードを維持します。このパスワードは、UNIXユーザーのパスワードとは異なります。 vncpasswdコマンドを使用して構成されます。
最初に、前の手順で作成したユーザー「mike」に切り替えて、vncサーバーのパスワードを設定します。
# su - mike
次に、vncpasswdコマンドを使用します
$ vncpasswd Using password file /home/mike/.vnc/passwd VNC directory /home/mike/.vnc does not exist, creating. Password: Verify: Would you like to enter a view-only password (y/n)? example@unixlinux.online:~$
passwdファイルは存在せず、このステップで初めて作成されたことに注意してください。
以前にvncserverコマンドをすでに実行している場合は、ファイルが作成されています。初めてvncserverを実行すると、デフォルトの起動スクリプトが作成されます
$ vncserver You will require a password to access your desktops. Password: Password too short example@unixlinux.online:~$ vncserver You will require a password to access your desktops. Password: Verify: Would you like to enter a view-only password (y/n)? n New 'X' desktop is desktop:1 Creating default startup script /home/enlightened/.vnc/xstartup Starting applications specified in /home/enlightened/.vnc/xstartup Log file is /home/enlightened/.vnc/desktop:1.log
ただし、vncserverコマンドを実行する必要はありません。起動スクリプトを使用して自動的に起動されます。
xstartupスクリプトを作成する
次の重要なファイルはxstartupスクリプトです。どのXアプリを起動するかについての説明が含まれています。デスクトップ環境は、起動する必要のあるXアプリケーションです。
ファイルがすでに存在する場合は、最初にファイルのバックアップを取ります
mv ~/.vnc/xstartup ~/.vnc/xstartup.bak
次に、nanoを使用して編集します
example@unixlinux.online:~$ nano .vnc/xstartup
注-これは、ユーザーvncのホームディレクトリ、つまり/home/mike/.vnc/xstartup
内にあります。xstartupスクリプトに次の行を入力します
#!/bin/bash xrdb $HOME/.Xresources startxfce4 &
startxfce4コマンドは、xfceデスクトップを起動します。ファイルを保存して閉じます。
xstartupファイルを実行可能にします。これは、vncserverがこのファイルを実行できるようにするために必要です。
$ chmod +x ~/.vnc/xstartup
vncサービスファイルを作成する
次のステップは、サービスコマンドを使用してvncサーバーを起動でき、毎回vncserverコマンドを実行する必要がないように、vncサービスファイルを作成することです。
USER変数に正しいユーザー名を入力してください。これは、vncサーバーがデスクトップセッションを開始するために使用するユーザーです。
example@unixlinux.online:~# sudo nano /etc/init.d/vncserver
次のスクリプトを貼り付けます
#!/bin/bash
PATH="$PATH:/usr/bin/"
export USER="mike"
DISPLAY="1"
DEPTH="16"
GEOMETRY="1024x768"
OPTIONS="-depth ${DEPTH} -geometry ${GEOMETRY} :${DISPLAY}"
. /lib/lsb/init-functions
case "$1" in
start)
log_action_begin_msg "Starting vncserver for user '${USER}' on localhost:${DISPLAY}"
su ${USER} -c "/usr/bin/vncserver ${OPTIONS}"
;;
stop)
log_action_begin_msg "Stoping vncserver for user '${USER}' on localhost:${DISPLAY}"
su ${USER} -c "/usr/bin/vncserver -kill :${DISPLAY}"
;;
restart)
$0 stop
$0 start
;;
esac
exit 0 ファイルを保存して閉じます。実行可能にする
# chmod +x /etc/init.d/vncserver
サービスを開始する
これですべてが読み取られます。 vncserverを実行し、ステップをテストするときが来ました。
最初にsystemctlをリロードして、vncserver起動スクリプトを使用できるようにします。
systemctl daemon-reload
次に、vncserverを起動します。ポート5901でサーバーを起動します
# service vncserver start
実行されていることを確認します
example@unixlinux.online:~# service vncserver status
● vncserver.service
Loaded: loaded (/etc/init.d/vncserver; bad; vendor preset: enabled)
Active: active (exited) since Thu 2017-03-02 05:36:42 UTC; 6s ago
Docs: man:systemd-sysv-generator(8)
Process: 24877 ExecStart=/etc/init.d/vncserver start (code=exited, status=0/SUCCESS)
Mar 02 05:36:40 bintu systemd[1]: Starting vncserver.service...
Mar 02 05:36:40 bintu vncserver[24877]: * Starting vncserver for user 'vnc' on localhost:1...
Mar 02 05:36:40 bintu su[24885]: Successful su for vnc by root
Mar 02 05:36:40 bintu su[24885]: + ??? root:vnc
Mar 02 05:36:40 bintu su[24885]: pam_unix(su:session): session opened for user vnc by (uid=0)
Mar 02 05:36:42 bintu vncserver[24877]: New 'X' desktop is bintu:1
Mar 02 05:36:42 bintu vncserver[24877]: Starting applications specified in /home/vnc/.vnc/xstartup
Mar 02 05:36:42 bintu vncserver[24877]: Log file is /home/vnc/.vnc/bintu:1.log
Mar 02 05:36:42 bintu systemd[1]: Started vncserver.service. $ cat ~/.vnc/*.pid 18577 18731
# ps -ef | grep tightvnc vnc 24574 1 0 05:32 ? 00:00:00 Xtightvnc :1 -desktop X -auth /home/vnc/.Xauthority -geometry 1024x768 -depth 16 -rfbwait 120000 -rfbauth /home/vnc/.vnc/passwd -rfbport 5901 -fp /usr/share/fonts/X11/misc/,/usr/share/fonts/X11/Type1/,/usr/share/fonts/X11/75dpi/,/usr/share/fonts/X11/100dpi/ -co /etc/X11/rgb root 24744 10412 0 05:33 pts/0 00:00:00 grep --color=auto tightvnc example@unixlinux.online:~#
VNCサーバーの開いているポートを確認します。 vncクライアントから接続する場合は正しいポート番号が必要です
# netstat -nlp | grep vnc tcp 0 0 0.0.0.0:5901 0.0.0.0:* LISTEN 24574/Xtightvnc tcp 0 0 0.0.0.0:6001 0.0.0.0:* LISTEN 24574/Xtightvnc unix 2 [ ACC ] STREAM LISTENING 5225386 24574/Xtightvnc /tmp/.X11-unix/X1
スクリプトを直接呼び出すことでVncサーバーを起動することもできます。
# /etc/init.d/vncserver start [ ok ] Starting vncserver (via systemctl): vncserver.service. example@unixlinux.online:~#
vncserverを停止するには
# service vncserver stop
デスクトップにvncviewerクライアントをインストールする
これで、vncサーバーがGUIデスクトップ環境で稼働しているので、vncクライアントをインストールしてリモートデスクトップにアクセスします。
Ubuntuにxtightvncviewerをインストールします。
$ sudo apt-get install xtightvncviewer
次に、vncviewerコマンドを使用してリモートvncサーバーに接続します。
$ vncviewer -quality 5 -encodings "copyrect tight hextile zlib corre rre raw" -compresslevel 5 IPADDR:5901
転送される画像データを圧縮して高速化するために、低品質の圧縮エンコーディングを使用しています。
KRDCのような他のvncビューアを使用すると遅くなる可能性があります。
リソース
詳細については、これらの便利なリンクを確認してください-
http://tightvnc.com/vncviewer.1.htmlhttps://www.digitalocean.com/community/tutorials/how-to-install-and-configure-vnc-on-ubuntu-16-04
https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-vnc-on-ubuntu-14-04
https://www.digitalocean.com/community/tutorials / how-to-setup-vnc-for-ubuntu-12