Practical Exercises
To be completed by 4/10/2008

  • Login scripts; checking the run sequence.

    In a user home directory (non-root), there should be scripts .bash_profile and .bashrc. In /root, there would be .profile instead of .bash_profile.
    Create a new user account, for example jerry:
    useradd -m -s /bin/bash jerry
    
    In file .bash_profile, put entry
    echo BASH_PROFILE
    
    In file .bashrc, put entry
    echo BASHRC
    

    Create files .bash_login and .profile with the only entries echo BASH_LOGIN and echo PROFILE, accordingly.
    Logout and login as user jerry and notice BASH_PROFILE in the login prompt. Start a new shell,
    /bin/bash
    
    notice BASHRC in the new shell prompt.
    Remove file .bash_profile, logout and login as user jerry again. Notice BASH_LOGIN in the new login prompt.
    Remove file .bash_login, logout and login as user jerry again. Notice PROFILE in the login prompt.

  • Switching between run levels.

    Modify /etc/inittab to boot into default Run Level 5; reboot the system using /sbin/init 6 command. When the system boots up, verify the current run level with command
    /sbin/runlevel
    
    Change sequentially to Run Levels 3, 2, and 1 using command
    /sbin/init N 
    
    where N is the next Run Level. Run /sbin/runlevel command in every Run Level.

    Modify file /etc/inittab to disable authentication in the single user mode:
    #~:S:wait:/sbin/sulogin
    ~:S:wait:/bin/bash
    
    Make init to re-read /etc/inittab by executing command
    /sbin/telinit q
    
    Enter Run Level 1 to verify if you don't get prompt for password.

  • Adding and removing system services from the startup

    Remove portmap, nfs-common, nfs-kernel-server, and exim4 from the startup:
    update-rc.d -f portmap remove 
    update-rc.d -f nfs-common remove 
    update-rc.d -f nfs-kernel-server remove 
    update-rc.d -f exim4 remove 
    

    Add portmap, nfs-common and nfs-kernel-server to the startup to start in Level 3 and stop in Levels 0 1 2 6:
    update-rc.d portmap start 20  3 . stop 20  0 1 2 6 .
    update-rc.d nfs-common start 21  3 . stop 21  0 1 2 6 .
    update-rc.d nfs-kernel-server start 21  3 . stop 21  0 1 2 6 .
    

    Add exim4 to the startup to start in Level 2 and stop in Levels 0 1 3 6:
    update-rc.d exim4 start 20 2 . stop 20 0 1 3 6 .
    

    Enter Run Level 3 and check which of the above services are running.
    ps -ef | grep exim
    ps -ef | grep portmap
    ps -ef | grep rpc
    
    Enter Run Level 2 and check which of the above services are running.

  • Creating startup scripts.

    Copy the script from the lecture, sample.sh, in /etc/init.d directory; make it executable; try to run it:
       
       /etc/init.d/sample.sh
       /etc/init.d/sample.sh start
       /etc/init.d/sample.sh stop
    
    Create symbolic links of the script to various /etc/rcN.d directories:
       ln -s /etc/init.d/sample.sh /etc/rc1.d/K99sample  
       ln -s /etc/init.d/sample.sh /etc/rc2.d/S99sample  
       ln -s /etc/init.d/sample.sh /etc/rc3.d/K99sample  
    
    Init to Run Levels 1, 2, and 3 and notice if the init runs S99sample and K99sample scripts: check the updated content of file /var/log/sample.log Remove the links. Set the script to "stop" at run level 1, 3, and 5 and "start" at Run Levels 2 and 4:
    update-rc.d sample.sh start 20  2 4 . stop 20  1 3 5 .
    

    Check what kind of links with sample.sh have been created in /etc/rc1.d /etc/rc2.d, /etc/rc3.d, /etc/rc4.d and /etc/rc5.d
    Remove the log file
    rm /var/log/sample.log  
    
    Enter Run Levels 1, 2, 3, 4, and 5. Check the content of file /var/log/sample.log.

  • Scheduled jobs through at and cron.
    Create a script, mem.sh, to check memory status:
    #!/bin/sh
    # Checks memory status through cron
    
    of=/home/$USER/mem.out
    dt=`date`
    
    echo "Memory status (in MB) on $dt:" >> $of 
    free -m >> $of
    echo "------------------" >> $of 
    
    Make it executable and run through at:
    at -f mem.sh now + 1 minute
    
    Run atq to make sure the job is scheduled. In a minute, check content of a new file, mem.out; run atq again.

    Remove file mem.out. Modify the script specifying the full path to your home directory in "of", i.e. "of=/home/your_username/mem.out". Add the following entry in /etc/crontab before the line "# run-parts":
    */2 * * * *  root  /home/your_username/mem.sh 
    
    Check if the content of mem.out is updated every 2 minutes.
    Previous Pageprevious First Pagetop