Linux commands

Linux version:

$ lsb_release -a

$ uname -a

Users & Groups commands:

$ groupadd groupname

$ groupdel groupname

$ useradd -m -g groupname  -c “user description” user1

Change the primary group to groupname for user1

$ usermod -g groupname user1

Add some secondary groups to user1

$ usermod -a -G group1,group2,group3 user1

To remove a secondary group from user just keep the groups you want to keep, rest will be removed:

$ usermod -G group1 user1

$ id username

Add multiple groups using a script:

$ for group in group1 group2 group3 group4; do groupadd -r ${group};done

SUDO Access:

To give sudo permission to users in group “group1” to run commands as user1 or user2 example: $sudo -u user1 cmd1

$ visudo

%group1 ALL=(user1,user2) /usr/bin/cmd1, /usr/bin/cmd2

In order to give the user full root privileges, add the following line to the file:

userxyz ALL=(ALL) ALL

If you only want a user to run certain commands as root, you can create a command alias in your sudoers file, like so:

User_Alias ADMINS = userxyz
Cmnd_Alias HTTPD = /etc/init.d/httpd
ADMINS ALL = HTTPD

User_Alias creates a group of users that you can then assign command aliases to. (Additional users can be listed on the same line, separated by commas.) Cmnd_Alias then lists the various commands that the users of that Alias can run.

Disk/Files commands in rhel/centos:

$ locate filename

This will let you how many directories or files are indexed.

$ locate –statistics

$ find /dir-to-search -name “filename-to-find”

Find the disk usage by one level directory depth:

du -hsx * | sort -rh | head -10

$ du -ch –max-depth=1 /

Remove directory with all its contents without prompting (be careful there is no undo delete):

$ rm -rf dirname

Unzip and untar files in linux:

$ gunzip filename.tar.gz

$ tar -xvf  filename.tar

gzip an entire directory:

tar -zcvf directory1.gz directory1

To decompress and unpack the archive into the current directory you would use

tar -zxvf directory1.gz

That command is effectively the same as

gunzip < directory1.gz | tar -xv

Copy the directory “foo” from the local host to a remote host’s directory “bar”

$ scp -r foo your_username@remotehost.edu:/some/remote/directory/bar

Search linux command history:

Press control-R and type the past command to search.

Press control-R multiple times to go back to earlier times same command was run.

(reverse-i-search)`mysql’: mysql -u db1 -p

Last system reboot time:

$ last reboot

$ who -b

Who last logged in:

$ last

Top command change sort order:

Once inside top…

P -> Sort by CPU usage

M -> Sort by MEM usage

z -> Add cool visual colors

x -> Highlight column you are currently sorting by

Linux screen command:

Here are the steps you can follow to run a process in screen, detach from the terminal, and then reattach.

  1. From the command prompt, just run screen. This will give you a new subshell.
  2. Run your desired program
  3. Detach from the screen session using the key sequence Ctrl-a Ctrl-d (note that all screen key bindings start with Ctrl-a). This will drop you back to your original shell and display a message “[detached]”, indicating that the screen session is still running.
  4. You can then list the available screen sessions by running “screen -list
  5. You can reattach to this screen session by running “screen -r“. Once reattached, you will be able to take off where you left off and see any output that was printed to the screen during the time that you were detached. If you have multiple screen sessions, then you can specify the tty name (as displayed by screen -list) as an argument to screen -r to attach to a particular session.
  6. Type Ctrl-a n  to move between multiple screen sessions.

NTP time commands:

To install NTP on RHEL:

$ yum install ntp

$ ntpstat

$ service ntpd status   or  systemctl status ntpd

$ service ntpd restart  or systemctl restart ntpd

How to turn on NTP daemon at boot time:

$ chkconfig –list ntpd

ntpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off

Check the runlevel:

$ runlevel

N 3

$ chkconfig ntpd on   or    systemctl enable ntpd

Now check the ntpd chkconfig which shows runlevel 2,3,4,5:on which means the ntpd daemon will automatically start on boot time.

$ chkconfig –list ntpd
ntpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off

RHEL7/Centos7

$ systemctl isenabled ntpd

Networking commands:

If telnet or nc are not installed then check a port is open in server x.x.x.x

$ (echo > /dev/tcp/x.x.x.x/1521) >/dev/null 2>&1 && echo “Its up” || echo “its down”

$ nc -zv x.x.x.x 443

nc: connect to x.x.x.x port 443 (tcp) failed: Connection refused

Services commands:

$ systemctl list-unit-files –type=service

systemctl status name.service

systemctl is-active name.service # running
systemctl is-enabled name.service # will be activated when booting
systemctl is-failed name.service # failed to load

Cron commands:

List all cron jobs for a user:

$ crontab -l

Edit cron jobs:

$crontab -e

0 * * * * systemctl restart ntpd > /root/ntpdgc.log
0 * * * * echo ‘Run this command every hour’ >> /root/ntpdgc.log

Below will run myscript.sh every 5 minutes and send stdout and stderr to mycronout.txt:

*/5 * * * * /root/myscripd.sh >> /root/mycronout.txt 2>&1

The syntax for the cron job is

* * * * * command to be executed
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)

The first five fields specify the time of the scheduled running job, the * means that for each cycle of the time field (For example, the * on hour means each hour, the * on day means each day, if * appears on both hour and day field, it means every hour of every day). If we want to specify a particular time, we should put a number on relative time field. For example, if we want to run a cron job at 7:30 am every day, we should specify the cron job as

30 7 * * * command to be executed

Crontab Examples

A line in crontab file like below removes the tmp files from /home/someuser/tmp each day at 6:30 PM.

30     18     *     *     *         rm /home/someuser/tmp/*

Changing the parameter values as below will cause this command to run at different time schedule below :

minhourday/monthmonthday/weekExecution time
30011,6,12*— 00:30 Hrs  on 1st of Jan, June & Dec.
020*101-5–8.00 PM every weekday (Mon-Fri) only in Oct.
001,10,15**— midnight on 1st ,10th & 15th of month
5,10010*1— At 12.05,12.10 every Monday & on 10th of every month
:

Note : If you inadvertently enter the crontab command with no argument(s), do not attempt to get out with Control-d. This removes all entries in your crontab file. Instead, exit with Control-c.

Misc commands:

Run a command in the background even if the user logs off the command continues running in background:

$ nohup <some command to run in background> &

Email a file or message from linux:

$ mail -s “test subject” email.address@xyz.com < myfile.txt

$ echo “THIS IS A TEST EMAIL” | mail -s “test subject” email.address@xyz.com

Run commands on multiple servers using ssh login with key:

$ for i in $(cat myserverlist.txt); do mycmd $i;done

Bash script to read input userid and do some action:

#!/bin/bash

if [ $# -eq 0 ]; then
echo “ERROR: No arguments provided”
exit 1
fi

for i in “$@”
do
echo Argument: $i
kadmin.local -q “addprinc $i@REALM”
kinit $i
klist $i
passwd $i
mkdir /datar/$i
chown $i /data/$i
chmod 700 /data/$i
hdfs dfs -ls /
done

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.