Managing Users in Ubuntu

add a user: sudo adduser [newuser]

sudo adduser john

then give password, and setup home folder,when prompted

edit the list of super users

visudo

list users

cat /etc/passwd

change password of a user – sudo passwd [username]

sudo passwd john

to change your password you can just use sudo passwd.

delete user

sudo deluser username

This will not delete the home folder (and subfolder) those must be dealt with separately. A new user created with the old user name would have access to those files.

temporarily lock a user account – Simply locking a user account will not prevent a user from logging into your server remotely if they have previously set up RSA public key authentication.

sudo passwd -l username

To unlock the account

sudo passwd -u username

Groups

Groups are used in to control permissions (see file permissions)
add user to a group
usermod -G [group-name] [username]

usermod -G basketball john

using the -G switch ads the group as a supplemental group. Using -g would make the group that users primary group.

see what groups a user is in

id john

add a new group: groupadd [new_group_name]

groupadd ruby_developers

System Monitoring Tools for VPS

Tools for monitoring performance and troubleshooting Ubuntu VPS web servers

  • Munin – graphs of system resources over time. Very nice. Can be a bit difficult to setup.
  • top – system stats
  • iotop – like top, but for io stats. Install
    sudo apt-get install iotop

    Useful setup

    iotop -b -o -d 30 -t

    -b (batch – so you can keep a running tally of results) -o (only those processes with io) -d (delay and seconds – how often to print out stats) -t (include time in printout)

  • vmstat – stats on memory, io, swap, cpu and system. Example:
    vmstat 10

    (prints out stats every ten seconds.

  • iostat

Error logs

  • sudo nano /var/log/apache2/error.log

Apache web server access log statistics

  • Webalizer –
    sudo apt-get install webalizer

    GeoIP is required for webalizer

    sudo apt-get install geoip-bin

    detailed instructions

Linux/Ubuntu File and Directory Permissions

Linux (and therefore Ubuntu) has file permissions on each file and directory for the owner, group and everyone else. Those permissions determine if the file can be viewed, executed or edited.

Only the owner of a file or directory (or a privileged user, root for example) may change its mode.

Ownership of a file

To change the ownership of the file or directory: chown new_owner_username directory

chown john public_html

to change the ownership of directory (and all the files and folders in the directory) and also the group: chown -R new_owner_username:new_groupname directory

chown -R john:developers public_html

to change the ownership of all the files in the current directory and also the group: chown -R new_owner_username:new_groupname *

chown john:developers *

File permissions

The easiest way to set Linux file permissions is using a 3 digit sequence. The first digit designates owner permission; the second, the group permission; and the third, everyone else’s permission.

Read = 4
Write = 2
Execute = 1

The digit is the sum of those. So if you want to grant only read permission you use 4; read and execute 5; read, write and execute = 7.

chmod 775 index.html

That will set the permissions on index.html so the owner, and a user in the group specified can read, write and execute the file and everyone else can read and execute.

chmod -R 755 public_html

That will set the permissions on files and directories (recursively through all subdirectories) so the owner can read, write and execute; members of the group and everyone else can read and execute (but not write).

ls - l

That will give you a list of files and directories, in a directory, with the owner and group settings and the permissions for all 3 (those 2 and everyone else), which will look something like:

-rw-r--r-- 1 root developers   397 2008-05-25 20:33 index.html
-rw-r--r-- 1 mary developers  9177 2010-05-02 22:18 unix_file_permissions.html
...

The lines start with the permissions for the owner, group and then everyone else. There are 9 total characters, 3 for each. Taking the top line above:

rw-r--r--
rw-  (means the owner has read and write permission but not execute)
r--  (means the group has only read permission)
r--  (means everyone else has only read permission)

The next column tells you the number of hard links to the file or directory. Then column tells you the owner, then the group. Then the byte size of the file, the date it was last change and then the file name.

root
means the username of this file is named root

developers
group (means those users in the group named developers have the group permissions indicated)

Related: Ubuntu command line interface syntax examples

MySQL cli Syntax

Some MySQL cli syntax examples

How to create a MySQL database and import tables, data… from a sql file.

Login to the MySQL command line interface

mysql -u<username> -p
mysql -uroot -p
  mysql> create database some_database;
  mysql> use some_database;
  mysql> source some_database.sql;

The third line runs the named sql file for the database you names.

Create a new user

mysql -uroot -p
  mysql> USE 'some_database';
  mysql> CREATE USER 'new_user'@'localhost' IDENTIFIED BY 'user_password';
Query OK, 0 rows affected (0.00 sec)
  mysql> GRANT SELECT,INSERT,UPDATE,DELETE ON some_database.* TO 'new_user'@'localhost';

For some reason MySQL chooses to say 0 rows affected, when it succeeds and ads a row for the user. Anyway if you get that message, it worked.

Change password for a user

mysql -uroot -p
  mysql> SET PASSWORD FOR 'username'@'localhost' = PASSWORD('new_password');

Restart mysql:

sudo /etc/init.d/mysql restart

Adding a Model in Ruby on Rails

for Rails 2.*
Not for Rails 3

How to add a model in rails

  • ./script/generate resource MODELNAME
  • ./script/generate resource article
  • edit migrate file
  • edit model.rb – to add associations and validations
  • edit controller.rb – add the 7 rest methods
  • rake db:migrate – to run the new migration
  • If you have to add a table (like a join table) you can do something like, for example
  • ./script/generate migration add_join_table_for_articles_and_authors

Update to the routes file should be taken care of by generate resource, but might want to check).