"Turn" Linux use Tips

Source: Internet
Author: User
Tags perl interpreter

This article describes the Linux usage techniques that are accumulated in peacetime.

Properties of the file

Hidden properties of the file

Lsattr: List Hidden properties of a file
Chattr: Modifying hidden properties of a file

[Email protected] ~]# chattr [+-=][asacdistu] FileName
+: Add one attribute
-: Remove one attribute
=: Set To is the only attributes the files has
Important Options

A: You can only append the contents of a file, but you cannot modify or delete it
I: File cannot be deleted, renamed, Cannot create a link to it, cannot write to file
Special permissions for Files Suid/sgid/sticky Bit

If a suid or Sgid bit is set on an executable file, the file owner (set suid) or the group (with Sgid set) will have the permissions when the files are executed.

Example: An ordinary user cannot turn on the HTTPD service because the HTTPD service needs to use port 80, and the port below 1024 is only available to the root user. If we set the owner of the httpd executable to root and set the SUID bit at the same time, the HTTPD service can also be turned on by ordinary users.

If you set the sticky bit for a directory, only the owner of the file can delete the file. In a Linux system, the/tmp directory defaults to this bit:

DRWXRWXRWT root root 16384 Mar 6 09:04 tmp/
The main use methods are as follows:

SUID

For files: Run with the permissions of the file owner
For directory: Cannot set suid to directory
Set SUID:

chmod u+s file chmod 4755 file
SGID

For files: Run with permissions for the group to which the file belongs
For directories: Files in the directory inherit the properties of the directory
Set Sgid:

chmod g+s file/dir chmod 2771 file/dir
Sticky

For file: Cannot set sticky bit on file
For directories: For files in this directory, only their owners can delete them.
Set Sticky:

chmod o+t dir chmod 1777 dir
Set special permissions with letters:

U+s G+s o+t
Special permissions are represented by numbers:

4 for SUID
2 for SGID
1 for Sticky
Special Permissions for Scripts

It is important to note that script files such as Shell, Python, and Perl cannot be set suid bits because they are actually run by bash, Python, and Perl interpreter. For the script file to have a function similar to suid, we need a shell, which can set the Suid/sgid bit, the shell really works or the script.

For example, we have a script/home/jh/bin/myscript.sh, the owner is a normal user, but the script inside the operation requires root permission, now we use C language to write the shell, the name is called TRANSEUID.C:

/*
* author:jh Gao <[email protected]>
* Create date:2012-06-05
* function:transmit Euid and EGi D to other scripts
* Since shell/python/... scripts can ' t get suid permission in Linux
*/
#include <stdio.h> ;
#include <stdlib.h>
#include <unistd.h>
#define BUFFSIZE 1024x768
/*
* Usually euid is the UID Who run the program
* If Stick is setted to the program
* Euid are the UID or the program ' s owner
*/
INT Main (int argc, char *argv[]) {
Char *cmd = "/home/jh/bin/myscript.sh";
Char *pars[] = {"/home/jh/bin/myscript.sh", "Par1", "par2"};
/* Set UID and GID to euid and Egid */
Setuid (Geteuid ());
Setgid (Getegid ());
if (EXECVP (cmd, pars)) {
printf ("error");
Free (cmd);
Exit (1);
}
Free (cmd);
}
Compile this program, set the desired user to this program, then set SUID, then you can execute the script or command with this user's privileges:

$ gcc-t Transeuid TRANSEUID.C
$ sudo chown root transeuid
$ sudo chmod +s transeuid
$./transeuid ... Do SOMETHING
But it is important to note that this gimmick has great security implications.

/etc/fstab file error What to do

At this point, the system does not start normally, you can start to enter single user mode, while the root directory "/" is read-only, you can use the following command to re-mount "/" to "Read and write":

[Email protected]]# mount-n-o REMOUNT,RW/
-n:mount but does not change/etc/mtab
-o:options
partprobe– do not restart using the new partition table

Partprobe:reinitializes the kernel in memory of the partition table. After you change the partition settings, you are prompted to restart to change the partition table in kernel, and you can eliminate the restart by using Partprobe.

Ubuntu system switching between GDM and KDM

If you have both GNOME and KDE installed, there are times when you need to switch between GDM and KDM:

sudo dpkg-reconfigure GDM
Increase swap space

Create a new partition or a new file
Write special tags with the Mkswap tool
Add a new record to the/etc/fstab
Activate swap partition, command swapon-a or Swapon [Swapfile]
Check the status of the swap partition with the Swapon-s command
Example:

DD If=/dev/zero of=/swapfile bs=1m count=100
Mkswap/swapfile
Vi/etc/fstab ...
Swapon-a
One NIC binds multiple IPs

Example:

The system is CentOS, the NIC is eth0, if only 1 additional IP is set, the file is created
/etc/sysconfig/network-scripts/ifcfg-eth0:0
, set the IP information in the file.

If you need to set up an IP segment, create the file Ifcfg-ethx-rangex:

IFCFG-ETH0-RANGE0:

Device=eth0-range0
Bootproto=static
hwaddr=08:00:27:24:c2:72
Onboot=yes
ipaddr_start=192.168.56.20
ipaddr_end=192.168.56.30
netmask=255.255.255.0
Lsof

The lsof command can list all open files. The most common function of this command is to find the "lost" space.

For example, we use the DF command to see the/home partition with only 1 G left, but the result of the du command is that there should be 5G.
, this situation is often caused by the deletion of some files, but the file handles of the deleted files have not been released.
With the command

lsof | Grep-i deleted
See which files have been deleted but the file handle has not been released, kill or restart the response process will be able to retrieve the "lost" space.

Lsof can sometimes recover files that have been mistakenly deleted, please google for specific methods.

Directory length

The length of the directory is never 0, because it always contains. and.. Two items. The length of a symbolic connection is the number of characters that its pathname contains, and the length is not 0 because there is at least one character in the path name.

Create a folder named "-F"

To create a folder named "-F", using the command mkdir-f must fail, while using mkdir---f can create success.

Switch between the most recently used two directories with "CD-"

The difference between Su and Su-

Executing su The modern shell inherits the current shell environment, Su-simulates the actual root login session

Quick Erase History

Export histsize=0
$ related parameters in bash

$ A-Indicates the current file name
$*-Separates all parameters with a space, forming a string
[email protected]-separates all parameters with a space, forming a string combination. Unlike $*, "$*" is a string, while "[email protected]" contains multiple strings.
$#-Number of arguments passed to the process
$? -The execution result of the previous command, 0 if there is no error
$$-PID of this command
Bash techniques, variable names that are combined by the contents of a variable into another variable

EXAMPLE:

A_b_c_d= "Something"
t1= "B"
T2= "_d"
eval echo \ $A _${t1}_c${t2};
Bash command Line input tips

Use Ctrl+r to search for previously used commands
Use Ctrl+w to delete the current single time
Use Ctrl+u to delete a forward

Xargs

Xargs is very powerful, with-l{} You can specify the location of the parameter:

Cat Hosts | xargs-i{} SSH [email protected]{} hostname
Write a secure bash script

The most common ones are:

SET-E, when an error occurs, the script exits
Set-u, when Bash discovers that there are no initialized variables, exit
More information: Write a robust bash script

Tar package files listed in the specified list

Cat Yourlist.lst
/etc/fstab
/home/admin/bin/somefile.sh
/home/mysql/somefile
...

Tar cvzf xxx.tar.gz-t yourlist.lst
Specify a DNS server to query the domain name records

Dig @8.8.8.8 www.google.com
The most important parameters to note for the sort command are-K and-s:

-S,--stable
Stabilize sort by disabling last-resort comparison
Stable indicates that the final order depends on the original order.

$ cat A.txt
A
A
B
B
$ sort-f A.txt
A
A
B
B
$ sort-f-S A.txt
A
A
B
B
example,-f means case-insensitive, and-s indicates that the order depends on the order of the original file

-K,--key=pos1[,pos2]
Start a key at POS1 (Origin 1), end it at POS2 (default end of line).
So just sort the second column should write:

sort-k1,1
For more tips on sort, refer to
Sort Files like A Master with the Linux Sort Command (Bash)

The use of Man

The man page may have several sections, such as this:

$ Man-aw Man
/usr/share/man/man1/man.1.gz
/usr/share/man/man7/man.7.gz
There are 2 sections. It is also often seen in the man page similar to:

See ALSO
Epoll_create (2), epoll_create1 (2), Epoll_ctl (2), epoll_wait (2)
The numbers inside the brackets refer to section. Different sections represent different categories of content:

MANUAL SECTIONS
The standard sections of the manual include:

1 User Commands
2 System Calls
3 C Library Functions
4 Devices and Special Files
5 File Formats and conventions
6 games et. Al.
7 Miscellanea
8 System Administration Tools and Deamons

Distributions Customize the manual sections to their specifics,
Which often include additional sections.
View 7th section:

Mans 7 Man
MPlayer character Playback:

Mplayer-vo AA Xxx.avi is played with no color characters;
Mplayer-vo Caca Xxx.avi is played with colored ASCII characters;
Mplayer-vo Matrixiew Xxx.avi play with a terminal similar to the hacker Empire!
Custom Endpoint Auto-complete

For example, I want to ssh, Ping, myscript these three commands automatically complete parameters, where the parameter name is written in the/tmp/my_word_list file, we can do the following in the. BASHRC settings:

function _my_cmpl () {
Local My_cmpl_words cur
Compreply= ()
Cur= "${comp_words[comp_cword]}"
my_cmpl_words= ' Cat/tmp/my_word_list '
Compreply= ($ (compgen-w "$my _cmpl_words"--"$cur"))
}
Complete-f _my_cmpl ssh Ping myscript
SSH Save session

vi/home/User name/.ssh/config (no new one), add the following:

Jhost *
Jcontrolmaster Auto
Jcontrolpath/tmp/%[email protected]%h:%p
Save exit. Once you log in to the server and then log in to the same server in the new terminal, you don't have to lose the password.

Sshfq

SSH over the crab wall, if you have a server outside the wall, and you can ssh to it without a password,
You can use SSH to do port forwarding to realize the flip of the wall.
Plus the switchy on Chrome or the Autoproxy plugin on Firefox will be free to surf the web.

Use the local 7001 port as the forwarding gate:

ssh-qtfnn-d 7001 [Email protected]_server
The first parameter calls a function as a function name

Func_eval () {
Type= ' Type $ | head-1 | awk ' {print $NF} '
If [$?-gt 0]; Then
echo "ERROR Call Function: $ ... failed"
elif ["$TYPE" = = "function"]; Then
Eval $*
Else
echo "ERROR invalid function: $ ..."
Exit 1
Fi
}
The most understandable and efficient way to work with files on a row-by-line basis:

While Read line
Do
echo "$LINE" >> $OUTFILE
# do something
Done < $INPUTFILE
Get the length of a string represented by a variable:

' Echo ${#VAR} '
${pager:-more}

Shell Command ${pager:-more}
This means that if the shell variable pager is already defined and its value is not NULL, its value is used, otherwise the string more is used.

$$ represents the PID of the current process.

"Turn" Linux use Tips

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.