Automatically start VNC server on startup

许典
2023-12-01

Automatically start VNC server on startup

 

If you want a moredynamic configuration and the ability to connect for multiple users then there isa better way to do this. As root create the file (and directory if it doesn'texist) /etc/sysconfig/vncservers i.e. do:

mkdir -p /etc/vncserver

touch /etc/vncserver/vncservers.conf

Add servers asneeded for each user by adding something like the following to thevncservers.conf file you just created:

VNCSERVERS="1:justin 2:bob"

VNCSERVERARGS[1]="-geometry 1920x1080-depth 24"

VNCSERVERARGS[2]="-geometry 800x600-depth 8"

next create anempty init script and make it executable:

touch /etc/init.d/vncserver

chmod +x /etc/init.d/vncserver

add the followingto /etc/init.d/vncserver:

#!/bin/bash

 

unset VNCSERVERARGS

VNCSERVERS=""

[ -f /etc/vncserver/vncservers.conf ]&& . /etc/vncserver/vncservers.conf

prog=$"VNC server"

 

start() {

       . /lib/lsb/init-functions

       REQ_USER=$2

       echo -n $"Starting $prog: "

       ulimit -S -c 0 >/dev/null 2>&1

       RETVAL=0

       for display in ${VNCSERVERS}

       do

                exportUSER="${display##*:}"

                if test -z "${REQ_USER}"-o "${REQ_USER}" == ${USER} ; then

                        echo -n"${display} "

                        unset BASH_ENV ENV

                       DISP="${display%%:*}"

                        exportVNCUSERARGS="${VNCSERVERARGS[${DISP}]}"

                       su ${USER} -c"cd ~${USER} && [ -f .vnc/passwd ] && vncserver :${DISP}${VNCUSERARGS}"

                fi

       done

}

 

stop() {

       . /lib/lsb/init-functions

       REQ_USER=$2

       echo -n $"Shutting down VNCServer: "

       for display in ${VNCSERVERS}

       do

                exportUSER="${display##*:}"

                if test -z"${REQ_USER}" -o "${REQ_USER}" == ${USER} ; then

                        echo -n"${display} "

                        unset BASH_ENV ENV

                       exportUSER="${display##*:}"

                        su ${USER} -c"vncserver -kill :${display%%:*}" >/dev/null 2>&1

                fi

       done

       echo -e "\n"

       echo "VNCServer Stopped"

}

 

case "$1" in

start)

start $@

;;

stop)

stop $@

;;

restart|reload)

stop $@

sleep 3

start $@

;;

condrestart)

if [ -f /var/lock/subsys/vncserver ]; then

stop $@

sleep 3

start $@

fi

;;

status)

status Xvnc

;;

*)

echo $"Usage: $0{start|stop|restart|condrestart|status}"

exit 1

esac

As Stephen mentionedin his answer you'll need to run vncserver AT LEAST ONCE AS EACH USER you wantto login as. I put that in caps because if you skip that step none of it willwork. So as root you could do:

su justin -c vncserver

su bob -c vncserver

This will create a.vnc directory in each users home dir with the appropriate startup scripts.

Finally, do thefollowing:

update-rc.d vncserver defaults 99

now you can eitherreboot or start the service manually by typing:

service vncserver start

 

 

 

 

 

 

How-To: Managing Services With Update-Rc.D

Postedby chantra

 

 on July 5th, 2007

Linuxservices can be started, stopped and reloaded with the use of scripts stockedin /etc/init.d/.

However,during start up or when changing runlevel, those scripts are searched in/etc/rcX.d/ where X is the runlevel number.

Thistutorial will explain how one can activate, deactivate or modify a servicestart up.

Wheninstalling a new service under debian, the default is to enable it. So forinstance, if you just installed apache2 package,after you installed it, apache service will be started and so will it be uponthe next reboots.

Ifyou do not use apache all the time, you might want to disable this service fromstarting up upon boot up and simply start it manually when you actually need itby running this command:

Youcould either disable this service on boot up by removing any symbolic links in /etc/rcX.d/SYYapache2 or by using update-rc.d.

Theadvantage of using update-rc.d is that itwill take care of removing/adding any required links to /etc/init.d automatically.
Taking apache2 as an example, let's examine how /etc/rcX.d is lookinglike:

Asyou can see, for runlevels 0, 1 and 6 there is a K at the beginning of the link, for runlevels 2, 3, 4and 5, there is a S. Those two letters stands for Kill and Start.
On Debian and Ubuntu, runlevels 2, 3, 4 and 5 are multi-users runlevels.
Runlevel 0 is Halt.
Runlevel 1 is single user mode
Runlevel 6 is reboot

1.Removing A Service

Ifyou want to totally disable apache2 service by hand, you would need to deleteevery single link in /etc/rcX.d/. Using update-rc.d it is as simple as:

Theuse of -f is to force the removal of the symlinks even if thereis still /etc/init.d/apache2.

Note: Thiscommand will only disable the service until next time the service is upgraded.If you want to make sure the service won't be re-enabled upon upgrade, youshould also type the following:
# update-rc.d apache2 stop 80 0 1 2 3 45 6 .

2.Adding A Service

2.1.Default Priorities

Now,if you want to re-add this service to be started on boot up, you can simplyuse:

2.2.Custom Priorities

Butas you can see, the default value is 20 which is pretty different than 91 ... aS20 link is started before a S91 and and K91 is kill before K20.
To force apache2 to be started with priorities 91 for both Start andKill, we need to use the following command:

2.3.Different Priorities For Start And Kill

Alternatively,if you want to set different priorities for Start than for Kill, let say Startwith 20 and Kill with 80, you will need to run:

3.Specifying Custom Runlevels

Finally,if you only want to Start and Kill on specific runlevels, like for instancestarting apache with priority 20 on runlevels 2, 3, 4 and 5 and Kill withpriority 80 on runlevels 0, 1 and 6:

Or,to start with priority 20 for runlevel 2, 3 and 4 and priority 30 for runlevel5 and kill with priority 80 for runlevel 0, 1 and 6:

 

Version1.0
Last edited 11/Jun/2014

Thisguide explains the installation and configuration of a VNC server on Ubuntu14.04 server. I use a server here to show you a ay to have a remote desktop ona root server in a datacenter. The same steps will work for Ubuntu desktops aswell.VNC is a very convinient way of administrating the Ubuntu 14.04 desktopsremotely. The GUI can be accessed from anywhere over the internet or localnetwork with a VNC client on any OS. The only requirement is that theconnecting OS has a VNC-client installed on it.

Inmy case I have a fresh installed Ubuntu14.04 server in a datacenter (rootserver) were I will install the VNC server so that I can access the UbuntuGnome GUI remotely. If you do. In case that you havent a Ubuntu minimalinstallation yet, follow this guide for thebasic installation of the Ubuntu server till Chapter 11. All of the cases aresame as per the guide. My network details are as follows:

IPaddress 192.168.0.100
Gateway 192.168.0.1
DNS     8.8.8.8   8.8.4.4
Hostname server1.example.com

VNC-server benefits

·        RemoteGUI administration makes work easy & convenient.

·        Clipboardsharing between host Ubuntu server & VNC-client machine.

·        GUItools can be installed on the host Ubuntu server to make the administrationmore powerful

·        HostUbuntu server can be administered through any OS having the VNC-clientinstalled.

·        Morereliable and faster then running a X-Server over SSH.

·        Morereliable then RDP connections.

Iam logged in my system as root user onthe shell (e.g. by ssh). Now I will install the VNC-server.

Pleasemake sure that you are using only vnc-server & no other VNC-server areinstalled as this could give errors in future mostly that clipboard sharingbetween the host Ubuntu Server & vnc-client machine. You can check it asfollows:

Pleaseuninstall if tightvnc or anything similar installed.


Inmy case I am using user=srijan it will differ in your case. Youcan use any username for the same.


root@server1:~#adduser srijan
Addinguser `srijan' ...
Addingnew group `srijan' (1001) ...
Addingnew group `srijan' (1001) ...
Addingnew user `srijan' (1001) with group `srijan' ...
Creatinghome directory `/home/srijan' ...
Copyingfiles from `/etc/skel' ...

Enternew UNIX password: 
<--yourpassword
Retypenew UNIX password: 
<--yourpassword
passwd:password updated successfully
Changingthe user information for srijan
Enterthe new value, or press ENTER for the default
   Full Name []: 
<--ENTER
   Room Number []: 
<--ENTER 
   Work Phone []:
<--ENTER 
   Home Phone []:
<--ENTER
   Other []:
<--ENTER 
Is theinformation correct? [Y/n]
<--ENTER

Further we need to start the vncserverwith the user, for this use:

Itwill show this on the prompt:

NowI am going to make backup of the original file & then make the configurationas follows:

Itwill have some issues with the gnome-vnc session so please kill the present vncsession as follows:

FurtherI will make the startup script for the vncserver like this:


#!/bin/bash
unset VNCSERVERARGS
VNCSERVERS=""
[ -f /etc/vncserver/vncservers.conf ] && . /etc/vncserver/vncservers.conf
prog=$"VNC server"
start() {
 . /lib/lsb/init-functions
 REQ_USER=$2
 echo -n $"Starting $prog: "
 ulimit -S -c 0 >/dev/null 2>&1
 RETVAL=0
 for display in ${VNCSERVERS}
 do
 export USER="${display##*:}"
 if test -z "${REQ_USER}" -o "${REQ_USER}" == ${USER} ; then
 echo -n "${display} "
 unset BASH_ENV ENV
 DISP="${display%%:*}"
 export VNCUSERARGS="${VNCSERVERARGS[${DISP}]}"
 su ${USER} -c "cd ~${USER} && [ -f .vnc/passwd ] && vncserver :${DISP} ${VNCUSERARGS}"
 fi
 done
}
stop() {
 . /lib/lsb/init-functions
 REQ_USER=$2
 echo -n $"Shutting down VNCServer: "
 for display in ${VNCSERVERS}
 do
 export USER="${display##*:}"
 if test -z "${REQ_USER}" -o "${REQ_USER}" == ${USER} ; then
 echo -n "${display} "
 unset BASH_ENV ENV
 export USER="${display##*:}"
 su ${USER} -c "vncserver -kill :${display%%:*}" >/dev/null 2>&1
 fi
 done
 echo -e "\n"
 echo "VNCServer Stopped"
}
case "$1" in
start)
start $@
;;
stop)
stop $@
;;
restart|reload)
stop $@
sleep 3
start $@
;;
condrestart)
if [ -f /var/lock/subsys/vncserver ]; then
stop $@
sleep 3
start $@
fi
;;
status)
status Xvnc
;;
*)
echo $"Usage: $0 {start|stop|restart|condrestart|status}"
exit 1
esac

Nowmake it executable:

NextI will make the configuration file for the vncserver  by creating file asfollows:

Givethe entries like this.

Hereyour port comes to be 5901 & 1024x768 resolution for the VNC client, youcan choose resolution of your own choice.

NowI will add it into boot startups:

AndfFinally reboot the machine.

Installvnc server/remote desktop on ubuntu server and enable start at boot

Step1: Install vnc server

1

2

apt-get install vnc4server

vnc4server

Youwill be prompted for a password. This password will be used to log into the vncsession. After providing a password, you will get output that looks like: New‘IP.IP.IP.IP:1 (laptop)’ desktop is IP.IP.IP.IP:1

Takenotice of the number after the colon (:), in this case it is “1.” Starting the vnc4serverwill cause a .vnc directory to be placed in your home directory.
vnc4server -kill :1

Step2: tweak vnc4server

Openthe .vnc/xstartup file for editing.
vim ~/.vnc/xstartup
The file will look like:
#!/bin/sh
# Uncomment the following two lines for normal desktop:
#unset SESSION_MANAGER
#exec /etc/X11/xinit/xinitrc
[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
xsetroot -solid grey
vncconfig -iconic &
xterm -geometry 80×24+10+10 -ls -title “$VNCDESKTOP Desktop” &
twm &
Type :wq and hit enter. Uncomment the lines that start with unset andexec. Comment out the lines that start with xsetroot, vncconfig, xterm, andtwm.
The final file should look like:
#!/bin/sh
unset SESSION_MANAGER
exec /etc/X11/xinit/xinitrc
[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
#xsetroot -solid grey
#vncconfig -iconic &
#xterm -geometry 80×24+10+10 -ls -title “$VNCDESKTOP Desktop” &
#twm &
OR
#!/bin/sh
unset SESSION_MANAGER
exec /etc/X11/xinit/xinitrc
[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
xsetroot -solid grey
vncconfig -iconic &
x-terminal-emulator -geometry 80×24+10+10 -ls -title “$VNCDESKTOPDesktop” &
x-window-manager &
Change the permissions on the /etc/X11/xinit/xinitrc file to make itexecutable. Run the server, install Ubuntu Desktop.
chmod 755 /etc/X11/xinit/xinitrc
vnc4server
apt-get install ubuntu-desktop
Remember the number after the colon (:). Log into your remote desktop tocheck the configuration.
vncviewer IP.IP.IP.IP:1
Provide the password that you chose.

Nowif you run vnc4server you can login at port 5901, but it wont start at boot.

Step3: get it to start at boot

Createfile /etc/init.d/vncserver

###BEGIN INIT INFO
# Provides: startVNC
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop:
# Short-Description: Start VNC server at boot time
# Description: A sophisticated script to do the job.
### END INIT INFO

#!/bin/sh
echo “JOB RUN AT $(date)”
echo “============================”
echo “”

./lib/lsb/init-functions
su my_user -c “/usr/bin/vncserver -geometry 1280×1024 -depth 16″

exit0

sudochmod +x /etc/init.d/vncserver
sudo update-rc.d vncserver defaults

done

 类似资料:

相关阅读

相关文章

相关问答