Useful Linux commands

Following some of the most commonly used linux command which I have to use most of the times while managing hosted sites on my and client’ server. It is just few of them which I usually forget and just wanted to make a list here for my own personal reference.

Check os version in Linux

cat /etc/os-release
lsb_release -a
hostnamectl
cat /proc/version

Find Linux kernel version

uname -r

Change Directory

cd /home/admin/

Permissions

CHOWN

chown -R admin:admin Locale

chmod on folders/files

find /var/www -type d -exec sudo chmod 2775 {} +
find /var/www -type f -exec sudo chmod 0664 {} +

Remove (only) files recursively

By file type
find . -name “*.pyc” -exec rm -rf {} \;

Remove all files
find . -type f -exec rm -rf {} \;

Delete All files/folders of a certain group

find . -group mygroup -exec rm -fr {} \;

GIT checkout to a folder

cd /home/admin/myrepo.git && git fetch 2>&1
GIT_WORK_TREE=/home/admin/mysitefolder/public_html git checkout -f [branch]
chown -R admin:admin
/home/admin/mysitefolder/public_html

Run cron every minute to hit an url

1 * * * * curl –request GET ‘http://url_to_hit

Edit Crontab

crontab -e

Generate RSA Key Pair

ssh-keygen -t rsa

Generate a private key

openssl genrsa 2048 > your_private_key_name.pem

Generate CSR with private key

openssl req -new -key your_private_key_name .pem -out your_csr_name.pem

RSA Config Private Key

Host bitbucket.org
IdentityFile ~/.ssh/bitbucket_rsa

Search a string in files

grep -rnw '/path/to/somewhere/' -e 'string_to_search'

Adding the SSH-Keychain fingerprint

ssh-keyscan -H github.com >> ~/.ssh/known_hosts

MySQL Command

To export db to sql file

mysqldump -u username -p databasename > filename.sql

To import sql to db

mysql -u username -p -h localhost DATA-BASE-NAME < data.sql

To enable a virtual host on apache

sudo ln -s /etc/httpd/sites-available/default.conf /etc/httpd/sites-enabled/default.conf

To restart apache and php on centos 7

systemctl restart httpd.service
systemctl restart php-fpm.service

Leave a Reply

Your email address will not be published. Required fields are marked *