Set Up VNC on Ubuntu in Four Easy Steps
1) Install packages.
sudo apt-get install x11vnc vnc-java
2) Set up a password for clients.
x11vnc -storepasswd
3) Open up ports 5800 and 5900 on your firewall
4) Run the terminal command.
sudo x11vnc -forever -usepw -httpdir /usr/share/vnc-java/ -httpport 5800
and add it for auto-starting in future sessions
Internetworking, Cloud, Linux, Python and Startup.
Monday, March 26, 2012
How to Create a CentOS 5 AMI to run on EC2 or Eucalyptus
Note this article is taken from The Sysadmin Extravaganza
Building a CentOS 5 AMI
First of all, a bit of background and why I love the cloud. Previously, I have worked for large ISP's (UUNet, Level 3, British Telecom) and a couple of startups. In some cases, when I'd provision a new system, I'd go to the vendor website, spec out the box, order it, wait for it to arrive, rack and console it. Then, I'd provision switch ports, etc and through an OS and apps on it. Then, I'd unrack it, box it up and drive it to the data center. Re-rack, cable, test, switch port configure it, etc. Took a huge amount of effort. With ec2 and cloud computing, you run a few commands and you're up and running in like, oh.. 20 minutes or so. So, that's awesome, convenient, etc.
You can either run a public AMI, an image created by some random person, or you can build and run you're own. Here's how to do it my way.
First a couple of assumptions, you're running this sequence on an existing CentOS system. You have the fuse module loaded:
modprobe fuse && lsmod | grep fuse
Need that to do loopback mounts.
Ok, let's get started. Note that this whole thing can be scripted but I figured I'd give a little explanation on each step.
So, first let's create a directory to work in:
mkdir -p ~/ami/centos/ && cd ~/ami/
Now, we're going to create an empty disk image. If you know about anaconda, this is just like the downloading stage2 process (well, kinda). Mine is going to be 2G because I like to have a bunch of stuff in there. Change count to 1024 if you want it to be smaller.
dd if=/dev/zero of=centos.fs bs=1M count=2048
Now, create a file system on it:
mke2fs -F -j centos.fs
Good, now we're going to open it up to be written to by mounting it:
mount -o loop centos.fs ~/ami/centos/
Ok, now we're going to turn this thing into a Linux system you can boot up. The kernel likes to have this stuff. The directory ~/ami/centos/ is the root directory / on your new instance.
Make the /dev/ directory:
mkdir ~/ami/centos/dev
/sbin/MAKEDEV -d ~/ami/centos/dev/ -x console
/sbin/MAKEDEV -d ~/ami/centos/dev/ -x null
/sbin/MAKEDEV -d ~/ami/centos/dev/ -x zero
Create /etc/
mkdir ~/ami/centos/etc/
Now, we're going to use yum - just the way the default installer does to add a bunch of software to your new system. We create a yum.conf on the local file system to use to install OS and software.
vi ~/ami/yum.conf
Add this to the file:[main]
cachedir=/var/cache/yum
debuglevel=2
logfile=/var/log/yum.log
exclude=*-debuginfo
gpgcheck=0
obsoletes=1
pkgpolicy=newest
distroverpkg=redhat-release
tolerant=1
exactarch=1
reposdir=/dev/null
metadata_expire=1800
[base]
name=CentOS-5.5 Base
baseurl=http://mirror.centos.org/centos/5.5/os/x86_64/
gpgcheck=0
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-centos5.5
priority=1
protect=1
#released updates
[update]
name=CentOS-5.5 Updates
baseurl=http://mirror.centos.org/centos/5.5/updates/x86_64/
gpgcheck=0
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-centos5.5
priority=1
protect=1
#packages used/produced in the build but not released
[addons]
name=CentOS-5.5 Addons
baseurl=http://mirror.centos.org/centos/5.5/addons/x86_64/
gpgcheck=0
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-centos5.5
priority=1
[extras]
name=CentOS 5.5 Extras $releasever $basearch
baseurl=http://mirror.centos.org/centos/5.5/extras/x86_64/
enabled=1
So that pulls stuff from the official CentOS mirror list. GPG checking is off because we don't have the keys installed.
Next, create the proc file system and mount it up. This is where the kernel keeps track of all the stuff it's doing.
mkdir ~/ami/centos/proc
mount -t proc none ~/ami/centos/proc/
Ok, here's where the magic begins to happen. We're going to start loading os packages:
yum -c ~/ami/yum.conf --installroot=/root/ami/centos -y groupinstall Core
So using that command, you can install all the stuff you want. The following is my list, you probably don't need the group 'Development Tools'. That's a BUNCH of stuff you only really need if you're doing development. I like to have it on some instances, some it never gets used. So, you should probably ignore it. A good way to see what's available to install is to run:
yum grouplist | less
and that'll tell you what package groups exist. If you're creating a DNS server, you'll want to add the 'DNS Name Server' group. Pick through the list below and install what you want/need. Not everybody is going to want JDK for example.
yum -c ~/ami/yum.conf --installroot=/root/ami/centos -y groupinstall 'Text-based Internet'
yum -c ~/ami/yum.conf --installroot=/root/ami/centos -y groupinstall Ruby
yum -c ~/ami/yum.conf --installroot=/root/ami/centos -y groupinstall 'Web Server'
yum -c ~/ami/yum.conf --installroot=/root/ami/centos -y groupinstall 'Development Tools'
yum -c ~/ami/yum.conf --installroot=/root/ami/centos -y groupinstall 'Java'
yum -c ~/ami/yum.conf --installroot=/root/ami/centos -y groupinstall 'MySQL Database'
yum -c ~/ami/yum.conf --installroot=/root/ami/centos -y install curl wget rsync sudo mlocate lsof man tcpdump bc iptables
Once all your software is installed, configure sshd. Now, this is just an old sysadmin tip. Don't run sshd on port 22. It gets scanned 24x7x365 with all kinds of brute force attacks and everything else. I always, always, always run it on another port. You can do tcpwrappers and other tricks (iptables, etc) but just running it on some higher port saves you tons of problems.
So, in this example, I'm running on port 55000.
vi ~/ami/centos/etc/ssh/sshd_config
and add something like this:
Port 55000
Protocol 2
SyslogFacility AUTHPRIV
PermitRootLogin yes
MaxAuthTries 4
PasswordAuthentication no
ChallengeResponseAuthentication no
GSSAPIAuthentication yes
GSSAPICleanupCredentials yes
UsePAM yes
AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
AcceptEnv LC_IDENTIFICATION LC_ALL
X11Forwarding yes
Subsystem sftp /usr/libexec/openssh/sftp-server
Create a resolv.conf file with valid name servers. I pretty much always use google's because it's fast and anycasted so it's going to be fast no matter where you are, and it's google.
vi ~/ami/centos/etc/resolv.conf
and add:
# Google's public DNS servers.
nameserver 8.8.8.8
nameserver 8.8.4.4
Now, configure you motd. This is the banner that gets displayed whenever anyone logs in:
vi ~/ami/centos/etc/motd
Put whatever you want in there.. here's an example:
________________________________________
/ Unauthorized users will be killed and \
\ eaten. /
----------------------------------------
\ ^__^
\ (xx)\_______
(__)\ )\/\
U ||----w |
|| ||
This is optional too but I like to have ec2tools installed on my instances:
cd ~/ami/centos/ && wget http://s3.amazonaws.com/ec2-downloads/ec2-ami-tools.noarch.rpm*
chroot /root/ami/centos rpm -Uvh ec2-ami-tools.noarch.rpm
export EC2_HOME=/opt/ec2-tools
export PATH=$EC2_HOME/bin:$PATH
Now, configure DHCP for networking:
mkdir -p /etc/sysconfig/network-scripts/
vi /etc/sysconfig/network-scripts/ifcfg-eth0
Enter:
DEVICE=eth0
BOOTPROTO=dhcp
ONBOOT=yes
TYPE=Ethernet
USERCTL=yes
PEERDNS=yes
IPV6INIT=no
PERSISTENT_DHCLIENT=yes
That PERSISTENT_DHCLIENT is very nice to have. It means that if for some reason the DHCP server bites the dust, keep the lease you have. Otherwise your instance could loose it's IP at which point, it's game over.
Turn on networking:
vi /etc/sysconfig/network
add:
NETWORKING=yes
Now, configure fstab to mount everything up:
vi /etc/fstab
Add the following:
/dev/sda1 / ext3 defaults 1 1
none /dev/pts devpts gid=5,mode=620 0 0
none /dev/shm tmpfs defaults 0 0
none /proc proc defaults 0 0
none /sys sysfs defaults 0 0
/dev/sdc1 /mnt ext3 defaults 0 0
/dev/sdc2 swap swap defaults 0 0
Next, turn on the services you want.
chroot ~/ami/centos/ bash
chkconfig --level 345 sshd on
chkconfig --level 345 httpd on
exit
Almost done. I also like to add a user account with a password and sudo access, so if for some reason I can't get in with my ssh key(s), I can just login as this user and sudo to root and figure out what's going on. This step is optional but very useful:
chroot ~/ami/centos/ bash
useradd -g users mclovin
passwd mclovin
Then add your user to the sudoers file:
visudo
find this line:
root ALL=(ALL) ALL
and add your new user:
mclovin ALL=(ALL) ALL
Then, exit the chroot'd env:
exit
umount ~/ami/centos/
That's pretty much it holmes. Once that's all done you can unmount the image file. Bundle, upload and run.
Monday, July 18, 2011
Rename Multiple Files At Once
Let’s say you have a directory with hundreds of files with the wrong file names, and you’d like to replace every filename containing test with prod. (this is a contrived example). We can easily do this with the “for” command in bash, combined with a little bit of bash goodness. Today we’ll learn how to replace text in a variable in a for loop.
The “for” command works like this:
for var in;do $var;done
You can replace with any file match pattern, such as * or *.txt, and you can replace with any linux command. The command will be run in sequence on each of the files matched by the file match pattern.
This is where the bash variable handling makes it even more interesting. Instead of just doing something like “mv $var”, we can replace text in the filename using this syntax:
${var/originaltext/replacetext}
So now, if we run this command on our directory:
for f in *;do mv $f ${f/test/prod};done
For each file matched by *, bash will execute a command similar to this:
mv test.config prod.config
I’ve found that knowledge of the shell is invaluable when administering servers or just for managing your file collection, and has saved me hours of what would have otherwise been manual work.
And yes, I realize there are a number of tools that can accomplish renaming of multiple files.
I use it frequently for changing filename suffixes.
However, if you are stuck with filenames that contain spaces, you need to quote around the variable and the replacement expression, as shown in the example below.
for f in *;do mv “$f” “${f/\.oga/.ogg}”;done
Friday, June 24, 2011
Using 'rar' on Linux
To compress a big video file, movie.avi and split it into multiple files, each size up to 12MB,
A list of files (myarchive.part1.rar, myarchive.part2.rar, ..) will be created in current directory.
rar a -m5 -v12m myarchive movie.avi
You may change the compression quality, -m5 is the best and the slowest, while -m0 do no compression at all (-m3 is default).
If you prefer the old file naming style (myarchive.rar, myarchive.r00, myarchive.r01, ..), add one extra switch -vn before the archive name.
To uncompress the files (myarchive.part1.rar, myarchive.part2.rar, ..),
rar e myarchive.part1.rar
Thursday, June 16, 2011
How to install VNC server on an Ubuntu PC and remote from Windows PC
On the Ubuntu 9.10 PC
The Ubuntu 9.10 PC shall hereafter be referred to as the SERVER.- First of all, permit remote desktop on the SERVER:
From the menubar, select System | Preferences | Remote Desktop- Under Sharing:
- Check Allow other users to view your desktop
- Check Allow other users to control your desktop
- Under Security:
- Select Require the user to enter this password
and enter a password.
- Select Require the user to enter this password
- Under Notification area:
- Select Only display an icon where there is somebody connected.
- Make a note of the network address and name of the SERVER as displayed in the Remote Desktop Preferences.
- Under Sharing:
- Now ensure the required applications are installed:
From the menubar, select System | Administration | Synaptic Package Manager- In the quick search box, type vnc
- For each of the following four packages, right-click on them and select Mark for Installation
- vnc4server
- vnc4-common
- vinagre
- vino
- In the quick search box, type xinetd and install as described above.
- Finally, select Apply on the menubar to complete the installation.
- Now lets complete the VNC server install and configuration.
Open up a command terminal:- Run vncpasswd and enter the password that will be required by the client when attempting to connect to the SERVER
vncpasswd - Run the vncserver on display number 1 in order to generate the configuration files that we will subsequently customize.
vncserver :1 - If you were to connect via remote desktop from a client at this point, the resolution would be wrong and the screen would be plain grey. Do not worry, we will fix that now. First, kill the vncserver process by issuing the command:
vncserver -kill :1 - Edit the xstartup file in the folder .vnc (make a backup of the file first)
gedit ~/.vnc/xstartup - Ensure the file has the contents as follows:
#!/bin/sh
unset SESSION_MANAGER
sh /etc/X11/xinit/xinitrc - Run vncpasswd and enter the password that will be required by the client when attempting to connect to the SERVER
- Finally, to run the vnc server on the SERVER (on display 1), issue the command:
vncserver :1 -depth 16 -geometry 1028x1024
On the Windows PC
The Windows PC shall hereafter be referred to as the CLIENT.- Download and install the VNC viewer of your cloice. I am using the free open source UltraVNC viewer.
Note: UltraVNC is highly customizable. - Run vncviewer.exe
- Enter in the network address of the SERVER pc (as recorded in step 1) immediately followed by a colon : and the display number. The following is an example of the format:
- Under Quick Options, select Auto.
- When prompted, enter the password specified in step 3.1
111.222.333.444:1
- Well Done! You are now connected over remote desktop to the Ubuntu PC from your Windows PC.
How to configure the VNC server to run at bootup on the Ubuntu PC.
In practice, you will not want to logon to the SERVER just to initiate the VNC server. Moreover, you may not even have a monitor for your Ubuntu Server, because you may be using the monitor as a dual screen for your primary desktop, as in my case.These instructions have been referenced from here.
To that end I will now explain how to ensure the VNC server is initialised at bootup time (simply by switching on the SERVER). All of the following steps will required you to be logged in as root.
- Open up a terminal and login as root
sudo -i - Run all the commands outlined in step 3 (for the Ubuntu PC) as root.
- Create a new file in /etc/init.d called vncserver and ensure it has the following contents:
Note: Change the username (and optionally the other parameters) according to your circumstances. - Make the above script executable:
sudo chmod +x /etc/init.d/vncserver - Make this script into a system service by adding this script to the runtime scripts invoked at bootup time.
sudo update-rc.d vncserver defaults - To start the service without rebooting the SERVER:
sudo /etc/init.d/vncserver start - If you made a mistake/typo in the script, you can undo the creation of the system-wide service (step 5) by running the command:
sudo update-rc.d -f vncserver removeYou may then correct your mistake and then repeat steps 5 and 6. - Well done! You may now connect to your Ubuntu PC SERVER via remote desktop by simply powering on the SERVER and giving it enough time to bootup and run all of its system-wide services.
#!/bin/sh -e
### BEGIN INIT INFO
# Provides: vncserver
# Required-Start: networking
# Default-Start: S
# Default-Stop: 0 6
### END INIT INFO
PATH="$PATH:/usr/X11R6/bin/"
# The Username:Group that will run VNC
export USER="YOUR_USERNAME"
#${RUNAS}
# The display that VNC will use
DISPLAY="1"
# Color depth (between 8 and 32)
DEPTH="16"
# The Desktop geometry to use.
#GEOMETRY="x"
#GEOMETRY="800x600"
#GEOMETRY="1024x768"
GEOMETRY="1280x1024"
# The name that the VNC Desktop will have.
NAME="your-vnc-server"
OPTIONS="-name ${NAME} -depth ${DEPTH} -geometry ${GEOMETRY} :${DISPLAY}"
. /lib/lsb/init-functions
case "$1" in
start)
su ${USER} -c "/usr/bin/vncserver ${OPTIONS}"
;;
stop)
su ${USER} -c "/usr/bin/vncserver -kill :${DISPLAY}"
;;
restart)
$0 stop
$0 start
;;
esac
exit 0
Subscribe to:
Posts (Atom)