OSadmin help

From EIK wiki

OSADMIN HELP

This help page is a tool that can be used in the IT College course "Operating systems" practical exam. The page is suitable for those who did not have any experience regarding Linux/Unix server administrating before this course. The help page gives an overview of the main commands pirorly used in the practical labs of this course. However this pages does not claim to contain all the commands required in the practical exam. The purpose of this help page is to help students find the correct commands faster and understand them through practice.

Author

Translated from Estonian by Peep Kuulme

Author of the Estonian version

Kristiina Kaarna

OSadmin spikker

1. WORKING WITH FILES

Linux/Unix considers directories as files aswell, which is why i will handle working with directories alongside with other files.

1.1. Creating

  • Creating a file: touch file
touch textfile.txt

Result: A file named textfile.txt is created to the current working directory.

or

touch folder1/textfile.txt

Result: A file named textfile.txt is created to the directory named folder1 that is located in the home directory.

  • Creating a file by redirecting information to a file using >
echo "This is some text" > textfile.txt

Result: A file named textfile.txt containing text "This is some text" is created.

  • Creating a directory: mkdir directory
mkdir folder1

Result: A directory named folder1 is created to the current working directory.


  • Creating a subdirectory
mkdir folder1/sub1

Result: A subdirectory named sub1 is created inside directory folder1.

  • Creating a nested directory
mkdir -p folder1/sub1/sub2/sub3

Result: Inside folder1 is created a directory named folder2 witch contains folder3 witch contains folder4.

1.2. Deleting

  • Deleting a file: rm fail
rm textfile.txt

Result: File named textfile.txt is deleted.


  • Deleting a directory: rm -r directory

Option -r means that the directory will be deleted with all of its content.

rm -r folder1

Result: Directory named folder1 and all its content will be deleted.


  • Deleting the contents of a directory without deleting the directory itself: rm -r *

NB! User needs to be located inside the folder.

rm -r *

Result: The contents of the current directory will be deleted, the directory will remain.

1.3. Moving and renaming

  • Moving a file: mv filename destination
mv textfile.txt folder2

Result: File named textfile.txt is moved to a directory named folder1.


  • Moving a directory to another directory: mv directory_name destination

The contents of the directory being moved will move to the new directory.

mv folder1 folder2

Result: Directory named folder1 is being moved inside folder2.


  • Moving a directory to a subdirectory
mv folder1 folder2/sub2

Result: Directory folder1 will be moved to sub2 witch is a subdirectory of folder2.


  • Renaming a file or a directory: mv OLD_name NEW_name
mv textfile.txt story.txt

Result: File textfile.txt will be renamed to story.txt.

1.4. Copying

  • Copying a file: cp filename NEW_filename
cp old new

Result: Fileold will be copied to file new.

  • Copying a folder with of its content: ' cp -r filename NEW_filename'
cp -r old new

Result: Directory old and all of its content will be copied to file new.

1.5. Navigation between directories

  • Navigating to home directory: cd
cd

Result: User will navigate to home directory. Administrator will navigate to root directory.


  • Navigating one level up: cd ..
  • Navigating two levels up: cd ../..
  • Navigating to last visited directory: cd -
  • Navigating to subfolder /var/log
cd /var/log

1.6. Viewing

  • Viewing the contents of a directory: ls -l
  • viewing the contents of a file: less

Example: Use less command to open file messages located in /var/log/

less /var/log/messages
    • Useful commands for navigating inside a file opend with less command:

Navigating down: n

Navigating up: ?

Navigating to the beginning of the file: g

Navigating to the end of the file: G

Help: h

Exit file and less-command: q

1.7.Searching

  • Searching for a file by some criteria: find location criteria what
find /etc -type f -name "*.log"

Result: Directory etc will be scanned for all files with the extension .log

find /var/log -size 1M

Result: Searching for all files with size 1MB within var subdirectory log.

  • Viewing a file by CONTENT: grep WHAT WHERE
grep sda /var/log -i -R

Result: Searching recursively for expression sda from /var/log regardless if the expression has capital or small letters.

1.8. File permissions

When ls -l command is entered, the following information will be displayed:

d/- rwx rw- r-- fedor students 2007-02-01 16:00 textfile.txt
object type user permissions group permissions other permissions user group date modified name

For object type d stands for directory and - stands for file.


Permission types:

r w x
file: read permission;

directory: ls -l use permission

write permission file: execute permission;

directory: permission to enter directory

Permission groups:

a u g o
all user group others


  • Changing file or folder permissions: chmod <permissions> <file or directory>

Permissions can be changed on multiple files and folders simultaneously. To do that, just separate the files or folders with a space.

chmod a-x,g+w,o-w textfile.txt

Result: Execute permission revoked from all, write permission given to group and write permission revoked from others.

1.9. Changing file ownership and group

  • Changing file or folder owner: chown NEW_owner folder_name
chown fedor folder1

Result: Directory folder1 ownership will be set to Fedor.


  • Changing the owner of a folder and all of its contents: chown -R NEW_owner folder_name
chown -R fedor folder1

Result: Directory folder1 and all of it's content's ownership will be transferred to Fedor.


  • Changing the group of a file or folder: chgrp NEW_group directory
chgrp students folder1

Result: Directory folder1 will belong to group students.


This can also be done using chown :<group> <file/directory> command.

chown :students folder1

Result: Directory folder1 will belong to group students.


  • Changing the group and its permissions

Example: Give students group permission to write into sometextfile.txt.

First you need to change the group of textfile.txt and then change the group permissions.

chgrp students textfile.txt

chmod g+w textfile.txt


  • Changing owner and group with one command

Example: Assign Fedor as the owner of textfile.txt and student as the group.

chown fedor:student textfile.txt

Result: Fedor is the new owner of textfile.txt and the file belongs to student group.

2. MANAGING USERS

  • Creating a new user: addgroup students && adduser --ingroup students fedor
addgroup students && adduser --ingroup students fedor

Result: New user Fedor will be created and he belongs to students group

  • Granting administrator privileges: adduser username sudo
adduser fedor sudo

Result: User Fedor will be granted administrator permissions.

  • Creating a group: groupadd students
groupadd students


Result: New group students will be created.

  • Locking a useraccount: usermod -L username
usermod -L fedor

Result: Useraccount fedor will be locked.

  • Unlocking a useraccount: usermod -U username
usermod -U fedor

Result: Useraccount fedor will be unlocked.

  • Changing a username: usermod -l NEW_username OLD_username
usermod -l student fedor

Result: Username fedor will be changed to student.

  • Changing user home directory and username: usermod –d home_directory –m –l NEW_username OLD_username
usermod –d /home/students –m –l student professor

Result: User professor will be changed to user student using option -l. New user student home directory will become students using option -d . All files will be moved to the new home directory using option -m.

  • Deleting a user: userdel username
userdel fedor

Result: User Fedor Will be deleted.

  • Changing your account password when logged in: passwd

Then enter your current password and then a new password twice.

  • Changing another user password(only with root permission): passwd Username
passwd fedor

Then enter the password twice and a new password will be set to user "Fedor"..

  • Changing user shell value: usermod -s /bin/shell_value username
usermod -s /bin/false fedor

Result:User Fedor can no longer login with ssh. Access to commandline disabled when logged in. To restore, replace /bin/false with /bin/bash/ or another original shell

  • Finding information about local groups and users: getent group, getent passwd or with more detailsgetent passwd username

Command getent displays the information about the user in the system database.

getent passwd fedor

Result: Output for user Fedor

fedor:x:1000:1000:Fedor,,,:/home/fedor:/bin/bash


getent group

Result: Lists information about groups and users

root:x:0: daemon:x:1: bin:x:2: sys:x:3: adm:x:4:fedor itk:x:5: disk:x:6: lp:x:7: mail:x:8:

  • Information about users and groups can also be found in /etc/shadow and /etc/passwd and /etc/group files

3. Managing software(installation, removing, dependencies)

  • Software installation: apt-get install program
apt-get install firefox

Result: Application Firefox will be installed.

Before installing an application it is recomended to update the operating system software list.

sudo apt-get update

or

sudo apt update
    • Simulating software installation: apt-get install program -s
apt-get install firefox -s

Result: Installation of Firefox will be simulated.

    • Downloadable deb-file installation: dpkg -i debfail


  • Software removal: apt-get remove program
apt-get remove firefox

Result: Application Firefox will be removed.

    • Simulating software removal: apt-get remove program -s
apt-get remove firefox -s

Result: Removal of application Firefox will be simulated.

    • Previously installed deb-file removal: dpkg -r debfile
    • Removing a program with its configuration: apt-get remove --purge program
apt-get remove --purge postfix

Result: Mailing progam Postfix and all the configurations for it will be removed.

    • Removing installation files: apt-get clean

Installation files will be removed from /var/cache/apt/archives and disk space will be freed. To view disk space enter command du -h. Option -h notes human readable(eg. GB, MB)


  • Updating operation system software list: sudo apt-get update


  • Updating already installed software: sudo apt-get dist-upgrade or sudo apt full-upgrade


  • Creating aliases

Use a text editor(like nano) to edit ~/.bash_aliases located in the user home directory. Add the desired alias to the alias list.

nano ~/.bash_aliases

alias getapp='apt-get install'

To save the changes file .bashrc needs to be restarted

. .bashrc

Result: Result: Alias getapp is created and can now be used instead of apt-get install:

getapp firefox


  • Viewing software version: apt-cache policy program
apt-cache policy firefox

Result: Information if firefox is installed will be displayed, and if installed also the current version. Candidate means reccomended version.


  • Checking dependencies
    • Checking what software a certain application depends of: apt-cache depends program
apt-cache depends nano

Result: Information about text editor nano dependencies will be displayed.

    • Checking what other software depends on a specific program: apt-cache rdepends programm
apt-cache rdepends nano

Result: Displays all software that depends on nano text editor.


  • Fixing a disrupted installation

Command apt-get –f install checks and fixes disrupted dependencies. Command dpkg –configure –a completes an unfinished installation. These commands should be implemented one after another.


  • Migrating a software package to another server
    • Reading the software package list: dpkg --get-selections
    • Creating the old server software package list: dpkg --get-selections>installifail.txt
    • Implementing the old server software package list onto a new server:

dpkg --set-selections<installfile.txt

4. Disk partitions

  • Theory:

After creating a disk partition a file system needs to be created to it. The disk partition then needs to be connected to a directory(usually a home directory).

Primary partition There can be max 4 primary partitions. Numeration of those is 1-4. Eg. sdb4(4th partition of the second disk)

Extended partition Can be created into one of the primary partitions. Numeration is 1-4.

Logical partition Can be mutiple partitions and always located inside a virtual partition. Numeration starts from 5. Eg sdb5

  • Checking disk partition table: fdisk –l

All partitions on a disk will be displayed:

 Device     BootStart    End      Blocks      Id    System
 /dev/sdb1      1        62      497983+     83    Linux
 /dev/sdb2      133      1044    7325640      5    Extended
 /dev/sdb5      133      194     497983+     83    Linux


  • Creating a partition: fdisk /dev/sdX
fdisk /dev/sdb

Result: Entering into the second disc prompt, where commands are asked to enter.

    • Command n is used in the prompt to create a new partition to the disk.
    • New partition configuration:

- Command p creates a primary partition and e creates a extended partition. A logical partition can be created inside a virtual partition with the command l.

- Setting a partition number: 1-4


  • Other partition related commands inside fdisk /dev/sdX prompt:

- Command p displays the partition table.

- Command q exits the prompt without saving the changes.

- Command d deletes a partition.

- Command w saves the changes into the partition table.

- Selecting the first sector of a partition: Accept value given by default or change value manually.

- Selecting the last sector or the size of the partition: When choosing the size, the + sign must be used before the value. eg:+1GB



NB! If operating system gives an error message upon entering w command("Warning, error 16: Device or resource busy. The Kernel still uses the old table"), then the previously created partitions need to be unmounted(see unmount below).


  • Creating a file system: mkfs –t type partition
mkfs –t ext4 /dev/sdb1

Result: A file system is created to sdb1.

  • Mounting a partition to a (home)directory: mount partition directory
mkdir /var/primary

mount /dev/sdb1 /var/primary

Result:Partition sdb1 will be mounted to /var/primary .


  • Adding a connected filesystem to the boot file /etc/fstab using a text editor
nano /etc/fstab

After implementing changes the /etc/fstab usually looks like this:

<file system> <mount point> <type> <options> <dump> <pass>
/dev/sdb2 /var/primary ext4 defaults 0 0


  • Unmounting a partition: umount /partition
umount /dev/sdb1

Result: Partition sdb1 will be unmounted.

5. SWAP

  • Swap consists of the following phases:

1) Creating a partition for swap.

2) Assigning the created partition to swap: mkswap partition

mkswap /dev/sdb5

3) Changing the boot file/etc/fstab .

nano /etc/fstab

Partition information needs to be entered as such:

<file system> <mount point> <type> <options> <dump> <pass>
/dev/sdb5 none swap sw 0 0

4) Enable paging: swapon -a

Swap configuration will be loaded from /etc/fstab.


  • Check for swap area existence: swapon -s

6. WORKING WITH PROCESSES

  • Listing processes: ps -ef

Process list displays an id number for every process("""PID""")

  • Displaying processes sent to the backround: jobs
  • Killing a process: kill -signal PID
kill -9 1305

Result: A process with PID 1305 will be killed.

  • Killing a process running in the background: kill -signal %job_number
kill -9 %2

Result: Process number 2 running in the background will be killed.

  • Creating a soft link: ln -s /path/filename link
ln -s /var/log logs

Result: A soft link logs has been created pointing to /var/log directory.

  • Creating a hard link: ln /path/filename link
ln /var/log logis

Result: A hard link logs has been created pointing to /var/log.

  • Redirecting

Redirecting works with a "pipe" symbol |. To redirect into a file use > or >>. Double sign means adding to the file, single rewrites the file.

grep path | cowsay > text.txt

Result: Enviromental changers are beeing scanned for lines containing word "path". These lines are redirected to the program cowsay and its output will be redirected to text.txt, rewriting its contents.

grep top | grep –v grep

Result: Process table is being scanned for process "top", filtering the results so that process "grep top" will not be shown in the results. Instead only "top" process information will be displayed.

  • Sending a running process to the background ctrl+z.
  • To start a process on the background add the & sign after the program name: cowsay &.
  • Bringing a program back from the background: fg
  • Starting a program from the working directory: ./program_name