Tech C**P
14 subscribers
161 photos
9 videos
59 files
304 links
مدرس و برنامه نویس پایتون و لینوکس @alirezastack
Download Telegram
How to remove a user on CentOS 6?

userdel my-target-user

In case you want to remove all the associated user files too use -r parameter:

userdel -r my-target-user

Yes! As simple as that.

#linux #sysadmin #userdel #user #centos
Does that happen for you too, to CD into a wrong directory and need to get back to the previous directory?

Let's say you are in a long path like /mnt/new_volume/backup/files/archive, now you switch to home of your directory. In order to get back to the previous directory, you just need to issue the below command:

cd -

#linux #cd
If you forget to pull your projects from git in a regular interval and many users working on the same projects, then there is a solution for you!

Create a bash script file as follow and make it executable by chmod +x puller.sh:

puller.sh file content:

#!/bin/bash

echo 'Iterating over folders...'
for dir in *
do
test -d "$dir" && {
cd ${dir}
echo "git pull $dir"
git pull
cd ".."
} || {
echo "------> $dir is not a directory <-------"
}
done

NOTE: this file should reside in your folder's project root. In my case it is in /Your/Projects/Folder.

Now as a final step, put it in your crontab:

10 * * * * bash -c "cd /Your/Projects/Folder; bash puller.sh >> /var/log/git_pull_output.log"

#linux #git #pull #cronjob #crontab #cron #bash
We have talked before about how to get current month using the below line of code:

echo $(date +%m)

It prints out 01, 02, etc.

As per the GNU date manpage:

By default, date pads numeric fields with zeroes. The following
optional flags may follow '%':
- (hyphen) do not pad the field

So you can remove leading zero by hyphen as below:

echo $(date +%-m)

It prints out 1, 2, etc.

#linux #bash #date
How to SSH login without password?

You want to use Linux and OpenSSH to automate your tasks. Therefore you need an automatic login from host A / user a to Host B / user b. You don't want to enter any passwords, because you want to call ssh from a within a shell script.

How to do it?
First log in on A as user a and generate a pair of authentication keys. Do not enter a passphrase:

a@A:~> ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/a/.ssh/id_rsa):
Created directory '/home/a/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/a/.ssh/id_rsa.
Your public key has been saved in /home/a/.ssh/id_rsa.pub.
The key fingerprint is:
3e:4f:05:79:3a:9f:96:7c:3b:ad:e9:58:37:bc:37:e4 a@A


Now use ssh to create a directory ~/.ssh as user b on B. (The directory may already exist, which is fine):

a@A:~> ssh b@B mkdir -p .ssh
b@B's password:


Finally append a's new public key to b@B:.ssh/authorized_keys and enter b's password one last time:

a@A:~> cat .ssh/id_rsa.pub | ssh b@B 'cat >> .ssh/authorized_keys'
b@B's password:


From now on you can log into B as b from A as a without password:

a@A:~> ssh b@B


#linux #sysadmin #ssh #password_less #ssh_login
How to get default gateway IP address?

ip route | grep default


The output would be something like below:

default via 192.168.1.1 dev eth0 onlink


NOTE: it can be different on your system

#linux #sysadmin #ip #route #default_gateway
Virt-builder is a tool for quickly building new virtual machines. You can build a variety of VMs for local or cloud use, usually within a few minutes or less. Virt-builder also has many ways to customize these VMs. Everything is run from the command line and nothing requires root privileges, so automation and scripting is simple.

To see available virtual machines:

virt-builder --list


Sample command to create a debian-9 image:

sudo virt-builder debian-9 --size=50G --hostname prod.example.com --network --install network-manager --root-password password:YOUR_PASS


The above command creates a debian 9 image with disk size of 50GB and sets the hostname to prod.example.com. --network enables the networking on guest and --install installs packages on the target O
S. The last parameter sets the root password to YOUR_PASS.

To read more about the axtra parameters:
- http://libguestfs.org/virt-builder.1.html

#linux #sysadmin #virt_builder #debian #image
You can login to a server without entering a password by a simple command as below:

ssh-copy-id USERNAME@YOUR_HOST_IP -p 22


By issuing the above command it puts your public key content on server ~/.ssh/authorized_keys and prompts you to enter the password. You are all done by this.

#linux #sysadmin #ssh #passwordless_login #ssh_copy_id #authorized_keys #public_key
How I store & manage passwords in Linux for servers and other credentials? gopass

Password management should be simple and follow Unix philosophy. With pass, each secret lives inside of a gpg encrypted file whose filename is the title of the website or resource that requires the secret. These encrypted files may be organized into meaningful folder hierarchies, copied from computer to computer, and, in general, manipulated using standard command line file management utilities.

gopass is a rewrite of the pass password manager in Go with the aim of making it cross-platform and adding additional features. Our target audience are professional developers and sysadmins (and especially teams of those) who are well versed with a command line interface. One explicit goal for this project is to make it more approachable to non-technical users. We go by the UNIX philosophy and try to do one thing and do it well, providing a stellar user experience and a sane, simple interface:

- https://github.com/gopasspw/gopass

#unix #linux #sysadmin #pass #gopass #password_management #golang #github
In order to enable bash completion in Kubernetes you can usee the below command in linux bash:

source <(kubectl completion bash)


Now to test this enter the below command and you should see the completion:

kubectl cl<TAB>


It should be expanded to kubectl cluster-info.


#linux #bash #shell #kubernetes #kubectl
How tom remove dangling volumes in docker?

docker volume rm $(docker volume ls -qf dangling=true)


#docker #linux #dangling
In Dockerfile`s some people in the community use `alpine base image in order to reduce docker image size. apk is its package management tool that can be used to install OS packages. So for example if
you want to install network tools (like ping) you need to install netcat-openbsd:

apk add netcat-openbsd


You can squash your image size even further by some tips. When you install a package, linux distros first download the package and put it in a cache folder. In Alpine it is located in /var/cache/apk.
To tell the OS to delete the cache after installation you can provide --no-cache option to it:

apk add --no-cache netcat-openbsd


There are some package like g++ or git that is needed on installation of some other packages. After installation those packages is useless and just increase image size. You can remove those packages b
y using --virtual command:

apk add --no-cache --virtual .build-deps g++ \
&& # do you stuff here \
&& apk del .build-deps


Great job guys! You have reduced your alpine docker images so much :)

#docker #linux #alpine #apk #virtual #no_cache #apk_del #apk_add
pdsh: a high performance, parallel remote shell utility. Pdsh is a multithreaded remote shell client which executes commands on multiple remote hosts in parallel. Pdsh can use several different remote shell services, including standard "rsh", Kerberos IV, and ssh.

To see more about it head over to:
- https://github.com/grondo/pdsh

#linux #shell #pdsh #shell
How to recursively rename filenames?

find . -name '*txt' -exec bash -c ' mv $0 ${0/brand-/category-}' {} \;


The above command renames txt files starting with brand- to category-.

#linux #bash #find #rename #batch_rename
How to copy a file in the same path with a different name?

Well you just need to use curly braces, {}, to make it happen:

What you usually MIGHT do:

cp /usr/local/share/lib/sample.conf /usr/local/share/lib/settings.conf



You can take the smart path btw:

cp /usr/local/share/lib/{sample,settings}.conf


NOTE: if suffix of both files are different you can put that inside of curly braces too.

#linux #sysadmin #cp #copy #trick