Allow or deny access

Allow or Restrict sudo access to Users in linux

On collaborative work, you would give access to others who would be helping you out on the code or system admin stuff.

The question would be, how limited can you go with granting access to the users?

The first thing would be adding the user to the sudoers list, lets say you have created a new user named tom and you want to grant sudo access to this user

You can edit the sudoers file with whatever editor you are using. But the most recommended way would be using visudo


sudo visudo 

Grant all the access to user

If you want to give the world access to the user, then add the following line and save the file


tom ALL=(ALL) ALL

tom is the user you want to grant permissions and the first ALL is for the host and the ALL in the bracket is referring to other accounts the user sudouser can act as and the final ALL would be for list of commands – in this case tom has every access provided.

Grant only some commands

Lets say you want to give tom an access of copy and renaming a file


tom ALL=/bin/cp,/bin/mv

The above command would tell the system the user tom has access to cp and mv only. You might need to check the correct binary to cp and mv using which command. That is which cp would tell you the right path to the binary of the cp.

Allow the user everything but installing new softwares using apt-get

Ok, so you might want to give every access to the user but you want to limit installing new softwares using apt-get


tom ALL=ALL,!/usr/bin/apt-get

Here tom can do everything but not apt-get. If you add the ! in front of the command, it means don’t allow.

So using the combinations of the above you can reach the level where you can grant and deny any command for the user of interest

Screen Shot 2016-04-10 at 7.49.57 PM

install composer and make it globally accessible

Unless you have been living under rocks, you have heard about composer already. It is a dependency manager and more than that actually. If your PHP app is using a lot of dependencies, then it is time to consult composer.

Here I will put some notes on how to install it and make it available globally so that you can use it from any directory.

First thing first, go and grab the phar file from composer download page

It would be a single phar file and u can use that as it is:

composer.phar install

Of course you would need the composer.json file for the above to work where you would list the dependencies.

But, this would require you to call the phar from where you downloaded and that might not be handy

cd /to/composer/phar/file
sudo mv composer.phar /usr/local/bin/composer

Now, you can call composer from any directory and rock from there
get more @ get composer
That is how you install composer on linux/mac computer and make it globally accessible with in

Upgrade mysql 5.5 to mysql 5.6

This would probably make sense on EC2 instance where it comes by default with mysql 5.5

First shutdown the mysql server and then go ahead and move the mysql 5.5 to new folder

sudo mv /var/lib/mysql /var/lib/mysql55

Then grab the tar from Here

Once you download the tar file

sudo tar -xvf yourDownloadedTar.tar
sudo rpm -ivh MySQL-shared-5.6.17-1.el6.x86_64.rpm
sudo rpm -ivh MySQL-client-5.6.17-1.el6.x86_64.rpm
sudo rpm -ivh MySQL-server-5.6.17-1.el6.x86_64.rpm

yup.. that would be it.
Then restart the mysql and it shall be updated

Process Commands in Linux

To stop the current process and get back to the shell:

ctrl+z

To put the current process in the backgroup

bg

To bring the background process to front

fg

To send the the job to background on one line, put ambersand (&) at the end eg usingvi command:

vi somefile.txt &

To see all the background jobjs

jobs

Bringing specific job from background to foreground

fg %1 - this will bring the job number 1 to front

List all the running processes

ps aux

To filter some process, like to get the process id and stuff

ps aux | grep "process name"

You will have to enable the component called ‘universe’

Got this message while using apt-get install on ubuntu?

if so, here is how to get away with it,

go to System->administration->Software Sources
And the first tab you would see would be then check on the community maintained opensource (universe)


That would take of it.

Piping ls to cp in unix

How to copy the result of ls to directory

Actually this would be a generic implementation. But, as an illustration, if you want to pass the result of ls [list command] to copy, cp, command in unix you can do


ls /some/directory | xargs -i cp {} /new/copy/directory

Just piping the list to cp so that cp can you the listed values to copy.
As you can see this can be implemented to a lot more commands like mv, rm …

Say if you want to move the result of ls to new destination


ls /some/directory | xargs -i mv {} /new/copy/directory

How would you solve the following algorithms?

From billion numbers, find missing numbers with limited memory

Finding K complementary numbers from the given array

How to change default MySQL directory

Originally it will be located at /var/lib/mysql

Say you want to change it to /home/myApps/mysql

First if the mysql is running stop it: sudo /etc/init.d/mysql stop
and stop the server too: sudo/etc/init.d/apache2 stop

Go to /etc/mysql/my.cnf and edit:
datadir = /var/lib/mysql to datadir = /home/myApps/mysql

Copy files from existing path to the new path
cp -R /var/lib/mysql* /home/myApp/mysql

Grant ownership to new directory for mysql

sudo chown -R mysql:mysql /home/myApp/mysql

then start up your server and mysql – voila.