Linux tips

Debug a program.
To debug a program and read the errors run the command:
strace -tt -o log -p
log is going to the error log file.
Find all manuals of a program.
To find the all maunual of one program run the command:
man -k program_name
Manage ownership.
if want to run a project and users of that group work in this project with there user names then do:
1, add a group project
2, create a dir /opt/project
3, add the all users to the project group as well:
gpasswd -a user_name group_name
gpasswd -a example project
4, give the write permission to the group for project dir:
chmod -R g+w project
5, Set the S-GID means change the x bit to s so when any user creat any folder or files they inherent their ownership like their parents project:
chmod g+s project
one can set the set-UID by adding a 4 in the chmod like:
chmod 4755 mydir
the first 4 is the set-UID for set-GID use the 2 and for the sticky bit use the 1.
chmod g+s is for set-GID and u+s is for set-UID as well.
If you change the permisions of a hard link the real file permissions also changing but not with the soft link.
To make a folder read only change the line in fstab to this:
LABEL=/usr /usr ext4 ro,suid,dev,auto,nouser,async 1 2
even root cannot write to the usr dir until change ro to rw.
umask:
Umask set the default permission for a file and folder when a user creat that one, to see the default value run the umask command:
umask
022
That means 755 permission (minus the umaks values from 7 to see the permission)
Top
Export a program path.
If there is program other then default paths like /bin,/usr/bin for bash to use the program, export the path of the program:
export PATH=$:~/bin
above will add the bin dir from your home to the path.
export CDPATH=.;~
The above command will make cd program to search as will your home dir.
Top
Delete special chars.
To delete \ run the command:
rm -rf "\\"
to delete - :
rm -rf -- "-"
or
rm -rf "\-"
Top
script to close a programs.
To kill a program immediately run the script with the program name to kill:
#/bin/bash
# to kill process run the script as "killp procces_name". 
ps -ef | grep $1 | grep -v grep | awk '{print $2}' | xargs kill -9
Top
Prevent deleting of file even from root.
Here is a cool tip on how you can make files on your system immutable, Linux ships with a tool called chattr which can be used for the purpose.
To make your file protected:
# chattr +i test_file
To remove the protection:
# chattr -i test_file
To check the file attributes:
# lsattr test_file
To permit only appending the lines in a file, cannot change the written lines, or remove that:
# chattr +a test_file
# chattr -a test_file
Creat a empty file -i with touch in any directory, so when want to delete the files from this directory will prompt for permission and only the -f command can delte files from this dir:
touch -i
Top


<< Previous Next >>