Last active
April 1, 2021 11:46
-
-
Save idvoretskyi/f6ea58cf37afed086f2d to your computer and use it in GitHub Desktop.
Linux Tips&tricks.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ########### Benchmark | |
| wget -qO- bench.sh | bash | |
| curl -Lso- bench.sh | bash | |
| ########### User management | |
| # allow superuser access without password | |
| echo "$USER ALL=(ALL) NOPASSWD: ALL" | sudo tee -a /etc/sudoers | |
| OR | |
| echo 'debian ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers.d/debian # where debian is the username | |
| echo 'ubuntu ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers.d/ubuntu # where ubuntu is the username | |
| echo 'centos ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers.d/centos # where centos is the username | |
| ==== The following settings are useful for distinguishing the root prompt from non-root users. | |
| # Regular user | |
| # A green prompt for regular users: | |
| echo "PS1='\[\e[1;32m\][\u@\h \W]\$\[\e[0m\] '" >> ~/.bashrc | |
| # Root | |
| # A red prompt for root: | |
| echo "PS1='\[\e[1;31m\][\u@\h \W]\$\[\e[0m\] '" >> ~/.bashrc | |
| ########## Dropbox inotify fix | |
| echo fs.inotify.max_user_watches=100000 | sudo tee -a /etc/sysctl.conf; sudo sysctl -p | |
| ### Date prompt | |
| $ date +"%Y%m%d" | |
| 20131016 | |
| $ date +"%Y-%m-%d" | |
| 2013-10-16 | |
| $ date +"%Y%m%d_%H%M%S" | |
| 20131016_193655 | |
| ########## Pastebin CLI analoque ##### | |
| http://termbin.com/ | |
| # Sample | |
| ls -la | nc termbin.com 9999 | |
| # Sprunge | |
| curl -F 'sprunge=<-' http://sprunge.us | |
| ########### Docker ################### | |
| # Install docker on Linux (depends on distribution) | |
| curl -sSL https://get.docker.com/ | sudo bash | |
| # Allow non-root usage of docker | |
| sudo usermod -a -G docker $USER | |
| sudo usermod -aG docker ubuntu # e.g. with 'ubuntu' user | |
| # Return IP address of container | |
| docker inspect --format '{{ .NetworkSettings.IPAddress }}' ${CID} | |
| # Return IP addresses of all containers | |
| docker inspect --format '{{ .NetworkSettings.IPAddress }}' $(docker ps -q) | |
| ########### AWK ##################### | |
| # Print the first statement on first line of file: | |
| cat file | awk 'NR==1{print $1}' | |
| # Print the assigned IP addresses on network interface: | |
| arp -n | grep eth0 | awk '{print $1}' | |
| # Print all columns: | |
| awk '{print $0}' file | |
| # Print the 3rd column: | |
| awk '{print $3}' file | |
| # Print the 1st and the 3rd columns: | |
| awk '{print $1 $3}' file | |
| # Print all other columns but not the 3rd one: | |
| awk '{$3=""; print $0}' file | |
| # Print all other columns but not the 1st and the 2nd: | |
| awk '{$1=$2=""; print $0}' file | |
| ########### Find ##################### | |
| # Find and change permissionsusermod -a -G docker `whoami` | |
| # for directories | |
| find . -type d -exec chmod 755 {} + | |
| # for files | |
| find . -type f -exec chmod 644 {} + | |
| # ssh directory sample | |
| find $HOME/.ssh/ -type f -exec chmod 600 {} + | |
| # Find and change ownership of a directory | |
| sudo find . -type d -user root -exec chown idv:idv {} \; | |
| # Find all .sh files and make them executable | |
| find . -name "*.sh" -execdir chmod u+x {} + | |
| # Find and sort 10 biggest files | |
| du -hsx * | sort -rh | head -10 | |
| ########### Sed ###################### | |
| # GRUB to disable boot animation | |
| sed -i 's/quiet splash//' /etc/default/grub && update-grub | |
| # Locale correction | |
| sed -i 's/uk_UA.UTF-8/en_US.UTF-8/' /etc/default/locale | |
| # Replace hostname in /etc/hosts | |
| sed -i "s/ubuntu1404/$(hostname)/" /etc/hosts | |
| # text replace in some files | |
| grep -rl "windows" . | xargs sed -i "s/windows/linux/g" | |
| # find and replace smth in some files | |
| find /path -type f -exec sed 's/example.com/superexample.com/g' {} + | |
| more: https://rtcamp.com/tutorials/linux/search-replace-text-multiple-files/ | |
| ########## ifconfig.me ############# | |
| # My external IP | |
| curl ifconfig.co | |
| # All | |
| curl ifconfig.co/all | |
| ############# cat EOF ################# | |
| 1. Passing multiline string to a variable: | |
| $ sql=$(cat <<EOF | |
| SELECT foo, bar FROM db | |
| WHERE foo='baz' | |
| EOF | |
| ) | |
| The $sql variable now holds newlines as well, you can check it with echo -e "$sql" cmd. | |
| 2. Passing multiline string to a file: | |
| $ cat <<EOF > print.sh | |
| #!/bin/bash | |
| echo \$PWD | |
| echo $PWD | |
| EOF | |
| The print.sh file now contains: | |
| #!/bin/bash | |
| echo $PWD | |
| echo /home/user | |
| 3. Passing multiline string to a command/pipe: | |
| $ cat <<EOF | grep 'b' | tee b.txt | grep 'r' | |
| foo | |
| bar | |
| baz | |
| EOF | |
| This creates b.txt file with both bar and baz lines but prints only the bar. | |
| ######## SSH | ssh-add ############### | |
| # error could not open a connection to your authentication | |
| eval `ssh-agent`; echo $SSH_AUTH_SOCK | |
| # A local port forward can be established from the CLI with the following syntax: | |
| ssh -L <local_port>:<destination_server>:<destination_port> user@<ssh_server> -p <ssh_port> | |
| # For instance, if your router's WAN IP address is 12.23.34.35, its remote administration SSH port is 9999 and its LAN-accessible | |
| # web interface is at port 80: | |
| ssh -L 12345:localhost:80 [email protected] -p 9999 | |
| -L 8080:127.0.0.1:80 -L 8443:127.0.0.1:443 -L 8001:127.0.0.1:8001 -L 5901:127.0.0.1:5901 -L 1313:127.0.0.1:1313 | |
| ######### Bash loop sample ############# | |
| for i in {1..10}; do command; done | |
| for i in {18,20.21}; do sleep 10 && ssh node-$i "tar zcvf - /var/log/ceilometer | cat > /tmp/ceilometer-node$i.tar.gz"; done | |
| for i in {18,20,21}; do scp node-$i:/tmp/ceilometer-node$i.tar.gz . ; done | |
| ######### Download a TAR archive and extract it in one-command | |
| wget http://example.com/archive.tar -O - | tar -x | |
| wget http://example.com/archive.tar.gz -O - | tar -xz | |
| wget http://example.com/archive.tar.bz2 -O - | tar -xj | |
| curl http://example.com/archive.tar | tar -x | |
| curl http://example.com/archive.tar.gz | tar -xz | |
| curl http://example.com/archive.tar.bz2 | tar -xj | |
| ### | |
| # install pathogen plugins | |
| while read plugin; do git clone https://github.com/$plugin; done < plugins |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment