Articles

Adventures in Red Hat Enterprise Linux, CentOS, Fedora, OpenBSD and other open source solutions.

Linux interview questions

From time to time you might need to interview somebody for a Linux role. It's hard to think of good questions; you don't want to scare somebody with your questions, but you do want to know if the person is knowledgable.

Questions for a junior.

"People that use your Linux server complain it's slow. What tools would you use to check resource usage?"
top, sar, netstat -an, iostat, free, df.

"You discover a disk is full on your Linux server. What do you use to discover where the bigest files/directories are?"

  • To create a short report on the largest directories: du -sk * | sort -nr | head -n10
  • You might use "find" to find large files: find / -size +100M

"A system needs more disk space. You want to add a partition /var/log. You add a drive to the machine, it becomes /dev/sdb. What actions do you take to use this disk?"
Depending if you use LVM or not. If you don't use LVM:

  1. "fdisk /dev/sdb" to add one partition so /dev/sdb1 becomes available.
  2. "mkfs /dev/sdb1" to create a filesystem on it.
  3. "mount /dev/sdb1 /var/log". Actually the data needs to be copied into the new /var/log.

If you do use LVM:

  1. "fdisk /dev/sdb" to add one partition so /dev/sdb1 becomes available.
  2. "pvcreate /dev/sdb1" to make it an LVM device.
  3. "vgextend SomeVolumeGroup /dev/sdb1" to extend an existing Volume Group.
  4. "lvcreate -L 1G -n SomeLogicalVolumeName SomeVolumeGroup" to create a new Logical Volume.
  5. "mkfs /dev/SomeVolumeGroup/SomeLogicalVolumeName" to create a filesystem on it.
  6. "mount /dev/SomeVolumeGroup/SomeLogicalVolumeName /var/log". Actually the data needs to be copied into the new /var/log.

Questions for a medior.

"People that use your Linux server complain it's slow. You've seen the disk usage is high. What can you do to improve this?"

  • Move away services, devide them over different server.
  • Add more memory so disk caching can be used better.
  • Look into the application, why is it reading/writing so much.
  • User faster disks.

"What happens in relation to DNS, SMTP and IMAP when I send send an email to example@example.com?"
My computer is likely to be configure to send the email to a mail server on port 25, SMTP protocol. That mailserver will query the DNS for the MX records of example.com. The mailserver that show up with the lowest priority will be contacted to deliver the email. That mailserver at example.com can accept the email and put in into the imap folder for the user or alias of example@example.com.

"You need to setup 100 Red Hat Enterprise Linux systems. If you don't want to walk around and eject and insert a boot CD 100 times, what options would you have?"
Kickstarting would help out. Install one machine as you like it, save /root/anaconda-ks.cfg to a webserver. Setup a PXE (DHCP, TFTP, HTTP, DNS) environment and use that kickstart file to install the rest.

Questions for a senior.

"You have destroyed /etc/pam.d/system-auth and can't login anymore. Another machine has a propper version of /etc/pam.d/system-auth. How would you fix that broken machine?"
The machine needs to be booted in single user mode so you don't get a login prompt. After that here are some option:

  • Start network, use "nc" to get and replace that file.
  • Mount a CD that has the package to fix the broken file.
  • Manually repair it.

"You have installed apache, php and mysql and a webapplication such as Drupal. The webapplication tries to send emails to an external mailserver but fails. What could be the cause when these items have been verified:

  • The web application is correctly configured to use the external mailserver.
  • It's possible to connect to the mailserver on the specified port from the command line.
  • The logs of the mailserver don't record anything when the web application tries to send email."
  • IPtables allows connections out to port 25.

SELinux could be blocking apache from using port 25 on an external system. The logfile /var/log/messages might inform you about it. To fix it issue "setsebool -P httpd_can_network_connect=1".

"What determines the load (w, uptime) of a system?"
The number of processes that are waiting for execution. These processes are in the run queue. Processes could be waiting for io, network or memory allocation.

Drupal on Amazon's Elastic Compute Cloud (EC2)

Hosting Drupal site in the Amazon EC2 cloud is not difficult. Here is a recipe I have used, first attempt was a Fedora 14 EC2 ami, but Fedora 14 comes with php 5.3, which can't be combined with Drupal 5.x. If you only have Drupal 6 (or Drupal 7) sites to host, you can use Fedora 14. If you want to use a "small" instance, please read this bug about readdir64_r. The fix for that bug is easy:

echo "hwcap 1 nosegneg" > /etc/ld.so.conf.d/libc6-xen.conf

Let's continue with Drupal on CentOS. Rightscale provides perfect CentOS amis that can be used on Amazons EC2 platform. If you install one, these are the steps I took to make it Drupal 5, Drupal 6 and Drupal 7 ready:

# Update the software.
yum -y update

# Set the timezone for this machine.
cp /usr/share/zoneinfo/Europe/Amsterdam /etc/localtime

# This images came with 10Gb of ESB storage, I added another 64 Gb volume, here it's called "/dev/sdc".
# Use LVM to be ready to grow in the future.
pvcreate /dev/sdc
vgcreate vg0 /dev/sdc
lvcreate vg0 -L 32G -n var-www
lvcreate vg0 -L 1G -n var-lib-mysql
lvcreate vg0 -L 2G -n root

# Put filesystems on the logical volumes.
mke2fs -j /dev/vg0/var-www
mke2fs -j /dev/vg0/var-lib-mysql
mke2fs -j /dev/vg0/root

# Add the mountpoints to fstab.
echo "/dev/vg0/var-www /var/www/virtualhosts ext3 defaults 0 0" >> /etc/fstab
echo "/dev/vg0/var-lib-mysql /var/lib/mysql ext3 defaults 0 0" >> /etc/fstab
echo "/dev/vg0/root /root ext3 defaults 0 0" >> /etc/fstab

# Create the mountpoints.
mkdir /var/www /var/lib/mysql /root

# Mount all mounpoints in /etc/fstab.
mount -a

# Install the webserver.
yum -y install httpd
service httpd start
chkconfig httpd on

# Install the database server.
yum -y install mysql-server
service mysqld start
chkconfig mysqld on
/usr/bin/mysqladmin -u root password 'YourPassWord'

# Install PHP and all required Drupal php modules.
yum -y install php php-mysql php-mcrypt php-xml php-mbstring php-gd

# Add a single new file in the ESB root filesystem, that includes configurations from /var/www/conf.d
# Using this trick allows you to easily remount the volume on another host in case of troubles.
echo 'Include /var/www/conf.d/*.conf' >> /etc/httpd/conf.d/virtualhosts.conf
mkdir /var/www/conf.d

# Rightscale CentOS images comes with postfix and sendmail. Postfix is enabled, but sendmail is fine for me.
# First erase postfix.
yum -y erase postfix

# Now reinstall sendmail to fix a few permissions.
yum -y reinstall sendmail
service sendmail start
chkconfig on sendmail

# Reboot the box to make sure it's working properly.
reboot

Ranges in shell scripts

Het is altijd bewonderings waardig om te zien hoe mensen ranges kunnen gebruiken in shell scripts. Mensen die ranges beheersen, zijn sneller op de Linux command line.

Hier zijn wat ranges en patronen die je zou kunnen gebruiken:

Een oplopende reeks van karakters, in dit geval 1 tot 10, getoond op één lijn:

$ echo "file{1..9}"
file1 file2 file3 file4 file5 file6 file7 file8 file9
$ echo file{s..z}
files filet fileu filev filew filex filey filez

De volgorde van ranges kun je vinden in de man-page van "ascii".

Nog een patroon dat je kunt gebruiken:

$ echo file{1,2,4}
file1 file2 file4

Als je meer informatie zoek, Google eens naar "Brace Expansion" of kijk in de man-page van bash.

Een RPM maken van een shell-script

Dus je hebt een enterprise kwaliteit shell script gemaakt en wil het uitrollen over een setje Red Hat-achtige machines? Een RPM maken is makkelijk, hier zijn de stappen die nodig zijn:

1. Installeer rpmbuild zodat je jouw eigen RPMs kunt gaan bouwen.
2. Pak je script in een tar.gz file en verplaats het naar /usr/src/redhat/SOURCES/

# tar -cvzf shell-script-0.1.tar.gz shell-script-0.1
# mv shell-script-0.1.tar.gz /usr/src/redhat/SOURCES/

3. Maak een .spec file dat beschrijft waar alles is.
# cat /usr/src/redhat/SPECS/shell-script.spec
Summary: The do it all script. (Enterprise quality)
Name: shell-script
Version: 0.1
Release: 1
URL:     http://meinit.nl
License: GPL
Group: Applications/Internet
BuildRoot: %{_tmppath}/%{name}-root
Requires: bash
Source0: shell-script-%{version}.tar.gz
BuildArch: noarch

%description
A shell script.

%prep
%setup

%build

%install
rm -rf ${RPM_BUILD_ROOT}
mkdir -p ${RPM_BUILD_ROOT}/usr/bin
install -m 755 shell-script.sh ${RPM_BUILD_ROOT}%{_bindir}

%clean
rm -rf ${RPM_BUILD_ROOT}

%files
%defattr(-,root,root)
%attr(755,root,root) %{_bindir}/shell-script.sh

%changelog
* Tue Jan 12 2010 Robert de Bock <robert@meinit.nl>
- Uberscript!

3. Bouw het!
# rpmbuild --bb /usr/src/redhat/SPECS/shell-script.spec

4. Installeer het!
# rpm -Uvh /usr/src/redhat/RPMS/noarch/shell-script-0.1.1.noarch.rpm

Een loadbalancer maken met CentOS en Linux Virtual Server

Als je nieuw bent in de Red Hat terminology, kan het best wel moeilijk zijn om te begrijpen hoe je een simpele loadbalancer kunt maken met Linux. Hier staat wat informatie om je op weg te helpen.

Lees meer op the CentOS Virtual Server Administration paginas.

Terminologie:

  • piranha This is het pakket dat je helpt met opzetten van LVS.
  • ipvs De Linux module die het mogelijk maakt te load balancen.
  • ipvsadm Een pakket en commando om de loadbalancer te beheren. Let op dat je of piranha-gui of /etc/sysconfig/ipvsadm gebruikt om de loadbalancer te configuren, niet beide..
  • LVS Linux Virtual Server - de projectnaam die hier steeds genoemd wordt.
  • pulse Een service (/etc/init.d/pulse) die op de actieve en backup machine draait en een poort opent om het mogelijk te maken checks te doen. Piranha-gui configureert alles, pulse voert alle configuraties door naar andere machines.
  • nanny Een proces gestart door pulse om de echte servers (en services) the checken.
  • nat Network Address Translation. Een gebruikelijke combinatie met LVS. Als NAT gebruikt wordt, accepteert de loadbalancer/director/LVS verkeer op een VIP en stuurt het door naar de echte servers. Let op dat de huidige implementatie van ipvsadm niet goed kan Source NATten, het source adres wordt niet goed herschreven.
  • direct routing Een methode van LVS om te routeren. Verkeer wordt ontvangen op de VIP en doorgestuurd naar de echte server, alsof het verstuurd is naar de VIP van de echte server. Hierdoor moeten de echte servers wel reageren op het VIP adres. Om dit te laten werken, moeten er een aantal "trucs" gedaan worden zodat de arp-cache gezond blijft werken.
  • wlc Weighted Least Connections, een algoritme om verkeer naar de echte servers te balancere.
  • VIP Virtual IP. Het IP-address waarop de service geconfigureerd is.
  • RIP Real (Echte) server IP. Het IP-address van de echte server.
  • realserver/echte server De server die de uiteindelijke dienst draait. Dit kan een Windows, Linux of elk soort machine zijn.

Hier is een overzicht van een mogelijke setup:

Stappen die je moet volgen voor eeuwige glorie:

Installeer piranha-gui

# yum install piranha-gui

(Verander 172.16.0.0/24 naar het netwerk dat gebruikt wordt om de echte server te bereiken.)

Configureer services

# chkconfig httpd on
# chkconfig piranha-gui on
# chkconfig pulse on
# sed -i 's/net.ipv4.ip_forward = 0/net.ipv4.ip_forward = 1/' /etc/sysctl.conf
# echo "*nat
:PREROUTING ACCEPT [46:3346]
:POSTROUTING ACCEPT [431:32444]
:OUTPUT ACCEPT [431:32534]
-A POSTROUTING -o eth0 -j MASQUERADE
COMMIT
*filter
:INPUT ACCEPT [4186:1094786]
:FORWARD ACCEPT [729:111992]
:OUTPUT ACCEPT [4266:388099]
-A FORWARD -i eth1 -j ACCEPT
COMMIT" > /etc/sysconfig/iptables
# chkconfig iptables on
# sed -i 's/SELINUX=enabled/SELINUX=disabled/' /etc/sysconfig/selinux

Configureer via webinterface

Voor deze stappen uit in de piranha-gui web interface:

# service httpd start
# piranha-passwd
# service piranha-gui start

Open een browser en ga naar het IP adres van 1 van de loadbalancers, de URL ziet er ongeveer zo uit: http://192.168.202.50:3636

Synchroniseer /etc/sysconfig/ha/lvs.cf

Je zult de configuratie die piranha maakt in sync moeten houden op beide machines, dit is een suggestie om dat te doen:

# ssh-keygen
# scp .ssh/id_rsa* 192.168.202.110:./.ssh/
# cp .ssh/id_rsa.pub .ssh/authorized_keys

Nu moet je kunnen verbinden met beide machines zonder het opgeven van een wachtwoord.

# cat update-lvs.cf
#!/bin/sh

copiedserialno=$(grep serial_no /tmp/lvs.cf | awk '{ print $NF }')
runningserialno=$(grep serial_no /etc/sysconfig/ha/lvs.cf | awk '{ print $NF }')

if [ "$copiedserialno" -gt "$runningserialno" ] ; then
mv /tmp/lvs.cf /etc/sysconfig/ha/lvs.cf
fi
# crontab -l
* * * * * /usr/bin/scp /etc/sysconfig/ha/lvs.cf 192.168.1.46:/tmp > /dev/null
* * * * * /root/update-lvs.cf

Reboot de server.

Om alle veranderingen te testen kun je de load-balancer rebooten.

Fedora 14 komt eraan

Het duurt niet zo lang meer voordat Fedora 14 uit komt. Eens kijken welke features Fedora 14 heeft.

  • Een Amazon EC2 Image (AIM) voor Fedorakomt uit! Eindelijk van dat oude Fedora 8 image af.
  • Voor Desktop Virtualisatie kan, Spice gebruikt worden. Dit is een KVM extensie die het mogelijk maakt super snel gebruik te maken van de desktop die virtueel draait, of het nou Windows, Linux of een andere grafische desktop is.
  • Perl, Python en Ruby zijn geupdate, leuk maar voor mij niet van groot belang.
  • OpenSCAP bekijkt of je systeem not security compiant is. Een handige tool voor veel klanten.

Al met al een best wel interessante release, ookal is het moeilijk een goede distributie te verbeteren.

Veel van de veranderingen die gedaan worden in Fedora komen in RHEL terrecht. Het lijkt erop dat RHEL 7 ongeveer zal bestaan uit Fedora 16 (op zijn vroegst) tot Fedora 19. (op zijn laatst)

Apple Mac OS X als syslog server

Hier is een korte howto die je helpt je Mac OS X machine logs te ontvangen van apparaten, zoals de Apple Airport Extreme. Er zijn wat howto's online te vinden, maar ik denk dat er wat verandert is sinds Mac OS X 10.5, dus die howto's werken niet perfect.

Verander de syslogd configuratie

# echo "local0.notice /var/log/airport.log" >> /etc/syslog.conf

Touch het logfile

# touch /var/log/airport.log

Verander de syslogd startup procedure

Aan het einde van het file, uncomment het gedeelte dat remote logging accepteert.

# cat /System/Library/LaunchDaemons/com.apple.syslogd.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.apple.syslogd</string>
    <key>OnDemand</key>
    <false/>
    <key>ProgramArguments</key>
    <array>
<!--
Un-comment the following lines to run syslogd with a sandbox profile.
Sandbox profiles restrict processes from performing unauthorized
operations; so it may be necessary to update the profile
(/usr/share/sandbox/syslogd.sb) if any changes are made to the syslog
configuration (/etc/syslog.conf).
-->
<!--
<string>/usr/bin/sandbox-exec</string>
<string>-f</string>
<string>/usr/share/sandbox/syslogd.sb</string>
-->
<string>/usr/sbin/syslogd</string>
    </array>
<key>MachServices</key>
<dict>
<key>com.apple.system.logger</key>
<true/>
</dict>
<key>Sockets</key>
<dict>
<key>AppleSystemLogger</key>
<dict>
<key>SockPathName</key>
<string>/var/run/asl_input</string>
<key>SockPathMode</key>
<integer>438</integer>
</dict>
<key>BSDSystemLogger</key>
<dict>
<key>SockPathName</key>
<string>/var/run/syslog</string>
<key>SockType</key>
<string>dgram</string>
<key>SockPathMode</key>
<integer>438</integer>
</dict>
<!--
Un-comment the following lines to enable the network syslog protocol listener.
-->
<key>NetworkListener</key>
<dict>
<key>SockServiceName</key>
<string>syslog</string>
<key>SockType</key>
<string>dgram</string>
</dict>
</dict>
</dict>
</plist>

Herstart syslogd

# launchctl unload /System/Library/LaunchDaemons/com.apple.syslogd.plist
# launchctl load /System/Library/LaunchDaemons/com.apple.syslogd.plist

Open de firewall

Ga naar de System Preferences, klik Security, open het Firewall tabje en klik de +. Selecteer het file /usr/bin/syslog.
Als je niet in staat bent om /usr te openen, kun je deze hack proberen in de terminal:

$ cd
$ ln -s /usr/bin
$ ln -s /usr/sbin

Nu staan de files in je homedirectory ./bin/syslog en ./sbin/syslogd

Configureer de apparaten

Vertel nu aan alle apparaten waarvan je logs wilt zien (zoals de Apple Airport Extreme) dat ze de logs kunnen dumpen op het IP adres van je Mac OS X machine.

Bekijk het resultaat

Je kunt de applicatie "Console" openen of vanaf een terminal dit commando draaien: "tail -f /var/log/airport.log".

Gebruik Putty en een HTTP proxy om overal met ssh te verbinden, door firewalls heen.

Zit je ooit in de situatie (bijvoorbeeld je werk waar Windows gebruikt wordt) waar je naar jouw machine (bijvoorbeeld je Linux/UNIX machine thuis) zou willen verbinden via SSH, maar de firewall regels laten dat niet toe? Misschien kun je de http-proxy server gebruiken om toch te verbinden.

Let wel op, deze technische truc is misshien wel mogelijk, maar het kan zijn dat het niet toegestaan is. Bespreek het eerst eens met iemand in je organisatie.

Hier zijn de ingrediënten:

  1. Een Windows machines die je gebruikt als client.
  2. Een Linux/UNIX machine die je gebruikt als (ssh) server.
  3. Download Putty of Portable Putty op je client.
  4. Zoek uit welke proxy jouw organisatie gebruikt op je client.
  5. Configureer Putty om die proxy te gebruiken op je client.

Welke proxy gebruikt jouw organisatie?
Er zijn een paar opties beschreven, van makkelijk naar moeilijk, kies er één. Kijk maar welke het beste werkt voor jou.

  1. Open Internet Explorer, klik Tools Internet Options.... Ga naar het tabblad Connections en klik LAN Settings.... In het Proxy server. gebied, zie je de proxy server en poort. Zo niet, ga door naar de volgende mogelijkheid.
  2. Op je windows machine, open een goede website en klik Start of zoiets, dan Run... en tik cmd in. Je krijgt nu een half kapotte terminal te zien. Tik netstat -rn en zoek naar ESTABLISHED verbindingen op poort 8080 of poort 3128. Het IP van de proxy server zie je staan in de derde kolom. De regel die ik zocht ziet er zo uit: TCP 192.168.1.2:2210 192.168.1.1:3128 ESTABLISHED. Het IP 192.168.1.1 is mijn proxy server, 3128 de poort.

Configureer Putty om deze proxy te gebruiken.
Nu dat je de proxy server en poort hebt, kun je Putty configureren.

  1. Open Putty en tik de Host Name waarmee je wilt verbinden. (De Linux/UNIX machine)
  2. Open het plusje voor Connections en kies Proxy. Tik de Proxy hostname en Port in.
  3. Kies nu Open. Als het goed is krijg je een password prompt te zien.

Gefeliciteerd, je bent alsnog verbonden!

Moving a single Drupal instalation into a multisite environment.

If you'd like to move a single installation of Drupal into a multisite environment, use these steps, and replace example.com for your websites name. In this example, Drupal is installed in /var/www/drupal/ .

1. Change the webserver configuration.

This one is obvious, the website was first pointing to an individual installation of Drupal, it needs to be directed to the multi-site installation of Drupal.

2. Copy templates, settings.php and files into the multisite environment.

mkdir -p /var/www/drupal/sites/example.com/
cd /var/www/example.com
cp -Rip html/sites/all/themes html/sites/default/settings.php html/sites/default/files/ /var/www/drupal/sites/example.com/

3. Update the MySQL database with the new paths.

If you have used files (including images) on your website, the path needs to be updated. Earlier files were located on "sites/default/files/", but this will become "sites/example.com/files"

mysql -u root -pPaSsWoRd
USE examplecom;
UPDATE files SET filepath=REPLACE (filepath, 'default', 'example.com');
UPDATE files SET filepath=REPLACE ('filepath', 'images', 'sites/example.com/images') WHERE filepath REGEXP '^images.*';
QUIT;

4. Change the location of the icons for the selected theme.

Go to Administer - Site building - Themes - Your Theme "configure" and change the path to reflect the right one. Mostly this means changing the word "files/" to "sites/example.com/files/".

5. Restart the web server and clean up the old environment.

For Apache, that would be:

apachectl configtest
apachectl restart

Check the website, everything should work, maybe you have to reselect your template to make it look better. If all works well, remove the old code.

rm -R /var/www/example.com

Shrinking a filesystem with LVM

After an installation you might find some file systems are too large, they are almost empty. When you want to use that space for another file system, here are the steps you can take:
Imagine /opt is now 10 Gb, but 1 Gb would be sufficient.

  1. Check if the file system is in use. Using lsof /opt you will get a list of processes that currently use /opt. Stop these processes.
  2. Find out what device is used for /opt with df -h /opt or mount. In my example, I found /dev/mapper/VolGroup/opt hold files on /opt.
  3. Unmount the filesystem, using umount /opt
  4. Resize the filesystem using resize2fs /dev/mapper/VolGroup/opt 1G. This frees the "right" part of the disk that LVM will un-allocate in a moment. All data from the file system is on the "left hand side".
  5. Run lvreduce -L 1G /dev/mapper/VolGroup-opt to shrink the logical volume. (It might warn you that you need to run e2fsck -f /dev/mapper/VolGroup-opt before you can continue.
  6. Remount the filesystem with a command as mount /opt.

For /opt or any other filesystem that can easily be freed from open file handles, the above procedure works fine, but for "busy" filesystems, like /, /var, /usr, and so on, you'd have boot the machine without mounting filesystems. One way to do this is using the installation CD and starting up the "rescue" environment.

Syndicate content