Ugh. I needed to fire up the loan MacBook Air to do an iOS compile of my recently completed Appcelerator app. Naturally, I started off on the wrong foot by not recalling the MacBook’s password (luckily I found that I had mailed it to myself some time ago, so I did eventually get in). Then Appcelerator moaned that it needed an update, which failed because it needed a newer version of Node.js to be installed.
So off to Google I once more marched, the result of which I am now jotting down here as a future reminder: How to Update Node.js on a MacBook Air.
As it turns out, updating node.js is a pretty quick affair: First, fire up a terminal (which again I needed to march over to Google in order to learn how to do), and then run the following commands:
sudo npm cache clean -f sudo npm install -g n sudo n stable
Note the use of sudo to run these commands. Essentially we are first force clearing the npm cache, then installing and upgrading node to the latest version.
Related Link: MacBook Air | Node.js
Ubuntu Server: Add User to Sudo Group Tips, Tricks and Tutorials 06 JUL 2015
Using Ubuntu server, how does one add an existing user to the sudo group (which is the “administrators” group in Ubuntu), thus granting that user pretty much full rights on the box?
Turns out that the answer is pretty much the same way you add a user to any other group in Ubuntu. If the user has already been created via the adduser command earlier (for argument’s sake, you named him bob), you simply now run the following command from the terminal:
sudo usermod -a -G sudo bob
Nifty.
(Note, if logged in, that user must first log off and then back in again for this change to take effect.)
How to use MySQL to Import a .sql file Tips, Tricks and Tutorials 05 JUL 2015
Quite often I like backing my databases up as .sql files, which then comes in handy for also moving databases around, or making copies of them. The question is, given your Ubuntu server and the mysql command, how do you import a .sql file?
Well first, you enter the mysql console in the usual manner:
mysql -u USERNAME -p
Once in, switch to the database that you want to run the SQL import against:
mysql> use DATABASE_NAME;
With the database changed, the next step is to actually do the SQL import itself:
mysql> source PATH/TO/FILE.sql;
Easy as that. Of course, you could just have done the whole thing via the terminal command line if you didn’t want to access the MySQL console in the first place:
mysql -u USERNAME -p DATABASE_NAME < PATH/TO/FILE.sql
Nifty.
Ubuntu Server: How to See Group Members Tips, Tricks and Tutorials 12 JUN 2015
The scenario is this. Your file listing is showing you that a file belongs to a particular group, but now the next question that you have is what users are actually members of this group?
Well, getting this information via the command line terminal is pretty simple as it turns out – Just run the getent command:
getent group <GROUPNAME>
or
getent group <GROUPNAME> | awk -F: '{print $4}'
In case you aren’t familiar with getent:
“getent is a unix command that helps a user get entries in a number of important text files called databases. This includes the passwd and group databases which store user information – hence getent is a common way to look up user details on Unix. Since getent uses the same name service as the system, getent will show all information, including that gained from network information sources such as LDAP. The databases it searches in are: ahosts, ahostsv4, ahostsv6, aliases, ethers (Ethernet addresses), group, gshadow, hosts, netgroup, networks, passwd, protocols, rpc, services, and shadow.”
Easy peasy :)
Related Link: https://en.wikipedia.org/wiki/Getent
Ubuntu Server: How to Create a Symbolic Link Tips, Tricks and Tutorials 05 JUN 2015
Just as a quick note to myself because I’m doing this more often in server setups these days (which nevertheless is still infrequent enough that I need to drop notes for myself) – to create a symbolic link (symlink) on an Ubuntu server via the terminal, in particular to link to a folder, I need to use the ln command combined with the -s switch (The ln link command has a whole lot of uses, but we’re interested in symbolic links here).
The syntax is as follows:
ln -s <real folder> <link folder>
What the above achieves is a pseudo <link folder> which whatever you do inside it, actually happens in <real folder>. Nifty.
You can verify the symbolic link with the command ls -l which will show an arrow to where the link points.
Related Link: http://manpages.ubuntu.com/manpages/vivid/man1/ln.1.html
Ubuntu Server: How to Empty a Text Log File Tips, Tricks and Tutorials 26 DEC 2013
When it comes to debugging a problem on an Ubuntu Server, often the only way forward is to consult the various log files on the system. Of course, log files tend to be left unattended, meaning they get very large, which in turn means every now and then that you simply need to wipe them so that you can start collecting some fresh debug data.
Naturally you don’t want to tamper with these files too much, but in the event that you need to empty a text file from a terminal window, this is what you do:
cat /dev/null > logfiletoempty.log
In Linux, /dev/null, or the null device, is a special file that discards all data written to it but reports that the write operation succeeded. It provides no data to any process that reads from it, yielding EOF immediately. So looking at the above, we’re reading the empty file and then writing it’s ‘contents’ to the specified file (in this case the log file we want to empty). This action overwrites the existing file with blank content, effectively emptying it.
Nice and simple.
Ubuntu Server: How to Move or Copy Files Starting with a Dash (-) Tips, Tricks and Tutorials 16 SEP 2013
It’s not often that you do run into this particular problem, but when you do it is a bit of an annoyance – most Ubuntu Server terminal commands (in other words linux commands), in our examples the cp (copy) and mv (move) instructions, can handle files beginning with a dash (-).
Why?
Well obviously because all commands accept parameter switches that are indicated with a dash (-) or double dash (–), meaning that when the command hits a file name staring with a dash, it basically assumes it to be a switch declaration which is obviously very wrong.
Luckily for us, even though it isn’t immediately all that apparent, there is a simple way to get around this and allow for the moving or copying of files starting with a dash – basically we delimit the file options by essentially instructing the application that we’ve finished including our switches.
We do this by placing a double dash (–) after the last lot of switches which tells the mv or cp command that what follows are filenames, thus allowing them to happily work on everything coming in next.
Useful to know.
Ubuntu Terminal: How to set a User’s Shell to Bash Tips, Tricks and Tutorials 19 AUG 2013
Given an installation of Ubuntu Server (or pretty much any other Linux variant that requires you to use a terminal), changing a user’s shell is thankfully pretty easy.
I generally like to use bash as my shell of choice, as it gives me the most user friendly and verbose interface to work in, meaning that I often find myself having to switch a newly installed system’s default out for bash for my freshly added user account.
And given that this post is more a reference for me than anything else, these are a useful set of steps to follow in order to change to Bash:
1. Check the user’s current shell by reading the entry in the /etc/passwd user information file.
grep craig /etc/passwd
Grep will return something that looks like craig:x:1006:1006::/home/craig:/bin/sh. That particular line tells us that the system is currently using sh as my shell.
2. Grab a list of available shells by reading the common /etc/shells file.
cat /etc/shells
3. Change the shell using the chsh command. If you are logged into your account, then chsh will first prompt you for your password, followed by a prompt for the new shell. Note, you have to enter the full path, as listed in /etc/shells.
Oh, you can also quicken the process by calling chsh with parameters, as indicated by the man page:
chsh -s /bin/bash craig
Done!
PHP: What is the Maximum Amount of Variables in $_POST? Tips, Tricks and Tutorials 01 AUG 2013
I came across an interesting problem the other day when a fairly large (poorly written) form seemingly wasn’t being completely processed, and no one could figure out exactly why. Now we’re all familiar with the post_max_size PHP configuration option which controls the maximum size of a form POST, and usually this is the first one that gets turned to when it appears as if a POST isn’t coming through correctly.
However, in this particular case it was only small strings being pushed through, meaning that size wasn’t an issue here – so I did a little digging and found this little bugger: max_input_vars.
It turns out that by default, PHP applied a 1000 element limit to the $_POST variable. If the limit was exceeded, then PHP would simply truncate the $_POST variable back to the correct size – which was exactly what was happening with the large form in our case.
Luckily for us, with version 5.3.9 of PHP came this new configuration option, max_input_vars, which allows us to change this limit. From the documentation:
max_input_vars: How many input variables may be accepted (limit is applied to $_GET, $_POST and $_COOKIE superglobal separately). Use of this directive mitigates the possibility of denial of service attacks which use hash collisions. If there are more input variables than specified by this directive, an E_WARNING is issued, and further input variables are truncated from the request. This limit applies only to each nesting level of a multi-dimensional input array.
Note that you can’t set this configuration option at runtime meaning that you must add it to your php.ini file and reload the Apache server.
If you are working in Ubuntu from a terminal:
# add your new value anywhere in the php.ini file: max_input_vars = 2000 sudo nano /etc/php5/apache2/php.ini # reload apache sudo service apache2 reload
Nifty.