Linux Basics

5
Linux Basics A) GIT: Version Control System, really useful for tracking your changes. TODO: try.github.com 15 mins tutorial. B) Vi : Powerful Editor: Recommended to be used. Useful commands to remember :q - quit :wq - Save and close :syntax on - Turn on Syntax highlighting for C programming and other languages :set number - Turn on the line numbers TODO: 25-30 mins vimtutor C) We learned different commands a) whatis - provides a one line description of the commands b) su : change users or become superuser: Remember the difference between su - <username> and su <username> c) touch - create zero byte files, mainly used for changing the timestamps of the file. d) gcc -Wall -pedantic -g <C source file> -o <Executable file> -Wall -pedantic : to check for all the warnings and errors if any. -g to create the symbol file to be used by gdb -o to create the executable file. e) GDB: GNU debugger gdb -tui <Program name> -tui for listing the source while debugging b <linenumber> to set the break point p <variable name> to print the value of the variable bt to print the stack call, mainly useful to find segmentation fault when multiple functions are called. f) make: If your program source file name is test.c, then you can directly write make test, this would compile the test.c program. Remember this it's a faster way. h) /etc/issue : Contains the message which is displayed on terminal before login. i) /etc/motd: Contains the message which is displayed on terminal after login. j) cal and date: Display calendar and date. Remember the escapes sequences used in the /etc/issue. man agetty lists them. TODO: a) Do some work on all these commands, b) read there man pages. c) create one small program using vi with syntax on, compile it using gcc, try to debug it using gdb., try to track that program using git, upload them to a remote server, then pull your code, check if its the same. d) Try to change the messages before login, after login. © Vijay Kumar, RC Bose Center of Cryptology and Security, Indian Statistical Institute, Kolkata, India

description

A book on Linux

Transcript of Linux Basics

Page 1: Linux Basics

Linux Basics

A) GIT: Version Control System, really useful for tracking your changes.TODO: try.github.com 15 mins tutorial.

B) Vi : Powerful Editor: Recommended to be used.Useful commands to remember:q - quit:wq - Save and close:syntax on - Turn on Syntax highlighting for C programming and other languages:set number - Turn on the line numbers

TODO: 25-30 mins vimtutor

C) We learned different commandsa) whatis - provides a one line description of the commandsb) su : change users or become superuser: Remember the difference between su - <username> and su <username>c) touch - create zero byte files, mainly used for changing the timestamps of the file.d) gcc -Wall -pedantic -g <C source file> -o <Executable file>

-Wall -pedantic : to check for all the warnings and errors if any.-g to create the symbol file to be used by gdb -o to create the executable file.

e) GDB: GNU debuggergdb -tui <Program name>

-tui for listing the source while debuggingb <linenumber> to set the break pointp <variable name> to print the value of the variablebt to print the stack call, mainly useful to find segmentation fault when multiple functions are called.

f) make: If your program source file name is test.c, then you can directly write make test, this wouldcompile the test.c program. Remember this it's a faster way.

h) /etc/issue : Contains the message which is displayed on terminal before login. i) /etc/motd: Contains the message which is displayed on terminal after login.j) cal and date: Display calendar and date.

Remember the escapes sequences used in the /etc/issue. man agetty lists them.

TODO:a) Do some work on all these commands, b) read there man pages. c) create one small program using vi with syntax on, compile it using gcc, try to debug it using gdb.,try to track that program using git, upload them to a remote server, then pull your code, check if its the same.d) Try to change the messages before login, after login.

© Vijay Kumar, RC Bose Center of Cryptology and Security, Indian Statistical Institute, Kolkata, India

Page 2: Linux Basics

A) We learned how to update the debian system using apt-get.

apt-get update - Sync with Repositoryapt-get upgrade - Upgrade installed packagesapt-get dist-upgrade - Upgrade distribution packages.apt-get install <Package Name> - Install the packageapt-get remove <package name> - Uninstall the package.

TODO:Read about apt-get., do man apt-get, install a package call cmatrix, uninstall it and install it again.

Tip: As mostly, updating takes time, you can club all the commands likeapt-get update && apt-get upgrade && apt-get dist-upgrade && poweroff

poweroff would shutdown the system after everything is updated.

B) Using brackets to do work faster, as I gave the example of creating directories.

touch {2012,2013,2014}_{Jan,Feb,Mar} would create 9 files with 2012_Jan, 2012_Feb, 2012_Mar and same with 2013,2014.

C) Copy preserve file permissions and timestampscp -av/pv

-a similar to preserve,-p preserve-v verbose.

TODO: read man cp and copy something and check if the file permissions and timestamps are preserved.

D) chown, chgrp, chmod

TODO: do some practice with this

E) hostname - prints out the hostname of the machine which is stored in /etc/hostname.

F)Pipes: > direct normal output2> direct error output&> direct all output

G) Translate

tr 'a-z' 'A-Z' converts from smaller to uppercase.

H) Combine two commands (cal 2007 ; cal 2008)

© Vijay Kumar, RC Bose Center of Cryptology and Security, Indian Statistical Institute, Kolkata, India

Page 3: Linux Basics

I) tee command - saves output in file as well as forward it.

J) cat /proc/cpuinfo - provides information about cpu /proc/meminfo - provides information about memory/ RAM /proc/version - provides information about the version of your system.

K) less/ more - prints information one per page. head/tail - first 10 lines / last 10 lines.

L) cut / wc / sort / diff cut uses a delimiter and to print the fields

TODO:

Scenario: Supposed you got access via shell to a linux system and extract some information from it. Create a script (Present me everything in one script).

a) Create a alice, bob, eve with the password "password"HINT: set password using chpasswd, look some examples in google to change from cmdline.b) Login from eve.c) Copy and preserve all the configuration files from /etc and save it in eve home directory in the folder etc-backup-YYYYMMDD, direct all errors to cp.errd) Change the owner of all the files in the folder just created to bob and the group of all the files to alice and change the permission of all the files to 440 i.e r--r-----HINT: would have to be logined as roote) Provide me all the unique shells used by the user present in the system in CAPS.HINT: /etc/passwd file contains all the shells, three four commands would be used.f) Cover your tracks, clear out the /var/log/auth.log (Have a look at this file and create a backup before clearing), clean your terminal historyHINT: man pages would help you.h) Delete all the user bob, alice, eve. Make sure you delete there files too.

A) sed: Short from stream editor really very useful.

in vi editor

:s/test/learn would replace test to learn in current line but only first instance.:s/test/learn/g would replace test to learn in current line all the instance.:s/test/learn/gi would replace test (all cases) to learn in current line all the instance.:%s/test/learn/gi would replace test to learn in the file (all lines)

B) Bash configuration files

- ~/.bash_profile- ~/.bash_history - contains all the history of the commands.- ~/.bash_logout - contains the command which are executed when bash is exited- ~/.bashrc - setting of variables for bash.- /etc/profile - setting of PATH variable and PS1

C) last, lastb, lastlog -

© Vijay Kumar, RC Bose Center of Cryptology and Security, Indian Statistical Institute, Kolkata, India

Page 4: Linux Basics

last - shows all the login attempts and the reboot occurred.lastb - shows all the bad login attempts lastlog - shows the list of all the users and when did they login.

D)tar - tar archiving utility

-c create archive-t list the content of the file-x extract the files-j bzip2 format-z gzip format

E) Linux has six runlevels 0-6

Scripts are contained in /etc/rc[0-6,S].d/

Each folder contains the scripts which are followed by either K or S. If the first letter is K that scriptis not executed. If S, that script is executed.

/etc/inittab contains the default run level.

F) pstree - Process tree

G) Sysctl - configure kernel parameters

/etc/sysctl.conf - contains the variables for kernel parameters.

sysctl -a display all the kernel parameterssysctl -w <kernel parameter>

H) Kernel Modules

contained in /lib/modules/$(uname -r)/

lsmod - list all loaded modulesmodprobe - load kernel moduleslspci - list all pci deviceslsusb - list all usb deviceshal-device - list all the Hardware Abstraction layer devices

I) mount and umount - Mount/unmount a filesystem

J) Groupadd, groupdel/ groupmod

-add, delete or modify any group

K) SUDO

/etc/sudoers

© Vijay Kumar, RC Bose Center of Cryptology and Security, Indian Statistical Institute, Kolkata, India

Page 5: Linux Basics

-File should be edited by visudo command

Follows a pattern like

username ALL=(ALL)

For example:--User Alias SpecificationUser_Alias LIMITEDTRUST=student1,student2

--Command Alias SpecificationCmd_Alias MINIMUM=/etc/rc.d/init.d/httpd

--User privilege specification sectionLIMITEDTRUST ALL=MINIMUM

Users student1 and student2 can use sudo only with the commands listed with MINIMUM (httpd)

users student1, student2 should exists in the group sudo. (/etc/group)

man sudoers

L) nm-applet - Just a applet for network manager.

TODO:Have a look at the man pages of all the commands at read the abstract, some common optionsa) Turn off the ping responses for your system permanently and turn on the Syn-cookies protection mechanism. {Search on Google}b) Use your previous script to create three users alice, bob, eve. -create a folder dept inside it two folder hr, web. -create two group hr and web.-change group of web folder to web and hr to hr.-add alice and bob user to web group-add alice to hr group.-check that bob is not able to enter in the hr folder and alice is able to enter in both hr and web folder-add user bob to sudo group and check if it is able to run sudo ifconfig ?

© Vijay Kumar, RC Bose Center of Cryptology and Security, Indian Statistical Institute, Kolkata, India