Linux Hint Sheet




Last updated: Sunday 3rd April 2022, 13:08 PT, AD



***********************************************************************************



The vi editor


************************************************************************************


Linux Updates:

Linux updates via command line

sudo apt-get clean         # this should clean the cache
sudo apt-get update        # Fetches the list of available updates
sudo apt-get upgrade       # Strictly upgrades the current packages
sudo apt-get dist-upgrade  # Installs updates (new ones)

*************************************************************************************

Linux Update Failures:

If URLs are not found when update starts - could be internet is not connected 
- or if it is connected, there is a DNS problem.

This worked for me on server AD4:

// ***  Make sure DNS works first for updates: ***
// echo "nameserver 8.8.8.8" | sudo tee /etc/resolv.conf > /dev/null


Linux updates fails to fetch files


****************************************************************************************


Linux/Unix Command Reference


*****************************************************************************************


The shutdown command


*****************************************************************************************


FILE PERMISSIONS EXAMPLES

   1.  To add a type of permission to several files, enter:
	    chmod g+w chap1 chap2

       This adds write permission for group members to the files chap1 and
       chap2.

   2.  To make several permission changes at once, enter:
	    chmod go-w+x mydir

       This denies group members and others the permission to create or
       delete files in mydir (go-w).  It allows them to search mydir or use
       it in a pathname (go+x). This is equivalent to the following command
       sequence:
	    chmod g-w mydir
	    chmod o-w mydir
	    chmod g+x mydir
	    chmod o+x mydir

   3.  To permit only the owner to use a shell procedure as a command, enter:
	    chmod u=rwx,go= cmd

       This gives read, write, and execute permission to the user who owns
       the file (u=rwx).  It also denies the group and others the permission
       to access cmd in any way (go=).

       If you have permission to execute the cmd shell command file, you can
       run it by entering:
	    cmd

       or
	    ./cmd


    4.  To deny read and write permission to all others (the world) to all files and directories
        in the current directory:

        chmod o-rw *

    5.  Before you make a new virtual Android device, 
        you may (as root) need to set the permission on the /dev/kvm directory:

        # sudo chmod 777 /dev/kvm

        The above gives user, group and other read, write and execute permission
        on directory /dev/kvm



********************************************************************************************




The Sticky Bit
--------------

The "t" in "drwxrwxrwt." 
When a directory is set with the "t" permission, 
this is known as the sticky bit. 


The "sticky bit" is used to impose extra file removal permissions on a directory. 
A directory that has write permissions enabled for a user, allows that user to add, 
as well as delete any files from this directory. 

If the sticky bit is enabled on the directory, 
files may only be removed if you match any one of these criteria:

- The owner of the sticky directory
- The owner of the file being removed
- The Superuser more commonly known as root

Enabling the sticky bit for a directory 
should be considered for any directories 
that can be written to by nonpriveleged users 
such as temp directories or public file upload directories.



*****************************************************************************************

Add Users to Existing Groups in Ubuntu Linux 11.10 (Oneiric Ocelot):

http://www.liberiangeek.net/2011/10/add-users-to-existing-groups-in-ubuntu-11-10-oneiric-ocelot-2/



*******************************************************************************************


chown & chgrp command tips:

http://www.dba-oracle.com/linux/chown_chgrp_command_tips.htm


***************************************************************************************


CSCI165 Server User SSH
-----------------------

Server-User-SSH



******************************************************************************************



redo previous command:

$ !!     



*****************************************************************************************



change the password for user student1

sudo -s (to log in as root - you must be root to change passwords)
passwd student1

(To apply random passwords to multiple users - see shell script batch file elsewhere on this page.)

******************************************************************************************


To eject the cdrom:

# eject cdrom

To close the cdrom tray: 

# eject -t cdrom


*****************************************************************************************

To list all users with home directory in /var/www/

/var/www# cat /etc/passwd |grep "/var/www/" |cut -d: -f1

taken from:
http://www.linuxquestions.org/linux/answers/Networking/How_to_list_all_your_USERs


********************************************************************************************







recursively search for a file from root directory:

$ find / -name filename

e.g. find / -name ifconfig
results in:

/sbin/ifconfig




********************************************************************************************

The following command pipes the output of the cat command 
into the input of grep. 
The command prints out all the lines in sources.list 
that contain the word src.

$ cat sources.list | grep src


****************************************************************************************


moving a file which has spaces in its name:

mv Great\ Lakes\ of\ the\ great\ outdoors.doc raid1/data/documents/
mv "Great Lakes of the great outdoors.doc" raid1/data/documents/
mv 'Great Lakes of the great outdoors.doc' raid1/data/documents/




****************************************************************************************





to find the ip address of your machine:

# /sbin/ifconfig   (full path needed)





*****************************************************************************************




copy an entire directory, its subdirectories, and files:

cp -R dirtocopy/ newdir/

e.g. cp -R data/ /var/www/data   (data is created in /var/www)





**************************************************************************************

completely delete an entire directory, its subdirectories, and files:

$ rm -r dirname 

e.g. rm -r  www 

This will recursively directory www including its contents.



****************************************************************************************


delete all files in a directory and any subdirectories,
but retain the directories:

First of all let's do a detailed listing of the current directory:

find . -type f -exec ls -l {} \;

To delete only files (be careful command will delete all files from the current directory):

find . -type f -exec /bin/rm -f {} \;

You can also specify path

find /path/to/delete -type f -exec /bin/rm -f {} \;

from: http://nixcraft.com/shell-scripting/8330-script-removing-all-files-inside-folder-its-sub-folder.html

Note, you can create a batch file containing a host of linux statements, 
save it under an appropriate file name, make it executable, 
then run it to perform such tasks as deleting all files and directories (folders)
in student folders in /var/www/ for example. 

You might want to create a batch file to give passwords to student users -
for example, to allow the CSCI165A students to connect to the remote web server.



***********************************************************************************

BATCH FILES / SHELL SCRIPTS




Word of warning about batch files - shell scripts - 
be careful copying and pasting shell scripts from web pages
because you may also be copying invisible characters 
which corrupt and crash your shell scripts.

For example, if you get the error message:

/bin/bash^M:  bad interpreter: No such file or directory

you probably have a script corrupted with extra hidden characters. 
Make sure you save shell scripts as text only.







What follows is a simple batch file to delete all the files and directories within 
/var/www/student1, /var/www/student2 etc
so that the main folders (/var/www/student1, /var/www/student2 ... etc) are empty, 
ready for new CSCI165A students:

The following lines are saved in a text file named clean.sh in /var/www/
which is made executable using the Linux command: chmod +x clean.sh (but see important note below),
and is executed using the FULL path to the script, e.g. by typing this at a Linux prompt:

/var/www/clean.sh

(You may need to be logged in as root to run some shell scripts)

YOU MUST SPECIFY THE FULL PATH TO THE SCRIPT TO RUN IT

/var/www/clean.sh

You must type in the full path as shown above, even if the current directory (.) is /var/www/


The following is the contents of the file: /var/www/clean.sh



#!/bin/bash
cd /var/www/student1
find . -type f -exec /bin/rm -f {} \;
rmdir *
cp /var/www/CSCI165A.txt .
cd /var/www/student2
find . -type f -exec /bin/rm -f {} \;
rmdir *
cp /var/www/CSCI165A.txt .
cd /var/www/student3
find . -type f -exec /bin/rm -f {} \;
rmdir *
cp /var/www/CSCI165A.txt .
cd /var/www/student4
find . -type f -exec /bin/rm -f {} \;
rmdir *
cp /var/www/CSCI165A.txt .
cd /var/www/student5
find . -type f -exec /bin/rm -f {} \;
rmdir *
cp /var/www/CSCI165A.txt .
cd /var/www/student6
find . -type f -exec /bin/rm -f {} \;
rmdir *
cp /var/www/CSCI165A.txt .
cd /var/www/student7
find . -type f -exec /bin/rm -f {} \;
rmdir *
cp /var/www/CSCI165A.txt .
cd /var/www/student8
find . -type f -exec /bin/rm -f {} \;
rmdir *
cp /var/www/CSCI165A.txt .
cd /var/www/student9
find . -type f -exec /bin/rm -f {} \;
rmdir *
cp /var/www/CSCI165A.txt .
cd /var/www/student10
find . -type f -exec /bin/rm -f {} \;
rmdir *
cp /var/www/CSCI165A.txt .
cd /var/www/student11
find . -type f -exec /bin/rm -f {} \;
rmdir *
cp /var/www/CSCI165A.txt .
cd /var/www/student12
find . -type f -exec /bin/rm -f {} \;
rmdir *
cp /var/www/CSCI165A.txt .
cd /var/www/student13
find . -type f -exec /bin/rm -f {} \;
rmdir *
cp /var/www/CSCI165A.txt .
cd /var/www/student14
find . -type f -exec /bin/rm -f {} \;
rmdir *
cp /var/www/CSCI165A.txt .
cd /var/www/student15
find . -type f -exec /bin/rm -f {} \;
rmdir *
cp /var/www/CSCI165A.txt .
cd /var/www/student16
find . -type f -exec /bin/rm -f {} \;
rmdir *
cp /var/www/CSCI165A.txt .
cd /var/www/student17
find . -type f -exec /bin/rm -f {} \;
rmdir *
cp /var/www/CSCI165A.txt .
cd /var/www/student18
find . -type f -exec /bin/rm -f {} \;
rmdir *
cp /var/www/CSCI165A.txt .
cd /var/www/student19
find . -type f -exec /bin/rm -f {} \;
rmdir *
cp /var/www/CSCI165A.txt .
cd /var/www/student20
find . -type f -exec /bin/rm -f {} \;
rmdir *
cp /var/www/CSCI165A.txt .
cd /var/www/student21
find . -type f -exec /bin/rm -f {} \;
rmdir *
cp /var/www/CSCI165A.txt .
cd /var/www/student22
find . -type f -exec /bin/rm -f {} \;
rmdir *
cp /var/www/CSCI165A.txt .
cd /var/www/student23
find . -type f -exec /bin/rm -f {} \;
rmdir *
cp /var/www/CSCI165A.txt .
cd /var/www/student24
find . -type f -exec /bin/rm -f {} \;
rmdir *
cp /var/www/CSCI165A.txt .
cd /var/www/student25
find . -type f -exec /bin/rm -f {} \;
rmdir *
cp /var/www/CSCI165A.txt .
cd /var/www/student26
find . -type f -exec /bin/rm -f {} \;
rmdir *
cp /var/www/CSCI165A.txt .





The above batch file is a shell script called clean.sh (the .sh is by convention, any name is OK).
The first line of the file has to be the line: #!/bin/bash
which simply specifies which version of the shell will be used to carry out the commands in the file.
Commands in a shell script or batch file are carried out in the order they are issued, 
i.e. from top to bottom.

When run, the example clean.sh shell script issues batch commands to delete files and directories, 
within the student directories /var/www/student1, /var/www/student2 ... /var/www/student26, 
and copies the text file /var/www/CSCI165A.txt to each student folder.

The instructor may want to perform this task at the start of every new semester.

EXECUTING BATCH FILES / SHELL SCRIPTS

You execute clean.sh by using the FULL path to the script, e.g. by typing this at a Linux prompt:

/var/www/clean.sh

You MUST type in the full path as shown above, even if the current directory (.) is /var/www/



IMPORTANT NOTE:  You must have write permission or own the /var/www/ directory 
to be able to delete other users' files.  
See earlier on this page under "file permissions". 
You may have to log in and switch user to root (or other authorized user).



(Of course there is a much better way of cleaning directories using sed and awk, 
but the above works perfectly.)


**************************************************************************************



BATCH FILE TO CHANGE MULTIPLE PASSWORDS

IMPORTANT NOTE:  You must be logged in as root to change passwords.


From the command line, you can change passwords one by one as root at a terminal:


E.g. To change the password for user student1:

sudo -s           (to log in as root - you must be root to change passwords)
passwd student1

or use an automated shell script to do all the work for you, 
setting new passwords for all existing users ( ****  apart from root (i.e. anne)  **** ).


/var/www/chpasswd165.sh 
is a text file containing the following:

This file can be created using the vi or nano editor.


#!/bin/bash
# Script to update user password in batch mode
# You must be a root user to use this script
# -------------------------------------------------------------------------
# Copyright (c) 2005 nixCraft project
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# ----------------------------------------------------------------------
# /root is good place to store clear text password
FILE="/root/batch.passwd"
 
# get all non-root user account
# By default on most linux non-root uid starts
# from 1000
USERS=$(awk -F: '{ if ( $3 > 1000 ) print $1}' /etc/passwd)
 
# create file with random password
echo "Generating file, please wait..."
 
# overwrite file, this is bash specific a better solution is cat > $FILE
>$FILE
 
for u in $USERS
do
   p=$(pwgen -1 -n 8) # create random password
   echo "$u:$p" >> $FILE # save USERNAME:PASSWORD pair
done
echo ""
echo "Random password and username list stored in $FILE file"
echo "Review $FILE file, once satisfied execute command: "
echo "chpasswd < $FILE"
 
# Uncomment following line if you want immediately update all users password,
# be careful with this option, it is recommended that you review $FILE first
# chpasswd < $FILE


(Note: on Ubuntu, the first user of the system is counted as the root user, in my case user "anne".
This user's password is not changed by the script, 
ensuring you are not accidentally locked out of your own Linux system :-)  )

Make /var/www/chpasswd165.sh executable by issuing the following Linux command in a terminal:

   	chmod +x chpasswd165.sh

check using command: 
	
	ls -al chpasswd165.sh
 
Make /var/www/chpasswd165.sh owned by root by issuing the following Linux command in a terminal:

	chown root /var/www/chpasswd165.sh
	chown root /root/batch.passwd

	check files' status using command: 

	ls -al /var/www/chpasswd165.sh
	ls -al /root/batch.passwd


When using chpasswd, if you get this error:

"Authentication token lock busy"

or this error:

"chpasswd: can't lock password file"

the filesystem is in read-only mode. 
If so, you can make it read-write with the command:

mount -o remount,rw /

(I had to do this to get chpasswd to work when running chpasswd < /root/batch.passwd)



(if root does not own the shell script file, you may get this error: "chpasswd: can't lock password file"
when 

chpasswd < /root/batch.passwd
 
is run)





Execute chpasswd165.sh by typing the following into a Linux terminal as root:

/var/www/chpasswd165.sh (full path required)

The script creates the text file: /root/batch.passwd

from the terminal, type:

view /root/batch.passwd  

to view the password file (:q to quit)

- before issuing the following command to apply the new passwords to the users:

chpasswd < /root/batch.passwd

Then reset anne2's password:

passwd anne2

print a copy of /root/batch.passwd for instructor records for CSCI165A





NOTE: when I first ran the above, terminal messages showed - pwgen: command not found.
pwgen is the password generator called in the chpasswd165.sh script.

I installed pwgen from my Linux installation CD prompted by this command:

sudo aptitude install pwgen

then executed the script again:

/var/www/chpasswd165.sh (full path required)





*****************************************************************************************







Valid HTML5!

Valid CSS!